From e8ccda2faf6c241dfeb56008887d411078644b73 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Wed, 6 Dec 2017 11:44:17 +0100 Subject: [PATCH] Fixed map path and generation. Also found an issue with collisions and room-switching. This has been fixed also. --- data/mapgen.lua | 144 +++++++++++++++++++++++++++++++++++++++++++---- src/main.c | 1 + src/map_lua.c | 2 +- src/player.c | 9 ++- src/position.c | 14 +++++ src/position.h | 2 + src/roommatrix.c | 4 +- src/roommatrix.h | 2 + 8 files changed, 164 insertions(+), 14 deletions(-) diff --git a/data/mapgen.lua b/data/mapgen.lua index 19f924b..6549db0 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -1,3 +1,15 @@ +-- Setting up some functions +local time = os.time +local random = math.random +local randomseed = math.randomseed + +-- CONSTANTS +UP = 1 +LEFT = 2 +RIGHT = 3 +DOWN = 4 + +-- BEGIN FUNCTIONS function add_tiles_to_room (map, texture) for i=0,15 do for j=0,11 do @@ -46,17 +58,129 @@ function add_walls_to_room (map, texture) end end -map = create_map() --- add_texture(map, "path/to/a/fancy/texture") --- add_tile(map, 1, 2, 3) +function matrix_coverage (matrix) + local cov = 0 + for i=1,10 do + for j=1,10 do + if matrix[i][j] > 0 then cov = cov + 1 end + end + end + return cov +end -local floorTexture = add_texture(map, "assets/Objects/Floor.png") -local wallTexture = add_texture(map, "assets/Objects/Wall.png") +function create_room () + local room = {} + room.exits = {} +end -for i=0,9 do - for j=0,9 do - set_current_room(map, i, j); - add_tiles_to_room(map, floorTexture); - add_walls_to_room(map, wallTexture); +function generate_path () + local map_matrix = {} + for i=1,10 do + map_matrix[i] = {} + for j=1,10 do + map_matrix[i][j] = 0 + end + end + + local cx, cy = 1, 1 + local seed = time(); + print("[**] Map generation seed: " .. seed) + randomseed(seed) + local direction = 0; + local lastDirection = 0; + while matrix_coverage(map_matrix) < 30 do + local direction = random(4) + + if lastDirection > 0 then + if random(24) <= 8 then direction = lastDirection end + end + + if direction == UP and cy > 1 then -- UP + map_matrix[cx][cy] = direction + cy = cy - 1; + elseif direction == LEFT and cx > 1 then -- LEFT + map_matrix[cx][cy] = direction + cx = cx - 1; + elseif direction == RIGHT and cx < 10 then -- RIGHT + map_matrix[cx][cy] = direction + cx = cx + 1; + elseif direction == DOWN and cy < 10 then -- DOWN + map_matrix[cx][cy] = direction + cy = cy + 1; + end + lastDirection = direction + end + map_matrix[cx][cy] = 5 -- The final room + + return map_matrix; +end + +function add_exit(map, direction, floorTexture, wallTexture) + if direction > 4 then return end + + if direction == UP then + add_tile(map, 7, 0, floorTexture, 16, 64, false) + elseif direction == LEFT then + add_tile(map, 0, 5, floorTexture, 16, 64, false) + elseif direction == RIGHT then + add_tile(map, 15, 5, floorTexture, 16, 64, false) + elseif direction == DOWN then + add_tile(map, 7, 11, floorTexture, 16, 64, false) end end + +function add_reverse_exit(map, direction, floorTexture, wallTexture) + local reverseDirection + + if direction == UP then reverseDirection = DOWN + elseif direction == DOWN then reverseDirection = UP + elseif direction == LEFT then reverseDirection = RIGHT + elseif direction == RIGHT then reverseDirection = LEFT + else reverseDirection = 5 end + + add_exit(map, reverseDirection, floorTexture, wallTexture) +end +-- END FUNCTIONS + +-- BEGIN SCRIPT +map = create_map() -- 'map' needs to be global +local floorTexture = add_texture(map, "assets/Objects/Floor.png") +local wallTexture = add_texture(map, "assets/Objects/Wall.png") +local map_matrix = generate_path() + +-- Print path [Debug] +for i=1,10 do + for j=1,10 do + io.write(map_matrix[j][i] .. " ") + end + io.write("\n") +end + +for i=1,10 do + for j=1,10 do + if map_matrix[i][j] > 0 then + set_current_room(map, i-1, j-1); + add_tiles_to_room(map, floorTexture); + add_walls_to_room(map, wallTexture); + end + end +end +for i=1,10 do + for j=1,10 do + if map_matrix[i][j] > 0 then + set_current_room(map, i-1, j-1); + add_exit(map, map_matrix[i][j], floorTexture, wallTexture); + if map_matrix[i][j] == UP then + set_current_room(map, i-1, j-2) + elseif map_matrix[i][j] == LEFT then + set_current_room(map, i-2, j-1) + elseif map_matrix[i][j] == RIGHT then + set_current_room(map, i, j-1) + elseif map_matrix[i][j] == DOWN then + set_current_room(map, i-1, j) + end + add_reverse_exit(map, map_matrix[i][j], floorTexture, wallTexture); + end + end +end +-- END SCRIPT diff --git a/src/main.c b/src/main.c index 96090f9..f680f1e 100644 --- a/src/main.c +++ b/src/main.c @@ -157,6 +157,7 @@ void close() { sprite_destroy(gPlayer); map_destroy(gMap); + roommatrix_destroy(gRoomMatrix); SDL_DestroyWindow(gWindow); gWindow = NULL; IMG_Quit(); diff --git a/src/map_lua.c b/src/map_lua.c index eb0428d..4f278fe 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -65,7 +65,7 @@ int l_map_set_current_room(lua_State *L) room_x = luaL_checkinteger(L, 2); room_y = luaL_checkinteger(L, 3); - map->currentRoom = (Position) { room_y, room_x }; + map->currentRoom = (Position) { room_x, room_y }; return 0; } diff --git a/src/player.c b/src/player.c index 879cba4..9de4713 100644 --- a/src/player.c +++ b/src/player.c @@ -4,8 +4,13 @@ static bool has_collided(Sprite *sprite, RoomMatrix *matrix) { - Position pos = position_to_matrix_coords(&sprite->pos); - return matrix->spaces[pos.x][pos.y].occupied; + Position roomCoord = position_to_room_coords(&sprite->pos); + if (roomCoord.x != matrix->roomPos.x || roomCoord.y != matrix->roomPos.y) { + return false; + } + + Position matrixPos = position_to_matrix_coords(&sprite->pos); + return matrix->spaces[matrixPos.x][matrixPos.y].occupied; } static diff --git a/src/position.c b/src/position.c index ca4bd94..37b054d 100644 --- a/src/position.c +++ b/src/position.c @@ -14,3 +14,17 @@ Position position_to_matrix_coords(Position *src) return pos; } + +Position position_to_room_coords(Position *src) +{ + unsigned int room_px_width, room_px_height; + Position pos; + + room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH; + room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT; + + pos.x = (src->x - (src->x % room_px_width)) / room_px_width; + pos.y = (src->y - (src->y % room_px_height)) / room_px_height; + + return pos; +} diff --git a/src/position.h b/src/position.h index ffd7fb7..5440cd9 100644 --- a/src/position.h +++ b/src/position.h @@ -8,4 +8,6 @@ typedef struct { Position position_to_matrix_coords(Position*); +Position position_to_room_coords(Position*); + #endif // POSITION_H_ diff --git a/src/roommatrix.c b/src/roommatrix.c index 13f083a..13dd2a1 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -14,7 +14,8 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m) int i, j; Room *r; - r = m->rooms[m->currentRoom.x][m->currentRoom.y]; + rm->roomPos = m->currentRoom; + r = m->rooms[rm->roomPos.x][rm->roomPos.y]; for (i = 0; i < MAP_ROOM_WIDTH; ++i) { for (j = 0; j < MAP_ROOM_HEIGHT; ++j) { @@ -34,6 +35,7 @@ void roommatrix_reset(RoomMatrix *m) m->spaces[i][j].player = NULL; } } + m->roomPos = (Position) { 0, 0 }; } void roommatrix_destroy(RoomMatrix *m) diff --git a/src/roommatrix.h b/src/roommatrix.h index 22f2003..88e39a1 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -3,6 +3,7 @@ #include #include "defines.h" +#include "position.h" typedef struct Sprite_t Sprite; typedef struct Map_t Map; @@ -15,6 +16,7 @@ typedef struct { typedef struct { RoomSpace spaces[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; + Position roomPos; } RoomMatrix; RoomMatrix* roommatrix_create();