added weapon knife system

efd/v1
DavoSK 2023-02-02 15:41:16 +01:00
parent 65766d8748
commit 1f451f10be
3 changed files with 55 additions and 1 deletions

View File

@ -228,9 +228,15 @@ typedef struct {
} MobHuntPlayer; } MobHuntPlayer;
typedef struct { typedef struct {
char _unused; char _unused;
} MobMelee; } MobMelee;
typedef struct {
uint16_t projectile_count;
uint8_t damage;
uint8_t spawn_delay;
} WeaponKnife;
#define _COMPS\ #define _COMPS\
X(Vector2D)\ X(Vector2D)\
X(Position)\ X(Position)\
@ -247,6 +253,7 @@ typedef struct {
X(Mob)\ X(Mob)\
X(MobHuntPlayer)\ X(MobHuntPlayer)\
X(MobMelee)\ X(MobMelee)\
X(WeaponKnife)\
X(Classify)\ X(Classify)\
X(Vehicle)\ X(Vehicle)\
X(IsInVehicle)\ X(IsInVehicle)\

View File

@ -10,11 +10,13 @@
// custom systems // custom systems
#include "system_mob.c" #include "system_mob.c"
#include "system_weapon.c"
void mob_systems(ecs_world_t *ecs) { void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob); 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(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
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);
ECS_SYSTEM_TICKED(ecs, WeaponKnifeMechanic, EcsPostUpdate, components.WeaponKnife, components.Position, components.Input);
//ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob); //ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
} }
@ -36,6 +38,14 @@ void game_setup_ecs() {
void game_player_joined(uint64_t ent) { void game_player_joined(uint64_t ent) {
notification_push("test1", "Hello World!"); notification_push("test1", "Hello World!");
//NOTE(DavoSK): add weapon component for testing
ecs_world_t* world = world_ecs();
ecs_set(world, (ecs_entity_t)ent, WeaponKnife, {
.projectile_count = 1,
.damage = 10,
.spawn_delay = WEAPON_KNIFE_SPAWN_DELAY
});
} }
void game_player_departed(uint64_t ent) { void game_player_departed(uint64_t ent) {

View File

@ -0,0 +1,37 @@
#define WEAPON_KNIFE_SPAWN_DELAY 20
#define WEAPON_PROJECTILE_POS_OFFSET 0.2f
#define WEAPON_PROJECTILE_SPEED 10.0f
//TODO(DavoSK): move to helpers, add srand
float get_rand_between(float min, float max) {
float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
return min + scale * ( max - min ); /* [min, max] */
}
void WeaponKnifeMechanic(ecs_iter_t *it) {
WeaponKnife *weapon = ecs_field(it, WeaponKnife, 1);
const Position *pos = ecs_field(it, Position, 2);
const Input *input = ecs_field(it, Input, 3);
for (int i = 0; i < it->count; i++) {
if (weapon[i].spawn_delay > 0) {
TICK_VAR(weapon[i].spawn_delay);
continue;
}
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, {
.x=input[i].x*WEAPON_PROJECTILE_SPEED,
.y=input[i].y*WEAPON_PROJECTILE_SPEED
});
}
weapon[i].spawn_delay = WEAPON_KNIFE_SPAWN_DELAY;
}
}