2018-03-10 22:04:03 +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-03-13 09:37:51 +01:00
|
|
|
#include <stdlib.h>
|
2018-03-08 00:58:26 +01:00
|
|
|
#include "projectile.h"
|
|
|
|
#include "util.h"
|
2018-03-10 22:04:03 +01:00
|
|
|
#include "texturecache.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "monster.h"
|
|
|
|
#include "mixer.h"
|
2018-03-12 09:09:03 +01:00
|
|
|
#include "gui.h"
|
2018-03-13 16:13:54 +01:00
|
|
|
#include "item_builder.h"
|
|
|
|
#include "random.h"
|
2018-03-13 16:51:08 +01:00
|
|
|
#include "update_data.h"
|
2018-03-10 22:04:03 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
onDaggerRender(Sprite *s)
|
|
|
|
{
|
2018-05-05 21:29:08 +02:00
|
|
|
if (s->angle >= 0)
|
|
|
|
s->angle += 30;
|
|
|
|
else
|
|
|
|
s->angle -= 30;
|
2018-03-10 22:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Projectile *
|
|
|
|
projectile_dagger_create(void)
|
|
|
|
{
|
|
|
|
Projectile *p = projectile_create();
|
2018-05-05 21:29:08 +02:00
|
|
|
sprite_set_texture(p->sprite, texturecache_add("Items/ShortWep.png"), 0);
|
2018-03-10 22:04:03 +01:00
|
|
|
p->sprite->onRender = onDaggerRender;
|
|
|
|
p->sprite->animate = false;
|
|
|
|
p->sprite->clip = CLIP16(0, 0);
|
|
|
|
p->sprite->dim = (Dimension) { 32, 32 };
|
2018-05-05 21:29:08 +02:00
|
|
|
p->sprite->rotationPoint = (SDL_Point) { 16, 16 };
|
2018-03-10 22:04:03 +01:00
|
|
|
return p;
|
|
|
|
}
|
2018-03-08 00:58:26 +01:00
|
|
|
|
|
|
|
Projectile *
|
|
|
|
projectile_create(void)
|
|
|
|
{
|
|
|
|
Projectile *p = ec_malloc(sizeof(Projectile));
|
|
|
|
p->sprite = sprite_create();
|
|
|
|
p->velocity = VECTOR2D_NODIR;
|
|
|
|
p->alive = true;
|
|
|
|
p->lifetime = timer_create();
|
2018-03-10 22:04:03 +01:00
|
|
|
p->onRender = NULL;
|
2018-03-08 00:58:26 +01:00
|
|
|
timer_start(p->lifetime);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-03-13 16:51:08 +01:00
|
|
|
projectile_update(Projectile *p, UpdateData *data)
|
2018-03-08 00:58:26 +01:00
|
|
|
{
|
2018-03-13 16:51:08 +01:00
|
|
|
p->sprite->pos.x += (int) (p->velocity.x * data->deltatime);
|
|
|
|
p->sprite->pos.y += (int) (p->velocity.y * data->deltatime);
|
2018-03-08 00:58:26 +01:00
|
|
|
|
|
|
|
if (timer_get_ticks(p->lifetime) > 2000)
|
|
|
|
p->alive = false;
|
2018-03-10 22:04:03 +01:00
|
|
|
|
|
|
|
Position collisionPos = p->sprite->pos;
|
|
|
|
if (p->velocity.x > 0)
|
|
|
|
collisionPos.x += TILE_DIMENSION;
|
2018-03-15 16:30:41 +01:00
|
|
|
if(p->velocity.y > 0)
|
2018-03-10 22:04:03 +01:00
|
|
|
collisionPos.y += TILE_DIMENSION;
|
|
|
|
|
|
|
|
Position roomPos = position_to_matrix_coords(&collisionPos);
|
2018-03-13 16:51:08 +01:00
|
|
|
RoomSpace *space = &data->matrix->spaces[roomPos.x][roomPos.y];
|
2018-03-10 22:04:03 +01:00
|
|
|
if (!space->occupied)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (space->player)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (space->monster) {
|
2018-03-24 12:46:23 +01:00
|
|
|
Stats tmpStats = data->player->stats;
|
|
|
|
tmpStats.dmg *= 2;
|
|
|
|
Uint32 dmg = stats_fight(&tmpStats, &space->monster->stats);
|
2018-03-11 21:06:46 +01:00
|
|
|
if (dmg > 0) {
|
|
|
|
gui_log("Your dagger pierced %s for %u damage", space->monster->lclabel, dmg);
|
2018-03-25 23:30:26 +02:00
|
|
|
data->player->stat_data.hits += 1;
|
2018-03-11 21:06:46 +01:00
|
|
|
}
|
2018-03-24 12:46:23 +01:00
|
|
|
if (get_random(2) >= 1) {
|
2018-03-13 16:13:54 +01:00
|
|
|
Item *item = item_builder_build_item(DAGGER, 1);
|
|
|
|
item->sprite->pos = space->monster->sprite->pos;
|
2018-03-13 16:51:08 +01:00
|
|
|
linkedlist_append(&data->map->items, item);
|
2018-03-13 16:13:54 +01:00
|
|
|
}
|
2018-03-10 22:04:03 +01:00
|
|
|
monster_hit(space->monster, dmg);
|
2018-07-31 14:30:41 +02:00
|
|
|
player_monster_kill_check(data->player, space->monster);
|
2018-03-10 22:04:03 +01:00
|
|
|
}
|
2018-05-05 21:36:16 +02:00
|
|
|
mixer_play_effect(SWORD_HIT);
|
2018-03-10 22:04:03 +01:00
|
|
|
p->alive = false;
|
2018-03-08 00:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
projectile_render(Projectile *p, Camera *cam)
|
|
|
|
{
|
2018-03-10 22:04:03 +01:00
|
|
|
if (p->onRender)
|
|
|
|
p->onRender(p);
|
2018-03-08 00:58:26 +01:00
|
|
|
sprite_render(p->sprite, cam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
projectile_destroy(Projectile *p)
|
|
|
|
{
|
|
|
|
sprite_destroy(p->sprite);
|
|
|
|
timer_destroy(p->lifetime);
|
|
|
|
free(p);
|
2018-03-10 22:04:03 +01:00
|
|
|
}
|