diff --git a/data/maproombuilder.lua b/data/maproombuilder.lua index 0ec40f0..4716566 100644 --- a/data/maproombuilder.lua +++ b/data/maproombuilder.lua @@ -1,3 +1,6 @@ +-- FUNCTIONS +local random = math.random + -- CONSTANTS local UP = 1 local LEFT = 2 @@ -6,8 +9,9 @@ local DOWN = 4 -- Textures local textures = { - floorTexture = nil, - wallTexture = nil + floor = nil, + walls = nil, + decor = nil, } local floor = { @@ -19,7 +23,7 @@ local floor = { topleft = nil, topright = nil, bottomleft = nil, - bottomright = nil, + bottomright = nil } local wall = { @@ -31,29 +35,81 @@ local wall = { horizontal = nil } +local floorDecor = { } +local wallDecor = { } + function load_textures(map) - textures.floorTexture = add_texture(map, "assets/Objects/Floor.png") - textures.wallTexture = add_texture(map, "assets/Objects/Wall.png") + textures.floor = add_texture(map, "assets/Objects/Floor.png") + textures.walls = add_texture(map, "assets/Objects/Wall.png") + textures.decor = add_texture(map, "assets/Objects/Decor0.png") local xo = 0 local yo = 48 - floor.center = { textures.floorTexture, xo + 16, yo + 16, false } - floor.top = { textures.floorTexture, xo + 16, yo + 0, false } - floor.bottom = { textures.floorTexture, xo + 16, yo + 32, false } - floor.left = { textures.floorTexture, xo + 0, yo + 16, false } - floor.right = { textures.floorTexture, xo + 32, yo + 16, false } - floor.topleft = { textures.floorTexture, xo + 0, yo + 0, false } - floor.topright = { textures.floorTexture, xo + 32, yo + 0, false } - floor.bottomleft = { textures.floorTexture, xo + 0, yo + 32, false } - floor.bottomright = { textures.floorTexture, xo + 32, yo + 32, false } + floor.center = { textures.floor, xo + 16, yo + 16, false } + floor.top = { textures.floor, xo + 16, yo + 0, false } + floor.bottom = { textures.floor, xo + 16, yo + 32, false } + floor.left = { textures.floor, xo + 0, yo + 16, false } + floor.right = { textures.floor, xo + 32, yo + 16, false } + floor.topleft = { textures.floor, xo + 0, yo + 0, false } + floor.topright = { textures.floor, xo + 32, yo + 0, false } + floor.bottomleft = { textures.floor, xo + 0, yo + 32, false } + floor.bottomright = { textures.floor, xo + 32, yo + 32, false } - wall.topleft = { textures.wallTexture, xo + 0, yo + 0, true } - wall.topright = { textures.wallTexture, xo + 32, yo + 0, true } - wall.bottomleft = { textures.wallTexture, xo + 0, yo + 32, true } - wall.bottomright = { textures.wallTexture, xo + 32, yo + 32, true } - wall.vertical = { textures.wallTexture, xo + 0, yo + 16, true } - wall.horizontal = { textures.wallTexture, xo + 16, yo + 0, true } + wall.topleft = { textures.walls, xo + 0, yo + 0, true } + wall.topright = { textures.walls, xo + 32, yo + 0, true } + wall.bottomleft = { textures.walls, xo + 0, yo + 32, true } + wall.bottomright = { textures.walls, xo + 32, yo + 32, true } + wall.vertical = { textures.walls, xo + 0, yo + 16, true } + wall.horizontal = { textures.walls, xo + 16, yo + 0, true } + + -- Skulls + table.insert(floorDecor, { textures.decor, 0, 12 * 16, false }) + table.insert(floorDecor, { textures.decor, 32, 12 * 16, false }) + table.insert(floorDecor, { textures.decor, 0, 13 * 16, false }) + table.insert(floorDecor, { textures.decor, 32, 13 * 16, false }) + + -- Bones + table.insert(floorDecor, { textures.decor, 16, 12 * 16, false }) + table.insert(floorDecor, { textures.decor, 48, 12 * 16, false }) + table.insert(floorDecor, { textures.decor, 16, 13 * 16, false }) + table.insert(floorDecor, { textures.decor, 48, 13 * 16, false }) + + -- Urns + table.insert(floorDecor, { textures.decor, 0 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 1 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 2 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 3 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 4 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 5 * 16, 48, true }) + table.insert(floorDecor, { textures.decor, 6 * 16, 48, false }) + table.insert(floorDecor, { textures.decor, 7 * 16, 48, false }) + + -- Headstones + table.insert(floorDecor, { textures.decor, 0 * 16, 17 * 16, true }) + table.insert(floorDecor, { textures.decor, 1 * 16, 17 * 16, true }) + table.insert(floorDecor, { textures.decor, 2 * 16, 17 * 16, true }) + table.insert(floorDecor, { textures.decor, 3 * 16, 17 * 16, true }) + table.insert(floorDecor, { textures.decor, 4 * 16, 17 * 16, true }) + table.insert(floorDecor, { textures.decor, 0 * 16, 18 * 16, true }) + table.insert(floorDecor, { textures.decor, 1 * 16, 18 * 16, true }) + table.insert(floorDecor, { textures.decor, 2 * 16, 18 * 16, true }) + table.insert(floorDecor, { textures.decor, 3 * 16, 18 * 16, true }) + table.insert(floorDecor, { textures.decor, 4 * 16, 18 * 16, true }) + + -- Webs + table.insert(floorDecor, { textures.decor, 0 * 16, 19 * 16, false }) + table.insert(floorDecor, { textures.decor, 1 * 16, 19 * 16, false }) + table.insert(floorDecor, { textures.decor, 2 * 16, 19 * 16, false }) + table.insert(floorDecor, { textures.decor, 3 * 16, 19 * 16, false }) + table.insert(floorDecor, { textures.decor, 4 * 16, 19 * 16, false }) + + -- Altars + table.insert(floorDecor, { textures.decor, 0 * 16, 20 * 16, true }) + table.insert(floorDecor, { textures.decor, 1 * 16, 20 * 16, true }) + table.insert(floorDecor, { textures.decor, 2 * 16, 20 * 16, true }) + table.insert(floorDecor, { textures.decor, 3 * 16, 20 * 16, true }) + table.insert(floorDecor, { textures.decor, 4 * 16, 20 * 16, true }) end function create_room () @@ -66,6 +122,13 @@ function create_room () } end +local function add_random_decor_to_room() + local decor_count = random(4) - 1 + for i=1,decor_count do + add_decoration(map, random(11)+1, random(8)+1, unpack(floorDecor[random(#floorDecor)])) + end +end + local function add_tiles_to_room (map) for i=0,15 do for j=0,11 do @@ -92,6 +155,8 @@ local function add_tiles_to_room (map) end end end + + add_random_decor_to_room() end local function add_walls_to_room (map) diff --git a/src/main.c b/src/main.c index f680f1e..9a06a82 100644 --- a/src/main.c +++ b/src/main.c @@ -72,7 +72,6 @@ bool initSDL() printf("[!!] Unable to initiate img loading: %s\n", IMG_GetError()); return false; } - return true; } diff --git a/src/map.c b/src/map.c index 773b494..8e771ce 100644 --- a/src/map.c +++ b/src/map.c @@ -12,6 +12,7 @@ Room* create_room() for (i=0; i < MAP_ROOM_WIDTH; ++i) { for (j=0; j < MAP_ROOM_HEIGHT; ++j) { room->tiles[i][j] = NULL; + room->decorations[i][j] = NULL; } } return room; @@ -46,6 +47,17 @@ void map_add_tile(Map *map, Position *tile_pos, MapTile *tile) *oldTile = tile; } +void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile) +{ + const Position *cr = &map->currentRoom; + MapTile **oldTile = &map->rooms[cr->x][cr->y]->decorations[tile_pos->x][tile_pos->y]; + if (*oldTile != NULL) { + free(*oldTile); + *oldTile = NULL; + } + *oldTile = tile; +} + int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer) { Texture *t = texture_create(path, renderer); @@ -103,6 +115,7 @@ void map_render(Map *map, Camera *cam) roomCords.y + j*TILE_DIMENSION }; map_tile_render(map, room->tiles[i][j], &tilePos, cam); + map_tile_render(map, room->decorations[i][j], &tilePos, cam); } } } @@ -143,6 +156,9 @@ void map_room_destroy(Room *room) if (room->tiles[i][j]) { free(room->tiles[i][j]); } + if (room->decorations[i][j]) { + free(room->decorations[i][j]); + } } } free(room); diff --git a/src/map.h b/src/map.h index cb05f29..9792a92 100644 --- a/src/map.h +++ b/src/map.h @@ -18,6 +18,7 @@ typedef struct MapTile_t { typedef struct Room_t { MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; + MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; } Room; typedef struct Map_t { @@ -33,6 +34,8 @@ int map_add_texture(Map*, const char *path, SDL_Renderer*); void map_add_tile(Map *map, Position *tile_pos, MapTile*); +void map_add_decoration(Map *map, Position *tile_pos, MapTile*); + void map_render(Map*, Camera*); void map_set_current_room(Map*, Position*); diff --git a/src/map_lua.c b/src/map_lua.c index b1b6690..7a927c0 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -113,6 +113,32 @@ int l_add_tile(lua_State *L) return 0; } +static int l_add_decoration(lua_State *L) +{ + Map *map; + int tile_x, tile_y; + int texture_index, tile_clip_x, tile_clip_y; + bool collider; + + map = luaL_checkmap(L, 1); + tile_x = luaL_checkinteger(L, 2); + tile_y = luaL_checkinteger(L, 3); + texture_index = luaL_checkinteger(L, 4); + tile_clip_x = luaL_checkinteger(L, 5); + tile_clip_y = luaL_checkinteger(L, 6); + collider = lua_toboolean(L, 7); + + Position tilePos = (Position) { tile_x, tile_y }; + SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 }; + + MapTile *tile = malloc(sizeof(MapTile)); + *tile = (MapTile) { texture_index, clip, collider }; + + map_add_decoration(map, &tilePos, tile); + + return 0; +} + Map* map_lua_generator_run(SDL_Renderer *renderer) { int status, result; @@ -137,6 +163,9 @@ Map* map_lua_generator_run(SDL_Renderer *renderer) lua_pushcfunction(L, l_add_tile); lua_setglobal(L, "add_tile"); + lua_pushcfunction(L, l_add_decoration); + lua_setglobal(L, "add_decoration"); + lua_pushcfunction(L, l_add_texture); lua_setglobal(L, "add_texture"); diff --git a/src/roommatrix.c b/src/roommatrix.c index 8777acd..bb0cb9e 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -21,9 +21,10 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m) for (i = 0; i < MAP_ROOM_WIDTH; ++i) { for (j = 0; j < MAP_ROOM_HEIGHT; ++j) { - if (!r->tiles[i][j]) - continue; - rm->spaces[i][j].occupied = r->tiles[i][j]->collider; + if (r->tiles[i][j]) + rm->spaces[i][j].occupied = r->tiles[i][j]->collider; + if (!rm->spaces[i][j].occupied && r->decorations[i][j]) + rm->spaces[i][j].occupied = r->decorations[i][j]->collider; } } }