eco2d/code/game/src/entity.c

91 lines
2.5 KiB
C
Raw Normal View History

2021-05-08 09:05:15 +00:00
#include "entity.h"
2021-07-27 15:57:50 +00:00
#include "entity_view.h"
2021-05-08 09:05:15 +00:00
#include "flecs/flecs.h"
#include "librg.h"
2021-05-12 15:50:30 +00:00
#include "world/world.h"
2021-05-08 09:05:15 +00:00
2021-07-27 12:43:26 +00:00
#include "modules/components.h"
#include "modules/systems.h"
2021-05-08 09:05:15 +00:00
#include "zpl.h"
2021-07-28 09:49:09 +00:00
uint64_t entity_spawn(uint16_t class_id) {
2021-05-08 09:05:15 +00:00
ecs_entity_t e = ecs_new(world_ecs(), 0);
2022-08-09 14:46:23 +00:00
2021-08-09 15:18:34 +00:00
ecs_set(world_ecs(), e, Classify, { .id = class_id });
entity_wake(e);
2022-08-09 14:46:23 +00:00
if (class_id != EKIND_SERVER) {
ecs_set(world_ecs(), e, Velocity, {0});
2022-07-31 14:34:47 +00:00
Position *pos = ecs_get_mut(world_ecs(), e, Position);
2021-07-26 15:53:18 +00:00
#if 1
pos->x=(float)(rand() % world_dim());
pos->y=(float)(rand() % world_dim());
2021-07-26 15:53:18 +00:00
#else
pos->x=350.0f;
pos->y=88.0f;
2021-07-26 15:53:18 +00:00
#endif
2022-08-09 14:46:23 +00:00
2021-07-27 15:57:50 +00:00
librg_entity_track(world_tracker(), e);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));
2021-08-10 15:21:25 +00:00
librg_entity_owner_set(world_tracker(), e, (int64_t)e);
2021-07-27 15:57:50 +00:00
}
2022-08-09 14:46:23 +00:00
2021-05-08 09:05:15 +00:00
return (uint64_t)e;
}
void entity_batch_despawn(uint64_t *ids, size_t num_ids) {
for (size_t i = 0; i < num_ids; i++ ) {
librg_entity_untrack(world_tracker(), ids[i]);
ecs_delete(world_ecs(), ids[i]);
}
}
2021-05-08 09:05:15 +00:00
void entity_despawn(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
}
2022-08-01 09:28:54 +00:00
void entity_set_position(uint64_t ent_id, float x, float y) {
Position *p = ecs_get_mut(world_ecs(), ent_id, Position);
p->x = x;
p->y = y;
2022-08-09 14:46:23 +00:00
2022-08-01 09:28:54 +00:00
entity_wake(ent_id);
}
void entity_wake(uint64_t ent_id) {
StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo);
si->tick_delay = 0.0f;
si->last_update = 0.0f;
}
static ecs_query_t *ecs_streaminfo = NULL;
void entity_update_action_timers() {
static double last_update_time = 0.0f;
if (!ecs_streaminfo) {
ecs_streaminfo = ecs_query_new(world_ecs(), "components.StreamInfo");
last_update_time = zpl_time_rel();
}
2022-08-09 14:46:23 +00:00
ecs_iter_t it = ecs_query_iter(world_ecs(), ecs_streaminfo);
2022-08-09 14:46:23 +00:00
while (ecs_query_next(&it)) {
StreamInfo *si = ecs_field(&it, StreamInfo, 1);
2022-08-09 14:46:23 +00:00
for (size_t i = 0; i < it.count; i++) {
if (si[i].last_update < zpl_time_rel()) {
si[i].last_update = zpl_time_rel() + si[i].tick_delay;
si[i].tick_delay += (zpl_time_rel() - last_update_time) * 0.5f;
}
}
}
2022-08-09 14:46:23 +00:00
last_update_time = zpl_time_rel();
}
bool entity_can_stream(uint64_t ent_id) {
StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo);
return (si->last_update < zpl_time_rel());
}