Monster levels, player death and player levels

This commit is contained in:
Linus_Probert 2018-01-30 15:16:14 +01:00
parent 78fd6086f5
commit b13479b5e1
11 changed files with 88 additions and 41 deletions

View File

@ -116,7 +116,7 @@ end
-- END FUNCTIONS -- END FUNCTIONS
-- BEGIN SCRIPT -- BEGIN SCRIPT
map = create_map() -- 'map' needs to be global map = create_map(CURRENT_LEVEL) -- 'map' needs to be global
room_builder.load_textures(map) room_builder.load_textures(map)
local map_matrix = generate_path() local map_matrix = generate_path()

View File

@ -61,13 +61,6 @@ gui_create()
return gui; return gui;
} }
static void
clear_sprite_list(LinkedList *list)
{
while (list != NULL)
sprite_destroy(linkedlist_pop(&list));
}
void void
gui_set_max_health(Gui *gui, int max, SDL_Renderer *renderer) gui_set_max_health(Gui *gui, int max, SDL_Renderer *renderer)
{ {
@ -79,7 +72,9 @@ gui_set_max_health(Gui *gui, int max, SDL_Renderer *renderer)
if (((unsigned int) max / 3) == (unsigned int) linkedlist_size(gui->health)) if (((unsigned int) max / 3) == (unsigned int) linkedlist_size(gui->health))
return; return;
clear_sprite_list(gui->health); // Clear sprite list
while (gui->health != NULL)
sprite_destroy(linkedlist_pop(&gui->health));
texture = gui_add_texture(gui, "assets/GUI/GUI0.png", renderer); texture = gui_add_texture(gui, "assets/GUI/GUI0.png", renderer);
@ -87,7 +82,7 @@ gui_set_max_health(Gui *gui, int max, SDL_Renderer *renderer)
Sprite *sprite = sprite_create(); Sprite *sprite = sprite_create();
sprite->fixed = true; sprite->fixed = true;
sprite->clip = (SDL_Rect) { 0, 16, 16, 16 }; sprite->clip = (SDL_Rect) { 0, 16, 16, 16 };
sprite->pos = (Position) { 16 + i*16, 16 }; sprite->pos = (Position) { 16 + (i%8)*16, 16 + ((i-(i%8))/8)*16 };
sprite_set_texture(sprite, texture, 0); sprite_set_texture(sprite, texture, 0);
linkedlist_append(&gui->health, sprite); linkedlist_append(&gui->health, sprite);
} }

View File

@ -41,19 +41,11 @@ item_collected(Item *item, Player *player)
void void
item_destroy(Item *item) item_destroy(Item *item)
{ {
LinkedList *items;
if (item->sprite) if (item->sprite)
sprite_destroy(item->sprite); sprite_destroy(item->sprite);
items = item->items; while (item->items != NULL)
while (items != NULL) { item_destroy(linkedlist_pop(&item->items));
Item *subitem = items->data;
items->data = NULL;
items = items->next;
item_destroy(subitem);
}
linkedlist_destroy(&items);
free(item); free(item);
} }

View File

