entity control cmd

This commit is contained in:
jacob 2025-11-13 21:43:26 -06:00
parent e7a3e1f662
commit 3a21ad4886
3 changed files with 106 additions and 88 deletions

View File

@ -485,8 +485,7 @@ JobDef(S_SimWorker, _, __)
S_Ent *target = S_EntFromKey(&lookup, cmd.target);
if (target->active)
{
/* TODO: Clamp */
target->move = cmd.move;
target->move = ClampVec2Len(cmd.move, 1);
target->look = cmd.look;
}
}
@ -497,14 +496,13 @@ JobDef(S_SimWorker, _, __)
for (S_Ent *ent = S_FirstEnt(frame_arena, &iter, &lookup); ent->active; ent = S_NextEnt(frame_arena, &iter))
{
if (!S_IsKeyNil(ent->camera))
Xform xf = ent->local_xf;
if (!IsVec2Zero(ent->look))
{
Xform xf = ent->local_xf;
xf = RotateXform(xf, 0.1);
xf.og.x += 1;
xf.og.y += 1;
ent->local_xf = xf;
xf = XformWithWorldRotation(xf, AngleFromVec2(ent->look));
}
xf.og = AddVec2(xf.og, ent->move);
ent->local_xf = xf;
}
//////////////////////////////
@ -519,6 +517,7 @@ JobDef(S_SimWorker, _, __)
//////////////////////////////
//- Publish sim state
/* TODO: Only copy active entities */
LockTicketMutex(&shared->output_back_tm);
{
S_OutputState *output = &shared->output_states[shared->output_back_idx];

View File

@ -22,73 +22,6 @@ void V_Shutdown(void)
YieldOnFence(&shared->worker_completion_fence, shared->workers_count);
}
////////////////////////////////////////////////////////////
//~ Test ents
void V_PushTestEnts(Arena *arena, S_EntList *list)
{
S_Key player_key = S_RandKey();
S_Key child_key = S_RandKey();
S_Key camera_key = S_RandKey();
i32 count = 3;
for (u64 i = 0; i < count; ++i)
{
S_EntListNode *n = PushStruct(arena, S_EntListNode);
SllQueuePush(list->first, list->last, n);
++list->count;
S_Ent *ent = &n->ent;
*ent = S_nil_ent;
switch (i)
{
/* Test player */
case 0:
{
ent->tint = Color_Red;
ent->key = player_key;
ent->camera = camera_key;
{
ent->local_shape = S_ShapeFromDesc(
.mass = 10,
.count = 1,
.radius = 50,
);
}
} break;
/* Test child */
case 1:
{
ent->tint = Color_Cyan;
ent->key = child_key;
ent->parent = player_key;
{
ent->local_shape = S_ShapeFromDesc(
.count = 4,
.points = {
VEC2(-15, -15),
VEC2(15, -15),
VEC2(15, 15),
VEC2(-15, 15)
}
);
}
} break;
/* Test camera */
case 2:
{
// ent->key = camera_key;
} break;
default:
{
Assert(0);
} break;
}
}
}
////////////////////////////////////////////////////////////
//~ Vis worker
@ -178,6 +111,7 @@ JobDef(V_VisWorker, _, __)
Arena *world_arena = AcquireArena(Gibi(64));
S_World *world = PushStruct(world_arena, S_World);
S_Lookup lookup = ZI;
S_Key player_key = S_RandKey();
b32 shutdown = 0;
while (!shutdown)
@ -187,9 +121,73 @@ JobDef(V_VisWorker, _, __)
S_EntList spawn_ents = ZI;
//////////////////////////////
//- Spawn test ents
if (world->tick == 0)
{
V_PushTestEnts(frame_arena, &spawn_ents);
S_Key child_key = S_RandKey();
S_Key camera_key = S_RandKey();
i32 count = 3;
for (u64 i = 0; i < count; ++i)
{
S_EntListNode *n = PushStruct(frame_arena, S_EntListNode);
SllQueuePush(spawn_ents.first, spawn_ents.last, n);
++spawn_ents.count;
S_Ent *ent = &n->ent;
*ent = S_nil_ent;
switch (i)
{
/* Test player */
case 0:
{
ent->tint = Color_Red;
ent->key = player_key;
ent->camera = camera_key;
{
ent->local_shape = S_ShapeFromDesc(
.mass = 10,
.count = 1,
.radius = 50,
);
}
ent->local_xf = XformFromPos(VEC2(200, 200));
} break;
/* Test child */
case 1:
{
f32 width = 15;
f32 height = 100;
ent->tint = Color_Cyan;
ent->key = child_key;
ent->parent = player_key;
{
ent->local_shape = S_ShapeFromDesc(
.count = 4,
.points = {
VEC2(-width / 2, -height), VEC2(width / 2, -height),
VEC2(width / 2, 0), VEC2(-5, 0),
}
);
}
ent->local_xf = XformFromTrs(TRS(.t = { 0, 0 }, .r = Tau / 4));
} break;
/* Test camera */
case 2:
{
// ent->key = camera_key;
} break;
default:
{
Assert(0);
} break;
}
}
}
//////////////////////////////
@ -395,7 +393,6 @@ JobDef(V_VisWorker, _, __)
case V_CmdKind_spawn:
{
V_PushTestEnts(frame_arena, &spawn_ents);
} break;
}
}
@ -407,18 +404,45 @@ JobDef(V_VisWorker, _, __)
{
S_InputState *v2s = &sim_shared->input_states[sim_shared->input_back_idx];
/* Submit control cmd */
{
S_Cmd *cmd = 0;
{
S_CmdNode *cmd_node = PushStruct(v2s->arena, S_CmdNode);
SllQueuePush(v2s->first_cmd_node, v2s->last_cmd_node, cmd_node);
++v2s->cmds_count;
cmd = &cmd_node->cmd;
}
cmd->kind = S_CmdKind_Control;
Vec2 move = ZI;
{
if (held_buttons[Button_A]) move.x -= 1;
if (held_buttons[Button_D]) move.x += 1;
if (held_buttons[Button_W]) move.y -= 1;
if (held_buttons[Button_S]) move.y += 1;
}
Vec2 look = ZI;
{
S_Ent *player = S_EntFromKey(&lookup, player_key);
Vec2 center = MulXformV2(player->world_xf, player->local_shape.centroid);
look = SubVec2(UI_CursorPos(), center);
}
cmd->target = player_key;
cmd->move = move;
cmd->look = look;
}
/* Submit spawn cmds */
if (spawn_ents.count > 0)
{
S_CmdNode *cmd_node = PushStruct(v2s->arena, S_CmdNode);
S_Cmd *cmd = 0;
{
S_CmdNode *cmd_node = PushStruct(v2s->arena, S_CmdNode);
SllQueuePush(v2s->first_cmd_node, v2s->last_cmd_node, cmd_node);
++v2s->cmds_count;
cmd = &cmd_node->cmd;
}
S_Cmd *cmd = &cmd_node->cmd;
{
cmd->kind = S_CmdKind_Spawn;
}
cmd->kind = S_CmdKind_Spawn;
S_EntList *dst = &cmd->ents;
for (S_EntListNode *src_n = spawn_ents.first; src_n; src_n = src_n->next)
{
@ -436,9 +460,9 @@ JobDef(V_VisWorker, _, __)
//////////////////////////////
//- Build render data
/* Build shapes */
for (S_Ent *ent = S_FirstEnt(frame_arena, &iter, &lookup); ent->active; ent = S_NextEnt(frame_arena, &iter))
{
/* Draw shape */
b32 is_visible = ent->tint.w != 0;
if (is_visible)
{

View File

@ -87,11 +87,6 @@ Struct(V_SharedState)
void V_Startup(void);
void V_Shutdown(void);
////////////////////////////////////////////////////////////
//~ Test ents
void V_PushTestEnts(Arena *arena, S_EntList *list);
////////////////////////////////////////////////////////////
//~ Vis worker