Monsters now indicate their state with speech bubbles

This commit is contained in:
Linus Probert 2018-05-17 21:41:23 +02:00
parent f049d66219
commit c4dc8382a9
6 changed files with 90 additions and 15 deletions

View File

@ -521,6 +521,8 @@ run_game(void)
if (!is_player_dead()) if (!is_player_dead())
player_render(gPlayer, gCamera); player_render(gPlayer, gCamera);
map_render_top_layer(gMap, 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

@ -246,8 +246,6 @@ map_update(UpdateData *data)
void map_render(Map *map, Camera *cam) void map_render(Map *map, Camera *cam)
{ {
unsigned int i, j; unsigned int i, j;
LinkedList *monsterItem;
LinkedList *items;
Room *room; Room *room;
if (!timer_started(map->renderTimer)) { if (!timer_started(map->renderTimer)) {
@ -278,14 +276,19 @@ void map_render(Map *map, Camera *cam)
particle_engine_wind(room->modifier.data.wind.direction); particle_engine_wind(room->modifier.data.wind.direction);
} }
monsterItem = map->monsters; }
void
map_render_top_layer(Map *map, Camera *cam)
{
LinkedList *monsterItem = map->monsters;
while (monsterItem != NULL) { while (monsterItem != NULL) {
Monster *monster = monsterItem->data; Monster *monster = monsterItem->data;
monsterItem = monsterItem->next; monsterItem = monsterItem->next;
monster_render(monster, cam); monster_render(monster, cam);
} }
items = map->items; LinkedList *items = map->items;
while (items != NULL) { while (items != NULL) {
Item *item = items->data; Item *item = items->data;
items = items->next; items = items->next;

View File

@ -94,6 +94,9 @@ map_update(struct UpdateData_t*);
void void
map_render(Map*, Camera*); map_render(Map*, Camera*);
void
map_render_top_layer(Map*, Camera*);
void void
map_set_current_room(Map*, Position*); map_set_current_room(Map*, Position*);

View File

@ -328,9 +328,7 @@ l_add_monster(lua_State *L)
monster_update_pos(monster, (Position) { x, y }); monster_update_pos(monster, (Position) { x, y });
sprite_set_texture(monster->sprite, texture1, 0); sprite_set_texture(monster->sprite, texture1, 0);
sprite_set_texture(monster->sprite, texture2, 1); sprite_set_texture(monster->sprite, texture2, 1);
monster->state.normal = nstate; monster_set_states(monster, nstate, cstate);
monster->state.challenge = cstate;
monster->state.current = nstate;
if (strlen(label)) { if (strlen(label)) {
monster->label = label; monster->label = label;
monster->lclabel = to_lower(label); monster->lclabel = to_lower(label);

View File

@ -32,6 +32,7 @@
#include "defines.h" #include "defines.h"
#include "update_data.h" #include "update_data.h"
#include "actiontextbuilder.h" #include "actiontextbuilder.h"
#include "texturecache.h"
Monster* Monster*
monster_create(void) monster_create(void)
@ -51,12 +52,16 @@ monster_create(void)
1 // lvl 1 // lvl
}; };
m->state.normal = PASSIVE;
m->state.challenge = AGRESSIVE;
m->state.current = m->state.normal;
m->label = NULL; m->label = NULL;
m->lclabel = NULL; m->lclabel = NULL;
m->steps = 0; m->steps = 0;
m->stateIndicator.sprite = sprite_create();
sprite_set_texture(m->stateIndicator.sprite, texturecache_add("GUI/GUI0.png"), 0);
sprite_set_texture(m->stateIndicator.sprite, texturecache_add("GUI/GUI1.png"), 1);
m->stateIndicator.sprite->dim = GAME_DIMENSION;
m->stateIndicator.displayCount = 0;
m->stateIndicator.shownOnPlayerRoomEnter = false;
monster_set_states(m, PASSIVE, AGRESSIVE);
return m; return m;
} }
@ -66,8 +71,9 @@ monster_update_pos(Monster *m, Position pos)
{ {
m->sprite->pos = pos; m->sprite->pos = pos;
Position textPos = pos; Position indicatorPos = pos;
textPos.y += 10; indicatorPos.y -= 32;
m->stateIndicator.sprite->pos = indicatorPos;
} }
static bool static bool
@ -253,6 +259,8 @@ monster_move(Monster *m, RoomMatrix *rm)
m->steps++; m->steps++;
if (m->steps >= m->stats.speed) { if (m->steps >= m->stats.speed) {
if (m->stateIndicator.displayCount > 0)
m->stateIndicator.displayCount -= 1;
m->steps = 0; m->steps = 0;
return true; return true;
} }
@ -263,8 +271,47 @@ monster_move(Monster *m, RoomMatrix *rm)
void void
monster_update(Monster *m, UpdateData *data) monster_update(Monster *m, UpdateData *data)
{ {
UNUSED(m); Position monsterRoomPos = position_to_room_coords(&m->sprite->pos);
UNUSED(data); if (position_equals(&data->matrix->roomPos, &monsterRoomPos)) {
if (m->stateIndicator.shownOnPlayerRoomEnter)
return;
m->stateIndicator.shownOnPlayerRoomEnter = true;
if (m->state.current != PASSIVE)
m->stateIndicator.displayCount = 5;
} else {
m->stateIndicator.shownOnPlayerRoomEnter = false;
}
}
static void
monster_set_sprite_clip_for_current_state(Monster *m)
{
switch (m->state.current) {
case AGRESSIVE:
m->stateIndicator.sprite->clip = CLIP16(16 * 11, 16 * 3);
break;
case PASSIVE:
m->stateIndicator.sprite->clip = CLIP16(16 * 10, 16);
break;
case SCARED:
m->stateIndicator.sprite->clip = CLIP16(16 * 12, 16 * 3);
break;
default:
break;
}
}
static void
monster_state_change(Monster *m)
{
if (m->state.current == m->state.challenge)
return;
m->state.current = m->state.challenge;
m->stateIndicator.displayCount = 5;
monster_set_sprite_clip_for_current_state(m);
} }
void void
@ -287,7 +334,7 @@ monster_hit(Monster *monster, unsigned int dmg)
&monster->sprite->pos); &monster->sprite->pos);
} }
monster->state.current = monster->state.challenge; monster_state_change(monster);
} }
void void
@ -355,6 +402,17 @@ void
monster_render(Monster *m, Camera *cam) monster_render(Monster *m, Camera *cam)
{ {
sprite_render(m->sprite, cam); sprite_render(m->sprite, cam);
if (m->stateIndicator.displayCount > 0)
sprite_render(m->stateIndicator.sprite, cam);
}
void
monster_set_states(Monster *m, StateType normal, StateType challenge)
{
m->state.normal = normal;
m->state.current = normal;
m->state.challenge = challenge;
monster_set_sprite_clip_for_current_state(m);
} }
void void
@ -365,5 +423,6 @@ monster_destroy(Monster *m)
free(m->label); free(m->label);
if (m->lclabel) if (m->lclabel)
free(m->lclabel); free(m->lclabel);
sprite_destroy(m->stateIndicator.sprite);
free(m); free(m);
} }

View File

@ -36,12 +36,19 @@ typedef struct {
StateType challenge; StateType challenge;
} State; } State;
typedef struct MonsterStateIndicator {
Sprite *sprite;
bool shownOnPlayerRoomEnter;
int displayCount;
} MonsterStateIndicator;
typedef struct Monster_t { typedef struct Monster_t {
char *label; char *label;
char *lclabel; char *lclabel;
Sprite *sprite; Sprite *sprite;
Stats stats; Stats stats;
State state; State state;
MonsterStateIndicator stateIndicator;
unsigned int steps; unsigned int steps;
} Monster; } Monster;
@ -68,6 +75,9 @@ monster_update(Monster*, struct UpdateData_t*);
void void
monster_drop_loot(Monster*, Map*, Player*); monster_drop_loot(Monster*, Map*, Player*);
void
monster_set_states(Monster *, StateType normal, StateType challenge);
void void
monster_destroy(Monster*); monster_destroy(Monster*);