Fixed map path and generation.

Also found an issue with collisions and room-switching. This has been
fixed also.
This commit is contained in:
Linus Probert 2017-12-06 11:44:17 +01:00
parent bdc5b6d629
commit e8ccda2faf
8 changed files with 164 additions and 14 deletions

View File

@ -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) function add_tiles_to_room (map, texture)
for i=0,15 do for i=0,15 do
for j=0,11 do for j=0,11 do
@ -46,17 +58,129 @@ function add_walls_to_room (map, texture)
end end
end end
map = create_map() function matrix_coverage (matrix)
-- add_texture(map, "path/to/a/fancy/texture") local cov = 0
-- add_tile(map, 1, 2, 3) 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
function create_room ()
local room = {}
room.exits = {}
end
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 floorTexture = add_texture(map, "assets/Objects/Floor.png")
local wallTexture = add_texture(map, "assets/Objects/Wall.png") local wallTexture = add_texture(map, "assets/Objects/Wall.png")
local map_matrix = generate_path()
for i=0,9 do -- Print path [Debug]
for j=0,9 do for i=1,10 do
set_current_room(map, i, j); 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_tiles_to_room(map, floorTexture);
add_walls_to_room(map, wallTexture); add_walls_to_room(map, wallTexture);
end end
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

View File

@ -157,6 +157,7 @@ void close()
{ {
sprite_destroy(gPlayer); sprite_destroy(gPlayer);
map_destroy(gMap); map_destroy(gMap);
roommatrix_destroy(gRoomMatrix);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
gWindow = NULL; gWindow = NULL;
IMG_Quit(); IMG_Quit();

View File

@ -65,7 +65,7 @@ int l_map_set_current_room(lua_State *L)
room_x = luaL_checkinteger(L, 2); room_x = luaL_checkinteger(L, 2);
room_y = luaL_checkinteger(L, 3); room_y = luaL_checkinteger(L, 3);
map->currentRoom = (Position) { room_y, room_x }; map->currentRoom = (Position) { room_x, room_y };
return 0; return 0;
} }

View File

@ -4,8 +4,13 @@
static static
bool has_collided(Sprite *sprite, RoomMatrix *matrix) bool has_collided(Sprite *sprite, RoomMatrix *matrix)
{ {
Position pos = position_to_matrix_coords(&sprite->pos); Position roomCoord = position_to_room_coords(&sprite->pos);
return matrix->spaces[pos.x][pos.y].occupied; 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 static

View File

@ -14,3 +14,17 @@ Position position_to_matrix_coords(Position *src)
return pos; 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;
}

View File

@ -8,4 +8,6 @@ typedef struct {
Position position_to_matrix_coords(Position*); Position position_to_matrix_coords(Position*);
Position position_to_room_coords(Position*);
#endif // POSITION_H_ #endif // POSITION_H_

View File

@ -14,7 +14,8 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
int i, j; int i, j;
Room *r; 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 (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) { for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
@ -34,6 +35,7 @@ void roommatrix_reset(RoomMatrix *m)
m->spaces[i][j].player = NULL; m->spaces[i][j].player = NULL;
} }
} }
m->roomPos = (Position) { 0, 0 };
} }
void roommatrix_destroy(RoomMatrix *m) void roommatrix_destroy(RoomMatrix *m)

View File

@ -3,6 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "defines.h" #include "defines.h"
#include "position.h"
typedef struct Sprite_t Sprite; typedef struct Sprite_t Sprite;
typedef struct Map_t Map; typedef struct Map_t Map;
@ -15,6 +16,7 @@ typedef struct {
typedef struct { typedef struct {
RoomSpace spaces[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; RoomSpace spaces[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
Position roomPos;
} RoomMatrix; } RoomMatrix;
RoomMatrix* roommatrix_create(); RoomMatrix* roommatrix_create();