diff --git a/src/item.c b/src/item.c index 06717f3..34a6abf 100644 --- a/src/item.c +++ b/src/item.c @@ -26,6 +26,8 @@ item_create(void) Item *item = ec_malloc(sizeof(Item)); item->sprite = NULL; item->collected = false; + item->openable = false; + item->opened = false; m_strcpy(item->label, 50, ""); item->value = 0.0; item->items = NULL; @@ -42,18 +44,24 @@ item_render(Item *item, Camera *cam) void item_collected(Item *item, Player *player) { - LinkedList *items = item->items; + if (item->collected || item->opened) + return; - while (items != NULL) { - Item *subitem = items->data; - items = items->next; + while (item->items) { + Item *subitem = linkedlist_pop(&item->items); item_collected(subitem, player); + item_destroy(subitem); } if (item->effect != NULL) item->effect(item, player); - item->collected = true; + if (!item->openable) { + item->collected = true; + } else { + item->opened = true; + item->sprite->texture_index = 1; + } } void diff --git a/src/item.h b/src/item.h index 7f73d39..74a66f6 100644 --- a/src/item.h +++ b/src/item.h @@ -30,6 +30,8 @@ typedef struct Item_t { Sprite *sprite; bool collected; + bool openable; + bool opened; char label[50]; double value; LinkedList *items; diff --git a/src/item_builder.c b/src/item_builder.c index 3ee41c6..47b6080 100644 --- a/src/item_builder.c +++ b/src/item_builder.c @@ -25,7 +25,8 @@ #include "gui.h" #include "mixer.h" #include "random.h" -#include"texturecache.h" +#include "texturecache.h" +#include "sprite.h" static ItemBuilder *builder = NULL; @@ -78,16 +79,19 @@ pickup_dagger(Item *item, Player *player) } static Item * -create_item(const char *path, SDL_Rect clip, void (*cb)(Item*, Player*)) +create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item*, Player*)) { - Texture *t; + Texture *t0 = NULL, *t1 = NULL; Item *item; item = item_create(); - t = texturecache_add(path); + t0 = texturecache_add(path0); + if (path1) + t1 = texturecache_add(path1); item->sprite = sprite_create(); - sprite_set_texture(item->sprite, t, 0); + sprite_set_texture(item->sprite, t0, 0); + sprite_set_texture(item->sprite, t1, 1); item->sprite->dim = GAME_DIMENSION; item->sprite->clip = clip; item->effect = cb; @@ -153,7 +157,7 @@ create_treasure(int current_level) else if (amt <= 15) clip.x += 32; - Item *item = create_item("Items/Money.png", clip, &pickup_gold); + Item *item = create_item("Items/Money.png", NULL, clip, &pickup_gold); m_strcpy(item->label, 50, label); item->value = amt; return item; @@ -175,18 +179,21 @@ item_builder_build_item(ItemKey key, int level) break; case FLESH: item = create_item(path_flesh, + NULL, CLIP16(get_random(7) * 16, get_random(1) * 16), &eat_flesh); item->value = 1 + get_random(level); break; case HEALTH: item = create_item(path_potion, + NULL, CLIP16(0, 0), &drink_health); item->value = 1 + get_random(level); break; case DAGGER: item = create_item(path_short_wep, + NULL, CLIP16(0, 0), &pickup_dagger); item->value = 1; @@ -203,10 +210,23 @@ Item * item_builder_build_sack(void) { return create_item("Items/Chest0.png", + NULL, CLIP16(0, 32), NULL); } +Item * +item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip) +{ + Item *chest = create_item(path0, + path1, + clip, + NULL); + chest->openable = true; + chest->sprite->animate = false; + return chest; +} + void item_builder_close(void) { diff --git a/src/item_builder.h b/src/item_builder.h index 043e62c..0a3f670 100644 --- a/src/item_builder.h +++ b/src/item_builder.h @@ -50,6 +50,9 @@ item_builder_build_item(ItemKey key, int level); Item * item_builder_build_sack(void); +Item * +item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip); + void item_builder_close(void); diff --git a/src/main.c b/src/main.c index 45a89dc..c7549cc 100644 --- a/src/main.c +++ b/src/main.c @@ -538,14 +538,12 @@ run_game_render(void) SDL_RenderSetViewport(gRenderer, &gameViewport); map_render(gMap, gCamera); particle_engine_render_game(gCamera); - - if (!is_player_dead()) - player_render(gPlayer, gCamera); - map_render_top_layer(gMap, gCamera); - if (!is_player_dead()) + if (!is_player_dead()) { + player_render(gPlayer, gCamera); player_render_toplayer(gPlayer, gCamera); + } if (gPlayer->class == MAGE || gPlayer->class == PALADIN) roommatrix_render_mouse_square(gRoomMatrix, gCamera); diff --git a/src/map_lua.c b/src/map_lua.c index 3d062fb..663652f 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -33,6 +33,9 @@ #include "io_util.h" #include "texturecache.h" #include "trap.h" +#include "item.h" +#include "item_builder.h" +#include "random.h" static lua_State* load_lua_state(void) @@ -297,6 +300,50 @@ l_add_trap(lua_State *L) return 0; } +static int +l_add_chest(lua_State *L) +{ + Map *map = luaL_checkmap(L, 1); + int x = (int) luaL_checkinteger(L, 2); + int y = (int) luaL_checkinteger(L, 3); + int level = (int) luaL_checkinteger(L, 4); + + // Read the table + lua_settop(L, 5); + luaL_checktype(L, 5, LUA_TTABLE); + + lua_getfield(L, 5, "texturePath1"); + lua_getfield(L, 5, "texturePath2"); + lua_getfield(L, 5, "clipX"); + lua_getfield(L, 5, "clipY"); + + const char *texture_path_1 = luaL_checkstring(L, -4); + const char *texture_path_2 = luaL_checkstring(L, -3); + int clip_x = (int) luaL_checkinteger(L, -2); + int clip_y = (int) luaL_checkinteger(L, -1); + + Item *chest = item_builder_build_container(texture_path_1, + texture_path_2, + CLIP16(clip_x, clip_y)); + const Position *cr = &map->currentRoom; + chest->sprite->pos = (Position) { + cr->x * MAP_ROOM_WIDTH * TILE_DIMENSION + x * TILE_DIMENSION, + cr->y * MAP_ROOM_HEIGHT * TILE_DIMENSION + y * TILE_DIMENSION + }; + if (get_random(2) == 0) + linkedlist_append(&chest->items, item_builder_build_item(level, TREASURE)); + if (get_random(4) == 0) + linkedlist_append(&chest->items, item_builder_build_item(level, HEALTH)); + if (get_random(4) == 0) + linkedlist_append(&chest->items, item_builder_build_item(level, DAGGER)); + + lua_pop(L, 4); + + linkedlist_append(&map->items, chest); + + return 0; +} + static int l_add_monster(lua_State *L) { @@ -440,6 +487,9 @@ generate_map(unsigned int level, const char *file, SDL_Renderer *renderer) lua_pushcfunction(L, l_add_trap); lua_setglobal(L, "add_trap"); + lua_pushcfunction(L, l_add_chest); + lua_setglobal(L, "add_chest"); + lua_pushcfunction(L, l_add_texture); lua_setglobal(L, "add_texture");