@ -39,7 +39,7 @@ static void
eat_flesh(Item *item, Player *player) eat_flesh(Item *item, Player *player)
{ {
int original_hp = player->stats.hp; int original_hp = player->stats.hp;
player->stats.hp += (int) item->value; player->stats.hp += (int) item->value * player->stats.lvl;
if (player->stats.hp > player->stats.maxhp) if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp; player->stats.hp = player->stats.maxhp;
@ -50,7 +50,7 @@ static void
drink_health(Item *item, Player *player) drink_health(Item *item, Player *player)
{ {
int original_hp = player->stats.hp; int original_hp = player->stats.hp;
player->stats.hp += (int) item->value; player->stats.hp += (int) item->value * player->stats.lvl;
if (player->stats.hp > player->stats.maxhp) if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp; player->stats.hp = player->stats.maxhp;

View File

@ -165,6 +165,16 @@ bool handle_events(void)
return quit; return quit;
} }
static bool
check_if_dead(void)
{
if (gPlayer->stats.hp <= 0) {
gui_log("The dungeon consumed you");
return true;
}
return false;
}
static void static void
check_next_level(void) check_next_level(void)
{ {
@ -220,6 +230,9 @@ run_game(void)
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
if (check_if_dead())
gGameState = GAME_OVER;
check_next_level(); check_next_level();
} }
@ -242,13 +255,13 @@ void run(void)
run_game(); run_game();
break; break;
case MENU: case MENU:
error("MENU not implemented"); fatal("MENU not implemented");
break; break;
case IN_GAME_MENU: case IN_GAME_MENU:
error("IN_GAME_MENU not implemented"); fatal("IN_GAME_MENU not implemented");
break; break;
case GAME_OVER: case GAME_OVER:
error("GAME_OVER not implemented"); fatal("GAME_OVER not implemented");
break; break;
default: default:
break; break;

View File

@ -155,6 +155,7 @@ map_clear_collected_items(Map *map)
void void
map_add_monster(Map *map, Monster *m) map_add_monster(Map *map, Monster *m)
{ {
monster_update_stats_for_level(m, map->level);
linkedlist_append(&map->monsters, m); linkedlist_append(&map->monsters, m);
} }

View File

@ -22,6 +22,7 @@ static
int l_create_map(lua_State *L) int l_create_map(lua_State *L)
{ {
Map *map = map_create(); Map *map = map_create();
map->level = luaL_checkinteger(L, 1);
lua_pushlightuserdata(L, map); lua_pushlightuserdata(L, map);
return 1; return 1;
} }

View File

@ -239,6 +239,17 @@ monster_hit(Monster *monster, unsigned int dmg)
monster->state.current = monster->state.challenge; monster->state.current = monster->state.challenge;
} }
void
monster_update_stats_for_level(Monster *m, unsigned int level)
{
m->stats.lvl = level;
m->stats.maxhp *= level;
m->stats.hp = m->stats.maxhp;
m->stats.dmg *= level;
m->stats.atk *= level;
m->stats.def *= level;
}
void void
monster_drop_loot(Monster *monster, Map *map) monster_drop_loot(Monster *monster, Map *map)
{ {
@ -248,12 +259,12 @@ monster_drop_loot(Monster *monster, Map *map)
Item *items[2]; Item *items[2];
unsigned int item_count = 0; unsigned int item_count = 0;
if ((rand() % treasure_drop_chance) == 0 || true) { if ((rand() % treasure_drop_chance) == 0) {
item = item_builder_build_item(TREASURE); item = item_builder_build_item(TREASURE);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
items[item_count++] = item; items[item_count++] = item;
} }
if ((rand() % item_drop_chance) == 0 || true) { if ((rand() % item_drop_chance) == 0) {
item = item_builder_build_item(rand() % TREASURE); item = item_builder_build_item(rand() % TREASURE);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
items[item_count++] = item; items[item_count++] = item;

View File

@ -26,16 +26,25 @@ typedef struct Monster_t {
Monster* monster_create(SDL_Renderer*); Monster* monster_create(SDL_Renderer*);
void monster_update_pos(Monster*, Position); void
monster_update_pos(Monster*, Position);
void monster_move(Monster*, RoomMatrix*); void
monster_move(Monster*, RoomMatrix*);
void monster_render(Monster*, Camera*); void
monster_render(Monster*, Camera*);
void monster_hit(Monster*, unsigned int dmg); void
monster_hit(Monster*, unsigned int dmg);
void monster_drop_loot(Monster*, Map*); void
monster_update_stats_for_level(Monster*, unsigned int level);
void monster_destroy(Monster*); void
monster_drop_loot(Monster*, Map*);
void
monster_destroy(Monster*);
#endif // MONSTER_H_ #endif // MONSTER_H_

View File

@ -14,6 +14,26 @@
#define ROGUE_STATS { 12, 12, 5, 7, 3, 2, 1 } #define ROGUE_STATS { 12, 12, 5, 7, 3, 2, 1 }
#define WARRIOR_STATS { 12, 12, 8, 9, 2, 1, 1 } #define WARRIOR_STATS { 12, 12, 8, 9, 2, 1, 1 }
static void
player_levelup(Player *player)
{
player->stats.lvl += 1;
player->stats.maxhp += 3;
player->stats.dmg += 5;
player->stats.atk += 1;
player->stats.def += 1;
}
static void
player_gain_xp(Player *player, unsigned int xp_gain)
{
player->xp += xp_gain;
if (player->xp >= (player->stats.lvl * 50) + ((player->stats.lvl - 1) * 150)) {
player_levelup(player);
gui_log("You have reached level %u", player->stats.lvl);
}
}
static bool static bool
has_collided(Player *player, RoomMatrix *matrix) has_collided(Player *player, RoomMatrix *matrix)
{ {
@ -46,12 +66,12 @@ has_collided(Player *player, RoomMatrix *matrix)
gui_log("You missed %s", space->monster->lclabel); gui_log("You missed %s", space->monster->lclabel);
if (space->monster->stats.hp <= 0) { if (space->monster->stats.hp <= 0) {
// TODO(Linus): This needs some love later on. unsigned int gained_xp = 5 * space->monster->stats.lvl;
player->kills += 1; player->kills += 1;
player->xp += 10;
gui_log("You killed %s and gained %d xp", gui_log("You killed %s and gained %d xp",
space->monster->lclabel, 10); space->monster->lclabel, gained_xp);
player_gain_xp(player, gained_xp);
} }
} else if (collided) { } else if (collided) {
gui_log("Ouch! There is something in the way"); gui_log("Ouch! There is something in the way");

View File

@ -25,14 +25,19 @@ typedef struct Player_t {
void (*handle_event)(struct Player_t*, RoomMatrix*, SDL_Event*); void (*handle_event)(struct Player_t*, RoomMatrix*, SDL_Event*);
} Player; } Player;
Player* player_create(class_t, SDL_Renderer*); Player*
player_create(class_t, SDL_Renderer*);
void player_hit(Player*, unsigned int dmg); void
player_hit(Player*, unsigned int dmg);
void player_reset_steps(Player*); void
player_reset_steps(Player*);
void player_render(Player*, Camera*); void
player_render(Player*, Camera*);
void player_destroy(Player*); void
player_destroy(Player*);
#endif // PLAYER_H_ #endif // PLAYER_H_