1900 lines
71 KiB
C
1900 lines
71 KiB
C
////////////////////////////////
|
|
//~ Sim accel
|
|
|
|
SimAccel AcquireSimAccel(void)
|
|
{
|
|
SimAccel accel = ZI;
|
|
accel.space = space_acquire(SPACE_CELL_SIZE, SPACE_CELL_BINS_SQRT);
|
|
return accel;
|
|
}
|
|
|
|
void ReleaseSimAccel(SimAccel *accel)
|
|
{
|
|
space_release(accel->space);
|
|
}
|
|
|
|
void ResetSimAccel(Snapshot *ss, SimAccel *accel)
|
|
{
|
|
space_reset(accel->space);
|
|
|
|
/* Reset ent space handles */
|
|
for (u64 sim_ent_index = 0; sim_ent_index < ss->num_ents_reserved; ++sim_ent_index) {
|
|
Entity *ent = &ss->ents[sim_ent_index];
|
|
if (ent->valid) {
|
|
ZeroStruct(&ent->space_handle);
|
|
}
|
|
}
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ Spawn test operations
|
|
|
|
/* TODO: Remove this */
|
|
|
|
Entity *SpawnTestSmg(Entity *parent)
|
|
{
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
e->sprite = S_TagFromPath(Lit("sprite/gun.ase"));
|
|
|
|
EnableProp(e, Prop_Attached);
|
|
e->attach_slice = Lit("attach.wep");
|
|
e->layer = SIM_LAYER_RELATIVE_WEAPON;
|
|
|
|
EnableProp(e, Prop_Smg);
|
|
e->primary_fire_delay = 1.0f / 10.0f;
|
|
e->secondary_fire_delay = 1.0f / 10.0f;
|
|
|
|
return e;
|
|
}
|
|
|
|
Entity *SpawnTestLauncher(Entity *parent)
|
|
{
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
e->sprite = S_TagFromPath(Lit("sprite/gun.ase"));
|
|
|
|
EnableProp(e, Prop_Attached);
|
|
e->attach_slice = Lit("attach.wep");
|
|
e->layer = SIM_LAYER_RELATIVE_WEAPON;
|
|
|
|
EnableProp(e, Prop_Launcher);
|
|
e->primary_fire_delay = 1.0f / 10.0f;
|
|
e->secondary_fire_delay = 1.0f / 10.0f;
|
|
|
|
return e;
|
|
}
|
|
|
|
Entity *SpawnTestChucker(Entity *parent)
|
|
{
|
|
Entity *chucker = AcquireSyncSrc(parent);
|
|
chucker->sprite = S_TagFromPath(Lit("sprite/gun.ase"));
|
|
|
|
EnableProp(chucker, Prop_Attached);
|
|
chucker->attach_slice = Lit("attach.wep");
|
|
chucker->layer = SIM_LAYER_RELATIVE_WEAPON;
|
|
|
|
EnableProp(chucker, Prop_Chucker);
|
|
chucker->primary_fire_delay = 1.0f / 10.0f;
|
|
chucker->secondary_fire_delay = 1.0f / 2.0f;
|
|
|
|
/* Chucker zone */
|
|
{
|
|
Entity *zone = AcquireSyncSrc(chucker);
|
|
|
|
EnableProp(zone, Prop_ChuckerZone);
|
|
|
|
EnableProp(zone, Prop_Attached);
|
|
zone->attach_slice = Lit("out");
|
|
|
|
EnableProp(zone, Prop_Sensor);
|
|
CLD_Shape collider = ZI;
|
|
collider.count = 2;
|
|
collider.points[1] = VEC2(0, -0.5);
|
|
collider.radius = 0.1f;
|
|
zone->local_collider = collider;
|
|
|
|
chucker->chucker_zone = zone->id;
|
|
}
|
|
|
|
return chucker;
|
|
}
|
|
|
|
Entity *SpawnTestEmployee(Entity *parent)
|
|
{
|
|
/* Player */
|
|
Entity *employee = NilEntity();
|
|
{
|
|
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
Vec2 pos = VEC2(1, -1);
|
|
|
|
//Vec2 size = VEC2(0.5, 0.5);
|
|
//Vec2 size = VEC2(0.5, 0.25);
|
|
Vec2 size = VEC2(1.0, 1.0);
|
|
|
|
//f32 r = Pi / 4;
|
|
f32 r = 0;
|
|
|
|
{
|
|
EnableProp(e, Prop_Test);
|
|
e->sprite = S_TagFromPath(Lit("sprite/tim.ase"));
|
|
e->mass_unscaled = 10;
|
|
e->inertia_unscaled = 5;
|
|
}
|
|
|
|
//e->sprite = S_TagFromPath(Lit("sprite/box_rounded.ase"));
|
|
//e->sprite_span_name = Lit("idle.unarmed");
|
|
//e->sprite_span_name = Lit("idle.one_handed");
|
|
e->sprite_span_name = Lit("idle.two_handed");
|
|
e->layer = SIM_LAYER_SHOULDERS;
|
|
|
|
e->local_collider.points[0] = VEC2(0, 0);
|
|
e->local_collider.count = 1;
|
|
e->local_collider.radius = 0.25f;
|
|
|
|
Xform xf = XformFromTrs(TRS(.t = pos, .r = r, .s = size));
|
|
//xf.bx.y = -1.f;
|
|
|
|
SetXform(e, xf);
|
|
|
|
e->linear_ground_friction = 250;
|
|
e->angular_ground_friction = 200;
|
|
|
|
e->friction = 0;
|
|
|
|
|
|
|
|
|
|
EnableProp(e, Prop_LightTest);
|
|
e->sprite_emittance = VEC3(1, 1, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//e->control_force = 500;
|
|
e->control_force = 1200;
|
|
e->control_force_max_speed = 7;
|
|
|
|
//e->control_torque = 5000;
|
|
e->control_torque = F32Infinity;
|
|
|
|
EnableProp(e, Prop_Dynamic);
|
|
EnableProp(e, Prop_Solid);
|
|
|
|
employee = e;
|
|
}
|
|
|
|
/* Player weapon */
|
|
if (employee->valid) {
|
|
LAX SpawnTestSmg;
|
|
LAX SpawnTestLauncher;
|
|
LAX SpawnTestChucker;
|
|
|
|
Entity *e = SpawnTestChucker(employee);
|
|
employee->equipped = e->id;
|
|
|
|
EnableProp(e, Prop_LightTest);
|
|
e->sprite_emittance = VEC3(1, 1, 1);
|
|
}
|
|
|
|
return employee;
|
|
}
|
|
|
|
Entity *SpawnTestCamera(Entity *parent, Entity *follow)
|
|
{
|
|
Entity *camera_ent = NilEntity();
|
|
if (follow->valid) {
|
|
camera_ent = AcquireSyncSrc(parent);
|
|
SetXform(camera_ent, XformIdentity);
|
|
|
|
EnableProp(camera_ent, Prop_Camera);
|
|
EnableProp(camera_ent, Prop_ActiveCamera);
|
|
camera_ent->camera_follow = follow->id;
|
|
|
|
f32 width = (f32)DEFAULT_CAMERA_WIDTH;
|
|
f32 height = (f32)DEFAULT_CAMERA_HEIGHT;
|
|
camera_ent->camera_quad_xform = XformFromTrs(TRS(.s = VEC2(width, height)));
|
|
}
|
|
|
|
return camera_ent;
|
|
}
|
|
|
|
Entity *SpawnTestExplosion(Entity *parent, Vec2 pos, f32 strength, f32 radius)
|
|
{
|
|
Entity *ent = AcquireSyncSrc(parent);
|
|
SetXform(ent, XformFromPos(pos));
|
|
|
|
EnableProp(ent, Prop_Explosion);
|
|
ent->explosion_strength = strength;
|
|
ent->explosion_radius = radius;
|
|
|
|
EnableProp(ent, Prop_Sensor);
|
|
ent->local_collider.count = 1;
|
|
ent->local_collider.radius = radius;
|
|
|
|
return ent;
|
|
}
|
|
|
|
void TeleportTest(Entity *ent, Vec2 pos)
|
|
{
|
|
//++ent->continuity_gen;
|
|
Xform xf = XformFromEntity(ent);
|
|
xf.og = pos;
|
|
SetXform(ent, xf);
|
|
}
|
|
|
|
void SpawnTestEntities1(Entity *parent, Vec2 pos)
|
|
{
|
|
LAX pos;
|
|
|
|
/* Enemy */
|
|
{
|
|
Entity *e = SpawnTestEmployee(parent);
|
|
Xform xf = XformFromEntity(e);
|
|
xf.og = pos;
|
|
SetXform(e, xf);
|
|
}
|
|
}
|
|
|
|
void SpawnTestEntities2(Entity *parent, Vec2 pos)
|
|
{
|
|
LAX pos;
|
|
|
|
/* Small Box */
|
|
#if 1
|
|
{
|
|
//Entity *e = AcquireLocal(parent);
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
f32 rot = 0;
|
|
Vec2 size = VEC2(0.125, 0.125);
|
|
Xform xf = XformFromTrs(TRS(.t = pos, .r = rot, .s = size));
|
|
SetXform(e, xf);
|
|
|
|
e->sprite = S_TagFromPath(Lit("sprite/tile.ase"));
|
|
e->layer = SIM_LAYER_SHOULDERS;
|
|
|
|
//e->sprite_tint = Alpha32F(ColorBlue, 0.75);
|
|
//e->sprite_tint = Alpha32F(ColorWhite, 1);
|
|
|
|
EnableProp(e, Prop_Solid);
|
|
Quad collider_quad = QuadFromRect(RectFromScalar(-0.5, -0.5, 1, 1));
|
|
e->local_collider = CLD_ShapeFromQuad(collider_quad);
|
|
|
|
EnableProp(e, Prop_LightTest);
|
|
|
|
/* FIXME: Remove this */
|
|
{
|
|
static RandState rand = ZI;
|
|
f32 r = RandF64FromState(&rand, 1, 5);
|
|
f32 g = RandF64FromState(&rand, 1, 5);
|
|
f32 b = RandF64FromState(&rand, 1, 5);
|
|
e->sprite_emittance = VEC3(r, g, b);
|
|
e->sprite_tint = Rgba32F(r / 5, g / 5, b / 5, 1);
|
|
}
|
|
|
|
EnableProp(e, Prop_Dynamic);
|
|
e->mass_unscaled = 50;
|
|
e->inertia_unscaled = 2;
|
|
#if 0
|
|
e->linear_ground_friction = 100;
|
|
e->angular_ground_friction = 50;
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
/* Tiny box */
|
|
#if 0
|
|
{
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
f32 r = Pi / 4;
|
|
Vec2 size = VEC2(0.5, 0.25);
|
|
Xform xf = XformFromTrs(.t = pos, .r = r, .s = size);
|
|
SetXform(e, xf);
|
|
|
|
e->sprite = S_TagFromPath(Lit("sprite/bullet.ase"));
|
|
e->sprite_collider_slice = Lit("shape");
|
|
e->layer = SIM_LAYER_SHOULDERS;
|
|
|
|
EnableProp(e, Prop_Solid);
|
|
|
|
EnableProp(e, Prop_Dynamic);
|
|
e->mass_unscaled = 0.5;
|
|
e->inertia_unscaled = 1000;
|
|
e->linear_ground_friction = 0.001;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void SpawnTestEntities3(Entity *parent, Vec2 pos)
|
|
{
|
|
LAX pos;
|
|
|
|
/* Heavy box */
|
|
{
|
|
//Entity *e = AcquireLocal(parent);
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
f32 r = 0;
|
|
Vec2 size = VEC2(1, 1);
|
|
Xform xf = XformFromTrs(TRS(.t = pos, .r = r, .s = size));
|
|
SetXform(e, xf);
|
|
|
|
e->sprite = S_TagFromPath(Lit("sprite/box.ase"));
|
|
e->layer = SIM_LAYER_SHOULDERS;
|
|
|
|
e->sprite_tint = ColorRed;
|
|
|
|
EnableProp(e, Prop_Solid);
|
|
Quad collider_quad = QuadFromRect(RectFromScalar(-0.5, -0.5, 1, 1));
|
|
e->local_collider = CLD_ShapeFromQuad(collider_quad);
|
|
}
|
|
}
|
|
|
|
void SpawnTestEntities4(Entity *parent, Vec2 pos)
|
|
{
|
|
LAX pos;
|
|
|
|
/* Light box */
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
f32 r = 0;
|
|
Vec2 size = VEC2(2, 1);
|
|
Xform xf = XformFromTrs(TRS(.t = pos, .r = r, .s = size));
|
|
SetXform(e, xf);
|
|
|
|
//e->sprite = S_TagFromPath(Lit("sprite/box.ase"));
|
|
e->sprite = S_TagFromPath(Lit("sprite/tile.ase"));
|
|
e->layer = SIM_LAYER_SHOULDERS;
|
|
|
|
EnableProp(e, Prop_LightTest);
|
|
e->sprite_emittance = VEC3(2, 2, 2);
|
|
|
|
e->sprite_tint = Rgb32F(1, 1, 1);
|
|
}
|
|
|
|
void SpawnTestTile(Snapshot *world, Vec2 world_pos)
|
|
{
|
|
#if 0
|
|
Entity *e = AcquireSyncSrc(parent);
|
|
|
|
i32 sign_x = (world_pos.x >= 0) - (world_pos.x < 0);
|
|
i32 sign_y = (world_pos.y >= 0) - (world_pos.y < 0);
|
|
Vec2I32 tile_index = VEC2I32(world_pos.x * SIM_TILES_PER_UNIT_SQRT, world_pos.y * SIM_TILES_PER_UNIT_SQRT);
|
|
world_pos.x -= sign_x < 0;
|
|
world_pos.y -= sign_y < 0;
|
|
|
|
Vec2 tile_size = VEC2(1.f / SIM_TILES_PER_UNIT_SQRT, 1.f / SIM_TILES_PER_UNIT_SQRT);
|
|
|
|
Vec2 pos = VEC2((f32)tile_index.x / SIM_TILES_PER_UNIT_SQRT, (f32)tile_index.y / SIM_TILES_PER_UNIT_SQRT);
|
|
pos = AddVec2(pos, MulVec2(VEC2(tile_size.x * sign_x, tile_size.y * sign_y), 0.5));
|
|
|
|
Xform xf = XformFromTrs(.t = pos);
|
|
SetXform(e, xf);
|
|
|
|
e->layer = SIM_LAYER_WALLS;
|
|
e->sprite = S_TagFromPath(Lit("sprite/tile.ase"));
|
|
e->sprite_tint = ColorRed;
|
|
|
|
{
|
|
S_Scope *scope = S_BeginScope();
|
|
S_Sheet *sheet = S_SheetFromTagAwait(scope, e->sprite);
|
|
e->sprite_local_xform = XformFromTrs(.s = DivVec2(sheet->frame_size, PIXELS_PER_UNIT));
|
|
S_EndScope(scope);
|
|
}
|
|
|
|
EnableProp(e, Prop_Solid);
|
|
Quad collider_quad = QuadFromRect(RectFromScalar(-tile_size.x / 2, -tile_size.y / 2, tile_size.y, tile_size.y));
|
|
e->local_collider = CLD_ShapeFromQuad(collider_quad);
|
|
#else
|
|
Vec2I32 tile_index = sim_world_tile_index_from_pos(world_pos);
|
|
sim_snapshot_set_tile(world, tile_index, SIM_TILE_KIND_WALL);
|
|
#endif
|
|
}
|
|
|
|
void ClearLevelTest(SimStepCtx *ctx)
|
|
{
|
|
Snapshot *world = ctx->world;
|
|
for (u64 j = 0; j < world->num_ents_reserved; ++j) {
|
|
Entity *ent = &world->ents[j];
|
|
if (ent->valid) {
|
|
EnableProp(ent, Prop_Release);
|
|
}
|
|
}
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ Tile test operations
|
|
|
|
MergesortCompareFuncDef(SortTileXCmp, arg_a, arg_b, _)
|
|
{
|
|
Entity *a = *(Entity **)arg_a;
|
|
Entity *b = *(Entity **)arg_b;
|
|
i32 a_x = a->tile_chunk_index.x;
|
|
i32 b_x = b->tile_chunk_index.x;
|
|
|
|
i32 result = 0;
|
|
result = (a_x < b_x) - (a_x > b_x);
|
|
return result;
|
|
}
|
|
|
|
MergesortCompareFuncDef(SortTileYCmp, arg_a, arg_b, _)
|
|
{
|
|
Entity *a = *(Entity **)arg_a;
|
|
Entity *b = *(Entity **)arg_b;
|
|
i32 a_y = a->tile_chunk_index.y;
|
|
i32 b_y = b->tile_chunk_index.y;
|
|
|
|
i32 result = 0;
|
|
result = (a_y < b_y) - (a_y > b_y);
|
|
return result;
|
|
}
|
|
|
|
void GenerateTestWalls(Snapshot *world)
|
|
{
|
|
__prof;
|
|
TempArena scratch = BeginScratchNoConflict();
|
|
Entity *root = EntityFromId(world, RootEntityId);
|
|
|
|
/* Release existing walls and gather tile chunks.
|
|
* NOTE: We sort tile chunks before iterating so that chunk-edge tiles only
|
|
* need to check for adjacent walls to merge with in one direction */
|
|
Entity **x_sorted_tile_chunks = 0;
|
|
Entity **y_sorted_tile_chunks = 0;
|
|
u64 sorted_tile_chunks_count = 0;
|
|
{
|
|
x_sorted_tile_chunks = PushDry(scratch.arena, Entity *);
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ent->valid) continue;
|
|
if (HasProp(ent, Prop_TileChunk)) {
|
|
/* Append chunk to array */
|
|
*PushStructNoZero(scratch.arena, Entity *) = ent;
|
|
++sorted_tile_chunks_count;
|
|
} else if (HasProp(ent, Prop_Wall)) {
|
|
/* Release existing wall */
|
|
EnableProp(ent, Prop_Release);
|
|
}
|
|
}
|
|
y_sorted_tile_chunks = PushStructsNoZero(scratch.arena, Entity *, sorted_tile_chunks_count);
|
|
CopyBytes(y_sorted_tile_chunks, x_sorted_tile_chunks, sizeof(*x_sorted_tile_chunks) * sorted_tile_chunks_count);
|
|
|
|
/* NOTE: We sort x & y separately because it's possible that a wall
|
|
* should merge with another wall that was generated from a diagonal chunk. */
|
|
Mergesort(x_sorted_tile_chunks, sorted_tile_chunks_count, sizeof(*x_sorted_tile_chunks), SortTileXCmp, 0);
|
|
Mergesort(y_sorted_tile_chunks, sorted_tile_chunks_count, sizeof(*y_sorted_tile_chunks), SortTileYCmp, 0);
|
|
}
|
|
|
|
struct wall_node {
|
|
Vec2I32 start;
|
|
Vec2I32 end;
|
|
i32 wall_dir; /* = 0 up, 1 = right, 2 = down, 3 = left */
|
|
struct wall_node *next;
|
|
};
|
|
|
|
/* Dicts containing walls that end on edge of tile chunk, keyed by tile end index.
|
|
* Used to merge walls accross tile chunks. */
|
|
Dict *horizontal_ends_dict = InitDict(scratch.arena, 1024);
|
|
Dict *vertical_ends_dict = InitDict(scratch.arena, 1024);
|
|
|
|
struct wall_node *first_wall = 0;
|
|
|
|
/* Generate horizontal wall nodes */
|
|
for (u64 sorted_index = 0; sorted_index < sorted_tile_chunks_count; ++sorted_index) {
|
|
Entity *chunk = x_sorted_tile_chunks[sorted_index];
|
|
Vec2I32 chunk_index = chunk->tile_chunk_index;
|
|
Entity *top_chunk = TileChunkFromChunkIndex(world, VEC2I32(chunk_index.x, chunk_index.y - 1));
|
|
Entity *bottom_chunk = TileChunkFromChunkIndex(world, VEC2I32(chunk_index.x, chunk_index.y + 1));
|
|
/* If there's no chunk below this one, then do an extra iteration (since walls are created at the top of each tile) */
|
|
i32 y_iterations = SIM_TILES_PER_CHUNK_SQRT + !bottom_chunk->valid;
|
|
i32 x_iterations = SIM_TILES_PER_CHUNK_SQRT + 1;
|
|
for (i32 tile_y = 0; tile_y < y_iterations; ++tile_y) {
|
|
i32 wall_start = -1;
|
|
i32 wall_end = -1;
|
|
i32 wall_dir = -1;
|
|
for (i32 tile_x = 0; tile_x < x_iterations; ++tile_x) {
|
|
i32 desired_wall_dir = -1;
|
|
TileKind tile = SIM_TILE_KIND_NONE;
|
|
if (tile_x < SIM_TILES_PER_CHUNK_SQRT && tile_y < SIM_TILES_PER_CHUNK_SQRT) {
|
|
tile = TileKindFromChunk(chunk, VEC2I32(tile_x, tile_y));
|
|
}
|
|
if (tile_x < SIM_TILES_PER_CHUNK_SQRT) {
|
|
TileKind top_tile = SIM_TILE_KIND_NONE;
|
|
if (tile_y == 0) {
|
|
if (top_chunk->valid) {
|
|
Vec2I32 top_tile_local_index = VEC2I32(tile_x, SIM_TILES_PER_CHUNK_SQRT - 1);
|
|
top_tile = TileKindFromChunk(top_chunk, top_tile_local_index);
|
|
}
|
|
} else {
|
|
top_tile = TileKindFromChunk(chunk, VEC2I32(tile_x, tile_y - 1));
|
|
}
|
|
if (tile == SIM_TILE_KIND_WALL) {
|
|
/* Process wall tile */
|
|
if (top_tile != SIM_TILE_KIND_WALL) {
|
|
desired_wall_dir = 0;
|
|
}
|
|
} else {
|
|
/* Process non-wall tile */
|
|
if (top_tile == SIM_TILE_KIND_WALL) {
|
|
desired_wall_dir = 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Stop wall */
|
|
if (wall_dir >= 0 && desired_wall_dir != wall_dir) {
|
|
Vec2I32 start = sim_world_tile_index_from_local_tile_index(chunk_index, VEC2I32(wall_start, tile_y));
|
|
Vec2I32 end = sim_world_tile_index_from_local_tile_index(chunk_index, VEC2I32(wall_end, tile_y));
|
|
struct wall_node *node = 0;
|
|
if (wall_start == 0) {
|
|
u64 start_hash = RandU64FromSeed(*(u64 *)&start);
|
|
start_hash = RandU64FromSeeds(start_hash, wall_dir);
|
|
DictEntry *entry = DictEntryFromHash(horizontal_ends_dict, start_hash);
|
|
if (entry) {
|
|
/* Existing wall exists accross chunk boundary */
|
|
node = (struct wall_node *)entry->value;
|
|
RemoveDictEntry(horizontal_ends_dict, entry);
|
|
}
|
|
}
|
|
if (!node) {
|
|
node = PushStruct(scratch.arena, struct wall_node);
|
|
node->start = start;
|
|
node->next = first_wall;
|
|
node->wall_dir = wall_dir;
|
|
first_wall = node;
|
|
}
|
|
node->end = end;
|
|
if (wall_end == SIM_TILES_PER_CHUNK_SQRT) {
|
|
u64 end_hash = RandU64FromSeed(*(u64 *)&end);
|
|
end_hash = RandU64FromSeeds(end_hash, wall_dir);
|
|
SetDictValue(scratch.arena, horizontal_ends_dict, end_hash, (u64)node);
|
|
}
|
|
wall_start = -1;
|
|
wall_end = -1;
|
|
wall_dir = -1;
|
|
}
|
|
|
|
/* Start / extend wall */
|
|
if (desired_wall_dir >= 0) {
|
|
if (wall_dir != desired_wall_dir) {
|
|
/* Start wall */
|
|
wall_start = tile_x;
|
|
}
|
|
/* Extend wall */
|
|
wall_end = tile_x + 1;
|
|
wall_dir = desired_wall_dir;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Generate vertical wall nodes */
|
|
for (u64 sorted_index = 0; sorted_index < sorted_tile_chunks_count; ++sorted_index) {
|
|
Entity *chunk = y_sorted_tile_chunks[sorted_index];
|
|
Vec2I32 chunk_index = chunk->tile_chunk_index;
|
|
Entity *left_chunk = TileChunkFromChunkIndex(world, VEC2I32(chunk_index.x - 1, chunk_index.y));
|
|
Entity *right_chunk = TileChunkFromChunkIndex(world, VEC2I32(chunk_index.x + 1, chunk_index.y));
|
|
/* If there's no chunk to the right of this one, then do an extra iteration (since walls are created on the left of each tile) */
|
|
i32 y_iterations = SIM_TILES_PER_CHUNK_SQRT + 1;
|
|
i32 x_iterations = SIM_TILES_PER_CHUNK_SQRT + !right_chunk->valid;
|
|
for (i32 tile_x = 0; tile_x < x_iterations; ++tile_x) {
|
|
i32 wall_start = -1;
|
|
i32 wall_end = -1;
|
|
i32 wall_dir = -1;
|
|
for (i32 tile_y = 0; tile_y < y_iterations; ++tile_y) {
|
|
i32 desired_wall_dir = -1;
|
|
TileKind tile = SIM_TILE_KIND_NONE;
|
|
if (tile_x < SIM_TILES_PER_CHUNK_SQRT && tile_y < SIM_TILES_PER_CHUNK_SQRT) {
|
|
tile = TileKindFromChunk(chunk, VEC2I32(tile_x, tile_y));
|
|
}
|
|
|
|
if (tile_y < SIM_TILES_PER_CHUNK_SQRT) {
|
|
TileKind left_tile = SIM_TILE_KIND_NONE;
|
|
if (tile_x == 0) {
|
|
if (left_chunk->valid) {
|
|
Vec2I32 left_tile_local_index = VEC2I32(SIM_TILES_PER_CHUNK_SQRT - 1, tile_y);
|
|
left_tile = TileKindFromChunk(left_chunk, left_tile_local_index);
|
|
}
|
|
} else {
|
|
left_tile = TileKindFromChunk(chunk, VEC2I32(tile_x - 1, tile_y));
|
|
}
|
|
if (tile == SIM_TILE_KIND_WALL) {
|
|
/* Process wall tile */
|
|
if (left_tile != SIM_TILE_KIND_WALL) {
|
|
desired_wall_dir = 3;
|
|
}
|
|
} else {
|
|
/* Process non-wall tile */
|
|
if (left_tile == SIM_TILE_KIND_WALL) {
|
|
desired_wall_dir = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Stop wall */
|
|
if (wall_dir >= 0 && desired_wall_dir != wall_dir) {
|
|
Vec2I32 start = sim_world_tile_index_from_local_tile_index(chunk_index, VEC2I32(tile_x, wall_start));
|
|
Vec2I32 end = sim_world_tile_index_from_local_tile_index(chunk_index, VEC2I32(tile_x, wall_end));
|
|
struct wall_node *node = 0;
|
|
if (wall_start == 0) {
|
|
u64 start_hash = RandU64FromSeed(*(u64 *)&start);
|
|
start_hash = RandU64FromSeeds(start_hash, wall_dir);
|
|
DictEntry *entry = DictEntryFromHash(vertical_ends_dict, start_hash);
|
|
if (entry) {
|
|
/* Existing wall exists accross chunk boundary */
|
|
node = (struct wall_node *)entry->value;
|
|
RemoveDictEntry(vertical_ends_dict, entry);
|
|
}
|
|
}
|
|
if (!node) {
|
|
node = PushStruct(scratch.arena, struct wall_node);
|
|
node->start = start;
|
|
node->next = first_wall;
|
|
node->wall_dir = wall_dir;
|
|
first_wall = node;
|
|
}
|
|
node->end = end;
|
|
if (wall_end == SIM_TILES_PER_CHUNK_SQRT) {
|
|
u64 end_hash = RandU64FromSeed(*(u64 *)&end);
|
|
end_hash = RandU64FromSeeds(end_hash, wall_dir);
|
|
SetDictValue(scratch.arena, vertical_ends_dict, end_hash, (u64)node);
|
|
}
|
|
wall_start = -1;
|
|
wall_end = -1;
|
|
wall_dir = -1;
|
|
}
|
|
|
|
/* Start / extend wall */
|
|
if (desired_wall_dir >= 0) {
|
|
if (wall_dir != desired_wall_dir) {
|
|
/* Start wall */
|
|
wall_start = tile_y;
|
|
}
|
|
/* Extend wall */
|
|
wall_end = tile_y + 1;
|
|
wall_dir = desired_wall_dir;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Create wall entities */
|
|
for (struct wall_node *node = first_wall; node; node = node->next) {
|
|
Entity *wall_ent = AcquireSyncSrc(root);
|
|
EnableProp(wall_ent, Prop_Wall);
|
|
|
|
Vec2 start = sim_pos_from_world_tile_index(node->start);
|
|
Vec2 end = sim_pos_from_world_tile_index(node->end);
|
|
|
|
Xform xf = XformFromPos(start);
|
|
SetXform(wall_ent, xf);
|
|
|
|
EnableProp(wall_ent, Prop_Solid);
|
|
wall_ent->local_collider.count = 2;
|
|
wall_ent->local_collider.points[1] = SubVec2(end, start);
|
|
|
|
Vec2 dirs[4] = { VEC2(0, -1), VEC2(1, 0), VEC2(0, 1), VEC2(-1, 0) };
|
|
Assert(node->wall_dir >= 0 && (u32)node->wall_dir < countof(dirs));
|
|
wall_ent->collision_dir = dirs[node->wall_dir];
|
|
|
|
Activate(wall_ent, world->tick);
|
|
}
|
|
|
|
EndScratch(scratch);
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ On collision
|
|
|
|
CollisionCallbackFuncDef(OnEntityCollision, data, step_ctx)
|
|
{
|
|
Snapshot *world = step_ctx->world;
|
|
Entity *e0 = EntityFromId(world, data->e0);
|
|
Entity *e1 = EntityFromId(world, data->e1);
|
|
Entity *root = EntityFromId(world, RootEntityId);
|
|
b32 skip_solve = 0;
|
|
|
|
if (ShouldSimulate(e0) && ShouldSimulate(e1)) {
|
|
/* Bullet impact */
|
|
if (HasProp(e0, Prop_Bullet)) {
|
|
Vec2 normal = data->normal; /* Impact normal */
|
|
Vec2 vrel = data->vrel; /* Impact velocity */
|
|
|
|
Entity *bullet = e0;
|
|
Entity *target = e1;
|
|
Entity *src = EntityFromId(world, bullet->bullet_src);
|
|
|
|
/* Process collision if bullet already spent or * target share same top level parent */
|
|
if (!bullet->bullet_has_hit && !EqId(src->top, target->top) && HasProp(target, Prop_Solid)) {
|
|
Vec2 point = data->point;
|
|
|
|
/* Update tracer */
|
|
Entity *tracer = EntityFromId(world, bullet->bullet_tracer);
|
|
if (ShouldSimulate(tracer)) {
|
|
Xform xf = XformFromEntity(tracer);
|
|
xf.og = point;
|
|
SetXform(tracer, xf);
|
|
SetLinearVelocity(tracer, VEC2(0, 0));
|
|
}
|
|
|
|
/* Update target */
|
|
Vec2 knockback = MulVec2(NormVec2(vrel), bullet->bullet_knockback);
|
|
ApplyLinearImpulse(target, knockback, point);
|
|
|
|
/* Create test blood */
|
|
/* TODO: Remove this */
|
|
{
|
|
Xform xf = XformFromTrs(TRS(.t = point, .r = RandF64FromState(&step_ctx->rand, 0, Tau)));
|
|
Entity *decal = AcquireSyncSrc(root);
|
|
decal->sprite = S_TagFromPath(Lit("sprite/blood.ase"));
|
|
decal->sprite_tint = Rgba32F(1, 1, 1, 0.25f);
|
|
decal->layer = SIM_LAYER_FLOOR_DECALS;
|
|
SetXform(decal, xf);
|
|
|
|
f32 perp_range = 0.5;
|
|
Vec2 linear_velocity = MulVec2(normal, 0.5);
|
|
linear_velocity = AddVec2(linear_velocity, MulVec2(PerpVec2(normal), RandF64FromState(&step_ctx->rand, -perp_range, perp_range)));
|
|
|
|
f32 angular_velocity_range = 5;
|
|
f32 angular_velocity = RandF64FromState(&step_ctx->rand, -angular_velocity_range, angular_velocity_range);
|
|
|
|
EnableProp(decal, Prop_Kinematic);
|
|
SetLinearVelocity(decal, linear_velocity);
|
|
SetAngularVelocity(decal, angular_velocity);
|
|
|
|
decal->linear_damping = 5.0f;
|
|
decal->angular_damping = 5.0f;
|
|
}
|
|
|
|
/* Create explosion */
|
|
if (bullet->bullet_explosion_strength > 0) {
|
|
SpawnTestExplosion(root, point, bullet->bullet_explosion_strength, bullet->bullet_explosion_radius);
|
|
}
|
|
|
|
/* Update bullet */
|
|
bullet->bullet_has_hit = 1;
|
|
EnableProp(bullet, Prop_Release);
|
|
}
|
|
}
|
|
|
|
/* Explosion blast collision */
|
|
if (HasProp(e0, Prop_Explosion)) {
|
|
Entity *exp = e0;
|
|
Entity *victim = e1;
|
|
|
|
Xform xf = XformFromEntity(exp);
|
|
|
|
CLD_Shape origin_collider = ZI;
|
|
origin_collider.count = 1;
|
|
|
|
Xform victim_xf = XformFromEntity(victim);
|
|
CLD_ClosestPointData closest_points = CLD_ClosestPointDataFromShapes(&origin_collider, &victim->local_collider, xf, victim_xf);
|
|
Vec2 dir = SubVec2(closest_points.p1, closest_points.p0);
|
|
Vec2 point = closest_points.p1;
|
|
f32 distance = Vec2Len(dir);
|
|
#if 0
|
|
if (closest_points.colliding) {
|
|
dir = NegVec2(dir);
|
|
//distance = 0;
|
|
}
|
|
#else
|
|
if (DotVec2(data->normal, dir) < 0) {
|
|
dir = NegVec2(dir);
|
|
point = xf.og;
|
|
distance = 0;
|
|
}
|
|
#endif
|
|
|
|
/* TODO: Blast obstruction */
|
|
f32 radius = exp->explosion_radius;
|
|
f32 strength_center = exp->explosion_strength;
|
|
if (distance < radius) {
|
|
const f32 falloff_curve = 3; /* Cubic falloff */
|
|
f32 strength_factor = PowF32(1 - distance/radius, falloff_curve);
|
|
Vec2 impulse = Vec2WithLen(dir, strength_center * strength_factor);
|
|
ApplyLinearImpulse(victim, impulse, point);
|
|
}
|
|
}
|
|
|
|
/* Chucker zone */
|
|
if (HasProp(e0, Prop_ChuckerZone)) {
|
|
if (!EqId(e0->top, e1->top) && HasProp(e1, Prop_Solid)) {
|
|
e0->chucker_zone_ent = e1->id;
|
|
e0->chucker_zone_ent_tick = world->tick;
|
|
}
|
|
}
|
|
}
|
|
|
|
return skip_solve;
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ Step
|
|
|
|
void StepSim(SimStepCtx *ctx)
|
|
{
|
|
__prof;
|
|
TempArena scratch = BeginScratchNoConflict();
|
|
|
|
b32 is_master = ctx->is_master;
|
|
Snapshot *world = ctx->world;
|
|
|
|
ClientStore *client_store = world->client->store;
|
|
Client *world_client = world->client;
|
|
Client *user_input_client = ctx->user_input_client;
|
|
Client *publish_client = ctx->publish_client;
|
|
Client *master_client = ctx->master_client;
|
|
|
|
i64 sim_dt_ns = ctx->sim_dt_ns;
|
|
|
|
//- Begin frame
|
|
|
|
world->sim_dt_ns = MaxI64(0, sim_dt_ns);
|
|
world->sim_time_ns += world->sim_dt_ns;
|
|
f32 sim_dt = SecondsFromNs(world->sim_dt_ns);
|
|
|
|
S_Scope *sprite_frame_scope = S_BeginScope();
|
|
|
|
Entity *root = EntityFromId(world, RootEntityId);
|
|
root->owner = world->client->player_id;
|
|
|
|
//- Sync ents from cmd producing clients
|
|
|
|
{
|
|
/* FIXME: Ensure only cmds are synced to master player */
|
|
for (u64 client_index = 0; client_index < client_store->num_clients_reserved; ++client_index) {
|
|
Client *client = &client_store->clients[client_index];
|
|
if (client->valid && client != master_client && client != world_client && client != publish_client) {
|
|
Entity *player = EntityFromId(world, client->player_id);
|
|
|
|
/* Create player if necessary */
|
|
if (is_master && !player->valid) {
|
|
/* FIXME: Player never released upon disconnect */
|
|
player = AcquireSyncSrc(root);
|
|
player->player_client_handle = client->handle;
|
|
EnableProp(player, Prop_Player);
|
|
player->predictor = player->id;
|
|
Activate(player, world->tick);
|
|
client->player_id = player->id;
|
|
if (client == user_input_client) {
|
|
user_input_client->player_id = player->id;
|
|
world_client->player_id = player->id;
|
|
world->local_player = player->id;
|
|
player->owner = player->id;
|
|
EnableProp(player, Prop_IsMaster);
|
|
}
|
|
P_LogInfoF("Created player with id %F for sim client %F. is_master: %F", FmtUid(player->id.uid), FmtHandle(client->handle), FmtUint(HasProp(player, Prop_IsMaster)));
|
|
}
|
|
|
|
/* Update rtt */
|
|
if (is_master && player->valid) {
|
|
player->player_last_rtt_ns = client->last_rtt_ns;
|
|
player->player_average_rtt_seconds -= player->player_average_rtt_seconds / 200;
|
|
player->player_average_rtt_seconds += SecondsFromNs(client->last_rtt_ns) / 200;
|
|
}
|
|
|
|
/* Sync ents from client */
|
|
if (player->valid) {
|
|
Snapshot *src_ss = sim_snapshot_from_tick(client, world->tick);
|
|
if (src_ss->valid) {
|
|
sim_snapshot_sync_ents(world, src_ss, player->id, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Mark all incoming ents as sync dsts */
|
|
for (u64 i = 0; i < world->num_ents_reserved; ++i) {
|
|
Entity *ent = &world->ents[i];
|
|
if (ent->valid && HasProp(ent, Prop_SyncSrc) && !EqId(ent->owner, world_client->player_id)) {
|
|
DisableProp(ent, Prop_SyncSrc);
|
|
EnableProp(ent, Prop_SyncDst);
|
|
}
|
|
}
|
|
|
|
/* Mark incoming cmds with correct client */
|
|
for (u64 i = 0; i < world->num_ents_reserved; ++i) {
|
|
Entity *ent = &world->ents[i];
|
|
if (ent->valid && HasProp(ent, Prop_Cmd) && HasProp(ent, Prop_SyncDst)) {
|
|
ent->cmd_player = ent->owner;
|
|
}
|
|
}
|
|
|
|
/* Mark any locally created CMDs as sync sources */
|
|
if (!is_master) {
|
|
for (u64 i = 0; i < world->num_ents_reserved; ++i) {
|
|
Entity *ent = &world->ents[i];
|
|
if (IsValidAndActive(ent) && HasProp(ent, Prop_Cmd)) {
|
|
if (!IsNilId(ent->cmd_player) && EqId(ent->cmd_player, world->local_player)) {
|
|
EnableProp(ent, Prop_SyncSrc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Release entities at beginning of frame
|
|
|
|
ReleaseAllWithProp(world, Prop_Release);
|
|
ResetSimAccel(world, ctx->accel);
|
|
|
|
|
|
//- Activate entities
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ent->valid) continue;
|
|
if (HasProp(ent, Prop_SyncDst) && !IsOwner(ent) && !ShouldPredict(ent)) continue;
|
|
|
|
if (!HasProp(ent, Prop_Active)) {
|
|
u64 atick = ent->activation_tick;
|
|
if (atick != 0 || world->tick >= atick) {
|
|
Activate(ent, world->tick);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Process player cmds
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *cmd_ent = &world->ents[ent_index];
|
|
if (!is_master && !ShouldSimulate(cmd_ent)) continue;
|
|
|
|
if (HasProp(cmd_ent, Prop_Cmd)) {
|
|
Entity *player = EntityFromId(world, cmd_ent->cmd_player);
|
|
if (ShouldSimulate(player)) {
|
|
b32 persist_cmd = 0;
|
|
if (!is_master && !EqId(player->id, world->local_player)) {
|
|
/* We are not the master and the command is not our own, skip processing */
|
|
continue;
|
|
}
|
|
|
|
CmdKind kind = cmd_ent->cmd_kind;
|
|
switch (kind) {
|
|
case SIM_CMD_KIND_CONTROL:
|
|
{
|
|
/* Player's will send control cmds a lot, so keep it around to prevent re-creating it each time */
|
|
persist_cmd = 1;
|
|
|
|
/* Process control cmd for player */
|
|
ControlData old_control = player->player_control;
|
|
ControlData *control = &player->player_control;
|
|
*control = cmd_ent->cmd_control;
|
|
{
|
|
ControlFlag flags = control->flags;
|
|
|
|
player->player_cursor_pos = control->dbg_cursor;
|
|
player->player_hovered_ent = cmd_ent->cmd_control_hovered_ent;
|
|
player->player_dbg_drag_start = 0;
|
|
player->player_dbg_drag_stop = 0;
|
|
|
|
/* Cap movement vector magnitude */
|
|
if (Vec2LenSq(control->move) > 1) {
|
|
control->move = NormVec2(control->move);
|
|
}
|
|
|
|
/* Debug cmds */
|
|
if (ctx->is_master) {
|
|
if (flags & SIM_CONTROL_FLAG_DRAG) {
|
|
if (!(old_control.flags & SIM_CONTROL_FLAG_DRAG)) {
|
|
player->player_dbg_drag_start = 1;
|
|
}
|
|
} else {
|
|
if (old_control.flags & SIM_CONTROL_FLAG_DRAG) {
|
|
player->player_dbg_drag_stop = 1;
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_DELETE) {
|
|
Entity *ent = EntityFromId(world, player->player_hovered_ent);
|
|
if (ent->valid) {
|
|
EnableProp(ent, Prop_Release);
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_CLEAR_ALL) {
|
|
ClearLevelTest(ctx);
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_SPAWN1_TEST) {
|
|
P_LogDebugF("Spawn test 1");
|
|
u32 count = 1;
|
|
f32 spread = 0;
|
|
for (u32 j = 0; j < count; ++j) {
|
|
Vec2 pos = player->player_cursor_pos;
|
|
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
|
|
SpawnTestEntities1(root, pos);
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_SPAWN2_TEST) {
|
|
P_LogDebugF("Spawn test 2");
|
|
u32 count = 1;
|
|
f32 spread = 0;
|
|
for (u32 j = 0; j < count; ++j) {
|
|
Vec2 pos = player->player_cursor_pos;
|
|
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
|
|
SpawnTestEntities2(root, pos);
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_SPAWN3_TEST) {
|
|
P_LogDebugF("Spawn test 3");
|
|
u32 count = 1;
|
|
f32 spread = 0;
|
|
for (u32 j = 0; j < count; ++j) {
|
|
Vec2 pos = player->player_cursor_pos;
|
|
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
|
|
SpawnTestEntities3(root, pos);
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_SPAWN4_TEST) {
|
|
P_LogDebugF("Spawn test 4");
|
|
u32 count = 1;
|
|
f32 spread = 0;
|
|
for (u32 j = 0; j < count; ++j) {
|
|
Vec2 pos = player->player_cursor_pos;
|
|
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
|
|
SpawnTestEntities4(root, pos);
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_WALLS_TEST) {
|
|
GenerateTestWalls(world);
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_EXPLODE_TEST) {
|
|
P_LogDebugF("Explosion test");
|
|
SpawnTestExplosion(root, player->player_cursor_pos, 100, 2);
|
|
}
|
|
}
|
|
|
|
if (flags & SIM_CONTROL_FLAG_TILE_TEST) {
|
|
SpawnTestTile(world, player->player_cursor_pos);
|
|
} else if (old_control.flags & SIM_CONTROL_FLAG_TILE_TEST) {
|
|
GenerateTestWalls(world);
|
|
}
|
|
}
|
|
} break;
|
|
|
|
#if 0
|
|
case SIM_CMD_KIND_CHAT:
|
|
{
|
|
struct sim_data_key msg_key = cmd_ent->cmd_chat_msg;
|
|
String msg = sim_data_from_key(sim_data_store, msg_key);
|
|
if (msg.len > 0) {
|
|
Entity *chat_ent = AcquireSyncSrc(root);
|
|
EnableProp(chat_ent, Prop_CHAT);
|
|
chat_ent->chat_player = player->id;
|
|
chat_ent->chat_msg = msg_key;
|
|
}
|
|
} break;
|
|
#endif
|
|
|
|
default:
|
|
{
|
|
/* Invalid cmd kind */
|
|
Assert(0);
|
|
} break;
|
|
}
|
|
|
|
/* Release cmd */
|
|
if (!persist_cmd) {
|
|
EnableProp(cmd_ent, Prop_Release);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Update entity control from player control
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
|
|
if (HasProp(ent, Prop_Controlled)) {
|
|
Entity *player = EntityFromId(world, ent->controlling_player);
|
|
if (player->valid) {
|
|
ent->control = player->player_control;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Create employees
|
|
|
|
if (is_master) {
|
|
for (u64 i = 0; i < world->num_ents_reserved; ++i) {
|
|
Entity *ent = &world->ents[i];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (HasProp(ent, Prop_Player)) {
|
|
/* FIXME: Ents never released when client disconnects */
|
|
Entity *control_ent = EntityFromId(world, ent->player_control_ent);
|
|
if (!control_ent->valid) {
|
|
control_ent = SpawnTestEmployee(root);
|
|
control_ent->predictor = ent->id;
|
|
EnableProp(control_ent, Prop_Controlled);
|
|
ent->player_control_ent = control_ent->id;
|
|
control_ent->controlling_player = ent->id;
|
|
}
|
|
Entity *camera_ent = EntityFromId(world, ent->player_camera_ent);
|
|
if (!camera_ent->valid) {
|
|
camera_ent = SpawnTestCamera(root, control_ent);
|
|
camera_ent->predictor = ent->id;
|
|
ent->player_camera_ent = camera_ent->id;
|
|
}
|
|
Entity *camera_follow = EntityFromId(world, camera_ent->camera_follow);
|
|
if (!camera_follow->valid) {
|
|
camera_ent->camera_follow = control_ent->id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Update entities from sprite
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (S_IsTagNil(ent->sprite)) continue;
|
|
|
|
S_Sheet *sheet = S_SheetFromTagAwait(sprite_frame_scope, ent->sprite);
|
|
|
|
/* Update animation */
|
|
{
|
|
S_Span span = S_SpanFromName(sheet, ent->sprite_span_name);
|
|
if (ent->animation_last_frame_change_time_ns == 0) {
|
|
ent->animation_last_frame_change_time_ns = SecondsFromNs(world->sim_time_ns);
|
|
}
|
|
|
|
f64 time_in_frame = SecondsFromNs(world->sim_time_ns - ent->animation_last_frame_change_time_ns);
|
|
u64 frame_index = ent->animation_frame;
|
|
if (frame_index < span.start || frame_index > span.end) {
|
|
frame_index = span.start;
|
|
}
|
|
|
|
if (span.end > span.start) {
|
|
S_Frame frame = S_FrameFromIndex(sheet, frame_index);
|
|
while (time_in_frame > frame.duration) {
|
|
time_in_frame -= frame.duration;
|
|
++frame_index;
|
|
if (frame_index > span.end) {
|
|
/* Loop animation */
|
|
frame_index = span.start;
|
|
}
|
|
frame = S_FrameFromIndex(sheet, frame_index);
|
|
ent->animation_last_frame_change_time_ns = world->sim_time_ns;
|
|
}
|
|
}
|
|
|
|
ent->animation_frame = frame_index;
|
|
}
|
|
|
|
#if 0
|
|
/* Update sprite local xform */
|
|
{
|
|
S_Slice slice = S_SliceFromNameIndex(sheet, Lit("pivot"), ent->animation_frame);
|
|
Vec2 sprite_size = DivVec2(sheet->frame_size, (f32)PIXELS_PER_UNIT);
|
|
|
|
Vec2 dir = MulVec2Vec2(sprite_size, slice.dir);
|
|
f32 rot = AngleFromVec2(dir) + Pi / 2;
|
|
|
|
Xform xf = XformIdentity;
|
|
xf = RotateXform(xf, -rot);
|
|
xf = ScaleXform(xf, sprite_size);
|
|
xf = TranslateXform(xf, NegVec2(slice.center));
|
|
ent->sprite_local_xform = xf;
|
|
}
|
|
#endif
|
|
|
|
/* Update collider from sprite */
|
|
if (ent->sprite_collider_slice.len > 0) {
|
|
Xform cxf = ent->sprite_local_xform;
|
|
|
|
S_Slice slice = S_SliceFromNameIndex(sheet, ent->sprite_collider_slice, ent->animation_frame);
|
|
ent->local_collider = CLD_ShapeFromQuad(MulXformQuad(cxf, QuadFromRect(slice.rect)));
|
|
}
|
|
|
|
/* Test collider */
|
|
#if 0
|
|
if (HasProp(ent, Prop_Test)) {
|
|
//if ((1)) {
|
|
#if 0
|
|
ent->local_collider.points[0] = VEC2(0, 0);
|
|
ent->local_collider.count = 1;
|
|
ent->local_collider.radius = 0.5;
|
|
#elif 0
|
|
ent->local_collider.points[0] = Vec2WithLen(VEC2(0.08f, 0.17f), 0.15f);
|
|
ent->local_collider.points[1] = Vec2WithLen(VEC2(-0.07f, -0.2f), 0.15f);
|
|
ent->local_collider.count = 2;
|
|
ent->local_collider.radius = 0.075f;
|
|
#elif 1
|
|
#if 0
|
|
/* "Bad" winding order */
|
|
ent->local_collider.points[0] = VEC2(-0.15, 0.15);
|
|
ent->local_collider.points[1] = VEC2(0.15, 0.15);
|
|
ent->local_collider.points[2] = VEC2(0, -0.15);
|
|
#else
|
|
ent->local_collider.points[0] = VEC2(0, -0.15);
|
|
ent->local_collider.points[1] = VEC2(0.15, 0.15);
|
|
ent->local_collider.points[2] = VEC2(-0.15, 0.15);
|
|
#endif
|
|
ent->local_collider.count = 3;
|
|
ent->local_collider.radius = 0.25;
|
|
//ent->local_collider.radius = AbsF32(SinF32(ctx->tick.time) / 3);
|
|
#else
|
|
//ent->local_collider.radius = 0.5;
|
|
ent->local_collider.radius = 0.25;
|
|
//ent->local_collider.radius = 0.;
|
|
#endif
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|
|
//- Update attachments
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Attached)) continue;
|
|
|
|
Entity *parent = EntityFromId(world, ent->parent);
|
|
S_Tag parent_sprite = parent->sprite;
|
|
S_Sheet *parent_sheet = S_SheetFromTagAwait(sprite_frame_scope, parent_sprite);
|
|
|
|
Xform parent_sprite_xf = parent->sprite_local_xform;
|
|
|
|
S_Slice attach_slice = S_SliceFromNameIndex(parent_sheet, ent->attach_slice, parent->animation_frame);
|
|
Vec2 attach_pos = MulXformV2(parent_sprite_xf, attach_slice.center);
|
|
Vec2 attach_dir = MulXformBasisV2(parent_sprite_xf, attach_slice.dir);
|
|
|
|
Xform xf = LocalXformFromEntity(ent);
|
|
xf.og = attach_pos;
|
|
xf = XformWIthWorldRotation(xf, AngleFromVec2(attach_dir) + Pi / 2);
|
|
SetLocalXform(ent, xf);
|
|
}
|
|
|
|
|
|
//- Process ent control
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
|
|
if (HasProp(ent, Prop_Controlled)) {
|
|
ControlData *control = &ent->control;
|
|
ControlFlag flags = control->flags;
|
|
if (flags & SIM_CONTROL_FLAG_FIRE) {
|
|
Entity *equipped = EntityFromId(world, ent->equipped);
|
|
if (equipped->valid) {
|
|
++equipped->num_primary_triggers;
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_FIRE_ALT) {
|
|
Entity *equipped = EntityFromId(world, ent->equipped);
|
|
if (equipped->valid) {
|
|
++equipped->num_secondary_triggers;
|
|
}
|
|
}
|
|
if (flags & SIM_CONTROL_FLAG_TELEPORT_TEST) {
|
|
TeleportTest(ent, control->dbg_cursor);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Process triggered entities
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
|
|
b32 primary_triggered = ent->num_primary_triggers > 0;
|
|
b32 secondary_triggered = ent->num_secondary_triggers > 0;
|
|
ent->num_primary_triggers = 0;
|
|
ent->num_secondary_triggers = 0;
|
|
|
|
if (primary_triggered) {
|
|
i64 world_time_ns = world->sim_time_ns;
|
|
if ((world_time_ns - ent->last_primary_fire_ns >= NsFromSeconds(ent->primary_fire_delay)) || ent->last_primary_fire_ns == 0) {
|
|
ent->last_primary_fire_ns = world_time_ns;
|
|
} else {
|
|
primary_triggered = 0;
|
|
}
|
|
}
|
|
if (secondary_triggered) {
|
|
i64 world_time_ns = world->sim_time_ns;
|
|
if ((world_time_ns - ent->last_secondary_fire_ns >= NsFromSeconds(ent->secondary_fire_delay)) || ent->last_secondary_fire_ns == 0) {
|
|
ent->last_secondary_fire_ns = world_time_ns;
|
|
} else {
|
|
secondary_triggered = 0;
|
|
}
|
|
}
|
|
|
|
/* Fire smg */
|
|
if (HasProp(ent, Prop_Smg)) {
|
|
if (primary_triggered) {
|
|
S_Tag sprite = ent->sprite;
|
|
u32 animation_frame = ent->animation_frame;
|
|
S_Sheet *sheet = S_SheetFromTagAwait(sprite_frame_scope, sprite);
|
|
Xform sprite_local_xform = ent->sprite_local_xform;
|
|
S_Slice out_slice = S_SliceFromNameIndex(sheet, Lit("out"), animation_frame);
|
|
Vec2 rel_pos = MulXformV2(sprite_local_xform, out_slice.center);
|
|
Vec2 rel_dir = MulXformBasisV2(sprite_local_xform, out_slice.dir);
|
|
|
|
/* Spawn bullet */
|
|
Entity *bullet;
|
|
{
|
|
bullet = AcquireSyncSrc(root);
|
|
|
|
EnableProp(bullet, Prop_Bullet);
|
|
bullet->bullet_src = ent->id;
|
|
bullet->bullet_src_pos = rel_pos;
|
|
bullet->bullet_src_dir = rel_dir;
|
|
//bullet->bullet_launch_velocity = 0.75f;
|
|
bullet->bullet_launch_velocity = 50.0f;
|
|
bullet->bullet_knockback = 10;
|
|
bullet->layer = SIM_LAYER_BULLETS;
|
|
|
|
#if 1
|
|
/* Point collider */
|
|
bullet->local_collider.points[0] = VEC2(0, 0);
|
|
bullet->local_collider.count = 1;
|
|
#else
|
|
bullet->sprite = S_TagFromPath(Lit("sprite/bullet.ase"));
|
|
bullet->sprite_collider_slice = Lit("shape");
|
|
#endif
|
|
}
|
|
|
|
/* Spawn tracer */
|
|
{
|
|
Entity *tracer = AcquireSyncSrc(root);
|
|
tracer->tracer_fade_duration = 0.025f;
|
|
tracer->layer = SIM_LAYER_TRACERS;
|
|
EnableProp(tracer, Prop_Tracer);
|
|
|
|
bullet->bullet_tracer = tracer->id;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Fire launcher */
|
|
if (HasProp(ent, Prop_Launcher)) {
|
|
if (primary_triggered) {
|
|
S_Tag sprite = ent->sprite;
|
|
u32 animation_frame = ent->animation_frame;
|
|
S_Sheet *sheet = S_SheetFromTagAwait(sprite_frame_scope, sprite);
|
|
Xform sprite_local_xform = ent->sprite_local_xform;
|
|
S_Slice out_slice = S_SliceFromNameIndex(sheet, Lit("out"), animation_frame);
|
|
Vec2 rel_pos = MulXformV2(sprite_local_xform, out_slice.center);
|
|
Vec2 rel_dir = MulXformBasisV2(sprite_local_xform, out_slice.dir);
|
|
|
|
/* Spawn bullet */
|
|
Entity *bullet;
|
|
{
|
|
bullet = AcquireSyncSrc(root);
|
|
|
|
EnableProp(bullet, Prop_Bullet);
|
|
bullet->bullet_src = ent->id;
|
|
bullet->bullet_src_pos = rel_pos;
|
|
bullet->bullet_src_dir = rel_dir;
|
|
//bullet->bullet_launch_velocity = 0.75f;
|
|
bullet->bullet_launch_velocity = 15;
|
|
bullet->bullet_knockback = 50;
|
|
bullet->bullet_explosion_strength = 100;
|
|
bullet->bullet_explosion_radius = 4;
|
|
bullet->layer = SIM_LAYER_BULLETS;
|
|
|
|
/* Point collider */
|
|
bullet->local_collider.points[0] = VEC2(0, 0);
|
|
bullet->local_collider.count = 1;
|
|
bullet->local_collider.radius = 0.05f;
|
|
|
|
}
|
|
|
|
/* Spawn tracer */
|
|
{
|
|
Entity *tracer = AcquireSyncSrc(root);
|
|
tracer->tracer_fade_duration = 0.025f;
|
|
tracer->layer = SIM_LAYER_TRACERS;
|
|
EnableProp(tracer, Prop_Tracer);
|
|
|
|
bullet->bullet_tracer = tracer->id;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Fire chucker */
|
|
if (HasProp(ent, Prop_Chucker)) {
|
|
if (primary_triggered) {
|
|
}
|
|
if (secondary_triggered) {
|
|
Entity *zone = EntityFromId(world, ent->chucker_zone);
|
|
Entity *target = EntityFromId(world, zone->chucker_zone_ent);
|
|
Entity *old_joint_ent = EntityFromId(world, ent->chucker_joint);
|
|
if (IsValidAndActive(target) && zone->chucker_zone_ent_tick == world->tick - 1) {
|
|
if (!EqId(old_joint_ent->weld_joint_data.e1, target->id)) {
|
|
Entity *joint_ent = AcquireSyncSrc(root);
|
|
EnableProp(joint_ent, Prop_Active);
|
|
|
|
Xform xf0 = XformFromEntity(ent);
|
|
Xform xf1 = XformFromEntity(target);
|
|
Xform xf0_to_xf1 = MulXform(InvertXform(xf0), xf1);
|
|
|
|
EnableProp(joint_ent, Prop_WeldJoint);
|
|
WeldJointDesc def = CreateWeldJointDef();
|
|
def.e0 = ent->id;
|
|
def.e1 = target->id;
|
|
def.xf = xf0_to_xf1;
|
|
def.linear_spring_hz = 10;
|
|
def.linear_spring_damp = 0.3f;
|
|
def.angular_spring_hz = 10;
|
|
def.angular_spring_damp = 0.3f;
|
|
joint_ent->weld_joint_data = WeldJointFromDef(def);
|
|
ent->chucker_joint = joint_ent->id;
|
|
}
|
|
}
|
|
if (old_joint_ent->valid) {
|
|
EnableProp(old_joint_ent, Prop_Release);
|
|
DisableProp(old_joint_ent, Prop_Active);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Create & update motor joints from control move
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
|
|
if (HasProp(ent, Prop_Controlled)) {
|
|
Entity *joint_ent = EntityFromId(world, ent->move_joint);
|
|
if (is_master && !IsValidAndActive(joint_ent)) {
|
|
joint_ent = AcquireSyncSrc(root);
|
|
joint_ent->predictor = ent->predictor;
|
|
joint_ent->mass_unscaled = F32Infinity;
|
|
joint_ent->inertia_unscaled = F32Infinity;
|
|
EnableProp(joint_ent, Prop_Active);
|
|
EnableProp(joint_ent, Prop_Kinematic);
|
|
ent->move_joint = joint_ent->id;
|
|
|
|
EnableProp(joint_ent, Prop_MotorJoint);
|
|
MotorJointDesc def = CreateMotorJointDef();
|
|
def.e0 = joint_ent->id; /* Re-using joint entity as e0 */
|
|
def.e1 = ent->id;
|
|
def.correction_rate = 0;
|
|
def.max_force = ent->control_force;
|
|
def.max_torque = 0;
|
|
joint_ent->motor_joint_data = MotorJointFromDef(def);
|
|
}
|
|
|
|
if (ShouldSimulate(joint_ent)) {
|
|
SetXform(joint_ent, XformIdentity); /* Reset joint ent position */
|
|
SetLinearVelocity(joint_ent, MulVec2(ClampVec2Len(ent->control.move, 1), ent->control_force_max_speed));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Create & update motor joints from control focus (aim)
|
|
|
|
#if SIM_PLAYER_AIM
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
|
|
if (HasProp(ent, Prop_Controlled)) {
|
|
Xform xf = XformFromEntity(ent);
|
|
Xform sprite_xf = MulXform(xf, ent->sprite_local_xform);
|
|
|
|
/* Retrieve / create aim joint */
|
|
Entity *joint_ent = EntityFromId(world, ent->aim_joint);
|
|
if (is_master && !IsValidAndActive(joint_ent)) {
|
|
joint_ent = AcquireSyncSrc(root);
|
|
joint_ent->predictor = ent->predictor;
|
|
joint_ent->mass_unscaled = F32Infinity;
|
|
joint_ent->inertia_unscaled = F32Infinity;
|
|
EnableProp(joint_ent, Prop_Kinematic); /* Since we'll be setting velocity manually */
|
|
EnableProp(joint_ent, Prop_MotorJoint);
|
|
EnableProp(joint_ent, Prop_Active);
|
|
ent->aim_joint = joint_ent->id;
|
|
|
|
MotorJointDesc def = CreateMotorJointDef();
|
|
def.e0 = joint_ent->id; /* Re-using joint entity as e0 */
|
|
def.e1 = ent->id;
|
|
def.max_force = 0;
|
|
def.max_torque = ent->control_torque;
|
|
joint_ent->motor_joint_data = MotorJointFromDef(def);
|
|
}
|
|
|
|
if (ShouldSimulate(joint_ent)) {
|
|
/* Set correction rate dynamically since motor velocity is only set for one frame */
|
|
joint_ent->motor_joint_data.correction_rate = 10 * sim_dt;
|
|
|
|
|
|
/* Solve for final angle using law of sines */
|
|
f32 new_angle;
|
|
{
|
|
Vec2 ent_pos = xf.og;
|
|
Vec2 focus_pos = AddVec2(ent_pos, ent->control.focus);
|
|
|
|
Vec2 sprite_hold_pos;
|
|
Vec2 sprite_hold_dir;
|
|
{
|
|
S_Sheet *sheet = S_SheetFromTagAwait(sprite_frame_scope, ent->sprite);
|
|
S_Slice slice = S_SliceFromNameIndex(sheet, Lit("attach.wep"), ent->animation_frame);
|
|
sprite_hold_pos = slice.center;
|
|
sprite_hold_dir = slice.dir;
|
|
}
|
|
|
|
Vec2 hold_dir = MulXformBasisV2(sprite_xf, sprite_hold_dir);
|
|
Vec2 hold_pos = MulXformV2(sprite_xf, sprite_hold_pos);
|
|
if (EqVec2(hold_pos, ent_pos)) {
|
|
/* If hold pos is same as origin (E.G if pivot is being used as hold pos), then move hold pos forward a tad to avoid issue */
|
|
sprite_hold_pos = AddVec2(sprite_hold_pos, VEC2(0, -1));
|
|
hold_pos = MulXformV2(sprite_xf, sprite_hold_pos);
|
|
}
|
|
|
|
f32 forward_hold_angle_offset;
|
|
{
|
|
Xform xf_unrotated = XformWIthWorldRotation(xf, 0);
|
|
Vec2 hold_pos_unrotated = MulXformV2(xf_unrotated, MulXformV2(ent->sprite_local_xform, sprite_hold_pos));
|
|
forward_hold_angle_offset = AngleFromVec2Dirs(VEC2(0, -1), SubVec2(hold_pos_unrotated, xf_unrotated.og));
|
|
}
|
|
|
|
Vec2 hold_ent_dir = SubVec2(ent_pos, hold_pos);
|
|
Vec2 focus_ent_dir = SubVec2(ent_pos, focus_pos);
|
|
|
|
f32 hold_ent_len = Vec2Len(hold_ent_dir);
|
|
f32 focus_ent_len = Vec2Len(focus_ent_dir);
|
|
|
|
f32 final_hold_angle_btw_ent_and_focus = AngleFromVec2Dirs(hold_ent_dir, hold_dir);
|
|
f32 final_focus_angle_btw_ent_and_hold = ArcSinF32((SinF32(final_hold_angle_btw_ent_and_focus) * hold_ent_len) / focus_ent_len);
|
|
f32 final_ent_angle_btw_focus_and_hold = Pi - (final_focus_angle_btw_ent_and_hold + final_hold_angle_btw_ent_and_focus);
|
|
|
|
new_angle = UnwindAngleF32(AngleFromVec2Dirs(VEC2(0, -1), SubVec2(focus_pos, ent_pos)) + final_ent_angle_btw_focus_and_hold - forward_hold_angle_offset);
|
|
}
|
|
|
|
f32 new_vel = 0;
|
|
if (!IsF32Nan(new_angle)) {
|
|
const f32 angle_error_allowed = 0.001f;
|
|
Xform joint_xf = XformFromEntity(joint_ent);
|
|
f32 diff = UnwindAngleF32(new_angle - RotationFromXform(joint_xf));
|
|
if (AbsF32(diff) > angle_error_allowed) {
|
|
/* Instantly snap joint ent to new angle */
|
|
new_vel = diff / sim_dt;
|
|
}
|
|
}
|
|
SetAngularVelocity(joint_ent, new_vel);
|
|
}
|
|
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
//- Create motor joints from ground friction (gravity)
|
|
|
|
#if 1
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Dynamic)) continue;
|
|
|
|
Entity *joint_ent = EntityFromId(world, ent->ground_friction_joint);
|
|
|
|
MotorJointDesc def = CreateMotorJointDef();
|
|
def.e0 = root->id;
|
|
def.e1 = ent->id;
|
|
def.correction_rate = 0;
|
|
def.max_force = ent->linear_ground_friction;
|
|
def.max_torque = ent->angular_ground_friction;
|
|
if (joint_ent->motor_joint_data.max_force != def.max_force || joint_ent->motor_joint_data.max_torque != def.max_torque) {
|
|
if (is_master && !IsValidAndActive(joint_ent)) {
|
|
joint_ent = AcquireSyncSrc(root);
|
|
joint_ent->predictor = ent->predictor;
|
|
EnableProp(joint_ent, Prop_MotorJoint);
|
|
EnableProp(joint_ent, Prop_Active);
|
|
joint_ent->motor_joint_data = MotorJointFromDef(def);
|
|
ent->ground_friction_joint = joint_ent->id;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
//- Create mouse joints from client debug drag
|
|
|
|
if (is_master) {
|
|
for (u64 i = 0; i < world->num_ents_reserved; ++i) {
|
|
Entity *player = &world->ents[i];
|
|
if (!ShouldSimulate(player)) continue;
|
|
if (!HasProp(player, Prop_Player)) continue;
|
|
|
|
Vec2 cursor = player->player_cursor_pos;
|
|
b32 start_dragging = player->player_dbg_drag_start;
|
|
b32 stop_dragging = player->player_dbg_drag_stop;
|
|
|
|
Entity *joint_ent = EntityFromId(world, player->player_dbg_drag_joint_ent);
|
|
Entity *target_ent = EntityFromId(world, joint_ent->mouse_joint_data.target);
|
|
|
|
if (stop_dragging) {
|
|
target_ent = NilEntity();
|
|
} else if (start_dragging) {
|
|
target_ent = EntityFromId(world, player->player_hovered_ent);
|
|
}
|
|
|
|
if (ShouldSimulate(target_ent)) {
|
|
if (!IsValidAndActive(joint_ent)) {
|
|
/* FIXME: Joint ent may never release */
|
|
joint_ent = AcquireLocal(root);
|
|
joint_ent->mass_unscaled = F32Infinity;
|
|
joint_ent->inertia_unscaled = F32Infinity;
|
|
player->player_dbg_drag_joint_ent = joint_ent->id;
|
|
EnableProp(joint_ent, Prop_MouseJoint);
|
|
EnableProp(joint_ent, Prop_Active);
|
|
}
|
|
Xform xf = XformFromEntity(target_ent);
|
|
|
|
MouseJointDesc def = CreateMouseJointDef();
|
|
def.target = target_ent->id;
|
|
if (EqId(joint_ent->mouse_joint_data.target, target_ent->id)) {
|
|
def.point_local_start = joint_ent->mouse_joint_data.point_local_start;
|
|
} else {
|
|
def.point_local_start = InvertXformMulV2(xf, cursor);
|
|
}
|
|
def.point_end = cursor;
|
|
def.max_force = F32Infinity;
|
|
def.linear_spring_hz = 5;
|
|
def.linear_spring_damp = 0.7f;
|
|
def.angular_spring_hz = 1;
|
|
def.angular_spring_damp = 0.1f;
|
|
joint_ent->mouse_joint_data = MouseJointFromDef(def);
|
|
} else if (IsValidAndActive(joint_ent)) {
|
|
joint_ent->mouse_joint_data.target = target_ent->id;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Physics step
|
|
|
|
{
|
|
PhysStepCtx phys = ZI;
|
|
phys.sim_step_ctx = ctx;
|
|
phys.collision_callback = OnEntityCollision;
|
|
StepPhys(&phys, sim_dt);
|
|
}
|
|
|
|
|
|
//- Update explosions
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Explosion)) continue;
|
|
|
|
/* Explosion doesn't need to generate any more collisions after initial physics step */
|
|
DisableProp(ent, Prop_Sensor);
|
|
}
|
|
|
|
|
|
//- Update tracers
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Tracer)) continue;
|
|
|
|
Vec2 end = XformFromEntity(ent).og;
|
|
|
|
Vec2 tick_velocity = MulVec2(ent->tracer_start_velocity, sim_dt);
|
|
Vec2 gradient_start = AddVec2(ent->tracer_gradient_start, tick_velocity);
|
|
Vec2 gradient_end = AddVec2(ent->tracer_gradient_end, tick_velocity);
|
|
|
|
if (DotVec2(tick_velocity, SubVec2(gradient_start, end)) > 0) {
|
|
/* Tracer has disappeared */
|
|
EnableProp(ent, Prop_Release);
|
|
}
|
|
|
|
ent->tracer_gradient_start = gradient_start;
|
|
ent->tracer_gradient_end = gradient_end;
|
|
}
|
|
|
|
|
|
//- Initialize bullet kinematics from sources
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Bullet)) continue;
|
|
|
|
if (ent->activation_tick == world->tick) {
|
|
Entity *src = EntityFromId(world, ent->bullet_src);
|
|
Xform src_xf = XformFromEntity(src);
|
|
|
|
/* Activate collision */
|
|
EnableProp(ent, Prop_Sensor);
|
|
EnableProp(ent, Prop_Toi);
|
|
|
|
Vec2 pos = MulXformV2(src_xf, ent->bullet_src_pos);
|
|
Vec2 vel = MulXformBasisV2(src_xf, ent->bullet_src_dir);
|
|
vel = Vec2WithLen(vel, ent->bullet_launch_velocity);
|
|
|
|
#if 0
|
|
/* Add shooter velocity to bullet */
|
|
{
|
|
/* TODO: Add angular velocity as well? */
|
|
Entity *top = EntityFromId(ss_blended, src->top);
|
|
impulse = AddVec2(impulse, MulVec2(top->linear_velocity, dt));
|
|
}
|
|
#endif
|
|
|
|
Xform xf = XformFromTrs(TRS(.t = pos, .r = AngleFromVec2(vel) + Pi / 2));
|
|
SetXform(ent, xf);
|
|
|
|
EnableProp(ent, Prop_Kinematic);
|
|
SetLinearVelocity(ent, vel);
|
|
|
|
/* Initialize tracer */
|
|
Entity *tracer = EntityFromId(world, ent->bullet_tracer);
|
|
if (ShouldSimulate(tracer)) {
|
|
SetXform(tracer, xf);
|
|
EnableProp(tracer, Prop_Kinematic);
|
|
SetLinearVelocity(tracer, ent->linear_velocity);
|
|
tracer->tracer_start = pos;
|
|
tracer->tracer_start_velocity = ent->linear_velocity;
|
|
tracer->tracer_gradient_end = pos;
|
|
tracer->tracer_gradient_start = SubVec2(pos, MulVec2(ent->linear_velocity, tracer->tracer_fade_duration));
|
|
}
|
|
|
|
/* Spawn quake */
|
|
{
|
|
Entity *quake = AcquireSyncSrc(root);
|
|
SetXform(quake, XformFromPos(pos));
|
|
quake->quake_intensity = 0.2f;
|
|
quake->quake_fade = quake->quake_intensity / 0.1f;
|
|
EnableProp(quake, Prop_Quake);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//- Update cameras
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Camera)) continue;
|
|
|
|
Xform xf = XformFromEntity(ent);
|
|
|
|
/* Camera follow */
|
|
{
|
|
Entity *follow = EntityFromId(world, ent->camera_follow);
|
|
|
|
f32 aspect_ratio = 1.0;
|
|
{
|
|
Xform quad_xf = MulXform(XformFromEntity(ent), ent->camera_quad_xform);
|
|
Vec2 camera_size = ScaleFromXform(quad_xf);
|
|
if (!IsVec2Zero(camera_size)) {
|
|
aspect_ratio = camera_size.x / camera_size.y;
|
|
}
|
|
}
|
|
f32 ratio_y = 0.33f;
|
|
f32 ratio_x = ratio_y / aspect_ratio;
|
|
Vec2 camera_focus_dir = MulVec2Vec2(follow->control.focus, VEC2(ratio_x, ratio_y));
|
|
Vec2 camera_focus_pos = AddVec2(XformFromEntity(follow).og, camera_focus_dir);
|
|
ent->camera_xform_target = xf;
|
|
ent->camera_xform_target.og = camera_focus_pos;
|
|
|
|
/* Lerp camera */
|
|
if (ent->camera_applied_lerp_continuity_gen_plus_one == ent->camera_lerp_continuity_gen + 1) {
|
|
f32 t = 1 - PowF32(2.f, -20.f * (f32)sim_dt);
|
|
xf = LerpXform(xf, ent->camera_xform_target, t);
|
|
} else {
|
|
/* Skip lerp */
|
|
xf = ent->camera_xform_target;
|
|
}
|
|
ent->camera_applied_lerp_continuity_gen_plus_one = ent->camera_lerp_continuity_gen + 1;
|
|
}
|
|
|
|
/* Camera shake */
|
|
{
|
|
/* TODO: Update based on distance to quake */
|
|
ent->shake = 0;
|
|
for (u64 quake_ent_index = 0; quake_ent_index < world->num_ents_reserved; ++quake_ent_index) {
|
|
Entity *quake = &world->ents[quake_ent_index];
|
|
if (!ShouldSimulate(quake)) continue;
|
|
if (!HasProp(quake, Prop_Quake)) continue;
|
|
ent->shake += quake->quake_intensity;
|
|
}
|
|
}
|
|
|
|
SetXform(ent, xf);
|
|
}
|
|
|
|
|
|
//- Update quakes
|
|
|
|
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &world->ents[ent_index];
|
|
if (!ShouldSimulate(ent)) continue;
|
|
if (!HasProp(ent, Prop_Quake)) continue;
|
|
|
|
ent->quake_intensity = MaxF32(0, ent->quake_intensity - (ent->quake_fade * sim_dt));
|
|
if (ent->quake_intensity <= 0) {
|
|
EnableProp(ent, Prop_Release);
|
|
}
|
|
}
|
|
|
|
|
|
//- Update relative layers
|
|
|
|
{
|
|
TempArena temp = BeginTempArena(scratch.arena);
|
|
|
|
Entity **stack = PushStructNoZero(temp.arena, Entity *);
|
|
u64 stack_count = 1;
|
|
*stack = root;
|
|
|
|
while (stack_count > 0) {
|
|
Entity *parent;
|
|
PopStruct(temp.arena, Entity *, &parent);
|
|
--stack_count;
|
|
|
|
i32 parent_layer = parent->final_layer;
|
|
for (Entity *child = EntityFromId(world, parent->first); child->valid; child = EntityFromId(world, child->next)) {
|
|
if (ShouldSimulate(child)) {
|
|
child->final_layer = parent_layer + child->layer;
|
|
*PushStructNoZero(temp.arena, Entity *) = child;
|
|
++stack_count;
|
|
}
|
|
}
|
|
}
|
|
|
|
EndTempArena(temp);
|
|
}
|
|
|
|
|
|
//- Release entities at end of frame
|
|
|
|
ReleaseAllWithProp(world, Prop_Release);
|
|
|
|
|
|
//- Sync to publish client
|
|
|
|
if (publish_client->valid && world->tick > publish_client->last_tick) {
|
|
Snapshot *prev_pub_world = sim_snapshot_from_tick(publish_client, publish_client->last_tick);
|
|
Snapshot *pub_world = sim_snapshot_acquire(publish_client, prev_pub_world, world->tick);
|
|
|
|
/* Sync */
|
|
sim_snapshot_sync_ents(pub_world, world, world_client->player_id, 0);
|
|
|
|
/* Mark all synced ents as both sync dsts & sync srcs */
|
|
for (u64 ent_index = 2; ent_index < pub_world->num_ents_reserved; ++ent_index) {
|
|
Entity *ent = &pub_world->ents[ent_index];
|
|
if (ent->valid) {
|
|
EnableProp(ent, Prop_SyncDst);
|
|
EnableProp(ent, Prop_SyncSrc);
|
|
}
|
|
}
|
|
|
|
pub_world->sim_dt_ns = world->sim_dt_ns;
|
|
pub_world->sim_time_ns = world->sim_time_ns;
|
|
pub_world->continuity_gen = world->continuity_gen;
|
|
pub_world->phys_iteration = world->phys_iteration;
|
|
pub_world->local_player = world->local_player;
|
|
}
|
|
|
|
|
|
//- End frame
|
|
|
|
S_EndScope(sprite_frame_scope);
|
|
|
|
EndScratch(scratch);
|
|
}
|