Added enemy behaviour to lua and monster states.

This commit is contained in:
Linus Probert 2017-12-18 12:12:24 +01:00
parent 995a2f4475
commit 1c48f1dd9a
7 changed files with 85 additions and 44 deletions

3
.vimrc
View File

@ -1,3 +1,6 @@
nnoremap <F1> :Make<cr> nnoremap <F1> :Make<cr>
au FileType c setl makeprg=ninja\ -C\ build au FileType c setl makeprg=ninja\ -C\ build
au FileType h setl makeprg=ninja\ -C\ build
let g:syntastic_c_include_dirs = [ 'linkedlist', 'hashtable' ] let g:syntastic_c_include_dirs = [ 'linkedlist', 'hashtable' ]

View File

@ -7,13 +7,16 @@ x Add enemies (generated through lua)
x Move "clip" from texture to sprite x Move "clip" from texture to sprite
x Hitting enemies x Hitting enemies
x Nicer enemy hits (Text textures, healthbars?) x Nicer enemy hits (Text textures, healthbars?)
- This could need some love later on. (Misses seem to be broken) (Add hits to stats) - This could need some love later on.
- Add hits to stats
- Player hits
x Moving enemies x Moving enemies
x Stupid roaming enemies x Stupid roaming enemies
x Smart agressive enemies x Smart agressive enemies
x Fleeing enemies x Fleeing enemies
- Add hooks to lua x Add hooks to lua
- Lower levels x Add movement depending on state (aggro, scared, passive)
- Lower dungeon levels
o XP o XP
- Some xp amount logic - Some xp amount logic
- Level threshholds - Level threshholds

View File

@ -36,6 +36,13 @@ local texturePaths = {
undead1 = "assets/Characters/Undead1.png", undead1 = "assets/Characters/Undead1.png",
} }
local state = {
passive = 0,
agressive = 1,
scared = 2,
}
local enemies = { local enemies = {
-- PESTS -- PESTS
@ -87,32 +94,33 @@ local enemies = {
{ texturePaths.pest0, texturePaths.pest1, 48, 112 }, { texturePaths.pest0, texturePaths.pest1, 48, 112 },
-- UNDEAD -- UNDEAD
{ texturePaths.undead0, texturePaths.undead1, 0, 0 }; { texturePaths.undead0, texturePaths.undead1, 0, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 16, 0 }; { texturePaths.undead0, texturePaths.undead1, 16, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 32, 0 }; { texturePaths.undead0, texturePaths.undead1, 32, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 48, 0 }; { texturePaths.undead0, texturePaths.undead1, 48, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 64, 0 }; { texturePaths.undead0, texturePaths.undead1, 64, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 80, 0 }; { texturePaths.undead0, texturePaths.undead1, 80, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 96, 0 }; { texturePaths.undead0, texturePaths.undead1, 96, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 112, 0 }; { texturePaths.undead0, texturePaths.undead1, 112, 0, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 0, 16 }; { texturePaths.undead0, texturePaths.undead1, 0, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 16, 16 }; { texturePaths.undead0, texturePaths.undead1, 16, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 32, 16 }; { texturePaths.undead0, texturePaths.undead1, 32, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 48, 16 }; { texturePaths.undead0, texturePaths.undead1, 48, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 64, 16 }; { texturePaths.undead0, texturePaths.undead1, 64, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 80, 16 }; { texturePaths.undead0, texturePaths.undead1, 80, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 96, 16 }; { texturePaths.undead0, texturePaths.undead1, 96, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 112, 16 }; { texturePaths.undead0, texturePaths.undead1, 112, 16, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 0, 32 }; { texturePaths.undead0, texturePaths.undead1, 0, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 16, 32 }; { texturePaths.undead0, texturePaths.undead1, 16, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 32, 32 }; { texturePaths.undead0, texturePaths.undead1, 32, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 48, 32 }; { texturePaths.undead0, texturePaths.undead1, 48, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 64, 32 }; { texturePaths.undead0, texturePaths.undead1, 64, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 80, 32 }; { texturePaths.undead0, texturePaths.undead1, 80, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 96, 32 }; { texturePaths.undead0, texturePaths.undead1, 96, 32, state.passive, state.agressive };
{ texturePaths.undead0, texturePaths.undead1, 112, 32 }; { texturePaths.undead0, texturePaths.undead1, 112, 32,
state.passive, state.scared };
} }
local function repack(data) local function repack(data)
@ -121,6 +129,8 @@ local function repack(data)
texturePath2 = data[2], texturePath2 = data[2],
clipX = data[3], clipX = data[3],
clipY = data[4], clipY = data[4],
nstate = data[5] or state.passive,
cstate = data[6] or state.scared
} }
end end

View File

