projectile work

efd/v1
DavoSK 2023-02-02 16:55:49 +01:00
parent f6d5c76d1b
commit 698d3d2089
3 changed files with 40 additions and 8 deletions

View File

@ -252,6 +252,11 @@ typedef struct {
uint8_t spawn_delay;
} WeaponKnife;
typedef struct {
uint8_t damage;
float origin_x, origin_y;
} WeaponProjectile;
#define _COMPS\
X(Vector2D)\
X(Position)\
@ -270,6 +275,7 @@ typedef struct {
X(MobHuntPlayer)\
X(MobMelee)\
X(WeaponKnife)\
X(WeaponProjectile)\
X(Classify)\
X(Vehicle)\
X(IsInVehicle)\

View File

@ -16,7 +16,10 @@ void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
//NOTE(DavoSK): weapons
ECS_SYSTEM_TICKED(ecs, WeaponKnifeMechanic, EcsPostUpdate, components.WeaponKnife, components.Position, components.Input);
ECS_SYSTEM_TICKED(ecs, WeaponProjectileExpire, EcsPostUpdate, components.WeaponProjectile, components.Position);
//ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
}

View File

@ -1,6 +1,7 @@
#define WEAPON_KNIFE_SPAWN_DELAY 20
#define WEAPON_PROJECTILE_POS_OFFSET 0.2f
#define WEAPON_PROJECTILE_POS_OFFSET 3.2f
#define WEAPON_PROJECTILE_SPEED 10.0f
#define WEAPON_PROJECTILE_RANGE_LIFETIME 10.0f
//TODO(DavoSK): move to helpers, add srand
float get_rand_between(float min, float max) {
@ -20,18 +21,40 @@ void WeaponKnifeMechanic(ecs_iter_t *it) {
}
for(int j = 0; j < weapon[i].projectile_count; j++) {
ecs_entity_t projectile_entity = ecs_new_entity(it->world, "WeaponProjectile");
ecs_set(it->world, projectile_entity, Position, {
.x=pos[i].x,
.y=pos[i].y+get_rand_between(-WEAPON_PROJECTILE_POS_OFFSET, WEAPON_PROJECTILE_POS_OFFSET)
});
ecs_set(it->world, projectile_entity, Velocity, {
ecs_entity_t e = entity_spawn(EKIND_WEAPON);
ecs_set(it->world, e, WeaponProjectile, { .damage=weapon[i].damage });
ecs_set(it->world, e, Sprite, { .spritesheet=0, .frame=2347 });
ecs_set(it->world, e, InAir, {0});
ecs_set(it->world, e, Velocity, {
.x=input[i].x*WEAPON_PROJECTILE_SPEED,
.y=input[i].y*WEAPON_PROJECTILE_SPEED
});
Position *dest = ecs_get_mut(world_ecs(), e, Position);
dest->x = pos[i].x;
dest->y = pos[i].y+get_rand_between(-WEAPON_PROJECTILE_POS_OFFSET, WEAPON_PROJECTILE_POS_OFFSET);
}
weapon[i].spawn_delay = WEAPON_KNIFE_SPAWN_DELAY;
}
}
void WeaponProjectileExpire(ecs_iter_t *it) {
const WeaponProjectile *weapon = ecs_field(it, WeaponProjectile, 1);
const Position *pos = ecs_field(it, Position, 2);
for (int i = 0; i < it->count; i++) {
zpl_vec2 v_origin = { .x = weapon[i].origin_x, .y = weapon[i].origin_y };
zpl_vec2 v_pos = { .x = pos[i].x, .y = pos[i].y };
const float d = zpl_vec2_mag(v_origin, v_pos);
if(d > WEAPON_PROJECTILE_RANGE_LIFETIME) {
entity_despawn(it->entities[i]);
}
}
}
void WeaponProjectileHit(ecs_iter_t *it) {
}