Starts #31 Items: Chests

Lua gen code remains to do
This commit is contained in:
Linus Probert 2018-08-10 14:29:45 +02:00
parent 83aa92943f
commit 1c7631cf52
6 changed files with 97 additions and 16 deletions

View File

@ -26,6 +26,8 @@ item_create(void)
Item *item = ec_malloc(sizeof(Item)); Item *item = ec_malloc(sizeof(Item));
item->sprite = NULL; item->sprite = NULL;
item->collected = false; item->collected = false;
item->openable = false;
item->opened = false;
m_strcpy(item->label, 50, ""); m_strcpy(item->label, 50, "");
item->value = 0.0; item->value = 0.0;
item->items = NULL; item->items = NULL;
@ -42,18 +44,24 @@ item_render(Item *item, Camera *cam)
void void
item_collected(Item *item, Player *player) item_collected(Item *item, Player *player)
{ {
LinkedList *items = item->items; if (item->collected || item->opened)
return;
while (items != NULL) { while (item->items) {
Item *subitem = items->data; Item *subitem = linkedlist_pop(&item->items);
items = items->next;
item_collected(subitem, player); item_collected(subitem, player);
item_destroy(subitem);
} }
if (item->effect != NULL) if (item->effect != NULL)
item->effect(item, player); item->effect(item, player);
item->collected = true; if (!item->openable) {
item->collected = true;
} else {
item->opened = true;
item->sprite->texture_index = 1;
}
} }
void void

View File

@ -30,6 +30,8 @@
typedef struct Item_t { typedef struct Item_t {
Sprite *sprite; Sprite *sprite;
bool collected; bool collected;
bool openable;
bool opened;
char label[50]; char label[50];
double value; double value;
LinkedList *items; LinkedList *items;

View File

@ -25,7 +25,8 @@
#include "gui.h" #include "gui.h"
#include "mixer.h" #include "mixer.h"
#include "random.h" #include "random.h"
#include"texturecache.h" #include "texturecache.h"
#include "sprite.h"
static ItemBuilder *builder = NULL; static ItemBuilder *builder = NULL;
@ -78,16 +79,19 @@ pickup_dagger(Item *item, Player *player)
} }
static Item * 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;
item = item_create(); item = item_create();
t = texturecache_add(path); t0 = texturecache_add(path0);
if (path1)
t1 = texturecache_add(path1);
item->sprite = sprite_create(); 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->dim = GAME_DIMENSION;
item->sprite->clip = clip; item->sprite->clip = clip;
item->effect = cb; item->effect = cb;
@ -153,7 +157,7 @@ create_treasure(int current_level)
else if (amt <= 15) else if (amt <= 15)
clip.x += 32; 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); m_strcpy(item->label, 50, label);
item->value = amt; item->value = amt;
return item; return item;
@ -175,18 +179,21 @@ item_builder_build_item(ItemKey key, int level)
break; break;
case FLESH: case FLESH:
item = create_item(path_flesh, item = create_item(path_flesh,
NULL,
CLIP16(get_random(7) * 16, get_random(1) * 16), CLIP16(get_random(7) * 16, get_random(1) * 16),
&eat_flesh); &eat_flesh);
item->value = 1 + get_random(level); item->value = 1 + get_random(level);
break; break;
case HEALTH: case HEALTH:
item = create_item(path_potion, item = create_item(path_potion,
NULL,
CLIP16(0, 0), CLIP16(0, 0),
&drink_health); &drink_health);
item->value = 1 + get_random(level); item->value = 1 + get_random(level);
break; break;
case DAGGER: case DAGGER:
item = create_item(path_short_wep, item = create_item(path_short_wep,
NULL,
CLIP16(0, 0), CLIP16(0, 0),
&pickup_dagger); &pickup_dagger);
item->value = 1; item->value = 1;
@ -203,10 +210,23 @@ Item *
item_builder_build_sack(void) item_builder_build_sack(void)
{ {
return create_item("Items/Chest0.png", return create_item("Items/Chest0.png",
NULL,
CLIP16(0, 32), CLIP16(0, 32),
NULL); 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 void
item_builder_close(void) item_builder_close(void)
{ {

View File

@ -50,6 +50,9 @@ item_builder_build_item(ItemKey key, int level);
Item * Item *
item_builder_build_sack(void); item_builder_build_sack(void);
Item *
item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip);
void void
item_builder_close(void); item_builder_close(void);

View File

@ -538,14 +538,12 @@ run_game_render(void)
SDL_RenderSetViewport(gRenderer, &gameViewport); SDL_RenderSetViewport(gRenderer, &gameViewport);
map_render(gMap, gCamera); map_render(gMap, gCamera);
particle_engine_render_game(gCamera); particle_engine_render_game(gCamera);
if (!is_player_dead())
player_render(gPlayer, gCamera);
map_render_top_layer(gMap, gCamera); map_render_top_layer(gMap, gCamera);
if (!is_player_dead()) if (!is_player_dead()) {
player_render(gPlayer, gCamera);
player_render_toplayer(gPlayer, gCamera); player_render_toplayer(gPlayer, gCamera);
}
if (gPlayer->class == MAGE || gPlayer->class == PALADIN) if (gPlayer->class == MAGE || gPlayer->class == PALADIN)
roommatrix_render_mouse_square(gRoomMatrix, gCamera); roommatrix_render_mouse_square(gRoomMatrix, gCamera);

View File

@ -33,6 +33,9 @@
#include "io_util.h" #include "io_util.h"
#include "texturecache.h" #include "texturecache.h"
#include "trap.h" #include "trap.h"
#include "item.h"
#include "item_builder.h"
#include "random.h"
static static
lua_State* load_lua_state(void) lua_State* load_lua_state(void)
@ -297,6 +300,50 @@ l_add_trap(lua_State *L)
return 0; 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 static int
l_add_monster(lua_State *L) 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_pushcfunction(L, l_add_trap);
lua_setglobal(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_pushcfunction(L, l_add_texture);
lua_setglobal(L, "add_texture"); lua_setglobal(L, "add_texture");