eco2d/code/common/player.c

51 lines
1.5 KiB
C
Raw Normal View History

2021-05-05 07:55:45 +00:00
#include "player.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
#include "world.h"
#include "modules/general.h"
#include "modules/controllers.h"
2021-05-05 13:14:02 +00:00
#include "modules/net.h"
2021-05-07 14:43:54 +00:00
#include "modules/physics.h"
2021-05-06 18:24:01 +00:00
#include "zpl.h"
2021-05-05 07:55:45 +00:00
uint64_t player_spawn(char *name) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
2021-05-07 14:43:54 +00:00
ECS_IMPORT(world_ecs(), Physics);
2021-05-05 13:14:02 +00:00
ECS_IMPORT(world_ecs(), Net);
2021-05-05 07:55:45 +00:00
ecs_entity_t e = ecs_new(world_ecs(), 0);
2021-05-06 18:24:01 +00:00
if (!name) {
name = zpl_bprintf("player_%d", e);
}
2021-05-05 13:14:02 +00:00
ecs_add(world_ecs(), e, EcsClient);
ecs_set(world_ecs(), e, ClientInfo, {0});
2021-05-05 07:55:45 +00:00
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
2021-05-05 13:14:02 +00:00
ecs_set(world_ecs(), e, Input, {0});
2021-05-07 14:43:54 +00:00
ecs_set(world_ecs(), e, Velocity, {0});
ecs_add(world_ecs(), e, Walking);
2021-05-06 16:26:52 +00:00
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
uint16_t world_dim = world_block_size() * world_chunk_size() * world_world_size();
uint16_t half_world_dim = world_dim / 2;
pos->x=3*32*16;
pos->y=0;
/*pos->x = rand()%world_dim-half_world_dim;
pos->y = rand()%world_dim-half_world_dim;*/
2021-05-05 13:14:02 +00:00
2021-05-05 07:55:45 +00:00
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, (int64_t)e);
librg_entity_radius_set(world_tracker(), e, 4);
2021-05-06 16:26:52 +00:00
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));
2021-05-05 13:14:02 +00:00
2021-05-05 07:55:45 +00:00
return (uint64_t)e;
}
void player_despawn(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
2021-05-05 13:14:02 +00:00
}