wip demo npc test

isolation_bkp/dynres
Dominik Madarász 2021-05-08 11:05:15 +02:00
parent 6b4d5d467a
commit 235acfa122
11 changed files with 107 additions and 14 deletions

View File

@ -5,6 +5,7 @@
#include "packet.h"
#include "signal_handling.h"
#include "network.h"
#include "entity.h"
#include "world_view.h"
#include "entity_view.h"
#include "camera.h"
@ -14,6 +15,10 @@
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
#include "modules/general.h"
#include "modules/physics.h"
#include "modules/controllers.h"
#include "packets/pkt_00_init.h"
#include "packets/pkt_01_welcome.h"
#include "packets/pkt_send_keystate.h"
@ -115,6 +120,23 @@ void game_init(int8_t play_mode, uint32_t num_viewers, int32_t seed, uint16_t bl
for (uint32_t i = 0; i < num_viewers; i++) {
pkt_00_init_send(i);
}
// TODO(zaklaus): VERY TEMPORARY -- SPAWN SOME NPCS THAT RANDOMLY MOVE
for (int i = 0; i < 100; i++) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Physics);
uint64_t e = entity_spawn(NULL);
ecs_add(world_ecs(), e, EcsDemoNPC);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
uint16_t half_world_dim = world_dim() / 2;
pos->x=rand() % world_dim() - half_world_dim;
pos->y=rand() % world_dim() - half_world_dim;
Velocity *v = ecs_get_mut(world_ecs(), e, Velocity, NULL);
v->x = (rand()%3-1) * 100;
v->y = (rand()%3-1) * 100;
}
}
int8_t game_is_networked() {

View File

@ -185,6 +185,15 @@ void DEBUG_draw_entities(uint64_t key, entity_view data) {
float fixed_title_offset = 2;
switch (data.kind) {
case EKIND_THING: {
double x = data.x;
double y = data.y;
const char *title = TextFormat("Thing %d", key);
int title_w = MeasureTextEco(title, font_size, font_spacing);
DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, BLACK);
DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, RAYWHITE, font_spacing);
DrawCircleEco(x, y, size, BLUE);
}break;
case EKIND_PLAYER: {
double x = data.x;
double y = data.y;

View File

@ -1,5 +1,6 @@
#include "world_view.h"
#include "librg.h"
#include "zpl.h"
int32_t tracker_read_remove(librg_world *w, librg_event *e) {
int64_t owner_id = librg_event_owner_get(w, e);

View File

@ -0,0 +1,37 @@
#include "entity.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
#include "world.h"
#include "modules/general.h"
#include "modules/controllers.h"
#include "modules/net.h"
#include "modules/physics.h"
#include "zpl.h"
uint64_t entity_spawn(char *name) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Physics);
ecs_entity_t e = ecs_new(world_ecs(), 0);
if (!name) {
name = zpl_bprintf("entity_%d", e);
}
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_set(world_ecs(), e, Velocity, {0});
ecs_set(world_ecs(), e, Position, {0});
ecs_add(world_ecs(), e, Walking);
librg_entity_track(world_tracker(), e);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), 0, 0, 0));
return (uint64_t)e;
}
void entity_despawn(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
}

View File

@ -0,0 +1,5 @@
#pragma once
#include "system.h"
uint64_t entity_spawn(char *name);
void entity_despawn(uint64_t ent_id);

View File

@ -8,6 +8,7 @@
typedef enum {
EKIND_PLAYER,
EKIND_THING,
EKIND_CHUNK,
FORCE_EKIND_UINT16 = UINT16_MAX
} entity_kind;

View File

@ -1,4 +1,5 @@
#include "player.h"
#include "entity.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
@ -10,31 +11,26 @@
#include "modules/physics.h"
#include "zpl.h"
uint64_t player_spawn(char *name) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Physics);
ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = ecs_new(world_ecs(), 0);
uint64_t player_spawn(char *name) {
ecs_entity_t e = entity_spawn(NULL);
if (!name) {
name = zpl_bprintf("player_%d", e);
}
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Net);
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_add(world_ecs(), e, EcsClient);
ecs_set(world_ecs(), e, ClientInfo, {0});
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_set(world_ecs(), e, Input, {0});
ecs_set(world_ecs(), e, Velocity, {0});
ecs_add(world_ecs(), e, Walking);
ecs_add(world_ecs(), e, Player);
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
uint16_t half_world_dim = world_dim() / 2;
pos->x=rand() % world_dim() - half_world_dim;
pos->y=rand() % world_dim() - half_world_dim;
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, (int64_t)e);
librg_entity_radius_set(world_tracker(), e, 3);
librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0));

View File

@ -2,4 +2,4 @@
#include "system.h"
uint64_t player_spawn(char *name);
void player_despawn(uint64_t ent_id);
void player_despawn(uint64_t ent_id);

View File

@ -30,14 +30,16 @@ int32_t world_gen();
entity_view world_build_entity_view(int64_t e) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Net);
entity_view view = {0};
// TODO(zaklaus): branch out based on ECS tags
const Position *pos = ecs_get(world_ecs(), e, Position);
if (pos) {
view.kind = EKIND_PLAYER;
view.kind = ecs_has(world_ecs(), e, EcsClient) ? EKIND_PLAYER : EKIND_THING;
view.x = pos->x;
view.y = pos->y;
return view;
}
const Chunk *chpos = ecs_get(world_ecs(), e, Chunk);
@ -45,6 +47,7 @@ entity_view world_build_entity_view(int64_t e) {
view.kind = EKIND_CHUNK;
view.x = chpos->x;
view.y = chpos->y;
return view;
}
return view;

View File

@ -14,9 +14,11 @@ typedef struct {
ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder);
ECS_DECLARE_ENTITY(EcsDemoNPC);
ECS_DECLARE_TYPE(Player);
ECS_DECLARE_TYPE(Builder);
ECS_DECLARE_ENTITY(MovementImpulse);
ECS_DECLARE_ENTITY(DemoNPCMoveAround);
} Controllers;
#define ControllersImportHandles(handles)\
@ -26,6 +28,8 @@ typedef struct {
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, MovementImpulse);\
ECS_IMPORT_ENTITY(handles, DemoNPCMoveAround);\
void ControllersImport(ecs_world_t *ecs);

View File

@ -20,6 +20,18 @@ void MovementImpulse(ecs_iter_t *it) {
}
}
#define DEMO_NPC_CHANGEDIR_FACTOR 0.03
#define DEMO_NPC_MOVE_SPEED 100
void DemoNPCMoveAround(ecs_iter_t *it) {
Velocity *v = ecs_column(it, Velocity, 1);
for (int i = 0; i < it->count; i++) {
v[i].x = zpl_lerp(v[i].x, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
v[i].y = zpl_lerp(v[i].y, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
}
}
void ControllersImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Controllers);
ecs_set_name_prefix(ecs, "Controllers");
@ -33,8 +45,10 @@ void ControllersImport(ecs_world_t *ecs) {
ECS_TAG(ecs, EcsActor);
ECS_TAG(ecs, EcsPlayer);
ECS_TAG(ecs, EcsBuilder);
ECS_TAG(ecs, EcsDemoNPC);
ECS_SYSTEM(ecs, MovementImpulse, EcsOnUpdate, Input, physics.Velocity);
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnUpdate, physics.Velocity, EcsDemoNPC);
ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input, EcsActor);
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsActor, EcsPlayer);
@ -44,6 +58,7 @@ void ControllersImport(ecs_world_t *ecs) {
ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsPlayer);
ECS_SET_ENTITY(EcsBuilder);
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
ECS_SET_ENTITY(MovementImpulse);