2017-12-17 13:43:41 +01:00
|
|
|
#include <assert.h>
|
2017-12-13 14:26:30 +01:00
|
|
|
#include "monster.h"
|
|
|
|
#include "util.h"
|
2017-12-15 08:08:45 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "monster.h"
|
2017-12-17 13:43:41 +01:00
|
|
|
#include "random.h"
|
2017-12-13 14:26:30 +01:00
|
|
|
|
2017-12-15 15:03:29 +01:00
|
|
|
static void
|
|
|
|
monster_load_texts(Monster *m, SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
ActionText *t = actiontext_create();
|
|
|
|
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
|
|
|
|
t->color = (SDL_Color) { 255, 100, 0 };
|
|
|
|
actiontext_set_text(t, "HIT", renderer);
|
|
|
|
t->pos = m->sprite->pos;
|
|
|
|
m->hitText = t;
|
|
|
|
|
|
|
|
t = actiontext_create();
|
|
|
|
actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14);
|
|
|
|
t->color = (SDL_Color) { 255, 255, 0 };
|
|
|
|
actiontext_set_text(t, "MISS", renderer);
|
|
|
|
t->pos = m->sprite->pos;
|
|
|
|
m->missText = t;
|
|
|
|
}
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
Monster*
|
2017-12-15 15:03:29 +01:00
|
|
|
monster_create(SDL_Renderer *renderer)
|
2017-12-13 14:26:30 +01:00
|
|
|
{
|
|
|
|
Monster *m = ec_malloc(sizeof(Monster));
|
|
|
|
m->sprite = sprite_create();
|
2017-12-14 12:01:05 +01:00
|
|
|
m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
2017-12-17 13:43:41 +01:00
|
|
|
m->stats = (Stats) { 11, 1, 0, 0, 1, 1 };
|
2017-12-15 15:03:29 +01:00
|
|
|
|
|
|
|
monster_load_texts(m, renderer);
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2017-12-15 15:03:29 +01:00
|
|
|
void
|
|
|
|
monster_update_pos(Monster *m, Position pos)
|
|
|
|
{
|
|
|
|
m->sprite->pos = pos;
|
|
|
|
|
|
|
|
Position textPos = pos;
|
|
|
|
textPos.y += 10;
|
|
|
|
m->hitText->pos = textPos;
|
|
|
|
m->missText->pos = textPos;
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
static bool
|
|
|
|
has_collided(Monster *monster, RoomMatrix *matrix)
|
|
|
|
{
|
|
|
|
if (!position_in_room(&monster->sprite->pos, &matrix->roomPos))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Position roomPos = position_to_matrix_coords(&monster->sprite->pos);
|
|
|
|
RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y];
|
|
|
|
return space->occupied;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
move_left(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
m->sprite->pos.x -= TILE_DIMENSION;
|
|
|
|
if (has_collided(m, rm)) {
|
|
|
|
m->sprite->pos.x += TILE_DIMENSION;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
move_right(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
m->sprite->pos.x += TILE_DIMENSION;
|
|
|
|
if (has_collided(m, rm)) {
|
|
|
|
m->sprite->pos.x -= TILE_DIMENSION;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
move_up(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
m->sprite->pos.y -= TILE_DIMENSION;
|
|
|
|
if (has_collided(m, rm)) {
|
|
|
|
m->sprite->pos.y += TILE_DIMENSION;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
move_down(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
m->sprite->pos.y += TILE_DIMENSION;
|
|
|
|
if (has_collided(m, rm)) {
|
|
|
|
m->sprite->pos.y -= TILE_DIMENSION;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
monster_move(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
unsigned int i, maxMoveAttempts = 6;
|
|
|
|
Position monsterRoomPos;
|
|
|
|
|
|
|
|
if (get_random(4) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
monsterRoomPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false;
|
|
|
|
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < maxMoveAttempts; ++i) {
|
|
|
|
int move = get_random(3);
|
|
|
|
if (move == 0 && move_left(m, rm))
|
|
|
|
break;
|
|
|
|
else if (move == 1 && move_right(m, rm))
|
|
|
|
break;
|
|
|
|
else if (move == 2 && move_up(m, rm))
|
|
|
|
break;
|
|
|
|
else if (move == 3 && move_down(m, rm))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
monster_update_pos(m, m->sprite->pos);
|
|
|
|
|
|
|
|
monsterRoomPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = true;
|
|
|
|
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = m;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
void
|
|
|
|
monster_render(Monster *m, Camera *cam)
|
|
|
|
{
|
|
|
|
sprite_render(m->sprite, cam);
|
2017-12-15 15:03:29 +01:00
|
|
|
if (m->hitText)
|
|
|
|
actiontext_render(m->hitText, cam);
|
|
|
|
if (m->missText)
|
|
|
|
actiontext_render(m->missText, cam);
|
2017-12-13 20:31:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
void
|
|
|
|
monster_destroy(Monster *m)
|
|
|
|
{
|
|
|
|
sprite_destroy(m->sprite);
|
2017-12-15 15:03:29 +01:00
|
|
|
if (m->hitText)
|
|
|
|
actiontext_destroy(m->hitText);
|
|
|
|
if (m->missText)
|
|
|
|
actiontext_destroy(m->missText);
|
2017-12-13 14:26:30 +01:00
|
|
|
free(m);
|
|
|
|
}
|