set up dmg system for mobs
parent
cf200e8bf5
commit
410d6e5f11
|
@ -107,6 +107,9 @@ typedef struct {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float hp;
|
float hp;
|
||||||
float max_hp;
|
float max_hp;
|
||||||
|
|
||||||
|
// acumulated damage
|
||||||
|
float dmg;
|
||||||
} Health;
|
} Health;
|
||||||
|
|
||||||
typedef struct { char _unused; } Dead;
|
typedef struct { char _unused; } Dead;
|
||||||
|
@ -119,14 +122,6 @@ typedef struct {
|
||||||
uint8_t delay;
|
uint8_t delay;
|
||||||
} HealDelay;
|
} HealDelay;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t _unused;
|
|
||||||
} HealthDecreased;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t amount;
|
|
||||||
} Damage;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t id;
|
uint16_t id;
|
||||||
} Classify;
|
} Classify;
|
||||||
|
@ -286,8 +281,6 @@ typedef struct {
|
||||||
X(Dead)\
|
X(Dead)\
|
||||||
X(HealthRegen)\
|
X(HealthRegen)\
|
||||||
X(HealDelay)\
|
X(HealDelay)\
|
||||||
X(HealthDecreased)\
|
|
||||||
X(Damage)\
|
|
||||||
X(Mob)\
|
X(Mob)\
|
||||||
X(MobHuntPlayer)\
|
X(MobHuntPlayer)\
|
||||||
X(MobMelee)\
|
X(MobMelee)\
|
||||||
|
|
|
@ -125,6 +125,7 @@ uint64_t mob_spawn(void) {
|
||||||
hp->max_hp = hp->hp = 100.0f;
|
hp->max_hp = hp->hp = 100.0f;
|
||||||
|
|
||||||
ecs_add(world_ecs(), e, Mob);
|
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 });
|
ecs_set(world_ecs(), e, PhysicsBody, { .kind = PHYS_AABB, .mass = 1.0f });
|
||||||
|
|
||||||
return (uint64_t)e;
|
return (uint64_t)e;
|
||||||
|
|
|
@ -7,9 +7,7 @@ void HurtOnHazardBlock(ecs_iter_t *it) {
|
||||||
for (int i = 0; i < it->count; i++) {
|
for (int i = 0; i < it->count; i++) {
|
||||||
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
|
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
|
||||||
if (blocks_get_flags(l.bid) & BLOCK_FLAG_HAZARD) {
|
if (blocks_get_flags(l.bid) & BLOCK_FLAG_HAZARD) {
|
||||||
h->hp -= HAZARD_BLOCK_DMG;
|
h[i].dmg += HAZARD_BLOCK_DMG;
|
||||||
h->hp = zpl_max(0.0f, h->hp);
|
|
||||||
ecs_add(it->world, it->entities[i], HealthDecreased);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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++) {
|
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_set(it->world, it->entities[i], HealDelay, { .delay = 10 });
|
||||||
ecs_remove(it->world, it->entities[i], HealthDecreased);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnHealthChangeCheckDead(ecs_iter_t *it) {
|
if (hp->hp <= 0.0f) {
|
||||||
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) {
|
|
||||||
ecs_add(it->world, it->entities[i], Dead);
|
ecs_add(it->world, it->entities[i], Dead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void OnDead(ecs_iter_t *it) {
|
void OnDead(ecs_iter_t *it) {
|
||||||
for (int i = 0; i < it->count; i++) {
|
for (int i = 0; i < it->count; i++) {
|
||||||
|
|
|
@ -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, 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, RegenerateHP, EcsOnUpdate, 40.0f, components.Health, components.HealthRegen, !components.HealDelay);
|
||||||
ECS_SYSTEM_TICKED_EX(ecs, TickDownHealDelay, EcsOnUpdate, 20.0f, components.HealDelay);
|
ECS_SYSTEM_TICKED_EX(ecs, TickDownHealDelay, EcsOnUpdate, 20.0f, components.HealDelay);
|
||||||
ECS_OBSERVER(ecs, OnHealthChangePutDelay, EcsOnAdd, components.HealthDecreased);
|
ECS_SYSTEM_TICKED(ecs, ProcessHealthDamage, EcsPostUpdate, components.Health);
|
||||||
ECS_OBSERVER(ecs, OnHealthChangeCheckDead, EcsOnAdd, components.HealthDecreased);
|
|
||||||
ECS_OBSERVER(ecs, OnDead, EcsOnAdd, components.Dead);
|
ECS_OBSERVER(ecs, OnDead, EcsOnAdd, components.Dead);
|
||||||
|
|
||||||
// collisions and movement physics
|
// collisions and movement physics
|
||||||
|
|
|
@ -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);
|
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
|
||||||
|
|
||||||
//NOTE(DavoSK): weapons
|
//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, WeaponKnifeMechanic, EcsPostUpdate, components.WeaponKnife, components.Position, components.Input);
|
||||||
ECS_SYSTEM_TICKED(ecs, WeaponProjectileHit, EcsPostUpdate, components.WeaponProjectile, components.Position, components.Rotation);
|
ECS_SYSTEM_TICKED(ecs, WeaponProjectileHit, EcsPostUpdate, components.WeaponProjectile, components.Position, components.Rotation);
|
||||||
ECS_SYSTEM_TICKED(ecs, WeaponProjectileExpire, EcsPostUpdate, components.WeaponProjectile, components.Position);
|
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() {
|
void game_input() {
|
||||||
|
|
|
@ -74,10 +74,15 @@ void MobMeleeAtk(ecs_iter_t *it) {
|
||||||
float range = (dx*dx + dy*dy);
|
float range = (dx*dx + dy*dy);
|
||||||
|
|
||||||
if (range < MOB_MELEE_DIST) {
|
if (range < MOB_MELEE_DIST) {
|
||||||
Health *hp = ecs_get_mut_ex(it->world, m->plr, Health);
|
Health *health = ecs_get_mut_ex(it->world, m->plr, Health);
|
||||||
hp->hp = zpl_max(hp->hp-MOB_MELEE_DMG, 0.0f);
|
health->dmg += MOB_MELEE_DMG;
|
||||||
ecs_add(it->world, m->plr, HealthDecreased);
|
|
||||||
}
|
}
|
||||||
mob[i].atk_delay = MOB_ATK_DELAY;
|
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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ ZPL_DIAGNOSTIC_POP
|
||||||
#define WEAPON_PROJECTILE_POS_OFFSET 200.0f
|
#define WEAPON_PROJECTILE_POS_OFFSET 200.0f
|
||||||
#define WEAPON_PROJECTILE_SPEED 500.0f
|
#define WEAPON_PROJECTILE_SPEED 500.0f
|
||||||
#define WEAPON_PROJECTILE_RANGE_LIFETIME 800.0f
|
#define WEAPON_PROJECTILE_RANGE_LIFETIME 800.0f
|
||||||
|
#define WEAPON_HIT_FORCE_PUSH 40.0f
|
||||||
|
|
||||||
//TODO(DavoSK): move to helpers, add srand
|
//TODO(DavoSK): move to helpers, add srand
|
||||||
float get_rand_between(float min, float max) {
|
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);
|
const Rotation* rot = ecs_field(it, Rotation, 3);
|
||||||
|
|
||||||
for (int i = 0; i < it->count; i++) {
|
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)) {
|
while (ecs_query_next(&it2)) {
|
||||||
Mob* mob = ecs_field(&it2, Mob, 1);
|
Mob* mob = ecs_field(&it2, Mob, 1);
|
||||||
Position* mob_pos = ecs_field(&it2, Position, 2);
|
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++) {
|
for (int j = 0; j < it2.count; j++) {
|
||||||
float p_x = pos[i].x;
|
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_x = mob_pos[j].x /*+ v2[j].x*/;
|
||||||
float p2_y = mob_pos[j].y /*+ v2[j].y*/;
|
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 = {
|
c2AABB box_a = {
|
||||||
.min = { .x = bbox_min.x, .y = bbox_min.y },
|
.min = { p_x - WORLD_BLOCK_SIZE / 4 , p_y - WORLD_BLOCK_SIZE / 4 },
|
||||||
.max = { .x = bbox_max.x, .y = bbox_max.y }
|
.max = { p_x + WORLD_BLOCK_SIZE / 4 , p_y + WORLD_BLOCK_SIZE / 4 },
|
||||||
};
|
};
|
||||||
|
|
||||||
c2AABB box_b = {
|
c2AABB box_b = {
|
||||||
.min = { 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 / 2, 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);
|
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 r2y = (box_b.max.y - box_b.min.y);
|
||||||
float r2 = (r2x * r2x + r2y * r2y) * .5f;
|
float r2 = (r2x * r2x + r2y * r2y) * .5f;
|
||||||
|
|
||||||
{
|
|
||||||
float dx = (p2_x - p_x);
|
float dx = (p2_x - p_x);
|
||||||
float dy = (p2_y - p_y);
|
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;
|
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 };
|
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) {
|
if (m.count > 0) {
|
||||||
ecs_set(it->world, it2.entities[j], Damage, { .amount = weapon[i].damage });
|
mob_health[j].dmg += weapon[i].damage;
|
||||||
entity_despawn(it->entities[i]);
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue