Adds posibility for daggers hitting walls to be recoverable.

This commit is contained in:
Linus Probert 2018-08-11 23:35:16 +02:00
parent 4e1050dc8a
commit 2a80434547
3 changed files with 61 additions and 11 deletions

View File

@ -100,3 +100,12 @@ position_in_roommatrix(const Position *pos)
&& pos->y >= 0
&& pos->y < MAP_ROOM_HEIGHT;
}
Position
position_to_tile_pos(const Position *p)
{
return (Position) {
p->x - (p->x % TILE_DIMENSION),
p->y - (p->y % TILE_DIMENSION)
};
}

View File

@ -26,22 +26,47 @@ typedef struct {
int y;
} Position;
/*
* Return a matrix coord position for a given position
*/
Position
position_to_matrix_coords(Position*);
/*
* Get the room coord for the room containing the given position
*/
Position
position_to_room_coords(Position*);
/*
* Check is a position is within a given room
*/
bool
position_in_room(Position *pos, Position *roomPos);
/*
* Check if two positions are equal
*/
bool
position_equals(const Position*, const Position*);
/*
* Check if two positions are separated by equal
* or less of the provided distance.
*/
bool
position_proximity(unsigned int distance, const Position*, const Position*);
/*
* Check if a matrix coord is still within the matrix
*/
bool
position_in_roommatrix(const Position*);
/*
* Get the tile position for the given world position
*/
Position
position_to_tile_pos(const Position*);
#endif // POSITION_H_

View File

@ -68,6 +68,24 @@ projectile_create(void)
return p;
}
static Position
get_collision_pos_for(Projectile *p)
{
Position collisionPos = p->sprite->pos;
if (p->velocity.x > 0) collisionPos.x += TILE_DIMENSION;
if(p->velocity.y > 0) collisionPos.y += TILE_DIMENSION;
return collisionPos;
}
static Position
get_projectile_pos_for(Projectile *p)
{
Position projectilePos = p->sprite->pos;
if (p->velocity.x < 0) projectilePos.x += TILE_DIMENSION;
if (p->velocity.y < 0) projectilePos.y += TILE_DIMENSION;
return projectilePos;
}
void
projectile_update(Projectile *p, UpdateData *data)
{
@ -79,12 +97,7 @@ projectile_update(Projectile *p, UpdateData *data)
if (timer_get_ticks(p->lifetime) > 2000)
p->alive = false;
Position collisionPos = p->sprite->pos;
if (p->velocity.x > 0)
collisionPos.x += TILE_DIMENSION;
if(p->velocity.y > 0)
collisionPos.y += TILE_DIMENSION;
Position collisionPos = get_collision_pos_for(p);
if (!position_in_room(&collisionPos, &data->map->currentRoom)) {
p->alive = false;
return;
@ -102,6 +115,7 @@ projectile_update(Projectile *p, UpdateData *data)
if (space->player)
return;
Position projectilePos = get_projectile_pos_for(p);
if (space->monster) {
Stats tmpStats = data->player->stats;
tmpStats.dmg *= 2;
@ -115,13 +129,15 @@ projectile_update(Projectile *p, UpdateData *data)
monster_hit(space->monster, dmg);
player_monster_kill_check(data->player, space->monster);
alive = player_has_artifact(data->player, PIERCING_DAGGERS) > p->collisionCount;
projectilePos = space->monster->sprite->pos;
}
mixer_play_effect(SWORD_HIT);
if (!alive && get_random(5) <= player_has_artifact(data->player, DAGGER_RECOVERY)) {
Item *item = item_builder_build_item(DAGGER, 1);
item->sprite->pos = space->monster->sprite->pos;
item->sprite->pos = position_to_tile_pos(&projectilePos);
linkedlist_append(&data->map->items, item);
}
}
mixer_play_effect(SWORD_HIT);
p->alive = alive;
p->collisionCount++;
}