2018-02-24 07:13:28 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-24 21:24:19 +01:00
|
|
|
#include <stdlib.h>
|
2018-02-24 07:13:28 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include "texturecache.h"
|
|
|
|
#include "skill.h"
|
|
|
|
#include "util.h"
|
2018-02-28 22:31:38 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "roommatrix.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "monster.h"
|
|
|
|
#include "mixer.h"
|
|
|
|
#include "gui.h"
|
|
|
|
#include "random.h"
|
2018-03-01 19:37:57 +01:00
|
|
|
#include "particle_engine.h"
|
2018-03-10 22:04:03 +01:00
|
|
|
#include "projectile.h"
|
|
|
|
#include "linkedlist.h"
|
2018-03-12 09:09:03 +01:00
|
|
|
#include "item.h"
|
2018-02-24 07:13:28 +01:00
|
|
|
|
2018-03-01 13:48:03 +01:00
|
|
|
static void
|
|
|
|
set_player_clip_for_direction(Player *player, Vector2d *direction)
|
|
|
|
{
|
|
|
|
if (direction->y > 0) // Down
|
|
|
|
player->sprite->clip.y = 0;
|
|
|
|
else if (direction->y < 0) // Up
|
|
|
|
player->sprite->clip.y = 48;
|
|
|
|
else if (direction->x < 0) // Left
|
|
|
|
player->sprite->clip.y = 16;
|
2018-03-01 19:37:57 +01:00
|
|
|
else if (direction->x > 0) // Right
|
2018-03-01 13:48:03 +01:00
|
|
|
player->sprite->clip.y = 32;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:13:28 +01:00
|
|
|
static Skill *
|
|
|
|
create_default(const char *s_label, Sprite *s)
|
|
|
|
{
|
|
|
|
Skill *skill = ec_malloc(sizeof(Skill));
|
|
|
|
m_strcpy(skill->label, 20, s_label);
|
2018-02-28 15:28:45 +01:00
|
|
|
skill->resetTime = 5;
|
|
|
|
skill->resetCountdown = 0;
|
2018-02-24 07:13:28 +01:00
|
|
|
skill->icon = s;
|
2018-02-28 22:31:38 +01:00
|
|
|
skill->actionRequired = true;
|
|
|
|
skill->instantUse = false;
|
|
|
|
skill->active = false;
|
2018-03-13 16:13:54 +01:00
|
|
|
skill->available = NULL;
|
2018-02-24 07:13:28 +01:00
|
|
|
skill->use = NULL;
|
2018-03-23 22:03:34 +01:00
|
|
|
skill->levelcap = 1;
|
2018-02-24 07:13:28 +01:00
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:28:45 +01:00
|
|
|
static bool
|
|
|
|
skill_use_flurry(Skill *skill, SkillData *data)
|
|
|
|
{
|
2018-03-01 06:04:12 +01:00
|
|
|
UNUSED (skill);
|
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
Position playerPos = position_to_matrix_coords(&data->player->sprite->pos);
|
|
|
|
Position targetPos = playerPos;
|
|
|
|
targetPos.x += (int) data->direction.x;
|
|
|
|
targetPos.y += (int) data->direction.y;
|
|
|
|
|
2018-03-01 13:48:03 +01:00
|
|
|
set_player_clip_for_direction(data->player, &data->direction);
|
|
|
|
|
2018-03-01 19:37:57 +01:00
|
|
|
if (!position_in_roommatrix(&targetPos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
Monster *monster = data->matrix->spaces[targetPos.x][targetPos.y].monster;
|
2018-03-01 06:47:16 +01:00
|
|
|
mixer_play_effect(TRIPPLE_SWING);
|
2018-02-28 22:31:38 +01:00
|
|
|
if (monster) {
|
|
|
|
gui_log("You attack %s with a flurry of strikes", monster->lclabel);
|
2018-03-01 06:47:16 +01:00
|
|
|
unsigned int hitCount = 0;
|
2018-02-28 22:31:38 +01:00
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
2018-03-01 06:47:16 +01:00
|
|
|
unsigned int originalHp = monster->stats.hp;
|
2018-02-28 22:31:38 +01:00
|
|
|
unsigned int dmg = stats_fight(&data->player->stats, &monster->stats);
|
2018-03-01 06:47:16 +01:00
|
|
|
if (dmg > 0 && originalHp > 0) {
|
2018-02-28 22:31:38 +01:00
|
|
|
gui_log("You hit for %u damage", dmg);
|
2018-03-01 06:47:16 +01:00
|
|
|
hitCount++;
|
2018-02-28 22:31:38 +01:00
|
|
|
}
|
2018-03-01 19:37:57 +01:00
|
|
|
monster_hit(monster, dmg);
|
2018-02-28 22:31:38 +01:00
|
|
|
}
|
2018-03-01 06:47:16 +01:00
|
|
|
if (hitCount == 1) {
|
|
|
|
mixer_play_effect(SWORD_HIT);
|
|
|
|
} else if (hitCount == 2) {
|
|
|
|
mixer_play_effect(DOUBLE_SWORD_HIT);
|
|
|
|
} else if (hitCount == 3) {
|
|
|
|
mixer_play_effect(TRIPPLE_SWORD_HIT);
|
|
|
|
}
|
2018-02-28 22:31:38 +01:00
|
|
|
|
2018-03-25 23:30:26 +02:00
|
|
|
data->player->stat_data.hits += hitCount;
|
2018-03-01 13:48:03 +01:00
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
} else {
|
|
|
|
gui_log("You swing at thin air with a flurry of strikes");
|
|
|
|
}
|
|
|
|
player_monster_kill_check(data->player, monster);
|
|
|
|
|
2018-02-28 15:28:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:13:28 +01:00
|
|
|
static Skill *
|
|
|
|
create_flurry(void)
|
|
|
|
{
|
2018-03-11 21:06:46 +01:00
|
|
|
Texture *t = texturecache_add("Extras/Skills.png");
|
2018-02-24 07:13:28 +01:00
|
|
|
Sprite *s = sprite_create();
|
|
|
|
sprite_set_texture(s, t, 0);
|
2018-03-11 21:06:46 +01:00
|
|
|
s->dim = GAME_DIMENSION;
|
|
|
|
s->clip = CLIP32(0, 0);
|
2018-02-24 07:13:28 +01:00
|
|
|
s->fixed = true;
|
|
|
|
Skill *skill = create_default("Flurry", s);
|
2018-03-23 22:03:34 +01:00
|
|
|
skill->levelcap = 2;
|
2018-02-28 15:28:45 +01:00
|
|
|
skill->use = skill_use_flurry;
|
2018-02-24 07:13:28 +01:00
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:13:54 +01:00
|
|
|
static bool
|
|
|
|
skill_throw_dagger_available(Player *player)
|
|
|
|
{
|
|
|
|
return player->daggers > 0;
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:04:03 +01:00
|
|
|
static bool
|
|
|
|
skill_throw_dagger(Skill *skill, SkillData *data)
|
|
|
|
{
|
|
|
|
UNUSED(skill);
|
|
|
|
|
2018-03-13 16:13:54 +01:00
|
|
|
if (data->player->daggers == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data->player->daggers--;
|
|
|
|
|
2018-03-10 22:04:03 +01:00
|
|
|
Projectile *p = projectile_dagger_create();
|
|
|
|
if (vector2d_equals(VECTOR2D_UP, data->direction))
|
2018-03-12 09:09:03 +01:00
|
|
|
p->velocity = (Vector2d) { 0, -DAGGER_VELOCITY };
|
2018-03-10 22:04:03 +01:00
|
|
|
else if (vector2d_equals(VECTOR2D_DOWN, data->direction))
|
2018-03-12 09:09:03 +01:00
|
|
|
p->velocity = (Vector2d) { 0, DAGGER_VELOCITY };
|
2018-03-10 22:04:03 +01:00
|
|
|
else if (vector2d_equals(VECTOR2D_RIGHT, data->direction))
|
2018-03-12 09:09:03 +01:00
|
|
|
p->velocity = (Vector2d) { DAGGER_VELOCITY, 0 };
|
2018-03-10 22:04:03 +01:00
|
|
|
else
|
2018-03-12 09:09:03 +01:00
|
|
|
p->velocity = (Vector2d) { -DAGGER_VELOCITY, 0 };
|
2018-03-10 22:04:03 +01:00
|
|
|
|
2018-03-12 12:35:28 +01:00
|
|
|
set_player_clip_for_direction(data->player, &data->direction);
|
|
|
|
|
2018-03-12 09:09:03 +01:00
|
|
|
mixer_play_effect(SWOOSH);
|
2018-03-10 22:04:03 +01:00
|
|
|
p->sprite->pos = data->player->sprite->pos;
|
|
|
|
linkedlist_append(&data->player->projectiles, p);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Skill *
|
|
|
|
create_throw_dagger(void)
|
|
|
|
{
|
2018-03-11 21:06:46 +01:00
|
|
|
Texture *t = texturecache_add("Extras/Skills.png");
|
2018-03-10 22:04:03 +01:00
|
|
|
Sprite *s = sprite_create();
|
|
|
|
sprite_set_texture(s, t, 0);
|
2018-03-11 21:06:46 +01:00
|
|
|
s->dim = GAME_DIMENSION;
|
|
|
|
s->clip = CLIP32(64, 0);
|
2018-03-10 22:04:03 +01:00
|
|
|
s->fixed = true;
|
|
|
|
Skill *skill = create_default("Throw dagger", s);
|
2018-03-23 22:03:34 +01:00
|
|
|
skill->levelcap = 1;
|
2018-03-10 22:04:03 +01:00
|
|
|
skill->instantUse = false;
|
2018-03-13 16:13:54 +01:00
|
|
|
skill->resetTime = 1;
|
|
|
|
skill->available = skill_throw_dagger_available;
|
2018-03-10 22:04:03 +01:00
|
|
|
skill->use = skill_throw_dagger;
|
|
|
|
skill->actionRequired = false;
|
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:13:54 +01:00
|
|
|
static bool
|
|
|
|
skill_sip_health_available(Player *player)
|
|
|
|
{
|
|
|
|
return player->potion_sips > 0 && player->stats.hp != player->stats.maxhp;
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
static bool
|
|
|
|
skill_sip_health(Skill *skill, SkillData *data)
|
|
|
|
{
|
2018-03-01 06:04:12 +01:00
|
|
|
UNUSED (skill);
|
2018-02-28 22:31:38 +01:00
|
|
|
player_sip_health(data->player);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Skill *
|
2018-03-01 06:04:12 +01:00
|
|
|
create_sip_health(void)
|
2018-02-28 22:31:38 +01:00
|
|
|
{
|
|
|
|
Texture *t = texturecache_add("Items/Potion.png");
|
|
|
|
Sprite *s = sprite_create();
|
|
|
|
sprite_set_texture(s, t, 0);
|
|
|
|
s->dim = DEFAULT_DIMENSION;
|
|
|
|
s->clip = CLIP16(0, 0);
|
|
|
|
s->fixed = true;
|
|
|
|
Skill *skill = create_default("Sip health", s);
|
2018-03-23 22:03:34 +01:00
|
|
|
skill->levelcap = 1;
|
2018-02-28 22:31:38 +01:00
|
|
|
skill->instantUse = true;
|
2018-03-13 16:13:54 +01:00
|
|
|
skill->available = skill_sip_health_available;
|
2018-02-28 22:31:38 +01:00
|
|
|
skill->use = skill_sip_health;
|
|
|
|
skill->resetTime = 0;
|
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2018-03-01 13:48:03 +01:00
|
|
|
static bool
|
|
|
|
skill_charge(Skill *skill, SkillData *data)
|
|
|
|
{
|
2018-03-02 17:05:13 +01:00
|
|
|
UNUSED(skill);
|
|
|
|
|
2018-03-01 13:48:03 +01:00
|
|
|
Player *player = data->player;
|
|
|
|
RoomMatrix *matrix = data->matrix;
|
|
|
|
|
|
|
|
Position playerPos = position_to_matrix_coords(&player->sprite->pos);
|
|
|
|
Position destination = playerPos;
|
|
|
|
|
|
|
|
unsigned int steps = 0;
|
|
|
|
|
|
|
|
// Find collider
|
2018-03-01 19:37:57 +01:00
|
|
|
destination.x += (int) data->direction.x;
|
|
|
|
destination.y += (int) data->direction.y;
|
2018-03-12 09:09:03 +01:00
|
|
|
RoomSpace *space = &matrix->spaces[destination.x][destination.y];
|
|
|
|
while (position_in_roommatrix(&destination) && !space->occupied)
|
2018-03-01 19:37:57 +01:00
|
|
|
{
|
2018-03-01 13:48:03 +01:00
|
|
|
destination.x += (int) data->direction.x;
|
|
|
|
destination.y += (int) data->direction.y;
|
2018-03-12 09:09:03 +01:00
|
|
|
if (space->items != NULL) {
|
|
|
|
LinkedList *items = space->items;
|
|
|
|
while (items != NULL) {
|
|
|
|
Item *item = items->data;
|
|
|
|
items = items->next;
|
|
|
|
item_collected(item, player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
space = &matrix->spaces[destination.x][destination.y];
|
2018-03-01 13:48:03 +01:00
|
|
|
steps++;
|
2018-03-01 19:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!position_in_roommatrix(&destination)) {
|
|
|
|
destination.x -= (int) data->direction.x;
|
|
|
|
destination.y -= (int) data->direction.y;
|
|
|
|
}
|
2018-03-01 13:48:03 +01:00
|
|
|
|
|
|
|
// Move player
|
2018-03-01 19:37:57 +01:00
|
|
|
Position playerOriginPos = player->sprite->pos;
|
2018-03-01 13:48:03 +01:00
|
|
|
player->sprite->pos.x += (steps * TILE_DIMENSION) * (int) data->direction.x;
|
|
|
|
player->sprite->pos.y += (steps * TILE_DIMENSION) * (int) data->direction.y;
|
2018-03-01 19:37:57 +01:00
|
|
|
Position playerDestinationPos = player->sprite->pos;
|
2018-03-01 13:48:03 +01:00
|
|
|
set_player_clip_for_direction(data->player, &data->direction);
|
|
|
|
|
2018-03-01 19:37:57 +01:00
|
|
|
// Add motion particles
|
|
|
|
bool horizontal = data->direction.x != 0;
|
|
|
|
Dimension particleArea;
|
|
|
|
if (horizontal)
|
|
|
|
particleArea = (Dimension) { steps * TILE_DIMENSION, TILE_DIMENSION };
|
|
|
|
else
|
|
|
|
particleArea = (Dimension) { TILE_DIMENSION, steps * TILE_DIMENSION };
|
|
|
|
|
|
|
|
Position speedLinePos;
|
|
|
|
if (playerOriginPos.x < playerDestinationPos.x || playerOriginPos.y < playerDestinationPos.y)
|
|
|
|
speedLinePos = playerOriginPos;
|
|
|
|
else
|
|
|
|
speedLinePos = playerDestinationPos;
|
|
|
|
|
|
|
|
particle_engine_speed_lines(speedLinePos, particleArea, horizontal);
|
2018-03-01 19:46:23 +01:00
|
|
|
mixer_play_effect(SWOOSH);
|
2018-03-01 19:37:57 +01:00
|
|
|
|
2018-03-01 13:48:03 +01:00
|
|
|
if (matrix->spaces[destination.x][destination.y].monster) {
|
|
|
|
Monster *monster = matrix->spaces[destination.x][destination.y].monster;
|
|
|
|
Stats tmpStats = player->stats;
|
|
|
|
tmpStats.dmg *= steps > 0 ? steps : 1;
|
|
|
|
mixer_play_effect(SWING0 + get_random(3) - 1);
|
|
|
|
unsigned int dmg = stats_fight(&tmpStats, &monster->stats);
|
|
|
|
if (dmg > 0) {
|
|
|
|
gui_log("You charged %s for %u damage", monster->lclabel, dmg);
|
|
|
|
mixer_play_effect(SWORD_HIT);
|
2018-03-25 23:30:26 +02:00
|
|
|
data->player->stat_data.hits += 1;
|
2018-03-01 13:48:03 +01:00
|
|
|
}
|
2018-03-01 19:37:57 +01:00
|
|
|
monster_hit(monster, dmg);
|
2018-03-01 13:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Skill *
|
|
|
|
create_charge(void)
|
|
|
|
{
|
2018-03-11 21:06:46 +01:00
|
|
|
Texture *t = texturecache_add("Extras/Skills.png");
|
2018-03-01 13:48:03 +01:00
|
|
|
Sprite *s = sprite_create();
|
|
|
|
sprite_set_texture(s, t, 0);
|
2018-03-11 21:06:46 +01:00
|
|
|
s->dim = GAME_DIMENSION;
|
|
|
|
s->clip = CLIP32(32, 0);
|
2018-03-01 13:48:03 +01:00
|
|
|
s->fixed = true;
|
|
|
|
Skill *skill = create_default("Charge", s);
|
2018-03-23 22:03:34 +01:00
|
|
|
skill->levelcap = 4;
|
2018-03-01 13:48:03 +01:00
|
|
|
skill->use = skill_charge;
|
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:13:28 +01:00
|
|
|
Skill*
|
|
|
|
skill_create(enum SkillType t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case FLURRY:
|
|
|
|
return create_flurry();
|
2018-02-28 22:31:38 +01:00
|
|
|
case SIP_HEALTH:
|
|
|
|
return create_sip_health();
|
2018-03-01 13:48:03 +01:00
|
|
|
case CHARGE:
|
|
|
|
return create_charge();
|
2018-03-10 22:04:03 +01:00
|
|
|
case DAGGER_THROW:
|
|
|
|
return create_throw_dagger();
|
2018-02-24 07:13:28 +01:00
|
|
|
default:
|
|
|
|
fatal("Unknown SkillType %u", (unsigned int) t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
skill_destroy(Skill *skill)
|
|
|
|
{
|
|
|
|
sprite_destroy(skill->icon);
|
|
|
|
free(skill);
|
|
|
|
}
|