Nicer map creation with walls.
This commit is contained in:
parent
0b376a4998
commit
f5a23d6798
|
@ -0,0 +1,10 @@
|
||||||
|
x Add walls and stuff to maps
|
||||||
|
- Implement simple box collisions
|
||||||
|
- Add enemies (generated through lua)
|
||||||
|
- Htting enemies
|
||||||
|
- Moving enemies
|
||||||
|
- Lower levels
|
||||||
|
- XP
|
||||||
|
- gui
|
||||||
|
- Items
|
||||||
|
- More gui
|
|
@ -1,24 +1,46 @@
|
||||||
function add_tiles_to_room (map, room_x, room_y, texture)
|
function add_tiles_to_room (map, texture)
|
||||||
for i=0,11 do
|
for i=0,15 do
|
||||||
for j=0,15 do
|
for j=0,11 do
|
||||||
if (i == 0 and j == 0) then
|
if (i >= 1 and i <= 14 and j >= 1 and j <= 10) then
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 0, 48)
|
if (i == 1 and j == 1) then
|
||||||
elseif (i == 11 and j == 0) then
|
add_tile(map, i, j, texture, 0, 48, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 0, 80)
|
elseif (i == 1 and j == 10) then
|
||||||
elseif (i == 0 and j == 15) then
|
add_tile(map, i, j, texture, 0, 80, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 32, 48)
|
elseif (i == 14 and j == 1) then
|
||||||
elseif (i == 11 and j == 15) then
|
add_tile(map, i, j, texture, 32, 48, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 32, 80)
|
elseif (i == 14 and j == 10) then
|
||||||
elseif (i == 0) then
|
add_tile(map, i, j, texture, 32, 80, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 16, 48)
|
elseif (i == 1) then
|
||||||
elseif (j == 0) then
|
add_tile(map, i, j, texture, 0, 64, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 0, 64)
|
elseif (j == 1) then
|
||||||
elseif (i == 11) then
|
add_tile(map, i, j, texture, 16, 48, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 16, 80)
|
elseif (i == 14) then
|
||||||
elseif (j == 15) then
|
add_tile(map, i, j, texture, 32, 64, false)
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 32, 64)
|
elseif (j == 10) then
|
||||||
|
add_tile(map, i, j, texture, 16, 80, false)
|
||||||
else
|
else
|
||||||
add_tile(map, room_x, room_y, i, j, texture, 16, 64)
|
add_tile(map, i, j, texture, 16, 64, false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function add_walls_to_room (map, texture)
|
||||||
|
for i=0,15 do
|
||||||
|
for j=0,11 do
|
||||||
|
if (i == 0 and j == 0) then
|
||||||
|
add_tile(map, i, j, texture, 0, 48, true)
|
||||||
|
elseif (i == 15 and j == 0) then
|
||||||
|
add_tile(map, i, j, texture, 32, 48, true)
|
||||||
|
elseif (i == 0 and j == 11) then
|
||||||
|
add_tile(map, i, j, texture, 0, 80, true)
|
||||||
|
elseif (i == 15 and j == 11) then
|
||||||
|
add_tile(map, i, j, texture, 32, 80, true)
|
||||||
|
elseif (i == 0 or i == 15) then
|
||||||
|
add_tile(map, i, j, texture, 0, 64, true)
|
||||||
|
elseif (j == 0 or j == 11) then
|
||||||
|
add_tile(map, i, j, texture, 16, 48, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -29,9 +51,12 @@ map = create_map()
|
||||||
-- add_tile(map, 1, 2, 3)
|
-- add_tile(map, 1, 2, 3)
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
for i=0,9 do
|
for i=0,9 do
|
||||||
for j=0,9 do
|
for j=0,9 do
|
||||||
add_tiles_to_room(map, i, j, floorTexture);
|
set_current_room(map, i, j);
|
||||||
|
add_tiles_to_room(map, floorTexture);
|
||||||
|
add_walls_to_room(map, wallTexture);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,11 +18,15 @@ void camera_follow_position(Camera *cam, Position *pos)
|
||||||
|
|
||||||
if (pos->x <= 0)
|
if (pos->x <= 0)
|
||||||
cam->pos.x = 0;
|
cam->pos.x = 0;
|
||||||
|
else if (pos->x >= room_width * MAP_H_ROOM_COUNT)
|
||||||
|
cam->pos.x = room_width * (MAP_H_ROOM_COUNT - 1);
|
||||||
else
|
else
|
||||||
cam->pos.x = pos->x - (pos->x % room_width);
|
cam->pos.x = pos->x - (pos->x % room_width);
|
||||||
|
|
||||||
if (pos->y <= 0)
|
if (pos->y <= 0)
|
||||||
cam->pos.y = 0;
|
cam->pos.y = 0;
|
||||||
|
else if (pos->y >= room_height * MAP_V_ROOM_COUNT)
|
||||||
|
cam->pos.y = room_height * (MAP_V_ROOM_COUNT - 1);
|
||||||
else
|
else
|
||||||
cam->pos.y = pos->y - (pos->y % room_height);
|
cam->pos.y = pos->y - (pos->y % room_height);
|
||||||
}
|
}
|
||||||
|
|
34
src/map.c
34
src/map.c
|
@ -9,8 +9,8 @@ Room* create_room()
|
||||||
Room *room;
|
Room *room;
|
||||||
|
|
||||||
room = ec_malloc(sizeof(Room));
|
room = ec_malloc(sizeof(Room));
|
||||||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
||||||
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
||||||
room->tiles[i][j] = NULL;
|
room->tiles[i][j] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,8 @@ Map* map_create()
|
||||||
map->currentRoom = (Position) { 0, 0 };
|
map->currentRoom = (Position) { 0, 0 };
|
||||||
map->level = 1;
|
map->level = 1;
|
||||||
|
|
||||||
for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
|
for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
|
||||||
for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
|
for (j=0; j < MAP_V_ROOM_COUNT; ++j) {
|
||||||
map->rooms[i][j] = create_room();
|
map->rooms[i][j] = create_room();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,10 @@ Map* map_create()
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
void map_add_tile(Map *map, Position *room_pos, Position *tile_pos, MapTile *tile)
|
void map_add_tile(Map *map, Position *tile_pos, MapTile *tile)
|
||||||
{
|
{
|
||||||
MapTile **oldTile = &map->rooms[room_pos->x][room_pos->y]->tiles[tile_pos->x][tile_pos->y];
|
const Position *cr = &map->currentRoom;
|
||||||
|
MapTile **oldTile = &map->rooms[cr->x][cr->y]->tiles[tile_pos->x][tile_pos->y];
|
||||||
if (*oldTile != NULL) {
|
if (*oldTile != NULL) {
|
||||||
free(*oldTile);
|
free(*oldTile);
|
||||||
*oldTile = NULL;
|
*oldTile = NULL;
|
||||||
|
@ -87,11 +88,11 @@ void map_render(Map *map, Camera *cam)
|
||||||
};
|
};
|
||||||
|
|
||||||
room = map->rooms[roomPos.x][roomPos.y];
|
room = map->rooms[roomPos.x][roomPos.y];
|
||||||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
||||||
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
||||||
Position tilePos = (Position) {
|
Position tilePos = (Position) {
|
||||||
roomCords.x + j*TILE_DIMENSION,
|
roomCords.x + i*TILE_DIMENSION,
|
||||||
roomCords.y + i*TILE_DIMENSION
|
roomCords.y + j*TILE_DIMENSION
|
||||||
};
|
};
|
||||||
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
|
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
|
||||||
}
|
}
|
||||||
|
@ -118,14 +119,19 @@ void map_set_current_room(Map *map, Position *pos)
|
||||||
unsigned int room_cord_y = pos->y - (pos->y % room_height);
|
unsigned int room_cord_y = pos->y - (pos->y % room_height);
|
||||||
map->currentRoom.y = room_cord_y / room_height;
|
map->currentRoom.y = room_cord_y / room_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (map->currentRoom.x >= MAP_H_ROOM_COUNT)
|
||||||
|
map->currentRoom.x = MAP_H_ROOM_COUNT - 1;
|
||||||
|
if (map->currentRoom.y >= MAP_V_ROOM_COUNT)
|
||||||
|
map->currentRoom.y = MAP_V_ROOM_COUNT - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void map_room_destroy(Room *room)
|
void map_room_destroy(Room *room)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
||||||
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
||||||
if (room->tiles[i][j]) {
|
if (room->tiles[i][j]) {
|
||||||
free(room->tiles[i][j]);
|
free(room->tiles[i][j]);
|
||||||
}
|
}
|
||||||
|
@ -137,8 +143,8 @@ void map_room_destroy(Room *room)
|
||||||
void map_destroy(Map *map)
|
void map_destroy(Map *map)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
|
for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
|
||||||
for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
|
for (j=0; j < MAP_V_ROOM_COUNT; ++j) {
|
||||||
map_room_destroy(map->rooms[i][j]);
|
map_room_destroy(map->rooms[i][j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define MAP_H_
|
#define MAP_H_
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <linkedlist.h>
|
#include <linkedlist.h>
|
||||||
|
|
||||||
#include "sprite.h"
|
#include "sprite.h"
|
||||||
|
@ -17,14 +18,15 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int textureIndex;
|
unsigned int textureIndex;
|
||||||
SDL_Rect clip;
|
SDL_Rect clip;
|
||||||
|
bool collider;
|
||||||
} MapTile;
|
} MapTile;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
MapTile* tiles[MAP_ROOM_HEIGHT][MAP_ROOM_WIDTH];
|
MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
|
||||||
} Room;
|
} Room;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Room* rooms[MAP_V_ROOM_COUNT][MAP_H_ROOM_COUNT];
|
Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT];
|
||||||
LinkedList *textures;
|
LinkedList *textures;
|
||||||
Position currentRoom;
|
Position currentRoom;
|
||||||
int level;
|
int level;
|
||||||
|
@ -34,7 +36,7 @@ Map* map_create();
|
||||||
|
|
||||||
int map_add_texture(Map*, const char *path, SDL_Renderer*);
|
int map_add_texture(Map*, const char *path, SDL_Renderer*);
|
||||||
|
|
||||||
void map_add_tile(Map *map, Position *room_pos, Position *tile_pos, MapTile*);
|
void map_add_tile(Map *map, Position *tile_pos, MapTile*);
|
||||||
|
|
||||||
void map_render(Map*, Camera*);
|
void map_render(Map*, Camera*);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
|
@ -54,6 +55,21 @@ SDL_Renderer* luaL_checksdlrenderer(lua_State *L)
|
||||||
return renderer;
|
return renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
int l_map_set_current_room(lua_State *L)
|
||||||
|
{
|
||||||
|
Map *map;
|
||||||
|
unsigned int room_x, room_y;
|
||||||
|
|
||||||
|
map = luaL_checkmap(L, 1);
|
||||||
|
room_x = luaL_checkinteger(L, 2);
|
||||||
|
room_y = luaL_checkinteger(L, 3);
|
||||||
|
|
||||||
|
map->currentRoom = (Position) { room_y, room_x };
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
int l_add_texture(lua_State *L)
|
int l_add_texture(lua_State *L)
|
||||||
{
|
{
|
||||||
|
@ -74,26 +90,25 @@ static
|
||||||
int l_add_tile(lua_State *L)
|
int l_add_tile(lua_State *L)
|
||||||
{
|
{
|
||||||
Map *map;
|
Map *map;
|
||||||
int room_x, room_y, tile_x, tile_y;
|
int tile_x, tile_y;
|
||||||
int texture_index, tile_clip_x, tile_clip_y;
|
int texture_index, tile_clip_x, tile_clip_y;
|
||||||
|
bool collider;
|
||||||
|
|
||||||
map = luaL_checkmap(L, 1);
|
map = luaL_checkmap(L, 1);
|
||||||
room_x = luaL_checkinteger(L, 2);
|
tile_x = luaL_checkinteger(L, 2);
|
||||||
room_y = luaL_checkinteger(L, 3);
|
tile_y = luaL_checkinteger(L, 3);
|
||||||
tile_x = luaL_checkinteger(L, 4);
|
texture_index = luaL_checkinteger(L, 4);
|
||||||
tile_y = luaL_checkinteger(L, 5);
|
tile_clip_x = luaL_checkinteger(L, 5);
|
||||||
texture_index = luaL_checkinteger(L, 6);
|
tile_clip_y = luaL_checkinteger(L, 6);
|
||||||
tile_clip_x = luaL_checkinteger(L, 7);
|
collider = lua_toboolean(L, 7);
|
||||||
tile_clip_y = luaL_checkinteger(L, 8);
|
|
||||||
|
|
||||||
Position roomPos = (Position) { room_x, room_y };
|
|
||||||
Position tilePos = (Position) { tile_x, tile_y };
|
Position tilePos = (Position) { tile_x, tile_y };
|
||||||
SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 };
|
SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 };
|
||||||
|
|
||||||
MapTile *tile = malloc(sizeof(MapTile));
|
MapTile *tile = malloc(sizeof(MapTile));
|
||||||
*tile = (MapTile) { texture_index, clip };
|
*tile = (MapTile) { texture_index, clip, collider };
|
||||||
|
|
||||||
map_add_tile(map, &roomPos, &tilePos, tile);
|
map_add_tile(map, &tilePos, tile);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -125,6 +140,9 @@ Map* map_lua_generator_run(SDL_Renderer *renderer)
|
||||||
lua_pushcfunction(L, l_add_texture);
|
lua_pushcfunction(L, l_add_texture);
|
||||||
lua_setglobal(L, "add_texture");
|
lua_setglobal(L, "add_texture");
|
||||||
|
|
||||||
|
lua_pushcfunction(L, l_map_set_current_room);
|
||||||
|
lua_setglobal(L, "set_current_room");
|
||||||
|
|
||||||
result = lua_pcall(L, 0, LUA_MULTRET, 0);
|
result = lua_pcall(L, 0, LUA_MULTRET, 0);
|
||||||
if (result) {
|
if (result) {
|
||||||
fprintf(stderr, "[!!] Failed to run script: %s\n", lua_tostring(L, -1));
|
fprintf(stderr, "[!!] Failed to run script: %s\n", lua_tostring(L, -1));
|
||||||
|
|
|
@ -59,6 +59,7 @@ Sprite* player_create(class_t class, SDL_Renderer *renderer)
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite_load_texture(player, asset, renderer);
|
sprite_load_texture(player, asset, renderer);
|
||||||
|
player->pos = (Position) { 64, 64 };
|
||||||
player->texture->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
player->texture->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
||||||
player->texture->dim = (Dimension) { 64, 64 };
|
player->texture->dim = (Dimension) { 64, 64 };
|
||||||
player->handle_event = &handle_player_input;
|
player->handle_event = &handle_player_input;
|
||||||
|
|
Loading…
Reference in New Issue