diff options
author | quou <quou@disroot.org> | 2024-10-02 21:25:33 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-10-02 21:25:33 +1000 |
commit | b645edebd21d2d93256b52ee0cd2063395908a74 (patch) | |
tree | 13a432f3935d565511ccdce2581819bb38f0b969 | |
parent | 3d12118b3a2447b3e1a9e87b81d702f13ad5c7ab (diff) |
Take damage when an enemy touches the player
-rw-r--r-- | config.h | 1 | ||||
-rw-r--r-- | obj.h | 1 | ||||
-rw-r--r-- | player.c | 38 |
3 files changed, 36 insertions, 4 deletions
@@ -50,6 +50,7 @@ #define enemy_demon_attack_dist f1 #define enemy_demon_jump_force (5 << fbits) #define enemy_slash_damage 1 +#define enemy_touch_damage 1 #define enemy_knockback (4 << fbits) #define enemy_hurt_freeze 1 @@ -95,6 +95,7 @@ typedef struct { void init_enemy(Enemy* e, Enemy_Type t, int x, int y); int update_enemy(Enemy* e, struct World* w); void ren_enemy(const Enemy* e, struct Renderer* r); +void get_enemy_rect(Enemy_Type t, Rect* d); typedef struct { Rect r; @@ -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) { |