breakhack/src/monster.c

321 lines
6.6 KiB
C
Raw Normal View History

2017-12-21 12:35:52 +01:00
#include <stdlib.h>
2017-12-17 13:43:41 +01:00
#include <assert.h>
2018-01-25 10:45:05 +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"
2018-01-23 14:11:03 +01:00
#include "gui.h"
2018-01-25 10:45:05 +01:00
#include "item.h"
#include "item_builder.h"
#include "map.h"
2018-02-03 23:39:49 +01:00
#include "particle_engine.h"
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, 255 };
2017-12-15 15:03:29 +01:00
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, 255 };
2017-12-15 15:03:29 +01:00
actiontext_set_text(t, "MISS", renderer);
t->pos = m->sprite->pos;
m->missText = t;
}
Monster*
2017-12-15 15:03:29 +01:00
monster_create(SDL_Renderer *renderer)
{
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 };
m->stats = (Stats) { 12, 12, 1, 0, 0, 1, 1 };
m->state.normal = PASSIVE;
m->state.challenge = AGRESSIVE;
m->state.current = m->state.normal;
2018-01-17 09:32:49 +01:00
m->label = NULL;
2018-01-25 10:45:05 +01:00
m->lclabel = NULL;
2017-12-15 15:03:29 +01:00
monster_load_texts(m, renderer);
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];
if (space->player && monster->state.current == AGRESSIVE) {
unsigned int dmg = stats_fight(&monster->stats,
&space->player->stats);
player_hit(space->player, dmg);
2018-01-23 14:11:03 +01:00
if (dmg > 0)
2018-01-25 10:45:05 +01:00
gui_log("%s hit you for %u damage", monster->label, dmg);
2018-01-23 14:11:03 +01:00
else
2018-01-25 10:45:05 +01:00
gui_log("%s missed you", monster->label);
}
2017-12-17 13:43:41 +01:00
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;
}
static void
monster_drunk_walk(Monster *m, RoomMatrix *rm)
2017-12-17 13:43:41 +01:00
{
unsigned int i, maxMoveAttempts = 6;
if (get_random(5) >= 2)
2017-12-17 13:43:41 +01:00
return;
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;
}
}
static void
monster_agressive_walk(Monster *m, RoomMatrix *rm)
{
int x_dist, y_dist;
Position mPos;
mPos = position_to_matrix_coords(&m->sprite->pos);
x_dist = mPos.x - rm->playerRoomPos.x;
y_dist = mPos.y - rm->playerRoomPos.y;
if (abs(x_dist) > abs(y_dist)) {
if (x_dist > 0)
move_left(m, rm);
else
move_right(m, rm);
} else {
if (y_dist > 0)
move_up(m, rm);
else
move_down(m, rm);
}
}
static void
monster_coward_walk(Monster *m, RoomMatrix *rm)
{
int x_dist, y_dist;
Position mPos;
mPos = position_to_matrix_coords(&m->sprite->pos);
x_dist = mPos.x - rm->playerRoomPos.x;
y_dist = mPos.y - rm->playerRoomPos.y;
if (abs(x_dist) > abs(y_dist)) {
if (x_dist > 0)
move_right(m, rm);
else
move_left(m, rm);
} else {
if (y_dist > 0)
move_down(m, rm);
else
move_up(m, rm);
}
}
void
monster_move(Monster *m, RoomMatrix *rm)
{
Position monsterRoomPos;
monsterRoomPos = position_to_matrix_coords(&m->sprite->pos);
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false;
rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL;
switch (m->state.current) {
case PASSIVE:
monster_drunk_walk(m, rm);
break;
case AGRESSIVE:
monster_agressive_walk(m, rm);
break;
case SCARED:
monster_coward_walk(m, rm);
break;
};
2017-12-17 13:43:41 +01:00
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;
}
void
monster_hit(Monster *monster, unsigned int dmg)
{
if (dmg > 0) {
monster->hitText->active = true;
monster->missText->active = false;
2018-02-03 23:39:49 +01:00
Position p = monster->sprite->pos;
p.x += 8;
p.y += 8;
Dimension d = { 8, 8 };
2018-02-09 13:55:57 +01:00
particle_engine_bloodspray(p, d, dmg);
} else {
monster->missText->active = true;
monster->hitText->active = false;
}
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
monster_drop_loot(Monster *monster, Map *map)
2018-01-25 10:45:05 +01:00
{
static unsigned int item_drop_chance = 3;
static unsigned int treasure_drop_chance = 2;
Item *item;
2018-01-30 13:44:21 +01:00
Item *items[2];
unsigned int item_count = 0;
if ((rand() % treasure_drop_chance) == 0) {
2018-01-31 09:15:33 +01:00
item = item_builder_build_item(TREASURE, map->level);
item->sprite->pos = monster->sprite->pos;
2018-01-30 13:44:21 +01:00
items[item_count++] = item;
}
if ((rand() % item_drop_chance) == 0) {
2018-01-31 09:15:33 +01:00
item = item_builder_build_item(rand() % TREASURE, map->level);
item->sprite->pos = monster->sprite->pos;
2018-01-30 13:44:21 +01:00
items[item_count++] = item;
}
2018-01-25 10:45:05 +01:00
2018-01-30 13:44:21 +01:00
if (item_count == 0)
return;
gui_log("%s dropped something", monster->label);
if (item_count == 1) {
linkedlist_append(&map->items, items[0]);
} else {
Item *container = item_builder_build_sack();
container->sprite->pos = monster->sprite->pos;
unsigned int i;
for (i = 0; i < item_count; ++i) {
linkedlist_append(&container->items, items[i]);
}
linkedlist_append(&map->items, container);
}
2018-01-25 10:45:05 +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);
}
void
monster_destroy(Monster *m)
{
sprite_destroy(m->sprite);
2018-01-17 09:32:49 +01:00
if (m->label)
free(m->label);
2018-01-25 10:45:05 +01:00
if (m->lclabel)
free(m->lclabel);
2017-12-15 15:03:29 +01:00
if (m->hitText)
actiontext_destroy(m->hitText);
if (m->missText)
actiontext_destroy(m->missText);
free(m);
}