feely_pona/feely_pona_game.cpp

969 lines
40 KiB
C++
Raw Normal View History

#if defined(_CLANGD)
#pragma once
#include "feely_pona_unity.h"
#endif
2023-12-01 05:46:58 +00:00
static bool FP_Game_KeyBindIsPressed(TELY_Input const *input, FP_GameControls const *controls, FP_GameKeyBind key_bind)
2023-10-23 09:54:28 +00:00
{
bool result = false;
if (controls->mode == FP_GameControlMode_Keyboard) {
2023-12-01 05:46:58 +00:00
result = TELY_Input_ScanKeyIsPressed(input, key_bind.scan_key);
2023-10-23 09:54:28 +00:00
} else {
2023-12-01 05:46:58 +00:00
result = TELY_Input_GamepadKeyIsPressed(input, controls->gamepad_index, key_bind.gamepad_key);
2023-10-23 09:54:28 +00:00
}
return result;
}
2023-12-01 05:46:58 +00:00
static bool FP_Game_KeyBindIsDown(TELY_Input const *input, FP_GameControls const *controls, FP_GameKeyBind key_bind)
2023-10-23 09:54:28 +00:00
{
bool result = false;
if (controls->mode == FP_GameControlMode_Keyboard) {
2023-12-01 05:46:58 +00:00
result = TELY_Input_ScanKeyIsDown(input, key_bind.scan_key);
2023-10-23 09:54:28 +00:00
} else {
2023-12-01 05:46:58 +00:00
result = TELY_Input_GamepadKeyIsDown(input, controls->gamepad_index, key_bind.gamepad_key);
2023-10-23 09:54:28 +00:00
}
return result;
}
2023-09-17 10:24:07 +00:00
static bool operator==(FP_GameEntityHandle const &lhs, FP_GameEntityHandle const &rhs)
{
bool result = lhs.id == rhs.id;
return result;
}
2023-09-17 10:24:07 +00:00
static bool operator!=(FP_GameEntityHandle const &lhs, FP_GameEntityHandle const &rhs)
{
bool result = !(lhs == rhs);
return result;
}
static FP_GameCameraM2x3 FP_Game_CameraModelViewM2x3(FP_GameCamera camera)
{
FP_GameCameraM2x3 result = {};
result.model_view = Dqn_M2x3_Identity();
result.view_model = Dqn_M2x3_Identity();
Dqn_V2 center_offset = camera.size * .5f;
Dqn_V2 rotate_origin = -camera.world_pos;
result.model_view = Dqn_M2x3_Mul(result.model_view, Dqn_M2x3_Translate(rotate_origin));
result.model_view = Dqn_M2x3_Mul(result.model_view, Dqn_M2x3_Rotate(camera.rotate_rads));
result.model_view = Dqn_M2x3_Mul(result.model_view, Dqn_M2x3_Scale(camera.scale));
result.model_view = Dqn_M2x3_Mul(result.model_view, Dqn_M2x3_Translate(center_offset));
Dqn_V2 inverse_scale = Dqn_V2_InitNx1(1) / camera.scale;
result.view_model = Dqn_M2x3_Mul(result.view_model, Dqn_M2x3_Translate(-center_offset));
result.view_model = Dqn_M2x3_Mul(result.view_model, Dqn_M2x3_Scale(inverse_scale));
result.view_model = Dqn_M2x3_Mul(result.view_model, Dqn_M2x3_Rotate(-camera.rotate_rads));
result.view_model = Dqn_M2x3_Mul(result.view_model, Dqn_M2x3_Translate(-rotate_origin));
#if 0
Dqn_M2x3 identity = Dqn_M2x3_Mul(result.model_view, result.view_model);
DQN_ASSERT(identity == Dqn_M2x3_Identity());
#endif
return result;
}
2023-09-17 10:24:07 +00:00
static FP_GameEntity *FP_Game_GetEntity(FP_Game *game, FP_GameEntityHandle handle)
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *result = nullptr;
if (!game)
return result;
2023-10-08 05:14:31 +00:00
result = game->play.entities.data;
2023-09-17 10:24:07 +00:00
uint64_t index_from_handle = handle.id & FP_GAME_ENTITY_HANDLE_INDEX_MASK;
2023-10-08 05:14:31 +00:00
if (index_from_handle >= game->play.entities.size)
return result;
2023-10-08 05:14:31 +00:00
FP_GameEntity *candidate = game->play.entities.data + index_from_handle;
if (candidate->handle == handle)
result = candidate;
return result;
}
2023-09-17 10:24:07 +00:00
static bool FP_Game_DFSPreOrderWalkEntityTree(FP_Game *game, FP_GameEntityIterator *it, FP_GameEntity *root)
{
if (!game || !it || !root)
return false;
it->last_visited = it->entity;
if (it->init) {
it->iteration_count++;
} else {
it->init = true;
it->entity = root;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
2023-10-25 09:17:30 +00:00
if (!it->entity)
return false;
if (it->entity_first_child) {
it->entity = it->entity_first_child;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
} else {
while (it->entity->handle != root->handle) {
if (it->entity_next) {
it->entity = it->entity_next;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
break;
} else {
if (!it->entity_parent)
break;
it->entity = it->entity_parent;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
}
}
return it->entity->handle != root->handle;
}
2023-09-17 10:24:07 +00:00
static bool FP_Game_DFSPostOrderWalkEntityTree(FP_Game *game, FP_GameEntityIterator *it, FP_GameEntity *root)
{
if (!game || !it || !root)
return false;
bool ascending_tree = it->entity ? (it->last_visited == it->entity->last_child) : false;
it->last_visited = it->entity;
if (it->init) {
it->iteration_count++;
} else {
it->init = true;
it->entity = root;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
2023-10-25 09:17:30 +00:00
if (!it->entity)
return false;
// NOTE: Descend to deepest leaf node
if (it->entity_first_child && !ascending_tree) {
while (it->entity_first_child) {
it->entity = it->entity_first_child;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
} else {
// NOTE: We are at the leaf node, try going across
if (it->entity != root && it->entity_next) {
it->entity = it->entity_next;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
ascending_tree = false;
}
// NOTE: Try descend again
if (it->entity_first_child && !ascending_tree) {
while (it->entity_first_child) {
it->entity = it->entity_first_child;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
}
// NOTE: If we could not move further across or down then we've
// exhausted the tree, start moving up.
if (it->last_visited == it->entity) {
it->entity = it->entity_parent;
it->entity_parent = it->entity->parent;
it->entity_next = it->entity->next;
it->entity_first_child = it->entity->first_child;
}
}
return it->entity->handle != root->handle;
}
// NOTE: Parent entity
2023-09-17 10:24:07 +00:00
static void FP_Game_PushParentEntity(FP_Game *game, FP_GameEntityHandle handle)
{
2023-10-08 05:14:31 +00:00
DQN_ASSERTF(game->play.parent_entity_stack.size >= 1, "Sentinel/nil entity has not been assigned as the 0th slot yet");
if (game)
2023-10-08 05:14:31 +00:00
Dqn_FArray_Add(&game->play.parent_entity_stack, handle);
}
2023-09-17 10:24:07 +00:00
static void FP_Game_PopParentEntity(FP_Game *game)
{
// NOTE: 0th slot is reserved for the nil entity
2023-10-08 05:14:31 +00:00
if (game && game->play.parent_entity_stack.size > 1)
Dqn_FArray_PopBack(&game->play.parent_entity_stack, 1);
}
2023-09-17 10:24:07 +00:00
static FP_GameEntityHandle FP_Game_ActiveParentEntity(FP_Game const *game)
{
2023-09-17 10:24:07 +00:00
FP_GameEntityHandle result = {};
2023-10-08 05:14:31 +00:00
if (!game || !game->play.parent_entity_stack.size)
return result;
2023-10-08 05:14:31 +00:00
result = game->play.parent_entity_stack.data[game->play.parent_entity_stack.size - 1];
return result;
}
2023-09-17 10:24:07 +00:00
static FP_GameEntity *FP_Game_ActiveParentEntityPointer(FP_Game const *game)
{
2023-09-17 10:24:07 +00:00
FP_GameEntityHandle handle = FP_Game_ActiveParentEntity(game);
FP_GameEntity *result = FP_Game_GetEntity(DQN_CAST(FP_Game *)game, handle);
return result;
}
2023-10-24 12:41:15 +00:00
static FP_GameEntity *FP_Game_MakeEntityPointerFV(FP_Game *game, DQN_FMT_ATTRIB char const *fmt, va_list args)
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *result = nullptr;
if (!game)
return result;
2023-10-08 05:14:31 +00:00
DQN_ASSERTF(game->play.entities.size > 0, "Sentinel/nil entity has not been initialised yet");
DQN_ASSERTF(game->play.root_entity, "Sentinel/nil entity has not been assigned yet");
2023-10-08 05:14:31 +00:00
result = game->play.root_entity; // TODO(doyle): Root entity or ... the nil entity?
if (game->play.entity_free_list) {
result = game->play.entity_free_list;
game->play.entity_free_list = game->play.entity_free_list->next;
result->next = nullptr;
} else {
if (game->play.entities.size > FP_GAME_ENTITY_HANDLE_INDEX_MAX)
return result;
2023-10-08 05:14:31 +00:00
result = Dqn_VArray_Make(&game->play.entities, Dqn_ZeroMem_Yes);
if (!result)
return result;
2023-10-08 05:14:31 +00:00
result->handle.id = (game->play.entities.size - 1) & FP_GAME_ENTITY_HANDLE_INDEX_MASK;
}
result->sprite_height.meters = 1;
result->parent = FP_Game_ActiveParentEntityPointer(game);
2023-10-08 05:14:31 +00:00
result->name = TELY_ChunkPool_AllocFmtFV(game->play.chunk_pool, fmt, args);
result->buildings_visited = FP_SentinelList_Init<FP_GameEntityHandle>(game->play.chunk_pool);
result->action.sprite_alpha = 1.f;
2023-10-07 12:05:46 +00:00
result->stamina_cap = 93;
result->stamina = result->stamina_cap;
2023-10-08 06:35:57 +00:00
result->hp_cap = DQN_CAST(uint32_t)(FP_DEFAULT_DAMAGE * .8f);
result->hp = result->hp_cap;
result->inventory.airports_base_price = 100;
result->inventory.churchs_base_price = 100;
result->inventory.kennels_base_price = 100;
result->inventory.clubs_base_price = 40;
2023-10-08 06:35:57 +00:00
result->base_attack = FP_DEFAULT_DAMAGE;
2023-10-08 08:04:11 +00:00
result->hp_recover_every_n_ticks = 12;
result->inventory.stamina_base_price = 10;
result->inventory.health_base_price = 10;
result->inventory.mobile_plan_base_price = 10;
result->inventory.attack_base_price = 10;
// NOTE: Attach entity as a child to the parent
2023-09-17 10:24:07 +00:00
FP_GameEntity *parent = result->parent;
if (parent->first_child)
parent->last_child->next = result;
else
parent->first_child = result;
result->prev = parent->last_child;
parent->last_child = result;
DQN_ASSERT(!result->next);
DQN_ASSERT(result->handle.id);
2023-10-08 05:14:31 +00:00
DQN_ASSERT(result->parent->handle == game->play.parent_entity_stack.data[game->play.parent_entity_stack.size - 1]);
return result;
}
2023-10-24 12:41:15 +00:00
static FP_GameEntity *FP_Game_MakeEntityPointerF(FP_Game *game, DQN_FMT_ATTRIB char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
2023-09-17 10:24:07 +00:00
FP_GameEntity *result = FP_Game_MakeEntityPointerFV(game, fmt, args);
va_end(args);
return result;
}
2023-09-17 10:24:07 +00:00
static bool FP_Game_IsNilEntity(FP_GameEntity *entity)
{
2023-09-17 10:24:07 +00:00
bool result = entity ? ((entity->handle.id & FP_GAME_ENTITY_HANDLE_INDEX_MASK) == 0) : true;
return result;
}
static bool FP_Game_IsNilEntityHandle(FP_Game *game, FP_GameEntityHandle handle)
{
bool result = true;
if (handle.id != 0) {
FP_GameEntity *entity = FP_Game_GetEntity(game, handle);
result = FP_Game_IsNilEntity(entity);
}
return result;
}
2023-09-17 10:24:07 +00:00
static void FP_Game_DetachEntityIntoFreeList(FP_Game *game, FP_GameEntityHandle handle)
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *entity = FP_Game_GetEntity(game, handle);
if (FP_Game_IsNilEntity(entity))
return;
// NOTE: Entities in the entity tree always have a parent (except for the
// nil/root entity). If an entity is passed in to this function and there's
// no parent, it's most likely you passed in an entity already in the free
// list (in which case only the next pointer will be set). This is most
// likely a mistake so we guard against it here.
if (!DQN_CHECK(entity->parent))
return;
2023-09-17 10:24:07 +00:00
uint64_t const entity_index_from_handle = entity->handle.id & FP_GAME_ENTITY_HANDLE_INDEX_MASK;
2023-10-08 05:14:31 +00:00
DQN_ASSERT(entity_index_from_handle < game->play.entities.size);
2023-09-17 10:24:07 +00:00
uint64_t const entity_generation_raw = entity->handle.id & FP_GAME_ENTITY_HANDLE_GENERATION_MASK;
uint64_t const entity_generation = entity_generation_raw >> FP_GAME_ENTITY_HANDLE_GENERATION_RSHIFT;
uint64_t const new_entity_generation = entity_generation + 1;
// NOTE: De-attach entity from adjacent children
if (entity->prev)
entity->prev->next = entity->next;
if (entity->next)
entity->next->prev = entity->prev;
// NOTE: De-attach from parent
2023-09-17 10:24:07 +00:00
FP_GameEntity *parent = entity->parent;
if (parent->first_child == entity)
parent->first_child = entity->next;
if (parent->last_child == entity)
parent->last_child = entity->prev;
if (entity->name.size)
2023-10-08 05:14:31 +00:00
TELY_ChunkPool_Dealloc(game->play.chunk_pool, entity->name.data);
2023-10-08 05:14:31 +00:00
FP_SentinelList_Deinit(&entity->spawn_list, game->play.chunk_pool);
FP_SentinelList_Deinit(&entity->waypoints, game->play.chunk_pool);
FP_SentinelList_Deinit(&entity->buildings_visited, game->play.chunk_pool);
if (new_entity_generation > entity_generation) {
// NOTE: Update the incremented handle disassociating all prior handles
// to this entity which would reference older generation values
*entity = {};
2023-10-08 05:14:31 +00:00
entity->parent = game->play.root_entity;
2023-09-17 10:24:07 +00:00
entity->handle.id = entity_index_from_handle | (new_entity_generation << FP_GAME_ENTITY_HANDLE_GENERATION_RSHIFT);
// NOTE: Attach entity to the free list
2023-10-08 05:14:31 +00:00
entity->next = game->play.entity_free_list;
entity->prev = nullptr;
2023-10-08 05:14:31 +00:00
game->play.entity_free_list = entity;
} else {
// NOTE: We've cycled through all possible generations for this handle
// We will not increment it and freeze it so it is no longer allocated
// out. This prevents code that is still holding onto *really* old
// handles
}
}
2023-09-17 10:24:07 +00:00
static void FP_Game_DeleteEntity(FP_Game *game, FP_GameEntityHandle handle)
{
2023-09-17 10:24:07 +00:00
uint64_t index_from_handle = handle.id & FP_GAME_ENTITY_HANDLE_INDEX_MASK;
2023-10-08 05:14:31 +00:00
if (!game || !DQN_CHECK(index_from_handle < game->play.entities.size))
return;
2023-10-08 05:14:31 +00:00
FP_GameEntity *root = game->play.entities.data + index_from_handle;
if (root->handle != handle)
return;
// NOTE: The iterator snaps a copy of all the internal n-ary tree pointers
// so as we delete we do not accidentally invalidate any of the pointers.
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, root); ) {
2023-10-25 09:17:30 +00:00
DQN_ASSERT(it.entity);
DQN_ASSERT(it.entity != root);
2023-09-17 10:24:07 +00:00
FP_GameEntity *entity = it.entity;
FP_Game_DetachEntityIntoFreeList(game, entity->handle);
}
2023-09-17 10:24:07 +00:00
FP_Game_DetachEntityIntoFreeList(game, root->handle);
}
2023-09-17 10:24:07 +00:00
static Dqn_V2 FP_Game_CalcEntityWorldPos(FP_Game const *game, FP_GameEntityHandle handle)
{
Dqn_V2 result = {};
if (!game)
return result;
2023-10-08 05:14:31 +00:00
FP_GameEntity const *first = FP_Game_GetEntity(DQN_CAST(FP_Game *)game, handle);
for (FP_GameEntity const *entity = first; entity != game->play.root_entity; entity = entity->parent)
result += entity->local_pos;
return result;
}
2023-09-17 10:24:07 +00:00
static Dqn_Rect FP_Game_CalcEntityLocalHitBox(FP_Game const *game, FP_GameEntityHandle handle)
{
FP_GameEntity *entity = FP_Game_GetEntity(DQN_CAST(FP_Game *)game, handle);
Dqn_V2 half_hit_box_size = entity->local_hit_box_size * .5f;
Dqn_Rect result = Dqn_Rect_InitV2x2(entity->local_hit_box_offset - half_hit_box_size, entity->local_hit_box_size);
return result;
}
2023-09-17 10:24:07 +00:00
static Dqn_Rect FP_Game_CalcEntityWorldHitBox(FP_Game const *game, FP_GameEntityHandle handle)
{
2023-09-17 11:55:59 +00:00
FP_GameEntity *entity = FP_Game_GetEntity(DQN_CAST(FP_Game *) game, handle);
2023-09-17 10:24:07 +00:00
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, handle);
Dqn_Rect local_hit_box = FP_Game_CalcEntityLocalHitBox(game, entity->handle);
Dqn_Rect result = Dqn_Rect_InitV2x2(world_pos + local_hit_box.pos, local_hit_box.size);
return result;
}
2023-09-17 10:24:07 +00:00
static Dqn_Rect FP_Game_CalcEntityAttackWorldHitBox(FP_Game const *game, FP_GameEntityHandle handle)
{
2023-10-05 10:10:50 +00:00
FP_GameEntity *entity = FP_Game_GetEntity(DQN_CAST(FP_Game *) game, handle);
2023-09-17 10:24:07 +00:00
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, handle);
Dqn_V2 half_hit_box_size = entity->attack_box_size * .5f;
Dqn_Rect result = Dqn_Rect_InitV2x2(world_pos + entity->attack_box_offset - half_hit_box_size, entity->attack_box_size);
return result;
}
2023-10-08 05:44:48 +00:00
static Dqn_FArray<Dqn_Rect, FP_GameDirection_Count> FP_Game_CalcEntityMeleeAttackBoxes(FP_Game const *game, FP_GameEntityHandle handle)
{
Dqn_FArray<Dqn_Rect, FP_GameDirection_Count> result = {};
Dqn_Rect hit_box = FP_Game_CalcEntityWorldHitBox(game, handle);
DQN_FOR_UINDEX (dir_index, FP_GameDirection_Count) {
Dqn_Rect *rect = Dqn_FArray_Make(&result, Dqn_ZeroMem_Yes);
rect->size = hit_box.size;
switch (dir_index) {
case FP_GameDirection_Left: {
rect->pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(-1.f, 0.f));
} break;
case FP_GameDirection_Right: {
rect->pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(+1.f, 0.f));
} break;
case FP_GameDirection_Up: {
rect->pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(0.f, -1.f));
} break;
case FP_GameDirection_Down: {
rect->pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(0.f, +1.f));
} break;
case FP_GameDirection_Count: break;
}
}
return result;
}
2023-09-17 10:24:07 +00:00
static Dqn_Rect FP_Game_CalcEntityArrayWorldBoundingBox(FP_Game const *game, FP_GameEntityHandle const *handles, Dqn_usize count)
{
Dqn_Rect result = {};
if (!game || !handles)
return result;
DQN_FOR_UINDEX(index, count) {
2023-09-17 10:24:07 +00:00
FP_GameEntityHandle handle = handles[index];
FP_GameEntity const *entity = FP_Game_GetEntity(DQN_CAST(FP_Game *) game, handle);
Dqn_Rect bbox = FP_Game_CalcEntityLocalHitBox(game, entity->handle);
for (FP_GameShape const &shape_ : entity->shapes) {
FP_GameShape const *shape = &shape_;
switch (shape->type) {
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_None: {
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Circle: {
Dqn_Rect rect =
Dqn_Rect_InitV2x2(shape->p1 - shape->circle_radius, Dqn_V2_InitNx1(shape->circle_radius * 2.f));
bbox = Dqn_Rect_Union(bbox, rect);
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Rect: /*FALLTHRU*/
case FP_GameShapeType_Line: {
Dqn_V2 min = Dqn_V2_Min(shape->p1, shape->p2);
Dqn_V2 max = Dqn_V2_Max(shape->p1, shape->p2);
Dqn_Rect rect = Dqn_Rect_InitV2x2(min, max - min);
2023-09-17 10:24:07 +00:00
if (shape->type == FP_GameShapeType_Rect)
rect.pos -= rect.size * .5f;
bbox = Dqn_Rect_Union(bbox, rect);
} break;
}
}
2023-09-17 10:24:07 +00:00
bbox.pos += FP_Game_CalcEntityWorldPos(game, entity->handle);
if (index)
result = Dqn_Rect_Union(result, bbox);
else
result = bbox;
}
return result;
}
2023-09-17 10:24:07 +00:00
static Dqn_Rect FP_Game_CalcEntityWorldBoundingBox(FP_Game *game, FP_GameEntityHandle handle)
{
2023-09-17 10:24:07 +00:00
Dqn_Rect result = FP_Game_CalcEntityArrayWorldBoundingBox(game, &handle, 1);
return result;
}
2023-09-18 13:00:30 +00:00
// Reset the timers and animation for the current action and set the duration
// for the new action.
static void FP_Game_EntityActionReset(FP_Game *game, FP_GameEntityHandle entity_handle, uint64_t duration_ms, TELY_AssetAnimatedSprite sprite)
2023-09-18 13:00:30 +00:00
{
FP_GameEntity *entity = FP_Game_GetEntity(game, entity_handle);
if (!entity)
2023-09-18 13:00:30 +00:00
return;
entity->action.sprite = sprite;
2023-10-08 05:14:31 +00:00
entity->action.started_at_clock_ms = game->play.clock_ms;
entity->action.end_at_clock_ms = DQN_MAX(duration_ms, game->play.clock_ms + duration_ms);
2023-09-18 13:00:30 +00:00
}
2023-09-23 02:33:59 +00:00
2023-10-09 13:06:59 +00:00
static Dqn_V2 FP_Game_CalcWaypointWorldPos(FP_Game *game, FP_GameEntityHandle entity_handle, FP_GameWaypoint const *waypoint)
2023-09-29 04:50:40 +00:00
{
Dqn_V2 result = {};
if (!game || !waypoint)
return result;
FP_GameEntity *waypoint_entity = FP_Game_GetEntity(game, waypoint->entity);
if (FP_Game_IsNilEntity(waypoint_entity))
return result;
// NOTE: We found a waypoint that is valid to move towards
switch (waypoint->type) {
case FP_GameWaypointType_At: {
result = FP_Game_CalcEntityWorldPos(game, waypoint_entity->handle);
} break;
2023-10-07 08:14:09 +00:00
case FP_GameWaypointType_ClosestSide: /*FALLTHRU*/
2023-09-29 04:50:40 +00:00
case FP_GameWaypointType_Side: {
2023-10-07 08:14:09 +00:00
// NOTE: Sweep entity with half the radius of the source entity
2023-10-09 13:06:59 +00:00
Dqn_Rect entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity_handle);
Dqn_Rect waypoint_hit_box = FP_Game_CalcEntityWorldHitBox(game, waypoint_entity->handle);
waypoint_hit_box.pos -= (entity_hit_box.size * .5f);
waypoint_hit_box.size += entity_hit_box.size;
2023-09-29 04:50:40 +00:00
2023-10-07 08:14:09 +00:00
Dqn_V2 side_pos_list[FP_GameDirection_Count] = {};
2023-10-09 13:06:59 +00:00
side_pos_list[FP_GameDirection_Up] = Dqn_V2_InitNx2(waypoint_hit_box.pos.x + waypoint_hit_box.size.w * .5f, waypoint_hit_box.pos.y - waypoint_hit_box.size.h * .1f);
side_pos_list[FP_GameDirection_Down] = Dqn_V2_InitNx2(waypoint_hit_box.pos.x + waypoint_hit_box.size.w * .5f, waypoint_hit_box.pos.y + waypoint_hit_box.size.h + waypoint_hit_box.size.h * .1f);
side_pos_list[FP_GameDirection_Left] = Dqn_V2_InitNx2(waypoint_hit_box.pos.x - waypoint_hit_box.size.w * .1f, waypoint_hit_box.pos.y + waypoint_hit_box.size.h * .5f);
side_pos_list[FP_GameDirection_Right] = Dqn_V2_InitNx2(waypoint_hit_box.pos.x + waypoint_hit_box.size.w + waypoint_hit_box.size.w * .1f, waypoint_hit_box.pos.y + waypoint_hit_box.size.h * .5f);
2023-09-29 04:50:40 +00:00
2023-10-07 08:14:09 +00:00
if (waypoint->type == FP_GameWaypointType_Side) {
result = side_pos_list[waypoint->type_direction];
} else {
Dqn_f32 best_dist = DQN_F32_MAX;
for (Dqn_V2 target_pos : side_pos_list) {
2023-10-09 13:06:59 +00:00
Dqn_f32 dist_squared = Dqn_V2_LengthSq_V2x2(entity_hit_box.pos, target_pos);
2023-10-07 08:14:09 +00:00
if (dist_squared < best_dist) {
best_dist = dist_squared;
result = target_pos;
}
}
2023-10-09 13:06:59 +00:00
}
} break;
2023-10-07 08:14:09 +00:00
2023-10-09 13:06:59 +00:00
case FP_GameWaypointType_Queue: {
Dqn_ArrayFindResult<FP_GameEntityHandle> find_result = Dqn_FArray_Find<FP_GameEntityHandle>(&waypoint_entity->building_queue, entity_handle);
Dqn_usize index_in_queue = find_result.index;
Dqn_Rect waypoint_hit_box = FP_Game_CalcEntityWorldHitBox(game, waypoint_entity->handle);
Dqn_V2 queue_starting_p = {};
if (waypoint_entity->type == FP_EntityType_ClubTerry || waypoint_entity->type == FP_EntityType_ChurchTerry) {
queue_starting_p = Dqn_Rect_InterpolatedPoint(waypoint_hit_box, Dqn_V2_InitNx2(0.5f, 1.1f));
} else {
queue_starting_p = Dqn_Rect_InterpolatedPoint(waypoint_hit_box, Dqn_V2_InitNx2(1.f, 1.1f));
2023-09-29 04:50:40 +00:00
}
2023-10-09 13:06:59 +00:00
Dqn_f32 queue_spacing = FP_Game_MetersToPixelsNx1(game->play, 1.f);
result = Dqn_V2_InitNx2(queue_starting_p.x - (queue_spacing * index_in_queue), queue_starting_p.y);
2023-09-29 04:50:40 +00:00
} break;
}
2023-10-08 08:48:17 +00:00
result += waypoint->offset;
2023-09-29 04:50:40 +00:00
return result;
}
FP_GameFindClosestEntityResult FP_Game_FindClosestEntityWithType(FP_Game *game, FP_GameEntityHandle src_entity_handle, FP_EntityType type)
{
FP_GameFindClosestEntityResult result = {};
FP_GameEntity *src_entity = FP_Game_GetEntity(game, src_entity_handle);
if (FP_Game_IsNilEntity(src_entity))
return result;
Dqn_V2 src_pos = FP_Game_CalcEntityWorldPos(game, src_entity_handle);
result.dist_squared = DQN_F32_MAX;
result.pos = Dqn_V2_InitNx1(DQN_F32_MAX);
2023-10-08 05:14:31 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->play.root_entity); ) {
2023-10-25 09:17:30 +00:00
DQN_ASSERT(it.entity);
FP_GameEntity *it_entity = it.entity;
if (it_entity->type != type)
continue;
Dqn_V2 pos = FP_Game_CalcEntityWorldPos(game, it_entity->handle);
Dqn_f32 dist = Dqn_V2_LengthSq_V2x2(pos, src_pos);
if (dist < result.dist_squared) {
result.pos = pos;
result.dist_squared = dist;
result.entity = it_entity->handle;
}
}
return result;
}
2023-10-01 05:47:40 +00:00
2023-10-07 14:29:50 +00:00
static FP_GameFindClosestEntityResult FP_Game_FindClosestEntityWithType(FP_Game *game, FP_GameEntityHandle src, FP_GameEntityHandle *entities, Dqn_usize size)
{
FP_GameFindClosestEntityResult result = {};
result.dist_squared = DQN_F32_MAX;
result.pos = Dqn_V2_InitNx1(DQN_F32_MAX);
Dqn_V2 src_pos = FP_Game_CalcEntityWorldPos(game, src);
DQN_FOR_UINDEX (index, size) {
FP_GameEntityHandle entity_handle = entities[index];
FP_GameEntity *entity = FP_Game_GetEntity(game, entity_handle);
if (FP_Game_IsNilEntity(entity))
continue;
Dqn_V2 pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
Dqn_f32 dist = Dqn_V2_LengthSq_V2x2(pos, src_pos);
if (dist < result.dist_squared) {
result.pos = pos;
result.dist_squared = dist;
result.entity = entity->handle;
}
}
return result;
}
2023-10-01 05:47:40 +00:00
static bool FP_Game_CanEntityAttack(FP_GameEntity *entity, uint64_t current_time_ms)
{
bool result = (current_time_ms - entity->last_attack_timestamp) >= entity->attack_cooldown_ms;
return result;
}
static void FP_Game_EntityTransitionState(FP_Game *game, FP_GameEntity *entity, uint32_t desired_state)
{
2023-10-01 06:47:52 +00:00
switch (entity->type) {
2023-10-21 05:30:15 +00:00
case FP_EntityType_Perry: /*FALLTHRU*/
2023-10-01 06:47:52 +00:00
case FP_EntityType_Terry: {
if (desired_state == FP_EntityTerryState_Attack ||
desired_state == FP_EntityTerryState_RangeAttack) {
2023-10-08 05:14:31 +00:00
if (!FP_Game_CanEntityAttack(entity, game->play.clock_ms)) {
2023-10-01 06:47:52 +00:00
// NOTE: Cooldown not met do not transition
return;
}
2023-10-08 05:14:31 +00:00
entity->last_attack_timestamp = game->play.clock_ms;
if (desired_state == FP_EntityTerryState_RangeAttack) {
if (entity->terry_mobile_data_plan < FP_TERRY_MOBILE_DATA_PER_RANGE_ATTACK)
return;
}
} else if (desired_state == FP_EntityTerryState_Dash) {
if (entity->stamina < FP_TERRY_DASH_STAMINA_COST)
return;
2023-10-01 05:47:40 +00:00
}
2023-10-01 06:47:52 +00:00
} break;
case FP_EntityType_Smoochie: {
if (desired_state == FP_EntitySmoochieState_Attack) {
2023-10-08 05:14:31 +00:00
if (!FP_Game_CanEntityAttack(entity, game->play.clock_ms)) {
2023-10-01 06:47:52 +00:00
// NOTE: Cooldown not met do not transition
return;
}
2023-10-08 05:14:31 +00:00
entity->last_attack_timestamp = game->play.clock_ms;
2023-10-01 05:47:40 +00:00
}
2023-10-01 06:47:52 +00:00
} break;
2023-10-01 05:47:40 +00:00
2023-10-01 06:47:52 +00:00
case FP_EntityType_Clinger: {
if (desired_state == FP_EntityClingerState_Attack) {
2023-10-08 05:14:31 +00:00
if (!FP_Game_CanEntityAttack(entity, game->play.clock_ms)) {
2023-10-01 06:47:52 +00:00
// NOTE: Cooldown not met do not transition
return;
}
2023-10-08 05:14:31 +00:00
entity->last_attack_timestamp = game->play.clock_ms;
2023-10-01 06:47:52 +00:00
}
2023-10-01 05:47:40 +00:00
} break;
case FP_EntityType_Catfish: {
if (desired_state == FP_EntityClingerState_Attack) {
2023-10-08 05:14:31 +00:00
if (!FP_Game_CanEntityAttack(entity, game->play.clock_ms)) {
// NOTE: Cooldown not met do not transition
return;
}
2023-10-08 05:14:31 +00:00
entity->last_attack_timestamp = game->play.clock_ms;
}
} break;
case FP_EntityType_AirportTerry: {
} break;
case FP_EntityType_ChurchTerry: {
} break;
case FP_EntityType_ClubTerry: {
} break;
case FP_EntityType_Nil:
case FP_EntityType_Heart:
case FP_EntityType_KennelTerry:
case FP_EntityType_Map:
case FP_EntityType_MerchantGraveyard:
case FP_EntityType_MerchantGym:
case FP_EntityType_MerchantPhoneCompany:
case FP_EntityType_MerchantTerry:
case FP_EntityType_PhoneMessageProjectile:
case FP_EntityType_Count: break;
case FP_EntityType_AirportTerryPlane:
case FP_EntityType_MobSpawner:
case FP_EntityType_PortalMonkey: break;
case FP_EntityType_Billboard: break;
2023-10-01 05:47:40 +00:00
}
// NOTE: If no returns are hit above we proceed with the state change
entity->action.next_state = desired_state;
}
2023-10-15 10:02:28 +00:00
static void FP_GameRenderScanlines(TELY_Renderer *renderer,
Dqn_f32 scanline_gap,
Dqn_f32 scanline_thickness,
Dqn_V2 screen_size)
{
2023-10-15 10:02:28 +00:00
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); };
Dqn_f32 scanline_interval = scanline_gap + scanline_thickness;
2023-10-15 10:02:28 +00:00
for (Dqn_f32 y = 0; y < screen_size.h; y += scanline_interval) {
Dqn_V2 start = Dqn_V2_InitNx2(0, y);
Dqn_V2 end = Dqn_V2_InitNx2(screen_size.w, y);
2023-10-06 08:57:59 +00:00
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.1f), scanline_thickness);
}
}
2023-10-19 13:20:41 +00:00
static FP_GameCanMoveToPositionResult FP_Game_CanEntityMoveToPosition(FP_Game *game, FP_GameEntityHandle entity_handle, Dqn_V2 delta_pos)
{
FP_GameCanMoveToPositionResult result = {};
FP_GameEntity *entity = FP_Game_GetEntity(game, entity_handle);
if (FP_Game_IsNilEntity(entity))
return result;
Dqn_Rect const entity_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 const entity_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
Dqn_f32 const SENTINEL_T = 999.f;
Dqn_f32 global_earliest_t = SENTINEL_T;
Dqn_V2 global_earliest_pos_just_before_collide = {};
Dqn_V2 const entity_new_pos = entity_pos + delta_pos;
if ((entity->flags & FP_GameEntityFlag_NoClip) == 0) {
for (FP_GameEntityIterator collider_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &collider_it, game->play.root_entity);) {
2023-10-25 09:17:30 +00:00
DQN_ASSERT(collider_it.entity);
2023-10-19 13:20:41 +00:00
FP_GameEntity *collider = collider_it.entity;
if (collider->handle == entity->handle)
continue;
// TODO(doyle): Calculate the list of collidables at the start of the frame
if ((collider->flags & FP_GameEntityFlag_NonTraversable) == 0)
continue;
bool skip_colliding_with_player = false;
for (FP_GameEntityHandle player : game->play.players) {
if (player == collider->handle) {
skip_colliding_with_player = true;
break;
}
}
if (skip_colliding_with_player)
continue;
2023-10-19 13:20:41 +00:00
bool entity_collides_with_collider = true;
switch (entity->type) {
case FP_EntityType_Catfish: /*FALLTHRU*/
case FP_EntityType_Smoochie: /*FALLTHRU*/
case FP_EntityType_Clinger: {
if (collider->type == FP_EntityType_Smoochie || collider->type == FP_EntityType_Clinger || collider->type == FP_EntityType_Catfish) {
entity_collides_with_collider = false;
} else if (FP_Entity_IsBuildingForMobs(collider)) {
#if 0
// NOTE: We disable collision on buildings we have visited to avoid some
// problems ...
if (FP_SentinelList_Find(&entity->buildings_visited, collider->handle)) {
entity_collides_with_collider = false;
}
#else
entity_collides_with_collider = false;
#endif
} else if (collider->type == FP_EntityType_Heart ||
collider->type == FP_EntityType_MerchantGym ||
collider->type == FP_EntityType_MerchantTerry ||
collider->type == FP_EntityType_MerchantGraveyard ||
collider->type == FP_EntityType_MerchantPhoneCompany) {
entity_collides_with_collider = false;
}
} break;
2023-10-21 05:30:15 +00:00
case FP_EntityType_Perry: /*FALLTHRU*/
2023-10-19 13:20:41 +00:00
case FP_EntityType_Terry: {
// NOTE: Don't collide with mobs when dashing (e.g. phase through)
FP_EntityTerryState state = *DQN_CAST(FP_EntityTerryState *)&entity->action.state;
if (state == FP_EntityTerryState_Dash || state == FP_EntityTerryState_DeadGhost) {
if (collider->type == FP_EntityType_Smoochie || collider->type == FP_EntityType_Clinger || collider->type == FP_EntityType_Catfish)
entity_collides_with_collider = false;
}
} break;
case FP_EntityType_Nil: break;
case FP_EntityType_MerchantTerry: break;
case FP_EntityType_Count: break;
case FP_EntityType_ClubTerry: break;
case FP_EntityType_Map: break;
case FP_EntityType_MerchantGraveyard: break;
case FP_EntityType_MerchantGym: break;
case FP_EntityType_MerchantPhoneCompany: break;
case FP_EntityType_Heart: break;
case FP_EntityType_AirportTerry: break;
case FP_EntityType_ChurchTerry: break;
case FP_EntityType_KennelTerry: break;
case FP_EntityType_PhoneMessageProjectile: break;
case FP_EntityType_AirportTerryPlane:
case FP_EntityType_MobSpawner:
case FP_EntityType_PortalMonkey: break;
case FP_EntityType_Billboard: break;
}
if (!entity_collides_with_collider)
continue;
// NOTE: Sweep collider with half the radius of the source entity
Dqn_Rect collider_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, collider->handle);
if (Dqn_V2_Area(collider_world_hit_box.size) <= 0)
continue;
Dqn_Rect swept_collider_world_hit_box = collider_world_hit_box;
swept_collider_world_hit_box.pos -= (entity_world_hit_box.size * .5f);
swept_collider_world_hit_box.size += entity_world_hit_box.size;
if (!Dqn_Rect_ContainsPoint(swept_collider_world_hit_box, entity_new_pos))
continue;
Dqn_f32 collider_left_wall_x = swept_collider_world_hit_box.pos.x;
Dqn_f32 collider_right_wall_x = swept_collider_world_hit_box.pos.x + swept_collider_world_hit_box.size.w;
Dqn_f32 collider_top_wall_y = swept_collider_world_hit_box.pos.y;
Dqn_f32 collider_bottom_wall_y = swept_collider_world_hit_box.pos.y + swept_collider_world_hit_box.size.h;
Dqn_V2 o = entity_pos;
Dqn_V2 d = delta_pos;
// NOTE: Solve collision by determining the 't' value at which
// we hit one of the walls of the collider and move the entity
// at exactly that point.
// O + td = x
// td = x - O
// t = (x - O) / d
Dqn_f32 earliest_t = SENTINEL_T;
if (d.x != 0.f) {
Dqn_f32 left_t = (collider_left_wall_x - o.x) / d.x;
Dqn_f32 right_t = (collider_right_wall_x - o.x) / d.x;
if (left_t >= 0.f && left_t <= 1.f)
earliest_t = DQN_MIN(earliest_t, left_t);
if (right_t >= 0.f && right_t <= 1.f)
earliest_t = DQN_MIN(earliest_t, right_t);
}
if (d.y != 0.f) {
Dqn_f32 top_t = (collider_top_wall_y - o.y) / d.y;
Dqn_f32 bottom_t = (collider_bottom_wall_y - o.y) / d.y;
if (top_t >= 0.f && top_t <= 1.f)
earliest_t = DQN_MIN(earliest_t, top_t);
if (bottom_t >= 0.f && bottom_t <= 1.f)
earliest_t = DQN_MIN(earliest_t, bottom_t);
}
if (earliest_t < global_earliest_t) {
global_earliest_t = earliest_t;
global_earliest_pos_just_before_collide = entity_pos + (d * earliest_t);
}
}
}
result.yes = global_earliest_t == SENTINEL_T;
if (!result.yes)
result.next_closest_valid_move = delta_pos * global_earliest_t;
return result;
}
static void FP_Game_MoveEntity(FP_Game *game, FP_GameEntityHandle entity_handle, Dqn_V2 acceleration_meters_per_s)
{
// f"(t) = a
// f'(t) = at + v
// f (t) = 0.5f*a(t^2) + vt + p
FP_GameEntity *entity = FP_Game_GetEntity(game, entity_handle);
if (FP_Game_IsNilEntity(entity))
return;
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(FP_GAME_PHYSICS_STEP);
Dqn_f32 t_squared = DQN_SQUARED(t);
Dqn_f32 velocity_falloff_coefficient = 0.82f;
Dqn_f32 acceleration_feel_good_factor = 15'000.f;
Dqn_V2 acceleration = FP_Game_MetersToPixelsV2(game->play, acceleration_meters_per_s) * acceleration_feel_good_factor;
entity->velocity = (acceleration * t) + entity->velocity * velocity_falloff_coefficient;
// NOTE: Zero out velocity with epsilon
if (DQN_ABS(entity->velocity.x) < 5.f)
entity->velocity.x = 0.f;
if (DQN_ABS(entity->velocity.y) < 5.f)
entity->velocity.y = 0.f;
2023-10-20 13:08:09 +00:00
Dqn_V2 const delta_pos = (acceleration * 0.5f * t_squared) + (entity->velocity * t);
2023-10-19 13:20:41 +00:00
FP_GameCanMoveToPositionResult move_to_result = FP_Game_CanEntityMoveToPosition(game, entity->handle, delta_pos);
if (move_to_result.yes) {
entity->local_pos += delta_pos;
} else {
entity->local_pos += move_to_result.next_closest_valid_move;
}
}
static Dqn_Rect FP_Game_GetBuildingPlacementRectForEntity(FP_Game *game, FP_GamePlaceableBuilding placeable_building, FP_GameEntityHandle handle)
{
Dqn_Rect result = {};
FP_GameEntity *entity = FP_Game_GetEntity(game, handle);
if (FP_Game_IsNilEntity(entity))
return result;
FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, placeable_building.type, placeable_building.state, entity->direction);
Dqn_Rect box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 build_p = {};
switch (entity->direction) {
case FP_GameDirection_Up: {
build_p = Dqn_Rect_InterpolatedPoint(box, Dqn_V2_InitNx2(0.5f, 0.f)) - Dqn_V2_InitNx2(0.f, render_data.render_size.h * .5f + 10.f);
} break;
case FP_GameDirection_Down: {
build_p = Dqn_Rect_InterpolatedPoint(box, Dqn_V2_InitNx2(0.5f, 1.f)) + Dqn_V2_InitNx2(0.f, render_data.render_size.h * .5f + 10.f);
} break;
case FP_GameDirection_Left: {
build_p = Dqn_Rect_InterpolatedPoint(box, Dqn_V2_InitNx2(0.0f, 0.5f)) - Dqn_V2_InitNx2(render_data.render_size.w * .5f + 10.f, 0);
} break;
case FP_GameDirection_Right: {
build_p = Dqn_Rect_InterpolatedPoint(box, Dqn_V2_InitNx2(1.f, 0.5f)) + Dqn_V2_InitNx2(render_data.render_size.w * .5f + 10.f, 0);
} break;
case FP_GameDirection_Count: DQN_INVALID_CODE_PATH; break;
}
result.size = render_data.render_size;
result.pos = build_p - (render_data.render_size * .5f);
return result;
}