@ -149,7 +149,7 @@ l_add_monster(lua_State *L)
{ {
Monster *monster; Monster *monster;
Map *map; Map *map;
int x, y, clip_x, clip_y; int x, y, clip_x, clip_y, nstate, cstate;
const char *texture_path_1, *texture_path_2; const char *texture_path_1, *texture_path_2;
Texture *texture1, *texture2; Texture *texture1, *texture2;
SDL_Renderer *renderer; SDL_Renderer *renderer;
@ -167,11 +167,15 @@ l_add_monster(lua_State *L)
lua_getfield(L, 4, "texturePath2"); lua_getfield(L, 4, "texturePath2");
lua_getfield(L, 4, "clipX"); lua_getfield(L, 4, "clipX");
lua_getfield(L, 4, "clipY"); lua_getfield(L, 4, "clipY");
lua_getfield(L, 4, "nstate");
lua_getfield(L, 4, "cstate");
texture_path_1 = luaL_checkstring(L, -4); texture_path_1 = luaL_checkstring(L, -6);
texture_path_2 = luaL_checkstring(L, -3); texture_path_2 = luaL_checkstring(L, -5);
clip_x = luaL_checkinteger(L, -2); clip_x = luaL_checkinteger(L, -4);
clip_y = luaL_checkinteger(L, -1); clip_y = luaL_checkinteger(L, -3);
nstate = luaL_checkinteger(L, -2);
cstate = luaL_checkinteger(L, -1);
texture1 = map_add_monster_texture(map, texture_path_1, renderer); texture1 = map_add_monster_texture(map, texture_path_1, renderer);
texture2 = map_add_monster_texture(map, texture_path_2, renderer); texture2 = map_add_monster_texture(map, texture_path_2, renderer);
@ -186,6 +190,9 @@ 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->state.challenge = cstate;
monster->state.current = nstate;
map_add_monster(map, monster); map_add_monster(map, monster);

View File

@ -30,7 +30,9 @@ monster_create(SDL_Renderer *renderer)
m->sprite = sprite_create(); m->sprite = sprite_create();
m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 }; m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
m->stats = (Stats) { 11, 1, 0, 0, 1, 1 }; m->stats = (Stats) { 11, 1, 0, 0, 1, 1 };
m->state = AGRESSIVE; m->state.normal = PASSIVE;
m->state.challenge = AGRESSIVE;
m->state.current = m->state.normal;
monster_load_texts(m, renderer); monster_load_texts(m, renderer);
@ -113,7 +115,7 @@ monster_drunk_walk(Monster *m, RoomMatrix *rm)
{ {
unsigned int i, maxMoveAttempts = 6; unsigned int i, maxMoveAttempts = 6;
if (get_random(2) == 0) if (get_random(5) >= 2)
return; return;
for (i = 0; i < maxMoveAttempts; ++i) { for (i = 0; i < maxMoveAttempts; ++i) {
@ -188,14 +190,14 @@ monster_move(Monster *m, RoomMatrix *rm)
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false;
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL;
switch (m->state) { switch (m->state.current) {
case PASSIVE: case PASSIVE:
monster_drunk_walk(m, rm); monster_drunk_walk(m, rm);
break; break;
case AGRESSIVE: case AGRESSIVE:
monster_agressive_walk(m, rm); monster_agressive_walk(m, rm);
break; break;
case COWARD: case SCARED:
monster_coward_walk(m, rm); monster_coward_walk(m, rm);
break; break;
}; };
@ -207,6 +209,20 @@ monster_move(Monster *m, RoomMatrix *rm)
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m; rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m;
} }
void
monster_hit(Monster *monster, unsigned int dmg)
{
if (dmg > 0) {
monster->hitText->active = true;
monster->missText->active = false;
} else {
monster->missText->active = true;
monster->hitText->active = false;
}
monster->state.current = monster->state.challenge;
}
void void
monster_render(Monster *m, Camera *cam) monster_render(Monster *m, Camera *cam)
{ {

View File

@ -6,7 +6,13 @@
#include "stats.h" #include "stats.h"
#include "actiontext.h" #include "actiontext.h"
typedef enum { PASSIVE, AGRESSIVE, COWARD } State; typedef enum { PASSIVE, AGRESSIVE, SCARED } StateType;
typedef struct {
StateType current;
StateType normal;
StateType challenge;
} State;
typedef struct Monster_t { typedef struct Monster_t {
Sprite *sprite; Sprite *sprite;
@ -24,6 +30,8 @@ void monster_move(Monster*, RoomMatrix*);
void monster_render(Monster*, Camera*); void monster_render(Monster*, Camera*);
void monster_hit(Monster*, unsigned int dmg);
void monster_destroy(Monster*); void monster_destroy(Monster*);
#endif // MONSTER_H_ #endif // MONSTER_H_

View File

@ -27,13 +27,7 @@ has_collided(Player *player, RoomMatrix *matrix)
if (space->monster != NULL) { if (space->monster != NULL) {
unsigned int hit = stats_fight(&player->stats, unsigned int hit = stats_fight(&player->stats,
&space->monster->stats); &space->monster->stats);
if (hit > 0) { monster_hit(space->monster, hit);
space->monster->hitText->active = true;
space->monster->missText->active = false;
} else {
space->monster->missText->active = true;
space->monster->hitText->active = false;
}
if (space->monster->stats.hp <= 0) { if (space->monster->stats.hp <= 0) {
// TODO(Linus): This needs some love later on. // TODO(Linus): This needs some love later on.