aboutsummaryrefslogtreecommitdiff
path: root/player.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-10-02 21:25:33 +1000
committerquou <quou@disroot.org>2024-10-02 21:25:33 +1000
commitb645edebd21d2d93256b52ee0cd2063395908a74 (patch)
tree13a432f3935d565511ccdce2581819bb38f0b969 /player.c
parent3d12118b3a2447b3e1a9e87b81d702f13ad5c7ab (diff)
Take damage when an enemy touches the player
Diffstat (limited to 'player.c')
-rw-r--r--player.c38
1 files changed, 34 insertions, 4 deletions
diff --git a/player.c b/player.c
index b3a1d00..33e6d2c 100644
--- a/player.c
+++ b/player.c
@@ -157,7 +157,7 @@ void update_player_move(Player* p, const App* a, World* w) {
}
void update_player_hurt(Player* p, World* w) {
- int i;
+ int i, cx, cy;
Rect r = player_rect;
r.x += p->x >> fbits;
r.y += p->y >> fbits;
@@ -165,17 +165,47 @@ void update_player_hurt(Player* p, World* w) {
p->inv--;
return;
}
+ cx = p->x + ((player_rect.w >> 1) << fbits);
+ cy = p->y + ((player_rect.h >> 1) << fbits);
for (i = 0; i < w->deathzone_count; i++) {
const Deathzone* dz = &w->deathzones[i];
if (!dz->friendly && rects_overlap(&r, &dz->r)) {
p->hp -= dz->hp;
p->vx -= dz->vx;
p->vy -= dz->vy;
- p->inv = player_inv_frames;
- w->freeze = player_hurt_freeze;
- return;
+ inst_effect(w, cx, cy, -dz->vx, -dz->vy, 32);
+ goto took_damage;
}
}
+ for (i = 0; i < w->enemy_count; i++) {
+ const Enemy* e = &w->enemies[i];
+ Rect er;
+ get_enemy_rect(e->t, &er);
+ er.x += e->x >> fbits;
+ er.y += e->y >> fbits;
+ if (rects_overlap(&r, &er)) {
+ int ecx = e->x + ((er.w >> 1) << fbits);
+ int ecy = e->y + ((er.h >> 1) << fbits);
+ int tx = cx - ecx;
+ int ty = cy - ecy;
+ int l = fxsqrt(
+ ((tx * tx) >> fbits) +
+ ((ty * ty) >> fbits)
+ );
+ tx <<= fbits;
+ ty <<= fbits;
+ tx /= l;
+ ty /= l;
+ p->hp -= enemy_touch_damage;
+ inst_effect(w, cx, cy, tx, ty, 32);
+ goto took_damage;
+ }
+ }
+ return;
+took_damage:
+ p->inv = player_inv_frames;
+ w->freeze = player_hurt_freeze;
+ play_sound(2048);
}
void update_player_special(Player* p, const App* a, World* w) {