set up dmg system for mobs

efd/v1
Dominik Madarász 2023-02-03 07:28:17 +01:00
parent cf200e8bf5
commit 410d6e5f11
7 changed files with 98 additions and 121 deletions

View File

@ -107,6 +107,9 @@ typedef struct {
typedef struct {
float hp;
float max_hp;
// acumulated damage
float dmg;
} Health;
typedef struct { char _unused; } Dead;
@ -119,14 +122,6 @@ typedef struct {
uint8_t delay;
} HealDelay;
typedef struct {
uint8_t _unused;
} HealthDecreased;
typedef struct {
uint8_t amount;
} Damage;
typedef struct {
uint16_t id;
} Classify;
@ -286,8 +281,6 @@ typedef struct {
X(Dead)\
X(HealthRegen)\
X(HealDelay)\
X(HealthDecreased)\
X(Damage)\
X(Mob)\
X(MobHuntPlayer)\
X(MobMelee)\

View File

@ -125,6 +125,7 @@ uint64_t mob_spawn(void) {
hp->max_hp = hp->hp = 100.0f;
ecs_add(world_ecs(), e, Mob);
ecs_set(world_ecs(), e, Health, { 60, 60, 0 });
ecs_set(world_ecs(), e, PhysicsBody, { .kind = PHYS_AABB, .mass = 1.0f });
return (uint64_t)e;

View File

@ -7,9 +7,7 @@ void HurtOnHazardBlock(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
if (blocks_get_flags(l.bid) & BLOCK_FLAG_HAZARD) {
h->hp -= HAZARD_BLOCK_DMG;
h->hp = zpl_max(0.0f, h->hp);
ecs_add(it->world, it->entities[i], HealthDecreased);
h[i].dmg += HAZARD_BLOCK_DMG;
}
}
}
@ -30,22 +28,20 @@ void RegenerateHP(ecs_iter_t *it) {
}
}
void OnHealthChangePutDelay(ecs_iter_t *it) {
void ProcessHealthDamage(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {
Health *hp = ecs_get_mut(it->world, it->entities[i], Health);
if (hp->dmg > 0.0f) {
hp->hp = zpl_max(hp->hp-hp->dmg, 0.0f);
hp->dmg = 0.0f;
ecs_set(it->world, it->entities[i], HealDelay, { .delay = 10 });
ecs_remove(it->world, it->entities[i], HealthDecreased);
}
}
void OnHealthChangeCheckDead(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {
const Health *hp = ecs_get(it->world, it->entities[i], Health);
if (hp && hp->hp <= 0.0f) {
if (hp->hp <= 0.0f) {
ecs_add(it->world, it->entities[i], Dead);
}
}
}
}
void OnDead(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {

View File

@ -356,8 +356,7 @@ void SystemsImport(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, HurtOnHazardBlock, EcsOnUpdate, 20.0f, components.Position, components.Health);
ECS_SYSTEM_TICKED_EX(ecs, RegenerateHP, EcsOnUpdate, 40.0f, components.Health, components.HealthRegen, !components.HealDelay);
ECS_SYSTEM_TICKED_EX(ecs, TickDownHealDelay, EcsOnUpdate, 20.0f, components.HealDelay);
ECS_OBSERVER(ecs, OnHealthChangePutDelay, EcsOnAdd, components.HealthDecreased);
ECS_OBSERVER(ecs, OnHealthChangeCheckDead, EcsOnAdd, components.HealthDecreased);
ECS_SYSTEM_TICKED(ecs, ProcessHealthDamage, EcsPostUpdate, components.Health);
ECS_OBSERVER(ecs, OnDead, EcsOnAdd, components.Dead);
// collisions and movement physics

View File

@ -20,11 +20,11 @@ void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
//NOTE(DavoSK): weapons
ecs_mobpos_query = ecs_query_new(world_ecs(), "components.Mob, components.Position");
ecs_mobpos_query = ecs_query_new(world_ecs(), "components.Mob, components.Position, components.Health, components.Velocity");
ECS_SYSTEM_TICKED(ecs, WeaponKnifeMechanic, EcsPostUpdate, components.WeaponKnife, components.Position, components.Input);
ECS_SYSTEM_TICKED(ecs, WeaponProjectileHit, EcsPostUpdate, components.WeaponProjectile, components.Position, components.Rotation);
ECS_SYSTEM_TICKED(ecs, WeaponProjectileExpire, EcsPostUpdate, components.WeaponProjectile, components.Position);
//ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
ECS_OBSERVER(ecs, MobOnDead, EcsOnAdd, components.Mob, components.Dead);
}
void game_input() {

View File

@ -74,10 +74,15 @@ void MobMeleeAtk(ecs_iter_t *it) {
float range = (dx*dx + dy*dy);
if (range < MOB_MELEE_DIST) {
Health *hp = ecs_get_mut_ex(it->world, m->plr, Health);
hp->hp = zpl_max(hp->hp-MOB_MELEE_DMG, 0.0f);
ecs_add(it->world, m->plr, HealthDecreased);
Health *health = ecs_get_mut_ex(it->world, m->plr, Health);
health->dmg += MOB_MELEE_DMG;
}
mob[i].atk_delay = MOB_ATK_DELAY;
}
}
void MobOnDead(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {
entity_despawn(it->entities[i]);
}
}

View File

@ -6,6 +6,7 @@ ZPL_DIAGNOSTIC_POP
#define WEAPON_PROJECTILE_POS_OFFSET 200.0f
#define WEAPON_PROJECTILE_SPEED 500.0f
#define WEAPON_PROJECTILE_RANGE_LIFETIME 800.0f
#define WEAPON_HIT_FORCE_PUSH 40.0f
//TODO(DavoSK): move to helpers, add srand
float get_rand_between(float min, float max) {
@ -88,10 +89,12 @@ void WeaponProjectileHit(ecs_iter_t *it) {
const Rotation* rot = ecs_field(it, Rotation, 3);
for (int i = 0; i < it->count; i++) {
ecs_iter_t it2 = ecs_query_iter(world_ecs(), ecs_mobpos_query);
ecs_iter_t it2 = ecs_query_iter(it->world, ecs_mobpos_query);
while (ecs_query_next(&it2)) {
Mob* mob = ecs_field(&it2, Mob, 1);
Position* mob_pos = ecs_field(&it2, Position, 2);
Health *mob_health = ecs_field(&it2, Health, 3);
Velocity *mob_velocity = ecs_field(&it2, Velocity, 4);
for (int j = 0; j < it2.count; j++) {
float p_x = pos[i].x;
@ -99,27 +102,14 @@ void WeaponProjectileHit(ecs_iter_t *it) {
float p2_x = mob_pos[j].x /*+ v2[j].x*/;
float p2_y = mob_pos[j].y /*+ v2[j].y*/;
//NOTE(DavoSK): since our weapon has also rotation
//we need to rotate its bbox by rot
float rot_radians = zpl_to_radians(rot[i].angle);
zpl_vec2 bbox_min = rotate_point(p_x, p_y, rot_radians, (zpl_vec2){
.x = p_x - WORLD_BLOCK_SIZE / 2,
.y = p_y - WORLD_BLOCK_SIZE / 4
});
zpl_vec2 bbox_max = rotate_point(p_x, p_y, rot_radians, (zpl_vec2){
.x = p_x + WORLD_BLOCK_SIZE / 2,
.y = p_y + WORLD_BLOCK_SIZE / 4
});
c2AABB box_a = {
.min = { .x = bbox_min.x, .y = bbox_min.y },
.max = { .x = bbox_max.x, .y = bbox_max.y }
.min = { p_x - WORLD_BLOCK_SIZE / 4 , p_y - WORLD_BLOCK_SIZE / 4 },
.max = { p_x + WORLD_BLOCK_SIZE / 4 , p_y + WORLD_BLOCK_SIZE / 4 },
};
c2AABB box_b = {
.min = { p2_x - WORLD_BLOCK_SIZE / 2, p2_y - WORLD_BLOCK_SIZE / 4 },
.max = { p2_x + WORLD_BLOCK_SIZE / 2, p2_y + WORLD_BLOCK_SIZE / 4 },
.min = { p2_x - WORLD_BLOCK_SIZE / 4, p2_y - WORLD_BLOCK_SIZE / 4 },
.max = { p2_x + WORLD_BLOCK_SIZE / 4, p2_y + WORLD_BLOCK_SIZE / 4 },
};
float r1x = (box_a.max.x - box_a.min.x);
@ -130,35 +120,28 @@ void WeaponProjectileHit(ecs_iter_t *it) {
float r2y = (box_b.max.y - box_b.min.y);
float r2 = (r2x * r2x + r2y * r2y) * .5f;
{
float dx = (p2_x - p_x);
float dy = (p2_y - p_y);
float d = (dx*dx + dy*dy);
float d2 = (dx * dx + dy * dy);
{
if (d > r1 && d > r2)
if (d2 > r1 && d2 > r2)
continue;
}
// c2Circle circle_a = {
// .p = { p_x, p_y },
// .r = r1/2.f,
// };
// c2Circle circle_b = {
// .p = { p2_x, p2_y },
// .r = r2/2.f,
// };
// const void *shapes_a[] = { &circle_a, &box_a };
// const void *shapes_b[] = { &circle_b, &box_b };
c2Manifold m = { 0 };
c2Collide(&box_a, 0, C2_TYPE_AABB, &box_b, 0, C2_TYPE_AABB, &m);
c2AABBtoAABBManifold(box_a, box_b, &m);
float dd = zpl_sqrt(d2);
if(m.count) {
ecs_set(it->world, it2.entities[j], Damage, { .amount = weapon[i].damage });
entity_despawn(it->entities[i]);
if (m.count > 0) {
mob_health[j].dmg += weapon[i].damage;
for (int k = 0; k < m.count; k++) {
float d = m.depths[k];
mob_velocity[j].x += (dx/dd)*d*WEAPON_HIT_FORCE_PUSH;
mob_velocity[j].y += (dy/dd)*d*WEAPON_HIT_FORCE_PUSH;
}
}
}
}