eco2d/code/foundation/src/ecs/systems.c

227 lines
8.3 KiB
C
Raw Normal View History

2021-05-10 06:28:56 +00:00
#include "zpl.h"
#include "ecs/systems.h"
#include "ecs/components.h"
2021-05-07 14:43:54 +00:00
#include "world/world.h"
2021-07-26 15:53:18 +00:00
#include "world/blocks.h"
#include "platform/profiler.h"
#include "debug/debug_draw.h"
#include "core/game.h"
2021-05-04 17:41:30 +00:00
2021-07-26 15:53:18 +00:00
#define PHY_BLOCK_COLLISION 1
#define PHY_WALK_DRAG 4.23f
2021-07-26 15:53:18 +00:00
#define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f)
2021-05-07 15:24:22 +00:00
#include "modules/system_onfoot.c"
#include "modules/system_demo.c"
#include "modules/system_vehicle.c"
#include "modules/system_items.c"
2021-05-08 09:22:25 +00:00
2021-11-01 18:20:07 +00:00
static inline float physics_correction(float x, float vx, float bounce) {
2021-08-29 11:48:54 +00:00
float r = (((zpl_max(0.0f, (WORLD_BLOCK_SIZE/2.0f) - zpl_abs(x))*zpl_sign(x)))*(WORLD_BLOCK_SIZE/2.0f));
return r + (-vx*bounce);
}
2021-07-26 15:53:18 +00:00
void IntegratePositions(ecs_iter_t *it) {
profile(PROF_INTEGRATE_POS) {
2022-07-31 14:34:47 +00:00
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
2022-09-13 16:24:43 +00:00
2021-07-26 15:53:18 +00:00
for (int i = 0; i < it->count; i++) {
2022-09-14 09:03:00 +00:00
if (ecs_get(it->world, it->entities[i], IsInVehicle)) {
continue;
}
2022-09-13 16:24:43 +00:00
if (zpl_abs(v[i].x) >= 0.001f || zpl_abs(v[i].y) >= 0.001f) {
// NOTE(zaklaus): world bounds
{
float w = (float)world_dim();
p[i].x = zpl_clamp(p[i].x, 0, w-1);
p[i].y = zpl_clamp(p[i].y, 0, w-1);
2021-07-26 15:53:18 +00:00
}
2022-09-13 16:24:43 +00:00
#if PHY_BLOCK_COLLISION==1
// NOTE(zaklaus): X axis
{
world_block_lookup lookup = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y);
uint32_t flags = blocks_get_flags(lookup.bid);
float bounce = blocks_get_bounce(lookup.bid);
if (flags & BLOCK_FLAG_COLLISION) {
v[i].x = physics_correction(lookup.ox, v[i].x, bounce);
}
}
// NOTE(zaklaus): Y axis
{
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y+PHY_LOOKAHEAD(v[i].y));
uint32_t flags = blocks_get_flags(lookup.bid);
float bounce = blocks_get_bounce(lookup.bid);
if (flags & BLOCK_FLAG_COLLISION) {
v[i].y = physics_correction(lookup.oy, v[i].y, bounce);
}
2021-07-26 15:53:18 +00:00
}
2022-09-13 16:24:43 +00:00
#endif
entity_set_position(it->entities[i], p[i].x+v[i].x*safe_dt(it), p[i].y+v[i].y*safe_dt(it));
2021-07-26 15:53:18 +00:00
}
2022-09-13 16:24:43 +00:00
2021-08-29 10:48:29 +00:00
{
debug_v2 a = {p[i].x, p[i].y};
debug_v2 b = {p[i].x+v[i].x, p[i].y+v[i].y};
debug_push_line(a, b, 0xFFFFFFFF);
}
2021-05-08 07:18:50 +00:00
}
2021-05-08 08:03:18 +00:00
}
}
2021-07-27 16:34:31 +00:00
#define HAZARD_BLOCK_TIME 1.0f
#define HAZARD_BLOCK_DMG 5.0f
void HurtOnHazardBlock(ecs_iter_t *it) {
2022-07-31 14:34:47 +00:00
Position *p = ecs_field(it, Position, 1);
Health *h = ecs_field(it, Health, 2);
2022-09-13 16:24:43 +00:00
2021-07-27 16:34:31 +00:00
for (int i = 0; i < it->count; i++) {
2022-09-13 16:24:43 +00:00
if (h->pain_time < 0.0f) {
h->pain_time = HAZARD_BLOCK_TIME;
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
if (blocks_get_flags(l.bid) & BLOCK_FLAG_HAZARD) {
2021-07-27 16:34:31 +00:00
h->hp -= HAZARD_BLOCK_DMG;
h->hp = zpl_max(0.0f, h->hp);
}
}
}
}
#define HP_REGEN_TIME 2.0f
#define HP_REGEN_PAIN_COOLDOWN 5.0f
#define HP_REGEN_RECOVERY 15.0f
void RegenerateHP(ecs_iter_t *it) {
2022-07-31 14:34:47 +00:00
Health *h = ecs_field(it, Health, 1);
2022-09-13 16:24:43 +00:00
2021-07-27 16:34:31 +00:00
for (int i = 0; i < it->count; i++) {
if (h[i].pain_time < 0.0f) {
if (h[i].heal_time < 0.0f && h[i].hp < h[i].max_hp) {
h[i].heal_time = HP_REGEN_TIME;
h[i].hp += HP_REGEN_RECOVERY;
h[i].hp = zpl_min(h[i].max_hp, h[i].hp);
entity_wake(it->entities[i]);
} else {
h[i].heal_time -= safe_dt(it);
2021-07-27 16:34:31 +00:00
}
} else {
h[i].pain_time -= safe_dt(it);
2021-07-27 16:34:31 +00:00
}
}
}
2021-11-02 09:22:04 +00:00
void ResetActivators(ecs_iter_t *it) {
2022-07-31 14:34:47 +00:00
Input *in = ecs_field(it, Input, 1);
2022-09-13 16:24:43 +00:00
2021-11-02 09:22:04 +00:00
for (int i = 0; i < it->count; i++) {
in[i].use = false;
in[i].swap = false;
in[i].drop = false;
2022-08-09 14:46:23 +00:00
in[i].pick = false;
2021-11-02 09:22:04 +00:00
in[i].num_placements = 0;
}
}
2021-08-11 16:19:10 +00:00
void ApplyWorldDragOnVelocity(ecs_iter_t *it) {
2022-07-31 14:34:47 +00:00
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
2022-09-13 16:24:43 +00:00
2021-08-11 16:19:10 +00:00
for (int i = 0; i < it->count; i++) {
2022-09-13 16:24:43 +00:00
if (zpl_abs(v[i].x) < 0.001f && zpl_abs(v[i].y) < 0.001f) continue;
2022-09-14 09:03:00 +00:00
if (ecs_get(it->world, it->entities[i], IsInVehicle)) {
continue;
}
2021-08-11 16:19:10 +00:00
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y);
2021-11-03 18:04:34 +00:00
float drag = zpl_clamp(blocks_get_drag(lookup.bid), 0.0f, 1.0f);
float friction = blocks_get_friction(lookup.bid);
float velx = blocks_get_velx(lookup.bid);
float vely = blocks_get_vely(lookup.bid);
2021-11-02 17:09:54 +00:00
v[i].x = zpl_lerp(v[i].x, zpl_max(0.0f, zpl_abs(velx))*zpl_sign(velx), PHY_WALK_DRAG*drag*friction*safe_dt(it));
v[i].y = zpl_lerp(v[i].y, zpl_max(0.0f, zpl_abs(vely))*zpl_sign(vely), PHY_WALK_DRAG*drag*friction*safe_dt(it));
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
if ( zpl_abs(v[i].x) > ENTITY_ACTION_VELOCITY_THRESHOLD
|| zpl_abs(v[i].y) > ENTITY_ACTION_VELOCITY_THRESHOLD) {
entity_wake(it->entities[i]);
}
}
}
#define PLAYER_MAX_INTERACT_RANGE 35.0f
void PlayerClosestInteractable(ecs_iter_t *it){
Input *in = ecs_field(it, Input, 1);
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
for (int i = 0; i < it->count; ++i) {
size_t ents_count;
int64_t *ents = world_chunk_fetch_entities_realpos(in[i].bx, in[i].by, &ents_count);
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
ecs_entity_t closest_pick = 0;
float min_pick = ZPL_F32_MAX;
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
for (size_t j = 0; j < ents_count; j++) {
const Position *p2 = ecs_get(it->world, ents[j], Position);
if (!p2) continue;
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
float dx = p2->x - in[i].bx;
float dy = p2->y - in[i].by;
float range = zpl_sqrt(dx*dx + dy*dy);
if (range <= PLAYER_MAX_INTERACT_RANGE && range < min_pick) {
min_pick = range;
closest_pick = ents[j];
}
}
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
in[i].pick_ent = closest_pick;
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
if (in[i].pick)
2022-08-09 15:54:00 +00:00
in[i].sel_ent = (in[i].sel_ent == closest_pick) ? 0 : closest_pick;
2021-08-11 16:19:10 +00:00
}
}
2021-08-30 15:50:05 +00:00
void EnableWorldEdit(ecs_iter_t *it) {
world_set_stage(it->world);
}
void DisableWorldEdit(ecs_iter_t *it) {
(void)it;
world_set_stage(NULL);
}
2021-07-27 12:43:26 +00:00
void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems);
2022-09-13 16:24:43 +00:00
2021-08-30 15:50:05 +00:00
ECS_SYSTEM(ecs, EnableWorldEdit, EcsOnLoad);
2021-08-11 10:22:46 +00:00
ECS_SYSTEM(ecs, MovementImpulse, EcsOnLoad, components.Input, components.Velocity, components.Position, !components.IsInVehicle);
2022-07-31 14:34:47 +00:00
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnLoad, components.Velocity, components.DemoNPC);
2022-09-13 16:24:43 +00:00
2021-08-11 16:19:10 +00:00
ECS_SYSTEM(ecs, ApplyWorldDragOnVelocity, EcsOnUpdate, components.Position, components.Velocity);
2021-07-27 16:34:31 +00:00
ECS_SYSTEM(ecs, HurtOnHazardBlock, EcsOnUpdate, components.Position, components.Health);
ECS_SYSTEM(ecs, RegenerateHP, EcsOnUpdate, components.Health);
2021-08-09 17:30:39 +00:00
ECS_SYSTEM(ecs, VehicleHandling, EcsOnUpdate, components.Vehicle, components.Position, components.Velocity);
2022-09-13 16:24:43 +00:00
2021-07-27 12:43:26 +00:00
ECS_SYSTEM(ecs, IntegratePositions, EcsOnValidate, components.Position, components.Velocity);
2022-09-13 16:24:43 +00:00
2021-08-29 12:59:00 +00:00
ECS_SYSTEM(ecs, EnterVehicle, EcsPostUpdate, components.Input, components.Position, !components.IsInVehicle);
ECS_SYSTEM(ecs, LeaveVehicle, EcsPostUpdate, components.Input, components.IsInVehicle, components.Velocity);
2022-09-13 16:24:43 +00:00
2022-08-09 14:46:23 +00:00
ECS_SYSTEM(ecs, PlayerClosestInteractable, EcsPostUpdate, components.Input);
2021-08-30 15:50:05 +00:00
ECS_SYSTEM(ecs, PickItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
ECS_SYSTEM(ecs, DropItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
ECS_SYSTEM(ecs, SwapItems, EcsPostUpdate, components.Input, components.Inventory);
2021-09-09 07:46:22 +00:00
//ECS_SYSTEM(ecs, MergeItems, EcsPostUpdate, components.Position, components.ItemDrop);
2021-08-30 15:50:05 +00:00
ECS_SYSTEM(ecs, UseItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
2022-08-09 14:46:23 +00:00
ECS_SYSTEM(ecs, InspectContainers, EcsPostUpdate, components.Input, !components.IsInVehicle);
2022-08-09 16:11:02 +00:00
ECS_SYSTEM(ecs, HarvestIntoContainers, EcsPostUpdate, components.ItemContainer, components.Position);
2022-09-13 16:24:43 +00:00
2021-11-02 09:22:04 +00:00
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
2022-09-13 16:24:43 +00:00
2021-08-11 08:54:52 +00:00
ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle);
2022-09-13 16:24:43 +00:00
2021-08-30 15:50:05 +00:00
ECS_SYSTEM(ecs, DisableWorldEdit, EcsPostUpdate);
2022-09-13 16:24:43 +00:00
2021-05-04 17:41:30 +00:00
}