Fixed animated decorations in map

This commit is contained in:
Linus Probert 2017-12-08 14:40:33 +01:00
parent 4a2b709900
commit f4e24bc8d7
6 changed files with 132 additions and 94 deletions

View File

@ -7,13 +7,6 @@ local LEFT = 2
local RIGHT = 3 local RIGHT = 3
local DOWN = 4 local DOWN = 4
-- Textures
local textures = {
floor = nil,
walls = nil,
decor = nil,
}
local floor = { local floor = {
center = nil, center = nil,
top = nil, top = nil,
@ -38,92 +31,105 @@ local wall = {
local floorDecor = { } local floorDecor = { }
local wallDecor = { } local wallDecor = { }
local function load_decor_textures()
td0 = add_texture(map, "assets/Objects/Decor0.png")
td1 = add_texture(map, "assets/Objects/Decor1.png")
-- Skulls
table.insert(floorDecor, { td0, td1, 0, 12 * 16, false })
table.insert(floorDecor, { td0, td1, 32, 12 * 16, false })
table.insert(floorDecor, { td0, td1, 64, 12 * 16, false })
table.insert(floorDecor, { td0, td1, 0, 13 * 16, false })
table.insert(floorDecor, { td0, td1, 32, 13 * 16, false })
table.insert(floorDecor, { td0, td1, 64, 13 * 16, false })
-- Bones
table.insert(floorDecor, { td0, td1, 16, 12 * 16, false })
table.insert(floorDecor, { td0, td1, 48, 12 * 16, false })
table.insert(floorDecor, { td0, td1, 16, 13 * 16, false })
table.insert(floorDecor, { td0, td1, 48, 13 * 16, false })
-- Urns
table.insert(floorDecor, { td0, td1, 0 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 1 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 2 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 3 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 4 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 5 * 16, 48, true })
table.insert(floorDecor, { td0, td1, 6 * 16, 48, false })
table.insert(floorDecor, { td0, td1, 7 * 16, 48, false })
-- Racks
table.insert(floorDecor, { td0, td1, 0 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 1 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 2 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 3 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 4 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 5 * 16, 11 * 16, true })
table.insert(floorDecor, { td0, td1, 6 * 16, 11 * 16, true })
-- Headstones
table.insert(floorDecor, { td0, td1, 0 * 16, 17 * 16, true })
table.insert(floorDecor, { td0, td1, 1 * 16, 17 * 16, true })
table.insert(floorDecor, { td0, td1, 2 * 16, 17 * 16, true })
table.insert(floorDecor, { td0, td1, 3 * 16, 17 * 16, true })
table.insert(floorDecor, { td0, td1, 4 * 16, 17 * 16, true })
table.insert(floorDecor, { td0, td1, 0 * 16, 18 * 16, true })
table.insert(floorDecor, { td0, td1, 1 * 16, 18 * 16, true })
table.insert(floorDecor, { td0, td1, 2 * 16, 18 * 16, true })
table.insert(floorDecor, { td0, td1, 3 * 16, 18 * 16, true })
table.insert(floorDecor, { td0, td1, 4 * 16, 18 * 16, true })
-- Altars
table.insert(floorDecor, { td0, td1, 0 * 16, 20 * 16, true })
table.insert(floorDecor, { td0, td1, 1 * 16, 20 * 16, true })
table.insert(floorDecor, { td0, td1, 2 * 16, 20 * 16, true })
-- Webs
--table.insert(floorDecor, { td0, td1, 0 * 16, 19 * 16, false })
--table.insert(floorDecor, { td0, td1, 1 * 16, 19 * 16, false })
--table.insert(floorDecor, { td0, td1, 2 * 16, 19 * 16, false })
--table.insert(floorDecor, { td0, td1, 3 * 16, 19 * 16, false })
--table.insert(floorDecor, { td0, td1, 4 * 16, 19 * 16, false })
-- Statues
--table.insert(floorDecor, { td0, td1, 3 * 16, 20 * 16, true })
--table.insert(floorDecor, { td0, td1, 4 * 16, 20 * 16, true })
wallDecor.candle0 = { td0, td1, 0, 8 * 16, false }
wallDecor.candle1 = { td0, td1, 16, 8 * 16, false }
end
function load_textures(map) function load_textures(map)
textures.floor = add_texture(map, "assets/Objects/Floor.png") t_floor = add_texture(map, "assets/Objects/Floor.png")
textures.walls = add_texture(map, "assets/Objects/Wall.png") t_wall = add_texture(map, "assets/Objects/Wall.png")
textures.decor = add_texture(map, "assets/Objects/Decor0.png")
math.randomseed(os.time()) math.randomseed(os.time())
local xo = (random(3) - 1) * 112 local xo = (random(3) - 1) * 112
local yo = (random(7)) * 48 local yo = (random(7)) * 48
floor.center = { textures.floor, xo + 16, yo + 16, false } floor.center = { t_floor, xo + 16, yo + 16, false }
floor.top = { textures.floor, xo + 16, yo + 0, false } floor.top = { t_floor, xo + 16, yo + 0, false }
floor.bottom = { textures.floor, xo + 16, yo + 32, false } floor.bottom = { t_floor, xo + 16, yo + 32, false }
floor.left = { textures.floor, xo + 0, yo + 16, false } floor.left = { t_floor, xo + 0, yo + 16, false }
floor.right = { textures.floor, xo + 32, yo + 16, false } floor.right = { t_floor, xo + 32, yo + 16, false }
floor.topleft = { textures.floor, xo + 0, yo + 0, false } floor.topleft = { t_floor, xo + 0, yo + 0, false }
floor.topright = { textures.floor, xo + 32, yo + 0, false } floor.topright = { t_floor, xo + 32, yo + 0, false }
floor.bottomleft = { textures.floor, xo + 0, yo + 32, false } floor.bottomleft = { t_floor, xo + 0, yo + 32, false }
floor.bottomright = { textures.floor, xo + 32, yo + 32, false } floor.bottomright = { t_floor, xo + 32, yo + 32, false }
floor.singletop = { textures.floor, xo + 48, yo + 0, false } floor.singletop = { t_floor, xo + 48, yo + 0, false }
floor.singlebottom = { textures.floor, xo + 48, yo + 32, false } floor.singlebottom = { t_floor, xo + 48, yo + 32, false }
floor.singleleft = { textures.floor, xo + 64, yo + 16, false } floor.singleleft = { t_floor, xo + 64, yo + 16, false }
floor.singleright = { textures.floor, xo + 96, yo + 16, false } floor.singleright = { t_floor, xo + 96, yo + 16, false }
wall.topleft = { textures.walls, xo + 0, yo + 0, true } wall.topleft = { t_wall, xo + 0, yo + 0, true }
wall.topright = { textures.walls, xo + 32, yo + 0, true } wall.topright = { t_wall, xo + 32, yo + 0, true }
wall.bottomleft = { textures.walls, xo + 0, yo + 32, true } wall.bottomleft = { t_wall, xo + 0, yo + 32, true }
wall.bottomright = { textures.walls, xo + 32, yo + 32, true } wall.bottomright = { t_wall, xo + 32, yo + 32, true }
wall.vertical = { textures.walls, xo + 0, yo + 16, true } wall.vertical = { t_wall, xo + 0, yo + 16, true }
wall.horizontal = { textures.walls, xo + 16, yo + 0, true } wall.horizontal = { t_wall, xo + 16, yo + 0, true }
-- Skulls load_decor_textures()
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 })
-- Racks
table.insert(floorDecor, { textures.decor, 0 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 1 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 2 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 3 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 4 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 5 * 16, 11 * 16, true })
table.insert(floorDecor, { textures.decor, 6 * 16, 11 * 16, true })
-- 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 end
function create_room () function create_room ()

View File

@ -25,6 +25,7 @@ Map* map_create()
Map *map = ec_malloc(sizeof(Map)); Map *map = ec_malloc(sizeof(Map));
map->textures = linkedlist_create(); map->textures = linkedlist_create();
map->currentRoom = (Position) { 0, 0 }; map->currentRoom = (Position) { 0, 0 };
map->renderTimer = timer_create();
map->level = 1; map->level = 1;
for (i=0; i < MAP_H_ROOM_COUNT; ++i) { for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
@ -76,9 +77,16 @@ int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
static static
void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam) void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam)
{ {
static bool second_texture = false;
if (tile == NULL) if (tile == NULL)
return; return;
if (timer_get_ticks(map->renderTimer) > 300) {
timer_start(map->renderTimer);
second_texture = !second_texture;
}
Position camPos = camera_to_camera_position(cam, pos); Position camPos = camera_to_camera_position(cam, pos);
SDL_Rect draw_box = (SDL_Rect) { SDL_Rect draw_box = (SDL_Rect) {
camPos.x, camPos.x,
@ -87,7 +95,12 @@ void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam)
TILE_DIMENSION TILE_DIMENSION
}; };
Texture *texture = linkedlist_get(&map->textures, tile->textureIndex); Texture *texture;
if (tile->textureIndex1 >= 0 && second_texture) {
texture = linkedlist_get(&map->textures, tile->textureIndex1);
} else {
texture = linkedlist_get(&map->textures, tile->textureIndex0);
}
SDL_RenderCopy(cam->renderer, SDL_RenderCopy(cam->renderer,
texture->texture, texture->texture,
@ -101,6 +114,11 @@ void map_render(Map *map, Camera *cam)
{ {
int i, j; int i, j;
Room *room; Room *room;
if (!timer_started(map->renderTimer)) {
timer_start(map->renderTimer);
}
Position roomPos = { map->currentRoom.x, map->currentRoom.y }; Position roomPos = { map->currentRoom.x, map->currentRoom.y };
Position roomCords = { Position roomCords = {
roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION, roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION,
@ -175,5 +193,6 @@ void map_destroy(Map *map)
while (map->textures != NULL) { while (map->textures != NULL) {
texture_destroy(linkedlist_poplast(&map->textures)); texture_destroy(linkedlist_poplast(&map->textures));
} }
timer_destroy(map->renderTimer);
free(map); free(map);
} }

View File

@ -8,16 +8,19 @@
#include "sprite.h" #include "sprite.h"
#include "camera.h" #include "camera.h"
#include "position.h" #include "position.h"
#include "timer.h"
#include "defines.h" #include "defines.h"
typedef struct MapTile_t { typedef struct MapTile_t {
unsigned int textureIndex; int textureIndex0;
int textureIndex1;
SDL_Rect clip; SDL_Rect clip;
bool collider; bool collider;
} MapTile; } MapTile;
typedef struct Room_t { typedef struct Room_t {
MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
MapTile* secondary_tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
} Room; } Room;
@ -25,6 +28,7 @@ typedef struct Map_t {
Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT]; Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT];
LinkedList *textures; LinkedList *textures;
Position currentRoom; Position currentRoom;
Timer *renderTimer;
int level; int level;
} Map; } Map;

View File

@ -106,7 +106,7 @@ int l_add_tile(lua_State *L)
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, collider }; *tile = (MapTile) { texture_index, -1, clip, collider };
map_add_tile(map, &tilePos, tile); map_add_tile(map, &tilePos, tile);
@ -117,22 +117,23 @@ static int l_add_decoration(lua_State *L)
{ {
Map *map; Map *map;
int tile_x, tile_y; int tile_x, tile_y;
int texture_index, tile_clip_x, tile_clip_y; int t_index0, t_index1, tile_clip_x, tile_clip_y;
bool collider; bool collider;
map = luaL_checkmap(L, 1); map = luaL_checkmap(L, 1);
tile_x = luaL_checkinteger(L, 2); tile_x = luaL_checkinteger(L, 2);
tile_y = luaL_checkinteger(L, 3); tile_y = luaL_checkinteger(L, 3);
texture_index = luaL_checkinteger(L, 4); t_index0 = luaL_checkinteger(L, 4);
tile_clip_x = luaL_checkinteger(L, 5); t_index1 = luaL_checkinteger(L, 5);
tile_clip_y = luaL_checkinteger(L, 6); tile_clip_x = luaL_checkinteger(L, 6);
collider = lua_toboolean(L, 7); tile_clip_y = luaL_checkinteger(L, 7);
collider = lua_toboolean(L, 8);
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, collider }; *tile = (MapTile) { t_index0, t_index1, clip, collider };
map_add_decoration(map, &tilePos, tile); map_add_decoration(map, &tilePos, tile);

View File

@ -19,6 +19,11 @@ void timer_stop(Timer *t)
t->startTime = 0; t->startTime = 0;
} }
bool timer_started(Timer *t)
{
return t->startTime != 0;
}
unsigned int timer_get_ticks(Timer *t) unsigned int timer_get_ticks(Timer *t)
{ {
if (!t->startTime) if (!t->startTime)

View File

@ -1,6 +1,8 @@
#ifndef TIMER_H_ #ifndef TIMER_H_
#define TIMER_H_ #define TIMER_H_
#include <stdbool.h>
typedef struct { typedef struct {
unsigned int startTime; unsigned int startTime;
} Timer; } Timer;
@ -8,6 +10,7 @@ typedef struct {
Timer* timer_create(); Timer* timer_create();
void timer_start(Timer*); void timer_start(Timer*);
void timer_stop(Timer*); void timer_stop(Timer*);
bool timer_started(Timer*);
unsigned int timer_get_ticks(Timer*); unsigned int timer_get_ticks(Timer*);
void timer_destroy(Timer*); void timer_destroy(Timer*);