feely_pona/feely_pona.cpp

2583 lines
139 KiB
C++
Raw Normal View History

2023-09-16 02:21:24 +00:00
#if defined(__clang__)
#pragma once
#include "feely_pona_unity.h"
#endif
Dqn_f32 const PHYSICS_STEP = 1 / 60.f;
2023-10-05 12:09:39 +00:00
Dqn_Rect FP_Game_GetBuildingPlacementRectForEntity(FP_Game *game, FP_GamePlaceableBuilding placeable_building, FP_GameEntityHandle handle)
2023-10-02 11:38:36 +00:00
{
Dqn_Rect result = {};
FP_GameEntity *entity = FP_Game_GetEntity(game, handle);
if (FP_Game_IsNilEntity(entity))
return result;
2023-10-05 12:09:39 +00:00
FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, placeable_building.type, placeable_building.state, entity->direction);
2023-10-02 11:38:36 +00:00
Dqn_Rect box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 build_p = {};
switch (entity->direction) {
case FP_GameDirection_Up: {
2023-10-05 12:09:39 +00:00
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);
2023-10-02 11:38:36 +00:00
} break;
case FP_GameDirection_Down: {
2023-10-05 12:09:39 +00:00
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);
2023-10-02 11:38:36 +00:00
} break;
case FP_GameDirection_Left: {
2023-10-05 12:09:39 +00:00
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);
2023-10-02 11:38:36 +00:00
} break;
case FP_GameDirection_Right: {
2023-10-05 12:09:39 +00:00
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);
2023-10-02 11:38:36 +00:00
} break;
case FP_GameDirection_Count: DQN_INVALID_CODE_PATH; break;
}
2023-10-05 12:09:39 +00:00
result.size = render_data.render_size;
result.pos = build_p - (render_data.render_size * .5f);
2023-10-02 11:38:36 +00:00
return result;
}
TELY_AssetSpriteSheet FP_LoadSpriteSheetFromSpec(TELY_Platform *platform, TELY_Assets *assets, Dqn_Arena *arena, Dqn_String8 sheet_name)
{
TELY_AssetSpriteSheet result = {};
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(arena);
result.sprite_size = Dqn_V2I_InitNx2(185, 170);
result.type = TELY_AssetSpriteSheetType_Rects;
// NOTE: Load the sprite meta file =========================================================
Dqn_String8 sprite_spec_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s.txt", DQN_STRING_FMT(assets->textures_dir), DQN_STRING_FMT(sheet_name));
Dqn_String8 sprite_spec_buffer = platform->func_load_file(scratch.arena, sprite_spec_path);
Dqn_String8SplitAllocResult lines = Dqn_String8_SplitAlloc(scratch.allocator, sprite_spec_buffer, DQN_STRING8("\n"));
Dqn_usize sprite_rect_index = 0;
Dqn_usize sprite_anim_index = 0;
DQN_FOR_UINDEX(line_index, lines.size) {
Dqn_String8 line = lines.data[line_index];
Dqn_String8SplitAllocResult line_splits = Dqn_String8_SplitAlloc(scratch.allocator, line, DQN_STRING8(";"));
if (line_index == 0) {
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @file lines");
DQN_ASSERT(Dqn_String8_StartsWith(line_splits.data[0], DQN_STRING8("@file"), Dqn_String8EqCase_Sensitive));
// NOTE: Sprite sheet path
Dqn_String8 sprite_sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s", DQN_STRING_FMT(assets->textures_dir), DQN_STRING_FMT(line_splits.data[1]));
result.tex_handle = platform->func_load_texture(assets, sheet_name, sprite_sheet_path);
DQN_ASSERTF(Dqn_Fs_Exists(sprite_sheet_path), "Required file does not exist '%.*s'", DQN_STRING_FMT(sprite_sheet_path));
// NOTE: Total sprite frame count
Dqn_String8ToU64Result total_frame_count = Dqn_String8_ToU64(line_splits.data[2], 0);
DQN_ASSERT(total_frame_count.success);
result.rects = Dqn_Slice_Alloc<Dqn_Rect>(arena, total_frame_count.value, Dqn_ZeroMem_No);
// NOTE: Total animation count
Dqn_String8ToU64Result total_anim_count = Dqn_String8_ToU64(line_splits.data[3], 0);
DQN_ASSERT(total_anim_count.success);
result.anims = Dqn_Slice_Alloc<TELY_AssetSpriteAnimation>(arena, total_anim_count.value, Dqn_ZeroMem_No);
// TODO: Sprite size?
// TODO: Texture name?
continue;
}
if (Dqn_String8_StartsWith(line, DQN_STRING8("@anim"))) {
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @anim lines");
Dqn_String8 anim_name = line_splits.data[1];
Dqn_String8ToU64Result frames_per_second = Dqn_String8_ToU64(line_splits.data[2], 0);
Dqn_String8ToU64Result frame_count = Dqn_String8_ToU64(line_splits.data[3], 0);
DQN_ASSERT(anim_name.size);
DQN_ASSERT(frame_count.success);
DQN_ASSERT(frames_per_second.success);
Dqn_Allocator allocator = Dqn_Arena_Allocator(arena);
TELY_AssetSpriteAnimation *anim = result.anims.data + sprite_anim_index++;
anim->label = Dqn_String8_Copy(allocator, anim_name);
anim->index = DQN_CAST(uint16_t)sprite_rect_index;
anim->count = DQN_CAST(uint16_t)frame_count.value;
anim->ms_per_frame = DQN_CAST(uint32_t)(1000.f / frames_per_second.value);
DQN_ASSERT(anim->ms_per_frame != 0);
} else {
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for sprite frame lines");
Dqn_String8ToU64Result x = Dqn_String8_ToU64(line_splits.data[0], 0);
Dqn_String8ToU64Result y = Dqn_String8_ToU64(line_splits.data[1], 0);
Dqn_String8ToU64Result w = Dqn_String8_ToU64(line_splits.data[2], 0);
Dqn_String8ToU64Result h = Dqn_String8_ToU64(line_splits.data[3], 0);
DQN_ASSERT(x.success);
DQN_ASSERT(y.success);
DQN_ASSERT(w.success);
DQN_ASSERT(h.success);
result.rects.data[sprite_rect_index++] =
Dqn_Rect_InitNx4(DQN_CAST(Dqn_f32) x.value,
DQN_CAST(Dqn_f32) y.value,
DQN_CAST(Dqn_f32) w.value,
DQN_CAST(Dqn_f32) h.value);
}
}
DQN_ASSERT(result.rects.size == sprite_rect_index);
DQN_ASSERT(result.anims.size == sprite_anim_index);
2023-09-24 11:54:08 +00:00
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(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, 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;
Dqn_V2 delta_pos = (acceleration * 0.5f * t_squared) + (entity->velocity * t);
Dqn_Rect entity_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
Dqn_V2 entity_new_pos = entity_pos + delta_pos;
Dqn_f32 const SENTINEL_T = 999.f;
Dqn_f32 global_earliest_t = SENTINEL_T;
Dqn_V2 global_earliest_pos_just_before_collide = {};
for (FP_GameEntityIterator collider_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &collider_it, game->root_entity); ) {
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;
2023-09-29 05:58:03 +00:00
bool entity_collides_with_collider = true;
switch (entity->type) {
case FP_EntityType_Catfish: /*FALLTHRU*/
case FP_EntityType_Smoochie: /*FALLTHRU*/
2023-09-29 05:58:03 +00:00
case FP_EntityType_Clinger: {
if (collider->type == FP_EntityType_Smoochie || collider->type == FP_EntityType_Clinger || collider->type == FP_EntityType_Catfish) {
2023-10-04 12:50:31 +00:00
entity_collides_with_collider = false;
} else if (FP_Entity_IsBuildingForMobs(collider)) {
// 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;
}
}
2023-10-04 12:50:31 +00:00
} break;
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) {
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;
2023-10-05 10:10:50 +00:00
case FP_EntityType_PhoneMessageProjectile: break;
2023-09-29 05:58:03 +00:00
}
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);
2023-10-05 10:10:50 +00:00
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);
}
}
if (global_earliest_t == SENTINEL_T) {
entity->local_pos += delta_pos;
} else {
Dqn_V2 new_delta_pos = global_earliest_pos_just_before_collide - entity_pos;
entity->local_pos += new_delta_pos;
}
}
2023-09-16 02:21:24 +00:00
extern "C" __declspec(dllexport)
void TELY_DLL_Reload(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
Dqn_Library_SetPointer(platform->core.dqn_lib);
2023-10-02 11:38:36 +00:00
2023-09-16 02:21:24 +00:00
}
extern "C" __declspec(dllexport)
void TELY_DLL_Init(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
TELY_DLL_Reload(user_data);
2023-09-24 08:05:28 +00:00
FP_UnitTests(platform);
2023-09-16 02:21:24 +00:00
// NOTE: TELY Game =============================================================================
TELY_Assets *assets = &platform->assets;
2023-09-17 10:24:07 +00:00
FP_Game *game = Dqn_Arena_New(&platform->arena, FP_Game, Dqn_ZeroMem_Yes);
game->chunk_pool = &platform->chunk_pool;
2023-09-25 14:07:39 +00:00
game->meters_to_pixels = 65.416f;
Dqn_PCG32_Seed(&game->rng, 0xABCDEF);
2023-09-25 14:07:39 +00:00
2023-09-24 08:05:28 +00:00
platform->user_data = game;
2023-09-16 02:21:24 +00:00
{
2023-09-17 10:24:07 +00:00
TELY_AssetSpriteSheet *sheet = &game->hero_sprite_sheet;
2023-09-16 02:21:24 +00:00
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
Dqn_String8 sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/adventurer-v1.5-sheet.png", DQN_STRING_FMT(assets->textures_dir));
sheet->tex_handle = platform->func_load_texture(assets, DQN_STRING8("Hero"), sheet_path);
sheet->sprite_count = 109;
sheet->sprites_per_row = 7;
sheet->sprite_size = Dqn_V2I_InitNx2(50, 37);
TELY_AssetSpriteAnimation hero_anims[] = {
{DQN_STRING8("Everything"), /*index*/ 0, /*count*/ sheet->sprite_count, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Idle"), /*index*/ 0, /*count*/ 3, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 4.f)},
{DQN_STRING8("Run"), /*index*/ 8, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 8.f)},
{DQN_STRING8("Jump"), /*index*/ 14, /*count*/ 10, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Floor slide"), /*index*/ 24, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Unknown"), /*index*/ 29, /*count*/ 9, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Attack A"), /*index*/ 42, /*count*/ 7, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Attack B"), /*index*/ 49, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 8.f)},
{DQN_STRING8("Attack C"), /*index*/ 53, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Hurt A"), /*index*/ 59, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Hurt B"), /*index*/ 64, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Unsheath sword"), /*index*/ 69, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Sheath sword"), /*index*/ 73, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Air drift"), /*index*/ 77, /*count*/ 2, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Air drop"), /*index*/ 79, /*count*/ 2, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Ladder climb"), /*index*/ 81, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Chi push"), /*index*/ 85, /*count*/ 8, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Leap slice A"), /*index*/ 93, /*count*/ 7, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Leap slice B"), /*index*/ 100, /*count*/ 3, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
{DQN_STRING8("Leap slice C"), /*index*/ 103, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
2023-09-16 02:21:24 +00:00
};
2023-09-17 10:24:07 +00:00
game->hero_sprite_anims = Dqn_Slice_Alloc<TELY_AssetSpriteAnimation>(&platform->arena, DQN_ARRAY_UCOUNT(hero_anims), Dqn_ZeroMem_No);
DQN_MEMCPY(game->hero_sprite_anims.data, &hero_anims, sizeof(hero_anims[0]) * DQN_ARRAY_UCOUNT(hero_anims));
2023-09-16 02:21:24 +00:00
}
// NOTE: Load sprite sheets ====================================================================
game->atlas_sprite_sheet = FP_LoadSpriteSheetFromSpec(platform, assets, &platform->arena, DQN_STRING8("atlas"));
2023-09-17 10:24:07 +00:00
game->entities = Dqn_VArray_Init<FP_GameEntity>(&platform->arena, 1024 * 8);
2023-09-18 11:46:42 +00:00
game->root_entity = Dqn_VArray_Make(&game->entities, Dqn_ZeroMem_No);
2023-09-16 02:21:24 +00:00
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
2023-10-01 10:15:05 +00:00
// NOTE: Map ===================================================================================
2023-09-30 09:14:35 +00:00
{
TELY_AssetSpriteAnimation *sprite_anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.map);
Dqn_Rect sprite_rect = game->atlas_sprite_sheet.rects.data[sprite_anim->index];
2023-09-30 09:14:35 +00:00
2023-10-02 11:38:36 +00:00
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Map");
entity->type = FP_EntityType_Map;
entity->sprite_height = FP_Entity_GetRenderData(game, entity->type, 0 /*state*/, FP_GameDirection_Down).height;
entity->local_pos = {};
2023-09-30 09:14:35 +00:00
Dqn_f32 size_scale = FP_Entity_CalcSpriteScaleForDesiredHeight(game, entity->sprite_height, sprite_rect);
Dqn_V2 sprite_rect_scaled = sprite_rect.size * size_scale;
entity->local_hit_box_size = sprite_rect_scaled;
FP_Entity_AddDebugEditorFlags(game, entity->handle);
game->map = entity;
2023-09-30 09:14:35 +00:00
}
2023-10-01 10:15:05 +00:00
// NOTE: Map walls =============================================================================
2023-10-02 12:41:08 +00:00
FP_GameEntity const *map = game->map;
Dqn_Rect const map_hit_box = FP_Game_CalcEntityWorldHitBox(game, map->handle);
2023-10-01 10:15:05 +00:00
{
Dqn_f32 wall_thickness = FP_Game_MetersToPixelsNx1(game, 1.f);
Dqn_f32 half_wall_thickness = wall_thickness * .5f;
FP_Entity_CreateWallAtPos(game,
DQN_STRING8("Left Wall"),
Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.f, 0.5f)) - Dqn_V2_InitNx2(half_wall_thickness, 0.f),
Dqn_V2_InitNx2(wall_thickness, map_hit_box.size.h));
FP_Entity_CreateWallAtPos(game,
DQN_STRING8("Right Wall"),
Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(1.f, 0.5f)) + Dqn_V2_InitNx2(half_wall_thickness, 0.f),
Dqn_V2_InitNx2(wall_thickness, map_hit_box.size.h));
FP_Entity_CreateWallAtPos(game,
DQN_STRING8("Top Wall"),
Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.5f, 0.f)) - Dqn_V2_InitNx2(0.f, half_wall_thickness),
Dqn_V2_InitNx2(map_hit_box.size.w, wall_thickness));
FP_Entity_CreateWallAtPos(game,
DQN_STRING8("Bottom Wall"),
Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.5f, 1.f)) + Dqn_V2_InitNx2(0.f, half_wall_thickness),
Dqn_V2_InitNx2(map_hit_box.size.w, wall_thickness));
}
2023-10-02 12:41:08 +00:00
// NOTE: Map building zones
{
{
FP_Entity_CreatePermittedBuildZone(game,
Dqn_V2_InitNx2(0.f, -1206),
Dqn_V2_InitNx2(map_hit_box.size.w, 335),
"Building Zone");
}
{
FP_Entity_CreatePermittedBuildZone(game,
Dqn_V2_InitNx2(-839.9, -460),
Dqn_V2_InitNx2(2991.3, 670),
"Building Zone");
}
{
FP_Entity_CreatePermittedBuildZone(game,
Dqn_V2_InitNx2(-839.9, 460),
Dqn_V2_InitNx2(2991.3, 670),
"Building Zone");
}
{
FP_Entity_CreatePermittedBuildZone(game,
Dqn_V2_InitNx2(0.f, 1200),
Dqn_V2_InitNx2(map_hit_box.size.w, 335),
"Building Zone");
}
}
2023-10-01 10:15:05 +00:00
// NOTE: Hero ==================================================================================
2023-09-16 02:21:24 +00:00
{
FP_GameEntityHandle terry = FP_Entity_CreateTerry(game, Dqn_V2_InitNx2(1434, 11), "Terry");
2023-09-25 12:13:32 +00:00
game->clicked_entity = terry;
game->player = terry;
2023-09-16 02:21:24 +00:00
}
2023-10-01 06:50:32 +00:00
{
Dqn_V2 base_top_left_pos = Dqn_V2_InitNx2(1018, -335);
Dqn_V2 base_bottom_right_pos = Dqn_V2_InitNx2(2050, +351);
2023-10-01 06:50:32 +00:00
Dqn_V2 base_top_left = base_top_left_pos;
Dqn_V2 base_top_right = Dqn_V2_InitNx2(base_bottom_right_pos.x, base_top_left_pos.y);
Dqn_V2 base_bottom_left = Dqn_V2_InitNx2(base_top_left_pos.x, base_bottom_right_pos.y);
Dqn_V2 base_bottom_right = Dqn_V2_InitNx2(base_bottom_right_pos.x, base_bottom_right_pos.y);
game->merchant_terry = FP_Entity_CreateMerchantTerry(game, base_top_left, "Merchant");
game->merchant_graveyard = FP_Entity_CreateMerchantGraveyard(game, base_bottom_left, "Graveyard");
game->merchant_gym = FP_Entity_CreateMerchantGym(game, base_bottom_right, "Gym");
game->merchant_phone_company = FP_Entity_CreateMerchantPhoneCompany(game, base_top_right, "PhoneCompany");
2023-10-01 06:50:32 +00:00
}
FP_Entity_CreateClubTerry(game, Dqn_V2_InitNx2(567, -191), "Club Terry");
2023-09-24 08:05:28 +00:00
2023-09-23 05:44:37 +00:00
game->tile_size = 37;
Dqn_V2I max_tile = platform->core.window_size / game->tile_size;
2023-09-30 13:24:04 +00:00
// NOTE: Mid lane mob spawner ==================================================================
Dqn_V2 base_mid_p = Dqn_V2_InitNx2(1580, 0.f);
Dqn_V2 mid_lane_mob_spawner_pos = Dqn_V2_InitNx2(game->map->local_hit_box_size.w * -0.5f, 0.f);
2023-10-02 11:38:36 +00:00
Dqn_usize spawn_cap = 16;
{
FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, mid_lane_mob_spawner_pos, spawn_cap, "Mob spawner");
2023-09-25 12:13:32 +00:00
FP_Game_PushParentEntity(game, mob_spawner);
2023-09-30 13:24:04 +00:00
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-mid_lane_mob_spawner_pos.x + base_mid_p.x, base_mid_p.y), "Waypoint");
FP_Game_PopParentEntity(game);
}
// NOTE: Bottom lane spawner ===================================================================
Dqn_V2 bottom_lane_mob_spawner_pos = Dqn_V2_InitNx2(mid_lane_mob_spawner_pos.x, mid_lane_mob_spawner_pos.y + 932.f);
{
FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, bottom_lane_mob_spawner_pos, spawn_cap, "Mob spawner");
2023-09-30 13:24:04 +00:00
FP_Game_PushParentEntity(game, mob_spawner);
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-bottom_lane_mob_spawner_pos.x + base_mid_p.x, 0.f), "Waypoint");
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-bottom_lane_mob_spawner_pos.x + base_mid_p.x, -932.f), "Waypoint");
FP_Game_PopParentEntity(game);
}
// NOTE: Top lane spawner ===================================================================
Dqn_V2 top_lane_mob_spawner_pos = Dqn_V2_InitNx2(mid_lane_mob_spawner_pos.x, mid_lane_mob_spawner_pos.y - 915.f);
{
FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, top_lane_mob_spawner_pos, spawn_cap, "Mob spawner");
2023-09-30 13:24:04 +00:00
FP_Game_PushParentEntity(game, mob_spawner);
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-top_lane_mob_spawner_pos.x + base_mid_p.x, 0.f), "Waypoint");
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-top_lane_mob_spawner_pos.x + base_mid_p.x, +915.f), "Waypoint");
FP_Game_PopParentEntity(game);
}
FP_Entity_CreateHeart(game, base_mid_p, "Heart");
2023-09-30 13:24:04 +00:00
2023-10-02 12:41:08 +00:00
2023-09-30 09:09:15 +00:00
uint16_t font_size = 18;
game->camera.scale = Dqn_V2_InitNx1(1);
game->inter_regular_font = platform->func_load_font(assets, DQN_STRING8("Inter (Regular)"), DQN_STRING8("Data/Inter-Regular.otf"), font_size);
game->inter_italic_font = platform->func_load_font(assets, DQN_STRING8("Inter (Italic)"), DQN_STRING8("Data/Inter-Italic.otf"), font_size);
game->jetbrains_mono_font = platform->func_load_font(assets, DQN_STRING8("JetBrains Mono NL (Regular)"), DQN_STRING8("Data/JetBrainsMonoNL-Regular.ttf"), font_size);
game->talkco_font = platform->func_load_font(assets, DQN_STRING8("Talkco"), DQN_STRING8("Data/Talkco.otf"), font_size);
game->talkco_font_large = platform->func_load_font(assets, DQN_STRING8("Talkco"), DQN_STRING8("Data/Talkco.otf"), DQN_CAST(uint16_t)(font_size * 1.5f));
2023-09-30 09:09:15 +00:00
game->audio[FP_GameAudio_TestAudio] = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa"));
game->audio[FP_GameAudio_TerryHit] = platform->func_load_audio(assets, DQN_STRING8("Terry Hit"), DQN_STRING8("Data/Audio/terry_hit.ogg"));
2023-09-30 13:27:19 +00:00
game->audio[FP_GameAudio_Smooch] = platform->func_load_audio(assets, DQN_STRING8("Smooch"), DQN_STRING8("Data/Audio/smooch.mp3"));
2023-09-16 02:21:24 +00:00
}
void FP_EntityActionStateMachine(FP_Game *game, TELY_Audio *audio, TELY_PlatformInput *input, FP_GameEntity *entity, Dqn_V2 *acceleration_meters_per_s)
{
TELY_AssetSpriteSheet *sheet = &game->atlas_sprite_sheet;
FP_GameEntityAction *action = &entity->action;
bool const we_are_clicked_entity = entity->handle == game->clicked_entity;
bool const entity_has_velocity = entity->velocity.x || entity->velocity.y;
bool const entering_new_state = action->state != action->next_state;
bool const action_has_finished = !entering_new_state && game->clock_ms >= action->end_at_clock_ms;
action->state = action->next_state;
2023-10-02 11:38:36 +00:00
// NOTE: Left-shift lets us strafe in the same direction
if (!TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_LeftShift)) {
if (acceleration_meters_per_s->x)
entity->direction = acceleration_meters_per_s->x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left;
else if (acceleration_meters_per_s->y)
entity->direction = acceleration_meters_per_s->y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up;
}
2023-10-02 11:38:36 +00:00
FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, entity->type, entity->action.state, entity->direction);
switch (entity->type) {
2023-09-24 08:16:14 +00:00
case FP_EntityType_Terry: {
2023-10-02 11:38:36 +00:00
FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state;
2023-09-24 11:54:08 +00:00
switch (*state) {
case FP_EntityTerryState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle);
} break;
case FP_EntityTerryState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack);
2023-10-05 10:10:50 +00:00
} else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_K) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_Y)) {
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_RangeAttack);
} else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Run);
2023-09-24 08:05:28 +00:00
}
}
} break;
case FP_EntityTerryState_Attack: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle);
}
} break;
2023-10-05 10:10:50 +00:00
case FP_EntityTerryState_RangeAttack: {
2023-10-04 12:50:31 +00:00
if (entering_new_state) {
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
entity->terry_mobile_data_plan -= FP_TERRY_MOBILE_DATA_PER_RANGE_ATTACK;
2023-10-04 12:50:31 +00:00
}
if (action_has_finished) {
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle);
}
} break;
case FP_EntityTerryState_Run: {
2023-10-02 11:38:36 +00:00
if (entering_new_state || action->sprite.anim->label != render_data.anim_name) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack);
2023-10-05 10:10:50 +00:00
} else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_K) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_Y)) {
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_RangeAttack);
2023-10-02 11:38:36 +00:00
} else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_LeftControl) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_A)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Dash);
2023-09-24 08:05:28 +00:00
}
}
if (!entity_has_velocity) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle);
}
} break;
case FP_EntityTerryState_Dash: {
if (entering_new_state) {
uint64_t duration_ms = 250;
2023-10-02 11:38:36 +00:00
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
*acceleration_meters_per_s *= 35.f;
}
if (action_has_finished)
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Run);
} break;
}
2023-10-05 10:10:50 +00:00
if (*state == FP_EntityTerryState_Attack || *state == FP_EntityTerryState_RangeAttack) {
DQN_ASSERT(action->sprite.anim);
uint64_t duration_ms = action->sprite.anim->count * action->sprite.anim->ms_per_frame;
DQN_ASSERT(duration_ms >= PHYSICS_STEP);
uint64_t midpoint_clock_ms = action->end_at_clock_ms - (duration_ms / 2);
// NOTE: Adding an attack_processed bool to make sure things only fire once
if (!entity->attack_processed && game->clock_ms >= midpoint_clock_ms) {
// NOTE: Position the attack box
2023-10-05 10:10:50 +00:00
Dqn_V2 dir_vector = {};
switch (entity->direction) {
case FP_GameDirection_Left: {
2023-10-05 10:10:50 +00:00
dir_vector.x = -1.f;
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Right: {
2023-10-05 10:10:50 +00:00
dir_vector.x = +1.f;
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Up: {
2023-10-05 10:10:50 +00:00
dir_vector.y = -1.f;
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y - entity->attack_box_size.h);
} break;
case FP_GameDirection_Down: {
2023-10-05 10:10:50 +00:00
dir_vector.y = +1.f;
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y + entity->attack_box_size.h);
} break;
case FP_GameDirection_Count: break;
}
2023-10-05 10:10:50 +00:00
if (*state == FP_EntityTerryState_RangeAttack) {
Dqn_Rect entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 projectile_pos = entity_hit_box.pos + entity->attack_box_offset;
Dqn_V2 projectile_acceleration = FP_Game_MetersToPixelsV2(game, dir_vector * 0.25f);
2023-10-06 10:57:34 +00:00
FP_Entity_CreatePhoneMessageProjectile(game,
entity->handle,
projectile_pos,
projectile_acceleration,
"Phone Message Projectile");
2023-10-05 10:10:50 +00:00
} else {
entity->attack_box_size = entity->local_hit_box_size;
TELY_Audio_Play(audio, game->audio[FP_GameAudio_TerryHit], 1.f);
}
entity->attack_processed = true;
} else {
entity->attack_box_size = {};
}
} else {
entity->attack_box_size = {};
entity->attack_processed = false;
}
} break;
2023-09-24 08:16:14 +00:00
case FP_EntityType_Smoochie: {
2023-09-24 11:54:08 +00:00
FP_EntitySmoochieState *state = DQN_CAST(FP_EntitySmoochieState *) & action->state;
switch (*state) {
case FP_EntitySmoochieState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle);
} break;
case FP_EntitySmoochieState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack);
} else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Run);
2023-09-24 08:05:28 +00:00
}
}
if (entity_has_velocity) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Run);
}
} break;
case FP_EntitySmoochieState_Attack: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-09-30 13:27:19 +00:00
// NOTE: Deal with this further down with the gameplay attack code.
TELY_Audio_Play(audio, game->audio[FP_GameAudio_Smooch], 1.f);
}
// NOTE: Check if the heart animation is playing
bool has_heart_flourish = false;
DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) {
FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index;
if (sprite->asset.anim->label == g_anim_names.smoochie_attack_heart) {
has_heart_flourish = true;
break;
}
}
// NOTE: If we don't have the anim playing make one
if (!has_heart_flourish) {
FP_GameRenderSprite *cosmetic_sprite = Dqn_FArray_Make(&entity->extra_cosmetic_anims, Dqn_ZeroMem_Yes);
if (cosmetic_sprite) {
cosmetic_sprite->asset = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.smoochie_attack_heart, TELY_AssetFlip_No);
cosmetic_sprite->started_at_clock_ms = game->clock_ms;
cosmetic_sprite->height.meters = entity->sprite_height.meters * .35f;
cosmetic_sprite->loop = true;
uint32_t max_rng_dist_x = DQN_CAST(uint32_t)(FP_Game_MetersToPixelsNx1(game, entity->sprite_height.meters) * 1.f);
uint32_t rng_x = Dqn_PCG32_Range(&game->rng, DQN_CAST(uint32_t)0, max_rng_dist_x);
cosmetic_sprite->offset.x = rng_x - (max_rng_dist_x * .5f);
uint32_t max_rng_dist_y = DQN_CAST(uint32_t)(FP_Game_MetersToPixelsNx1(game, entity->sprite_height.meters) * .25f);
uint32_t rng_y = Dqn_PCG32_Range(&game->rng, DQN_CAST(uint32_t)0, max_rng_dist_y);
cosmetic_sprite->offset.y = -DQN_CAST(Dqn_f32)rng_y;
}
}
if (action_has_finished) {
// NOTE: Ensure the heart animation terminates by removing the loop
DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) {
FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index;
if (sprite->asset.anim->label == g_anim_names.smoochie_attack_heart)
sprite->loop = false;
}
// NOTE: Transition out of the action
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle);
}
} break;
case FP_EntitySmoochieState_HurtSide: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished)
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle);
} break;
case FP_EntitySmoochieState_Death: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-10-05 10:10:50 +00:00
entity->local_hit_box_size = {};
}
if (action_has_finished)
FP_Game_DeleteEntity(game, entity->handle);
} break;
case FP_EntitySmoochieState_Run: {
2023-10-02 11:38:36 +00:00
if (entering_new_state || action->sprite.anim->label != render_data.anim_name) {
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, render_data.anim_name, render_data.flip);
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack);
2023-09-24 08:05:28 +00:00
}
}
if (!entity_has_velocity) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle);
}
} break;
}
if (entity->is_dying && *state != FP_EntitySmoochieState_Death) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Death);
}
if (*state == FP_EntitySmoochieState_Attack) {
entity->attack_box_size = entity->local_hit_box_size;
// NOTE: Position the attack box
switch (entity->direction) {
case FP_GameDirection_Left: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Right: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Up: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y - entity->attack_box_size.h);
} break;
case FP_GameDirection_Down: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y + entity->attack_box_size.h);
} break;
case FP_GameDirection_Count: break;
}
} else {
entity->attack_box_size = {};
}
} break;
2023-09-24 08:05:28 +00:00
case FP_EntityType_Clinger: {
FP_EntityClingerState *state = DQN_CAST(FP_EntityClingerState *)&action->state;
switch (*state) {
case FP_EntityClingerState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Idle);
} break;
case FP_EntityClingerState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack);
} else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Run);
}
}
if (entity_has_velocity) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Run);
}
} break;
case FP_EntityClingerState_Attack: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished)
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Idle);
} break;
case FP_EntityClingerState_Death: {
if (entering_new_state) {
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.clinger_death, TELY_AssetFlip_No);
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
2023-10-05 10:10:50 +00:00
entity->local_hit_box_size = {};
}
if (action_has_finished)
FP_Game_DeleteEntity(game, entity->handle);
} break;
case FP_EntityClingerState_Run: {
2023-10-02 11:38:36 +00:00
if (entering_new_state || action->sprite.anim->label != render_data.anim_name) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack);
}
}
if (!entity_has_velocity) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Idle);
}
} break;
}
if (entity->is_dying && *state != FP_EntityClingerState_Death) {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Death);
}
if (*state == FP_EntityClingerState_Attack) { // NOTE: Position the attack box
entity->attack_box_size = entity->local_hit_box_size;
switch (entity->direction) {
case FP_GameDirection_Left: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Right: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Up: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y - entity->attack_box_size.h);
} break;
case FP_GameDirection_Down: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y + entity->attack_box_size.h);
} break;
case FP_GameDirection_Count: DQN_INVALID_CODE_PATH; break;
}
} else {
entity->attack_box_size = {};
}
} break;
2023-10-01 06:50:32 +00:00
case FP_EntityType_MerchantTerry: {
FP_EntityMerchantTerryState *state = DQN_CAST(FP_EntityMerchantTerryState *)&action->state;
switch (*state) {
2023-10-01 06:50:32 +00:00
case FP_EntityMerchantTerryState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityMerchantTerryState_Idle);
} break;
2023-09-24 08:05:28 +00:00
2023-10-01 06:50:32 +00:00
case FP_EntityMerchantTerryState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-10-01 06:50:32 +00:00
}
} break;
}
} break;
case FP_EntityType_MerchantPhoneCompany: {
FP_EntityMerchantPhoneCompanyState *state = DQN_CAST(FP_EntityMerchantPhoneCompanyState *)&action->state;
switch (*state) {
case FP_EntityMerchantPhoneCompanyState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityMerchantPhoneCompanyState_Idle);
2023-10-01 06:50:32 +00:00
} break;
case FP_EntityMerchantPhoneCompanyState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-10-01 06:50:32 +00:00
}
} break;
}
} break;
case FP_EntityType_MerchantGym: {
FP_EntityMerchantGymState *state = DQN_CAST(FP_EntityMerchantGymState *)&action->state;
switch (*state) {
case FP_EntityMerchantGymState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityMerchantGymState_Idle);
2023-10-01 06:50:32 +00:00
} break;
case FP_EntityMerchantGymState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-10-01 06:50:32 +00:00
}
} break;
}
} break;
case FP_EntityType_MerchantGraveyard: {
FP_EntityMerchantGraveyardState *state = DQN_CAST(FP_EntityMerchantGraveyardState *)&action->state;
switch (*state) {
case FP_EntityMerchantGraveyardState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityMerchantGraveyardState_Idle);
2023-10-01 06:50:32 +00:00
} break;
case FP_EntityMerchantGraveyardState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
} break;
2023-09-24 08:05:28 +00:00
}
} break;
2023-09-29 07:42:58 +00:00
case FP_EntityType_ClubTerry: {
FP_EntityClubTerryState *state = DQN_CAST(FP_EntityClubTerryState *)&action->state;
switch (*state) {
case FP_EntityClubTerryState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClubTerryState_Idle);
2023-09-29 07:42:58 +00:00
} break;
case FP_EntityClubTerryState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-09-29 07:42:58 +00:00
}
2023-09-30 06:51:59 +00:00
} break;
case FP_EntityClubTerryState_PartyTime: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = 5000;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-09-30 06:51:59 +00:00
}
2023-09-29 07:42:58 +00:00
2023-09-30 06:51:59 +00:00
if (action_has_finished) {
if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) {
FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron);
patron->flags &= ~FP_GameEntityFlag_OccupiedInBuilding;
2023-09-30 06:51:59 +00:00
patron->base_acceleration_per_s.meters *= .5f;
}
entity->building_patron = {};
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClubTerryState_Idle);
2023-09-30 06:51:59 +00:00
}
2023-09-29 07:42:58 +00:00
} break;
}
2023-09-30 06:51:59 +00:00
2023-09-29 07:42:58 +00:00
} break;
2023-09-30 09:14:35 +00:00
case FP_EntityType_Map: {
2023-10-04 12:50:31 +00:00
FP_EntityMapState *state = DQN_CAST(FP_EntityMapState *) & action->state;
2023-09-30 09:14:35 +00:00
switch (*state) {
case FP_EntityMapState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityMapState_Idle);
2023-09-30 09:14:35 +00:00
} break;
case FP_EntityMapState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-09-30 09:14:35 +00:00
}
} break;
}
} break;
case FP_EntityType_Heart: {
2023-10-04 12:50:31 +00:00
FP_EntityHeartState *state = DQN_CAST(FP_EntityHeartState *) & action->state;
switch (*state) {
case FP_EntityHeartState_Nil: {
2023-10-01 05:47:40 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityHeartState_Idle);
} break;
case FP_EntityHeartState_Idle: {
if (entering_new_state) {
2023-10-02 11:38:36 +00:00
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
} break;
}
} break;
2023-10-04 12:50:31 +00:00
case FP_EntityType_AirportTerry: {
FP_EntityAirportTerryState *state = DQN_CAST(FP_EntityAirportTerryState *)&action->state;
switch (*state) {
case FP_EntityAirportTerryState_Nil: {
FP_Game_EntityTransitionState(game, entity, FP_EntityAirportTerryState_Idle);
} break;
case FP_EntityAirportTerryState_Idle: {
if (entering_new_state) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
} break;
case FP_EntityAirportTerryState_FlyPassenger: {
if (entering_new_state) {
uint64_t duration_ms = 5000;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished) {
if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) {
FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron);
patron->flags &= ~FP_GameEntityFlag_OccupiedInBuilding;
}
entity->building_patron = {};
FP_Game_EntityTransitionState(game, entity, FP_EntityAirportTerryState_Idle);
}
2023-10-04 12:50:31 +00:00
} break;
}
} break;
case FP_EntityType_Catfish: {
FP_EntityCatfishState *state = DQN_CAST(FP_EntityCatfishState *) & action->state;
switch (*state) {
case FP_EntityCatfishState_Nil: {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Idle);
} break;
case FP_EntityCatfishState_Idle: {
if (entering_new_state) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack);
} else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Run);
}
}
if (entity_has_velocity) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Run);
}
} break;
case FP_EntityCatfishState_Attack: {
if (entering_new_state) {
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished)
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Idle);
} break;
case FP_EntityCatfishState_Death: {
if (entering_new_state) {
uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
2023-10-05 10:10:50 +00:00
entity->local_hit_box_size = {};
2023-10-04 12:50:31 +00:00
}
if (action_has_finished)
FP_Game_DeleteEntity(game, entity->handle);
} break;
case FP_EntityCatfishState_Run: {
if (entering_new_state || action->sprite.anim->label != render_data.anim_name) {
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, render_data.anim_name, render_data.flip);
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
}
if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack);
}
}
if (!entity_has_velocity) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Idle);
}
} break;
}
if (entity->is_dying && *state != FP_EntityCatfishState_Death) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Death);
}
if (*state == FP_EntityCatfishState_Attack) { // NOTE: Position the attack box
entity->attack_box_size = entity->local_hit_box_size;
switch (entity->direction) {
case FP_GameDirection_Left: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Right: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} break;
case FP_GameDirection_Up: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y - entity->attack_box_size.h);
} break;
case FP_GameDirection_Down: {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
entity->local_hit_box_offset.y + entity->attack_box_size.h);
} break;
case FP_GameDirection_Count: DQN_INVALID_CODE_PATH; break;
}
} else {
entity->attack_box_size = {};
}
} break;
case FP_EntityType_ChurchTerry: {
FP_EntityChurchTerryState *state = DQN_CAST(FP_EntityChurchTerryState *)&action->state;
switch (*state) {
case FP_EntityChurchTerryState_Nil: {
FP_Game_EntityTransitionState(game, entity, FP_EntityChurchTerryState_Idle);
} break;
case FP_EntityChurchTerryState_Idle: {
if (entering_new_state) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
} break;
case FP_EntityChurchTerryState_ConvertPatron: {
if (entering_new_state) {
uint64_t duration_ms = 5000;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
if (action_has_finished) {
if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) {
FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron);
patron->flags &= ~FP_GameEntityFlag_OccupiedInBuilding;
}
entity->building_patron = {};
FP_Game_EntityTransitionState(game, entity, FP_EntityChurchTerryState_Idle);
}
} break;
2023-10-04 12:50:31 +00:00
}
} break;
case FP_EntityType_KennelTerry: {
FP_EntityKennelTerryState *state = DQN_CAST(FP_EntityKennelTerryState *)&action->state;
switch (*state) {
case FP_EntityKennelTerryState_Nil: {
FP_Game_EntityTransitionState(game, entity, FP_EntityKennelTerryState_Idle);
} break;
case FP_EntityKennelTerryState_Idle: {
if (entering_new_state) {
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite);
}
} break;
}
} break;
case FP_EntityType_Nil: break;
2023-10-04 12:50:31 +00:00
case FP_EntityType_Count: DQN_INVALID_CODE_PATH; break;
case FP_EntityType_PhoneMessageProjectile: break;
2023-09-30 09:14:35 +00:00
}
}
void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input)
2023-09-16 07:32:25 +00:00
{
Dqn_Profiler_ZoneScopeWithIndex("FP_Update", FP_ProfileZone_FPUpdate);
2023-09-16 02:21:24 +00:00
if (TELY_Platform_InputKeyIsReleased(input->mouse_left))
game->clicked_entity = game->prev_active_entity;
game->clock_ms = DQN_CAST(uint64_t)(platform->input.timer_s * 1000.f);
2023-09-16 02:21:24 +00:00
Dqn_V2 dir_vector = {};
// NOTE: Keyboard movement input
2023-09-16 02:21:24 +00:00
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_W))
dir_vector.y = -1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_A))
dir_vector.x = -1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_S))
dir_vector.y = +1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_D))
dir_vector.x = +1.f;
// NOTE: Gamepad movement input
// NOTE: button_codes 0 should be the first gamepad connected, we can
// get this working with other gamepads later
uint32_t gamepad = 0;
if (input->button_codes[gamepad]) {
dir_vector.x += input->left_stick[gamepad].x;
dir_vector.y += input->left_stick[gamepad].y;
}
2023-09-21 07:18:32 +00:00
if (dir_vector.x && dir_vector.y) {
dir_vector.x *= 0.7071067811865475244f;
dir_vector.y *= 0.7071067811865475244f;
}
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Escape))
game->clicked_entity = {};
2023-09-16 02:21:24 +00:00
if (game->clicked_entity.id) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Delete))
2023-09-17 10:24:07 +00:00
FP_Game_DeleteEntity(game, game->clicked_entity);
2023-10-05 12:09:39 +00:00
// NOTE: Building selector
Dqn_usize last_building_index = DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) - 1;
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Q)) {
if (game->build_mode_building_index <= 0) {
game->build_mode_building_index = last_building_index;
} else {
game->build_mode_building_index -= 1;
}
}
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_E)) {
if (game->build_mode_building_index >= last_building_index) {
game->build_mode_building_index = 0;
} else {
game->build_mode_building_index += 1;
}
}
2023-09-16 02:21:24 +00:00
} else {
2023-09-30 13:24:04 +00:00
Dqn_f32 pan_speed = 5.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_Space))
pan_speed *= 2.5f;
game->camera.world_pos += dir_vector * pan_speed;
2023-10-05 10:30:56 +00:00
2023-09-16 02:21:24 +00:00
}
2023-10-05 10:30:56 +00:00
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_F1))
game->debug_ui = !game->debug_ui;
Dqn_ProfilerZone update_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: Entity loop"), FP_ProfileZone_FPUpdate_EntityLoop);
2023-09-29 05:28:11 +00:00
// NOTE: Handle input ==========================================================================
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
entity->alive_time_s += PHYSICS_STEP;
2023-09-16 02:21:24 +00:00
if (entity->flags & FP_GameEntityFlag_OccupiedInBuilding)
2023-09-30 06:51:59 +00:00
continue;
// NOTE: Move entity by keyboard and gamepad ===============================================
2023-10-05 10:10:50 +00:00
Dqn_V2 acceleration_meters_per_s = entity->constant_acceleration_per_s;
if (game->clicked_entity == entity->handle) {
if (entity->flags & (FP_GameEntityFlag_MoveByKeyboard | FP_GameEntityFlag_MoveByGamepad)) {
bool move_entity = true;
switch (entity->type) {
2023-09-24 08:16:14 +00:00
case FP_EntityType_Terry: {
auto *state = DQN_CAST(FP_EntityTerryState *)&entity->action.state;
move_entity = *state == FP_EntityTerryState_Run || *state == FP_EntityTerryState_Idle;
} break;
2023-09-24 08:16:14 +00:00
case FP_EntityType_Smoochie: {
auto *state = DQN_CAST(FP_EntitySmoochieState *)&entity->action.state;
move_entity = *state == FP_EntitySmoochieState_Run || *state == FP_EntitySmoochieState_Idle;
} break;
2023-09-29 05:18:38 +00:00
case FP_EntityType_Clinger: {
auto *state = DQN_CAST(FP_EntityClingerState *)&entity->action.state;
move_entity = *state == FP_EntityClingerState_Run || *state == FP_EntityClingerState_Idle;
} break;
2023-10-05 10:10:50 +00:00
case FP_EntityType_Nil: break;
case FP_EntityType_ClubTerry: break;
case FP_EntityType_Count: break;
case FP_EntityType_Map: break;
case FP_EntityType_MerchantTerry: break;
case FP_EntityType_MerchantGraveyard: break;
case FP_EntityType_MerchantPhoneCompany: break;
case FP_EntityType_Heart: break;
case FP_EntityType_MerchantGym: break;
case FP_EntityType_AirportTerry: break;
case FP_EntityType_Catfish: break;
case FP_EntityType_ChurchTerry: break;
case FP_EntityType_KennelTerry: break;
case FP_EntityType_PhoneMessageProjectile: break;
}
if (move_entity) {
2023-09-30 06:51:59 +00:00
acceleration_meters_per_s = dir_vector * entity->base_acceleration_per_s.meters;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_Space))
acceleration_meters_per_s *= 2.5f;
}
2023-09-16 02:21:24 +00:00
}
} else {
if (entity->velocity.x)
entity->direction = entity->velocity.x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left;
else if (entity->velocity.y)
entity->direction = entity->velocity.y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up;
2023-09-16 02:21:24 +00:00
}
2023-09-25 13:06:39 +00:00
// NOTE: Determine AI movement =============================================================
2023-09-25 14:07:39 +00:00
if (acceleration_meters_per_s.x == 0 && acceleration_meters_per_s.y == 0) {
2023-09-25 13:06:39 +00:00
Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
if (entity->flags & FP_GameEntityFlag_AggrosWhenNearTerry) {
FP_GameFindClosestEntityResult closest_result = FP_Game_FindClosestEntityWithType(game, entity->handle, FP_EntityType_Terry);
Dqn_f32 aggro_dist_threshold = FP_Game_MetersToPixelsNx1(game, 4.f);
if (closest_result.dist_squared < DQN_SQUARED(aggro_dist_threshold)) {
bool has_waypoint_to_terry = false;
for (FP_SentinelListLink<FP_GameWaypoint> *link = nullptr;
!has_waypoint_to_terry && FP_SentinelList_Iterate<FP_GameWaypoint>(&entity->waypoints, &link); ) {
has_waypoint_to_terry = link->data.entity == closest_result.entity;
}
if (!has_waypoint_to_terry) {
FP_GameEntity *terry = FP_Game_GetEntity(game, closest_result.entity);
2023-09-29 04:50:40 +00:00
FP_GameDirection aggro_direction = FP_GameDirection_Count;
DQN_FOR_UINDEX(dir_index, FP_GameDirection_Count) {
FP_GameEntityHandle slot_entity_handle = terry->aggro_slot[dir_index];
2023-09-29 04:50:40 +00:00
FP_GameEntity *slot_entity = FP_Game_GetEntity(game, slot_entity_handle);
if (FP_Game_IsNilEntity(slot_entity)) {
aggro_direction = DQN_CAST(FP_GameDirection)dir_index;
break;
}
}
FP_SentinelListLink<FP_GameWaypoint> *link = FP_SentinelList_MakeBefore(&entity->waypoints,
FP_SentinelList_Front(&entity->waypoints),
game->chunk_pool);
FP_GameWaypoint *waypoint = &link->data;
waypoint->entity = terry->handle;
2023-09-29 04:50:40 +00:00
if (aggro_direction != FP_GameDirection_Count) {
waypoint->type = FP_GameWaypointType_Side;
waypoint->type_direction = aggro_direction;
terry->aggro_slot[aggro_direction] = entity->handle;
2023-09-29 04:50:40 +00:00
}
2023-09-25 13:06:39 +00:00
}
} else {
if (closest_result.dist_squared > DQN_SQUARED(aggro_dist_threshold * 2.f)) {
for (FP_SentinelListLink<FP_GameWaypoint> *link = nullptr; FP_SentinelList_Iterate<FP_GameWaypoint>(&entity->waypoints, &link); ) {
FP_GameEntity *maybe_terry = FP_Game_GetEntity(game, link->data.entity);
if (maybe_terry->type == FP_EntityType_Terry) {
link = FP_SentinelList_Erase(&entity->waypoints, link, game->chunk_pool);
}
}
}
2023-09-25 13:06:39 +00:00
}
}
if (entity->flags & FP_GameEntityFlag_RespondsToBuildings) {
FP_GameFindClosestEntityResult closest_building = {};
closest_building.dist_squared = DQN_F32_MAX;
closest_building.pos = Dqn_V2_InitNx1(DQN_F32_MAX);
for (FP_GameEntityIterator building_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &building_it, game->root_entity); ) {
FP_GameEntity *it_entity = building_it.entity;
if (it_entity->type != FP_EntityType_ClubTerry &&
it_entity->type != FP_EntityType_AirportTerry &&
it_entity->type != FP_EntityType_ChurchTerry)
continue;
bool already_visited_building = false;
for (FP_SentinelListLink<FP_GameEntityHandle> *link_it = {};
!already_visited_building && FP_SentinelList_Iterate(&entity->buildings_visited, &link_it);
) {
FP_GameEntityHandle visit_item = link_it->data;
already_visited_building = visit_item == it_entity->handle;
}
if (already_visited_building)
continue;
Dqn_V2 pos = FP_Game_CalcEntityWorldPos(game, it_entity->handle);
Dqn_f32 dist = Dqn_V2_LengthSq_V2x2(pos, entity_pos);
if (dist < closest_building.dist_squared) {
closest_building.pos = pos;
closest_building.dist_squared = dist;
closest_building.entity = it_entity->handle;
}
}
if (!FP_Game_IsNilEntityHandle(game, closest_building.entity) &&
closest_building.dist_squared < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game, 5.f))) {
bool has_waypoint_to_building = false;
for (FP_SentinelListLink<FP_GameWaypoint> *link = nullptr;
!has_waypoint_to_building && FP_SentinelList_Iterate<FP_GameWaypoint>(&entity->waypoints, &link); ) {
has_waypoint_to_building = link->data.entity == closest_building.entity;
}
if (!has_waypoint_to_building) {
Dqn_Rect hit_box = FP_Game_CalcEntityWorldHitBox(game, closest_building.entity);
Dqn_V2 top_left = Dqn_Rect_TopLeft(hit_box);
Dqn_V2 top_right = Dqn_Rect_TopRight(hit_box);
FP_SentinelListLink<FP_GameWaypoint> *link = FP_SentinelList_MakeBefore(&entity->waypoints, FP_SentinelList_Front(&entity->waypoints), game->chunk_pool);
FP_GameWaypoint *waypoint = &link->data;
waypoint->entity = closest_building.entity;
waypoint->type = FP_GameWaypointType_Side;
if (entity_pos.x <= top_left.x) {
waypoint->type_direction = FP_GameDirection_Left;
} else if (entity_pos.x >= top_right.x) {
waypoint->type_direction = FP_GameDirection_Right;
} else if (entity_pos.y <= top_left.y) {
waypoint->type_direction = FP_GameDirection_Up;
} else {
waypoint->type_direction = FP_GameDirection_Down;
}
}
}
}
if (entity->flags & FP_GameEntityFlag_PointOfInterestHeart) {
FP_GameFindClosestEntityResult closest_heart = FP_Game_FindClosestEntityWithType(game, entity->handle, FP_EntityType_Heart);
if (closest_heart.dist_squared < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game, 4.f))) {
bool has_waypoint_to = false;
for (FP_SentinelListLink<FP_GameWaypoint> *link = nullptr;
!has_waypoint_to && FP_SentinelList_Iterate<FP_GameWaypoint>(&entity->waypoints, &link); ) {
has_waypoint_to = link->data.entity == closest_heart.entity;
}
if (!has_waypoint_to) {
Dqn_Rect club_hit_box = FP_Game_CalcEntityWorldHitBox(game, closest_heart.entity);
Dqn_V2 club_top_left = Dqn_Rect_TopLeft(club_hit_box);
Dqn_V2 club_top_right = Dqn_Rect_TopRight(club_hit_box);
FP_SentinelListLink<FP_GameWaypoint> *link = FP_SentinelList_MakeBefore(&entity->waypoints, FP_SentinelList_Front(&entity->waypoints), game->chunk_pool);
FP_GameWaypoint *waypoint = &link->data;
waypoint->entity = closest_heart.entity;
waypoint->type = FP_GameWaypointType_Side;
if (entity_pos.x <= club_top_left.x) {
waypoint->type_direction = FP_GameDirection_Left;
} else if (entity_pos.x >= club_top_right.x) {
waypoint->type_direction = FP_GameDirection_Right;
} else if (entity_pos.y <= club_top_left.y) {
waypoint->type_direction = FP_GameDirection_Up;
} else {
waypoint->type_direction = FP_GameDirection_Down;
}
}
}
}
while (entity->waypoints.size) {
FP_SentinelListLink<FP_GameWaypoint> *waypoint_link = entity->waypoints.sentinel->next;
2023-09-29 04:50:40 +00:00
FP_GameWaypoint const *waypoint = &waypoint_link->data;
FP_GameEntity *waypoint_entity = FP_Game_GetEntity(game, waypoint_link->data.entity);
if (FP_Game_IsNilEntity(waypoint_entity)) {
FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->chunk_pool);
continue;
2023-09-16 14:37:26 +00:00
}
// NOTE: We found a waypoint that is valid to move towards
2023-09-29 04:50:40 +00:00
Dqn_V2 target_pos = FP_Game_CalcWaypointWorldPos(game, waypoint);
Dqn_V2 entity_to_waypoint = target_pos - entity_pos;
// NOTE: Check if we've arrived at the waypoint
Dqn_f32 dist_to_waypoint_sq = Dqn_V2_LengthSq(entity_to_waypoint);
Dqn_f32 arrival_threshold = {};
2023-09-29 04:50:40 +00:00
switch (waypoint->arrive) {
case FP_GameWaypointArrive_Default: {
arrival_threshold = FP_Game_MetersToPixelsNx1(game, 0.5f);
} break;
case FP_GameWaypointArrive_WhenWithinEntitySize: {
2023-09-29 04:50:40 +00:00
arrival_threshold = DQN_MAX(waypoint_entity->local_hit_box_size.w, waypoint_entity->local_hit_box_size.h) * waypoint_link->data.value;
} break;
}
// NOTE: We haven't arrived yet, calculate an acceleration vector to the waypoint
if (dist_to_waypoint_sq > DQN_SQUARED(arrival_threshold)) {
Dqn_V2 entity_to_waypoint_norm = Dqn_V2_Normalise(entity_to_waypoint);
2023-09-30 06:51:59 +00:00
acceleration_meters_per_s = entity_to_waypoint_norm * (entity->base_acceleration_per_s.meters * 0.25f);
break;
}
// NOTE: We have arrived at the waypoint
bool aggro_on_terry = (entity->flags & FP_GameEntityFlag_AggrosWhenNearTerry) && waypoint_entity->type == FP_EntityType_Terry;
bool building_response = (entity->flags & FP_GameEntityFlag_RespondsToBuildings) &&
waypoint_entity->type == FP_EntityType_ClubTerry ||
waypoint_entity->type == FP_EntityType_AirportTerry ||
waypoint_entity->type == FP_EntityType_ChurchTerry;
bool heart_response = (entity->flags & FP_GameEntityFlag_PointOfInterestHeart) && waypoint_entity->type == FP_EntityType_Heart;
if (((waypoint->flags & FP_GameWaypointFlag_NonInterruptible) == 0) && (aggro_on_terry || building_response || heart_response)) {
bool can_attack = !entity->is_dying; // TODO(doyle): State transition needs to check if it's valid to move to making this not necessary
2023-09-30 06:51:59 +00:00
if (building_response) {
FP_GameEntity *building = waypoint_entity;
if (FP_Game_IsNilEntityHandle(game, building->building_patron)) {
building->building_patron = entity->handle;
if (building->type == FP_EntityType_ClubTerry) {
FP_Game_EntityTransitionState(game, building, FP_EntityClubTerryState_PartyTime);
} else if (building->type == FP_EntityType_AirportTerry) {
FP_Game_EntityTransitionState(game, building, FP_EntityAirportTerryState_FlyPassenger);
} else {
DQN_ASSERT(building->type == FP_EntityType_ChurchTerry);
FP_Game_EntityTransitionState(game, building, FP_EntityChurchTerryState_ConvertPatron);
}
2023-09-30 06:51:59 +00:00
entity->flags |= FP_GameEntityFlag_OccupiedInBuilding;
Dqn_Rect hit_box = FP_Game_CalcEntityWorldHitBox(game, building->handle);
2023-09-30 06:51:59 +00:00
Dqn_V2 exit_pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(0.5f, 1.1f));
entity->local_pos = exit_pos; // TODO(doyle): Only works when parent world pos is 0,0
can_attack = false;
FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->chunk_pool);
FP_SentinelList_Add(&entity->buildings_visited, game->chunk_pool, building->handle);
2023-09-30 06:51:59 +00:00
}
}
if (can_attack) {
Dqn_V2 attack_dir_vectors[FP_GameDirection_Count] = {};
attack_dir_vectors[FP_GameDirection_Up] = Dqn_V2_InitNx2(+0, -1);
attack_dir_vectors[FP_GameDirection_Down] = Dqn_V2_InitNx2(+0, +1);
attack_dir_vectors[FP_GameDirection_Left] = Dqn_V2_InitNx2(-1, +0);
attack_dir_vectors[FP_GameDirection_Right] = Dqn_V2_InitNx2(+1, +0);
Dqn_V2 defender_entity_pos = FP_Game_CalcEntityWorldPos(game, waypoint_entity->handle);
Dqn_V2 entity_to_defender = defender_entity_pos - entity_pos;
Dqn_V2 entity_to_defender_norm = Dqn_V2_Normalise(entity_to_defender);
FP_GameDirection best_attack_dir = FP_GameDirection_Up;
Dqn_f32 best_attack_dir_scalar_projection_onto_entity_to_waypoint_vector = -1.1f;
DQN_FOR_UINDEX (dir_index, FP_GameDirection_Count) {
Dqn_V2 attack_dir = attack_dir_vectors[dir_index];
Dqn_f32 scalar_projection = Dqn_V2_Dot(attack_dir, entity_to_defender_norm);
if (scalar_projection > best_attack_dir_scalar_projection_onto_entity_to_waypoint_vector) {
best_attack_dir = DQN_CAST(FP_GameDirection)dir_index;
best_attack_dir_scalar_projection_onto_entity_to_waypoint_vector = scalar_projection;
}
}
switch (entity->type) {
case FP_EntityType_Terry: /*FALLTHRU*/
case FP_EntityType_Smoochie: /*FALLTHRU*/
2023-10-04 12:50:31 +00:00
case FP_EntityType_Catfish: /*FALLTHRU*/
case FP_EntityType_Clinger: {
// TODO(doyle): We should check if it's valid to enter this new state
// from the entity's current state
if (entity->type == FP_EntityType_Terry) {
2023-10-04 12:50:31 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack);
} else if (entity->type == FP_EntityType_Smoochie) {
2023-10-04 12:50:31 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack);
} else if (entity->type == FP_EntityType_Catfish) {
FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack);
} else {
DQN_ASSERT(entity->type == FP_EntityType_Clinger);
2023-10-04 12:50:31 +00:00
FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack);
}
entity->direction = best_attack_dir;
} break;
case FP_EntityType_Nil: break;
case FP_EntityType_ClubTerry: break;
case FP_EntityType_Map: break;
case FP_EntityType_Heart: break;
case FP_EntityType_MerchantTerry: break;
case FP_EntityType_MerchantGraveyard: break;
case FP_EntityType_MerchantGym: break;
case FP_EntityType_MerchantPhoneCompany: break;
case FP_EntityType_AirportTerry: break;
case FP_EntityType_ChurchTerry: break;
case FP_EntityType_KennelTerry: break;
case FP_EntityType_PhoneMessageProjectile: break;
case FP_EntityType_Count: DQN_INVALID_CODE_PATH; break;
}
}
// NOTE: Aggro makes the entity attack Terry, we will
// exit here preserving the waypoint in the entity.
break;
} else {
FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->chunk_pool);
}
2023-09-16 14:37:26 +00:00
}
}
2023-09-29 05:28:11 +00:00
// NOTE: Move entity by mouse ==============================================================
if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
entity->velocity = {};
acceleration_meters_per_s = {};
2023-10-05 10:10:50 +00:00
entity->local_pos += input->mouse_p_delta;
2023-09-29 05:28:11 +00:00
}
2023-09-30 09:14:35 +00:00
if (game->clicked_entity == entity->handle) {
if (game->active_menu == FP_GameActiveMenu_Nil || game->active_menu == FP_GameActiveMenu_Build) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_H))
game->active_menu = DQN_CAST(FP_GameActiveMenu)(DQN_CAST(uint32_t)game->active_menu ^ FP_GameActiveMenu_Build);
}
2023-10-02 11:38:36 +00:00
if (entity->flags & FP_GameEntityFlag_CameraTracking)
game->camera.world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle) - Dqn_V2_InitV2I(platform->core.window_size) * .5f;
2023-10-02 11:38:36 +00:00
game->build_mode_can_place_building = false;
if (game->active_menu == FP_GameActiveMenu_Build) {
2023-10-05 12:09:39 +00:00
FP_GamePlaceableBuilding placeable_building = PLACEABLE_BUILDINGS[game->build_mode_building_index];
Dqn_Rect dest_rect = FP_Game_GetBuildingPlacementRectForEntity(game, placeable_building, entity->handle);
Dqn_V2 placement_pos = Dqn_Rect_Center(dest_rect);
for (FP_GameEntityIterator zone_it = {};
2023-10-05 12:09:39 +00:00
FP_Game_DFSPreOrderWalkEntityTree(game, &zone_it, game->root_entity);
) {
2023-10-05 12:09:39 +00:00
FP_GameEntity *zone = zone_it.entity;
bool is_building = zone->type == FP_EntityType_KennelTerry ||
zone->type == FP_EntityType_AirportTerry ||
zone->type == FP_EntityType_ChurchTerry ||
zone->type == FP_EntityType_ClubTerry;
Dqn_Rect zone_hit_box = FP_Game_CalcEntityWorldHitBox(game, zone->handle);
if (is_building) {
if (Dqn_Rect_Intersects(zone_hit_box, dest_rect)) {
game->build_mode_can_place_building = false;
break;
}
}
if ((zone->flags & FP_GameEntityFlag_BuildZone) == 0)
continue;
zone_hit_box.pos += dest_rect.size * .5f;
zone_hit_box.size -= dest_rect.size;
zone_hit_box.size = Dqn_V2_Max(zone_hit_box.size, Dqn_V2_Zero);
2023-10-05 12:09:39 +00:00
game->build_mode_can_place_building |= Dqn_Rect_ContainsPoint(zone_hit_box, placement_pos);
}
if (game->build_mode_can_place_building &&
2023-10-05 12:09:39 +00:00
TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_F)) {
if (placeable_building.type == FP_EntityType_ClubTerry) {
FP_Entity_CreateClubTerry(game, placement_pos, "Club Terry");
} else if (placeable_building.type == FP_EntityType_ChurchTerry) {
FP_Entity_CreateChurchTerry(game, placement_pos, "Church Terry");
} else if (placeable_building.type == FP_EntityType_AirportTerry) {
FP_Entity_CreateAirportTerry(game, placement_pos, "Airport Terry");
} else {
DQN_ASSERT(placeable_building.type == FP_EntityType_KennelTerry);
FP_Entity_CreateKennelTerry(game, placement_pos, "Kennel Terry");
}
2023-10-02 11:38:36 +00:00
}
}
2023-09-30 09:14:35 +00:00
}
// NOTE: Tick the state machine
// NOTE: This can delete the entity! Take caution
FP_GameEntityHandle entity_handle = entity->handle;
FP_EntityActionStateMachine(game, &platform->audio, input, entity, &acceleration_meters_per_s);
// NOTE: Core equations of motion ==========================================================
FP_Game_MoveEntity(game, entity_handle, acceleration_meters_per_s);
2023-09-29 05:28:11 +00:00
}
2023-09-16 02:21:24 +00:00
2023-09-29 05:28:11 +00:00
// NOTE: Update entity =========================================================================
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
2023-10-05 10:10:50 +00:00
if (entity->flags & FP_GameEntityFlag_TTL) {
if (game->clock_ms >= entity->ttl_end_timestamp) {
FP_Game_DeleteEntity(game, entity->handle);
continue;
}
}
if (entity->type == FP_EntityType_Terry) {
entity->terry_mobile_data_plan =
2023-10-06 10:57:34 +00:00
DQN_MIN(entity->terry_mobile_data_plan + DQN_CAST(Dqn_usize)(FP_TERRY_MOBILE_DATA_PER_RANGE_ATTACK * .25f * PHYSICS_STEP),
entity->terry_mobile_data_plan_cap);
}
2023-09-29 05:28:11 +00:00
// NOTE: Derive dynmamic bounding boxes ====================================================
2023-09-23 02:33:59 +00:00
if (entity->flags & FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox) {
2023-09-16 02:21:24 +00:00
Dqn_Rect children_bbox = {};
// TODO(doyle): Is the hit box supposed to include the containing
// entity itself? Not sure
2023-09-17 10:24:07 +00:00
children_bbox.pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
2023-09-16 02:21:24 +00:00
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator child_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &child_it, entity);) {
FP_GameEntity *child = child_it.entity;
2023-09-16 02:21:24 +00:00
DQN_ASSERT(child != entity);
2023-09-17 10:24:07 +00:00
Dqn_Rect bbox = FP_Game_CalcEntityWorldBoundingBox(game, child->handle);
2023-09-16 02:21:24 +00:00
children_bbox = Dqn_Rect_Union(children_bbox, bbox);
}
Dqn_Rect padded_bbox = Dqn_Rect_Expand(children_bbox, 16.f);
entity->local_hit_box_offset = padded_bbox.pos - entity->local_pos + (padded_bbox.size * .5f);
entity->local_hit_box_size = padded_bbox.size;
}
// NOTE: Mob spawner =======================================================================
if (entity->flags & FP_GameEntityFlag_MobSpawner) {
// NOTE: Flush any spawn entities that are dead
for (FP_SentinelListLink<FP_GameEntityHandle> *link = nullptr; FP_SentinelList_Iterate<FP_GameEntityHandle>(&entity->spawn_list, &link); ) {
FP_GameEntity *spawned_entity = FP_Game_GetEntity(game, link->data);
if (FP_Game_IsNilEntity(spawned_entity)) // NOTE: Entity is dead remove it from the linked list
link = FP_SentinelList_Erase(&entity->spawn_list, link, game->chunk_pool);
}
2023-10-01 21:42:23 +00:00
if (entity->enemies_spawned_this_wave >= entity->enemies_per_wave) {
// NOTE: If all enemies for the current wave have been spawned,
// wait for the cooldown period before starting next wave
if (input->timer_s >= entity->wave_cooldown_timestamp_s) {
entity->current_wave++;
entity->enemies_per_wave *= 2; // NOTE: Double the enemies for the next wave
entity->enemies_spawned_this_wave = 0;
}
} else if (entity->spawn_list.size < entity->spawn_cap) { // NOTE: Spawn new entities
if (input->timer_s >= entity->next_spawn_timestamp_s) {
uint64_t hp_adjustment = entity->current_wave - 1;
entity->next_spawn_timestamp_s = DQN_CAST(uint64_t)(input->timer_s + 5.f);
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
FP_SentinelListLink<FP_GameEntityHandle> *link = FP_SentinelList_Make(&entity->spawn_list, game->chunk_pool);
2023-10-04 12:50:31 +00:00
Dqn_f32 mob_choice = Dqn_PCG32_NextF32(&game->rng);
if (mob_choice <= 0.33f)
link->data = FP_Entity_CreateClinger(game, entity_world_pos, hp_adjustment, "Clinger");
2023-10-04 12:50:31 +00:00
else if (mob_choice <= 0.66f)
link->data = FP_Entity_CreateSmoochie(game, entity_world_pos, hp_adjustment, "Smoochie");
2023-10-04 12:50:31 +00:00
else
link->data = FP_Entity_CreateCatfish(game, entity_world_pos, hp_adjustment, "Catfish");
// NOTE: Setup the mob with waypoints
2023-09-25 13:06:39 +00:00
FP_GameEntity *mob = FP_Game_GetEntity(game, link->data);
mob->waypoints = FP_SentinelList_Init<FP_GameWaypoint>(game->chunk_pool);
2023-09-25 13:06:39 +00:00
mob->flags |= FP_GameEntityFlag_AggrosWhenNearTerry;
mob->flags |= FP_GameEntityFlag_RespondsToBuildings;
for (FP_GameEntity *waypoint_entity = entity->first_child; waypoint_entity; waypoint_entity = waypoint_entity->next) {
if ((waypoint_entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) == 0)
continue;
// NOTE: Add the waypoint
FP_SentinelListLink<FP_GameWaypoint> *waypoint = FP_SentinelList_Make(&mob->waypoints, game->chunk_pool);
waypoint->data.entity = waypoint_entity->handle;
waypoint->data.arrive = FP_GameWaypointArrive_WhenWithinEntitySize;
waypoint->data.value = 1.5f;
}
2023-10-01 21:42:23 +00:00
entity->enemies_spawned_this_wave++;
// NOTE: If all enemies for the current wave have been spawned
// set the cooldown time for the next wave
if (entity->enemies_spawned_this_wave >= entity->enemies_per_wave) {
entity->wave_cooldown_timestamp_s = DQN_CAST(uint64_t)(input->timer_s + 30.f); // NOTE: 30s cooldown
}
}
}
}
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
// NOTE: Do attacks ============================================================================
{
Dqn_Profiler_ZoneScopeWithIndex("FP_Update: Attacks", FP_ProfileZone_FPUpdate_Attacks);
FP_GameEntity *attacker = entity;
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
// NOTE: Resolve attack boxes
if (!Dqn_V2_Area(attacker->attack_box_size))
continue;
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle);
Dqn_V2 attacker_world_pos = FP_Game_CalcEntityWorldPos(game, attacker->handle);
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->root_entity); ) {
FP_GameEntity *defender = defender_it.entity;
if (defender->handle == attacker->handle)
continue;
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
if ((defender->flags & FP_GameEntityFlag_Attackable) == 0)
continue;
2023-09-29 05:18:38 +00:00
bool permit_attack = true;
switch (attacker->type) {
case FP_EntityType_Smoochie: {
2023-10-04 12:50:31 +00:00
if (defender->type == FP_EntityType_Smoochie || defender->type == FP_EntityType_Clinger || defender->type == FP_EntityType_Catfish)
permit_attack = false;
} break;
case FP_EntityType_Catfish: {
if (defender->type == FP_EntityType_Smoochie || defender->type == FP_EntityType_Clinger || defender->type == FP_EntityType_Catfish)
permit_attack = false;
} break;
case FP_EntityType_Clinger: {
2023-10-04 12:50:31 +00:00
if (defender->type == FP_EntityType_Smoochie || defender->type == FP_EntityType_Clinger || defender->type == FP_EntityType_Catfish)
permit_attack = false;
} break;
2023-10-01 06:50:32 +00:00
case FP_EntityType_Nil: break;
case FP_EntityType_Terry: break;
case FP_EntityType_Count: break;
case FP_EntityType_ClubTerry: break;
case FP_EntityType_Map: break;
case FP_EntityType_MerchantTerry: break;
case FP_EntityType_MerchantGraveyard: break;
case FP_EntityType_MerchantGym: break;
case FP_EntityType_MerchantPhoneCompany: break;
case FP_EntityType_Heart: break;
2023-10-04 12:50:31 +00:00
case FP_EntityType_AirportTerry:
case FP_EntityType_ChurchTerry:
2023-10-05 10:10:50 +00:00
case FP_EntityType_KennelTerry: break;
case FP_EntityType_PhoneMessageProjectile: break;
}
if (!permit_attack)
continue;
2023-09-29 05:28:11 +00:00
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
if (!Dqn_Rect_Intersects(attacker_box, defender_box))
continue;
2023-09-16 07:32:25 +00:00
2023-09-29 05:28:11 +00:00
// NOTE: Do HP =========================================================================
defender->hp -= 1;
2023-10-06 10:57:34 +00:00
if (defender->hp <= 0) {
2023-09-29 05:28:11 +00:00
defender->is_dying = true;
2023-10-06 10:57:34 +00:00
FP_GameEntity *coin_receiver = FP_Game_GetEntity(game, attacker->projectile_owner);
if (FP_Game_IsNilEntity(coin_receiver))
coin_receiver = attacker;
coin_receiver->coins += 1;
}
2023-09-29 05:28:11 +00:00
// NOTE: Kickback ======================================================================
2023-10-05 10:10:50 +00:00
#if 0
2023-09-29 05:28:11 +00:00
Dqn_V2 defender_world_pos = Dqn_Rect_Center(defender_box);
Dqn_V2 attack_dir_vector = {};
if (attacker_world_pos.x < defender_world_pos.x)
attack_dir_vector.x = 1.f;
else
attack_dir_vector.x = -1.f;
2023-09-29 05:58:03 +00:00
Dqn_V2 attack_acceleration_meters_per_s = attack_dir_vector * 60.f;
FP_Game_MoveEntity(game, defender->handle, attack_acceleration_meters_per_s);
2023-10-05 10:10:50 +00:00
#endif
2023-09-29 05:28:11 +00:00
}
2023-09-16 07:32:25 +00:00
}
}
2023-09-30 13:24:04 +00:00
if (!FP_Game_IsNilEntityHandle(game, game->clicked_entity)) {
TELY_AssetSpriteAnimation *sprite_anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.map);
Dqn_Rect sprite_rect = game->atlas_sprite_sheet.rects.data[sprite_anim->index];
const Dqn_usize target_width = 1800;
const Dqn_usize target_height = 1046;
2023-09-30 13:24:04 +00:00
game->camera.world_pos.x = DQN_MIN(game->camera.world_pos.x, game->map->local_hit_box_size.w * +0.5f - target_width);
game->camera.world_pos.x = DQN_MAX(game->camera.world_pos.x, game->map->local_hit_box_size.w * -0.5f);
game->camera.world_pos.y = DQN_MAX(game->camera.world_pos.y, game->map->local_hit_box_size.h * -0.5f);
game->camera.world_pos.y = DQN_MIN(game->camera.world_pos.y, game->map->local_hit_box_size.h * +0.5f - target_height);
}
2023-09-29 05:28:11 +00:00
Dqn_Profiler_EndZone(update_zone);
2023-09-16 07:32:25 +00:00
}
2023-09-23 02:33:59 +00:00
void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer)
2023-09-16 07:32:25 +00:00
{
Dqn_Profiler_ZoneScopeWithIndex("FP_Render", FP_ProfileZone_FPRender);
2023-09-16 07:32:25 +00:00
TELY_PlatformInput *input = &platform->input;
2023-09-17 10:24:07 +00:00
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
2023-10-01 03:38:31 +00:00
Dqn_V2 world_mouse_p = input->mouse_p + game->camera.world_pos;
2023-09-16 07:32:25 +00:00
2023-09-16 09:06:16 +00:00
// NOTE: Draw tiles ============================================================================
2023-09-23 02:33:59 +00:00
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / game->tile_size);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / game->tile_size);
2023-09-16 09:06:16 +00:00
for (Dqn_usize x = 0; x < tile_count_x; x++) {
2023-09-23 02:33:59 +00:00
Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * game->tile_size, 0);
2023-09-16 09:06:16 +00:00
Dqn_V2 end = Dqn_V2_InitNx2(start.x, platform->core.window_size.h);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
}
for (Dqn_usize y = 0; y < tile_count_y; y++) {
2023-09-23 02:33:59 +00:00
Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * game->tile_size);
2023-09-16 09:06:16 +00:00
Dqn_V2 end = Dqn_V2_InitNx2(platform->core.window_size.w, start.y);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
}
2023-10-02 12:41:08 +00:00
TELY_Render_PushColourV4(renderer, TELY_COLOUR_BLACK_V4);
2023-09-16 09:06:16 +00:00
// NOTE: Draw entities =========================================================================
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
if (entity->flags & FP_GameEntityFlag_OccupiedInBuilding)
2023-09-30 06:51:59 +00:00
continue;
2023-09-16 02:21:24 +00:00
// NOTE: Render shapes in entity ===========================================================
2023-09-30 07:49:49 +00:00
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
2023-10-01 05:20:05 +00:00
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
2023-09-17 10:24:07 +00:00
for (FP_GameShape const &shape_ : entity->shapes) {
FP_GameShape const *shape = &shape_;
2023-09-16 02:21:24 +00:00
Dqn_V2 local_to_world_p1 = world_pos + shape->p1;
Dqn_V2 local_to_world_p2 = world_pos + shape->p2;
switch (shape->type) {
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_None: {
2023-09-16 02:21:24 +00:00
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Circle: {
2023-09-16 02:21:24 +00:00
TELY_Render_CircleColourV4(renderer, local_to_world_p1, shape->circle_radius, shape->render_mode, shape->colour);
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Rect: {
2023-09-16 02:21:24 +00:00
Dqn_Rect rect = Dqn_Rect_InitV2x2(local_to_world_p1, local_to_world_p2 - local_to_world_p1);
rect.pos -= rect.size * .5f;
TELY_Render_RectColourV4(renderer, rect, shape->render_mode, shape->colour);
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Line: {
2023-09-16 02:21:24 +00:00
TELY_Render_LineColourV4(renderer, local_to_world_p1, local_to_world_p2, shape->colour, shape->line_thickness);
} break;
}
}
// NOTE: Render entity sprites =============================================================
2023-09-24 11:54:08 +00:00
if (entity->action.sprite.anim) {
FP_GameEntityAction const *action = &entity->action;
TELY_AssetAnimatedSprite const sprite = action->sprite;
uint64_t elapsed_ms = game->clock_ms - action->started_at_clock_ms;
uint16_t anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite.anim->ms_per_frame) % sprite.anim->count;
2023-09-16 02:21:24 +00:00
Dqn_usize sprite_index = sprite.anim->index + anim_frame;
Dqn_Rect src_rect = {};
switch (sprite.sheet->type) {
case TELY_AssetSpriteSheetType_Uniform: {
Dqn_usize sprite_sheet_row = sprite_index / sprite.sheet->sprites_per_row;
Dqn_usize sprite_sheet_column = sprite_index % sprite.sheet->sprites_per_row;
src_rect.pos.x = DQN_CAST(Dqn_f32)(sprite_sheet_column * sprite.sheet->sprite_size.w);
src_rect.pos.y = DQN_CAST(Dqn_f32)(sprite_sheet_row * sprite.sheet->sprite_size.y);
src_rect.size.w = DQN_CAST(Dqn_f32)sprite.sheet->sprite_size.w;
src_rect.size.h = DQN_CAST(Dqn_f32)sprite.sheet->sprite_size.h;
} break;
case TELY_AssetSpriteSheetType_Rects: {
DQN_ASSERT(sprite_index < sprite.sheet->rects.size);
src_rect = sprite.sheet->rects.data[sprite_index];
} break;
}
2023-09-16 02:21:24 +00:00
Dqn_f32 sprite_in_meters = FP_Game_PixelsToMetersNx1(game, src_rect.size.y);
2023-09-25 14:07:39 +00:00
Dqn_f32 size_scale = entity->sprite_height.meters / sprite_in_meters;
2023-09-16 02:21:24 +00:00
Dqn_Rect dest_rect = {};
2023-09-25 14:07:39 +00:00
dest_rect.size = src_rect.size * size_scale;
dest_rect.pos = world_pos - (dest_rect.size * .5f);
2023-09-16 02:21:24 +00:00
if (sprite.flip & TELY_AssetFlip_X)
2023-09-16 02:21:24 +00:00
dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally
if (sprite.flip & TELY_AssetFlip_Y)
dest_rect.size.h *= -1.f; // NOTE: Flip the texture vertically
2023-09-30 07:49:49 +00:00
TELY_Render_TextureColourV4(renderer,
sprite.sheet->tex_handle,
src_rect,
dest_rect,
Dqn_V2_Zero /*rotate origin*/,
2023-09-30 07:50:57 +00:00
0.f /*rotate radians*/,
2023-09-30 07:49:49 +00:00
TELY_COLOUR_WHITE_V4);
2023-09-16 02:21:24 +00:00
}
DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) {
FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index;
uint64_t elapsed_ms = game->clock_ms - sprite->started_at_clock_ms;
uint16_t raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite->asset.anim->ms_per_frame);
if (raw_anim_frame > sprite->asset.anim->count && !sprite->loop) {
anim_index = Dqn_FArray_EraseRange(&entity->extra_cosmetic_anims, anim_index, 1, Dqn_ArrayErase_Unstable).it_index;
continue;
}
uint16_t anim_frame = raw_anim_frame % sprite->asset.anim->count;
Dqn_usize sprite_index = sprite->asset.anim->index + anim_frame;
Dqn_Rect src_rect = {};
switch (sprite->asset.sheet->type) {
case TELY_AssetSpriteSheetType_Uniform: {
Dqn_usize sprite_sheet_row = sprite_index / sprite->asset.sheet->sprites_per_row;
Dqn_usize sprite_sheet_column = sprite_index % sprite->asset.sheet->sprites_per_row;
src_rect.pos.x = DQN_CAST(Dqn_f32)(sprite_sheet_column * sprite->asset.sheet->sprite_size.w);
src_rect.pos.y = DQN_CAST(Dqn_f32)(sprite_sheet_row * sprite->asset.sheet->sprite_size.y);
src_rect.size.w = DQN_CAST(Dqn_f32)sprite->asset.sheet->sprite_size.w;
src_rect.size.h = DQN_CAST(Dqn_f32)sprite->asset.sheet->sprite_size.h;
} break;
case TELY_AssetSpriteSheetType_Rects: {
DQN_ASSERT(sprite_index < sprite->asset.sheet->rects.size);
src_rect = sprite->asset.sheet->rects.data[sprite_index];
} break;
}
Dqn_f32 sprite_in_meters = FP_Game_PixelsToMetersNx1(game, src_rect.size.y);
Dqn_f32 size_scale = sprite->height.meters / sprite_in_meters;
Dqn_Rect dest_rect = {};
dest_rect.size = src_rect.size * size_scale;
dest_rect.pos = world_pos - (dest_rect.size * .5f) + sprite->offset;
if (sprite->asset.flip & TELY_AssetFlip_X)
dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally
if (sprite->asset.flip & TELY_AssetFlip_Y)
dest_rect.size.h *= -1.f; // NOTE: Flip the texture vertically
2023-09-30 07:49:49 +00:00
TELY_Render_TextureColourV4(renderer,
sprite->asset.sheet->tex_handle,
src_rect,
dest_rect,
Dqn_V2_Zero /*rotate origin*/,
0.f /*rotation*/,
TELY_COLOUR_WHITE_V4);
}
if (entity->type == FP_EntityType_ClubTerry ||
entity->type == FP_EntityType_AirportTerry ||
entity->type == FP_EntityType_ChurchTerry) {
FP_GameEntityAction const *action = &entity->action;
bool draw_timer = entity->type == FP_EntityType_ClubTerry && action->state == FP_EntityClubTerryState_PartyTime ||
entity->type == FP_EntityType_AirportTerry && action->state == FP_EntityAirportTerryState_FlyPassenger ||
entity->type == FP_EntityType_ChurchTerry && action->state == FP_EntityChurchTerryState_ConvertPatron;
if (draw_timer) {
Dqn_f32 duration = action->end_at_clock_ms - DQN_CAST(Dqn_f32)action->started_at_clock_ms;
Dqn_f32 elapsed = DQN_CAST(Dqn_f32)(game->clock_ms - action->started_at_clock_ms);
2023-09-30 07:49:49 +00:00
Dqn_f32 t01 = DQN_MIN(1.f, elapsed / duration);
Dqn_Rect rect = {};
rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, -0.3f));
rect.size = Dqn_V2_InitNx2(world_hit_box.size.w * t01, 16.f);
TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, 1.0f));
}
}
if (game->active_menu == FP_GameActiveMenu_Build) {
2023-10-05 12:09:39 +00:00
if (entity->flags & FP_GameEntityFlag_BuildZone)
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLUE_CADET_V4, 0.5f));
}
2023-10-05 10:30:56 +00:00
if (game->debug_ui) {
2023-10-05 12:09:39 +00:00
// NOTE: Render waypoint entities ======================================================
2023-10-05 10:30:56 +00:00
if (entity->flags & FP_GameEntityFlag_MobSpawner) {
Dqn_V2 start = world_pos;
for (FP_GameEntity *waypoint_entity = entity->first_child; waypoint_entity; waypoint_entity = waypoint_entity->next) {
if ((waypoint_entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) == 0)
continue;
2023-10-05 10:30:56 +00:00
Dqn_V2 end = FP_Game_CalcEntityWorldPos(game, waypoint_entity->handle);
TELY_Render_LineColourV4(renderer, start, end, TELY_COLOUR_BLUE_CADET_V4, 2.f);
start = end;
}
}
2023-10-05 10:30:56 +00:00
if (entity->flags & FP_GameEntityFlag_MobSpawner)
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_BLUE_CADET_V4);
2023-09-16 07:32:25 +00:00
2023-10-05 10:30:56 +00:00
if (entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint)
TELY_Render_CircleColourV4(renderer, world_pos, 16.f /*radius*/, TELY_RenderShapeMode_Line, TELY_COLOUR_BLUE_CADET_V4);
if (entity->flags & FP_GameEntityFlag_BuildZone) {
{
Dqn_V2 line_p1 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, 0));
Dqn_V2 line_p2 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(1, 1));
TELY_Render_LineColourV4(renderer, line_p1, line_p2, TELY_COLOUR_BLACK_V4, 2.f);
}
{
Dqn_V2 line_p1 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, 1));
Dqn_V2 line_p2 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(1, 0));
TELY_Render_LineColourV4(renderer, line_p1, line_p2, TELY_COLOUR_BLACK_V4, 2.f);
}
}
// NOTE: Render attack box =================================================================
{
Dqn_Rect attack_box = FP_Game_CalcEntityAttackWorldHitBox(game, entity->handle);
TELY_Render_RectColourV4(renderer, attack_box, TELY_RenderShapeMode_Line, TELY_COLOUR_RED_TOMATO_V4);
}
// NOTE: Render world position =============================================================
TELY_Render_CircleColourV4(renderer, world_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4);
// NOTE: Render hot/active entity ==========================================================
if (game->clicked_entity == entity->handle) {
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
// NOTE: Draw the waypoints that the entity is moving along
Dqn_V2 start = world_pos;
for (FP_SentinelListLink<FP_GameWaypoint> *link = nullptr; FP_SentinelList_Iterate(&entity->waypoints, &link); ) {
Dqn_V2 end = FP_Game_CalcWaypointWorldPos(game, &link->data);
TELY_Render_LineColourV4(renderer, start, end, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4, 2.f);
start = end;
}
} else if (game->hot_entity == entity->handle || (entity->flags & FP_GameEntityFlag_DrawHitBox)) {
Dqn_V4 hot_colour = game->hot_entity == entity->handle ? TELY_COLOUR_RED_TOMATO_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_YELLOW_SANDY_V4, .5f);
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, hot_colour);
}
if (game->hot_entity == entity->handle) {
if (entity->name.size) {
Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / game->tile_size, world_pos.y / game->tile_size);
Dqn_f32 line_height = TELY_Render_FontHeight(renderer, &platform->assets);
Dqn_V2 draw_p = world_mouse_p;
TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "%.*s", DQN_STRING_FMT(entity->name)); draw_p.y += line_height;
TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "World Pos: (%.1f, %.1f)", world_pos.x, world_pos.y); draw_p.y += line_height;
TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "Hit Box Size: %.1fx%.1f", world_hit_box.size.x, world_hit_box.size.y); draw_p.y += line_height;
TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "Tile: %I32dx%I32d", player_tile.x, player_tile.y); draw_p.y += line_height;
TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "World Mouse Pos: (%.1f, %.1f)", world_mouse_p.x, world_mouse_p.y); draw_p.y += line_height;
}
}
}
}
2023-09-16 02:21:24 +00:00
// NOTE: Render the merchant menus =========================================
FP_GameEntity *player = FP_Game_GetEntity(game, game->player);
Dqn_V2 player_pos = FP_Game_CalcEntityWorldPos(game, game->player);
{
struct FP_MerchantToMenuMapping {
FP_GameEntityHandle merchant;
Dqn_String8 menu_anim;
} merchants[] = {
{game->merchant_terry, g_anim_names.merchant_terry_menu},
{game->merchant_graveyard, g_anim_names.merchant_graveyard_menu},
{game->merchant_gym, g_anim_names.merchant_gym_menu},
{game->merchant_phone_company, g_anim_names.merchant_phone_company_menu},
};
bool activated_merchant = false;
for (FP_MerchantToMenuMapping mapping : merchants) {
FP_GameEntityHandle merchant_handle = mapping.merchant;
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, merchant_handle);
Dqn_f32 dist_squared = Dqn_V2_LengthSq_V2x2(world_pos, player_pos);
if (dist_squared > DQN_SQUARED(FP_Game_MetersToPixelsNx1(game, 4)))
continue;
activated_merchant = true;
// NOTE: Render animated merchant menu
Dqn_Rect merchant_menu_rect = {};
{
FP_GameRenderSprite *sprite = &game->player_merchant_menu;
if (!sprite->asset.anim || sprite->asset.anim->label != mapping.menu_anim) {
sprite->asset = TELY_Asset_MakeAnimatedSprite(&game->atlas_sprite_sheet, mapping.menu_anim, TELY_AssetFlip_No);
sprite->started_at_clock_ms = game->clock_ms;
}
bool trigger_buy_anim = false;
uint64_t buy_duration_ms = 500;
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) {
game->player_trigger_purchase_timestamp = game->clock_ms + buy_duration_ms;
} else if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_J)) {
trigger_buy_anim = true;
if (game->clock_ms > game->player_trigger_purchase_timestamp) {
// TODO(doyle): Do buy logic
}
}
uint64_t elapsed_ms = game->clock_ms - sprite->started_at_clock_ms;
uint16_t raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite->asset.anim->ms_per_frame);
uint16_t anim_frame = raw_anim_frame % sprite->asset.anim->count;
Dqn_usize sprite_index = sprite->asset.anim->index + anim_frame;
Dqn_Rect src_rect = sprite->asset.sheet->rects.data[sprite_index];
merchant_menu_rect.size = src_rect.size;
merchant_menu_rect.pos = world_pos - (merchant_menu_rect.size * .5f) - Dqn_V2_InitNx2(0.f, src_rect.size.y);
Dqn_f32 sin_t = DQN_SINF(DQN_CAST(Dqn_f32)input->timer_s * 3.f);
merchant_menu_rect.pos.y += sin_t * 4.f;
TELY_Render_TextureColourV4(renderer,
sprite->asset.sheet->tex_handle,
src_rect,
merchant_menu_rect,
Dqn_V2_Zero /*rotate origin*/,
0.f /*rotation*/,
TELY_COLOUR_WHITE_V4);
if (trigger_buy_anim) {
uint64_t start_buy_time = game->player_trigger_purchase_timestamp - buy_duration_ms;
uint64_t elapsed_time = game->clock_ms - start_buy_time;
Dqn_f32 buy_t = DQN_MIN(elapsed_time / DQN_CAST(Dqn_f32)buy_duration_ms, 1.f);
Dqn_Rect buy_lerp_rect = {};
buy_lerp_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.68f, 0.215f));
buy_lerp_rect.size.w = (merchant_menu_rect.size.w * 0.211f) * buy_t;
buy_lerp_rect.size.h = merchant_menu_rect.size.h * .611f;
TELY_Render_RectColourV4(renderer, buy_lerp_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, 0.5f));
}
}
// NOTE: Render the merchant button
TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.merchant_button_a);
Dqn_Rect button_rect = game->atlas_sprite_sheet.rects.data[anim->index];
Dqn_Rect dest_rect = {};
dest_rect.size = button_rect.size * 1.5f;
dest_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.75f, 0.5f));
TELY_Render_TextureColourV4(renderer,
game->atlas_sprite_sheet.tex_handle,
button_rect,
dest_rect,
Dqn_V2_Zero /*rotate origin*/,
0.f /*rotation*/,
TELY_COLOUR_WHITE_V4);
TELY_Render_PushColourV4(renderer, TELY_COLOUR_WHITE_V4);
TELY_Render_PushFont(renderer, game->talkco_font_large);
TELY_Render_TextF(renderer, Dqn_Rect_InterpolatedPoint(dest_rect, Dqn_V2_InitNx2(0.5f, -1)), Dqn_V2_InitNx2(0.5, 0.f), "$2");
TELY_Render_PopFont(renderer);
TELY_Render_PopColourV4(renderer);
}
if (activated_merchant) {
game->active_menu = FP_GameActiveMenu_Merchant;
} else {
if (game->active_menu == FP_GameActiveMenu_Merchant) {
game->active_menu = FP_GameActiveMenu_Nil;
}
}
}
// NOTE: Render player avatar HUD ==========================================
Dqn_f32 ui_start_y = 32.f;
{
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); };
FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, FP_EntityType_Terry, FP_EntityTerryState_Idle, FP_GameDirection_Down);
Dqn_Rect dest = {};
dest.size = render_data.render_size;
dest.pos.y = ui_start_y;
dest.pos.x = ui_start_y;
TELY_Render_TextureColourV4(renderer,
render_data.sheet->tex_handle,
render_data.sheet_rect,
dest,
Dqn_V2_Zero,
0.f,
TELY_COLOUR_WHITE_V4);
TELY_Render_PushFont(renderer, game->talkco_font);
Dqn_V2 next_pos = Dqn_Rect_InterpolatedPoint(dest, Dqn_V2_InitNx2(1.f, 0));
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "Terry");
Dqn_f32 font_height = TELY_Render_FontHeight(renderer, &platform->assets);
next_pos.y += font_height;
TELY_Render_TextF(renderer,
next_pos,
Dqn_V2_Zero,
"%$$$d/%$$$d",
player->terry_mobile_data_plan,
player->terry_mobile_data_plan_cap);
2023-10-06 10:57:34 +00:00
next_pos.y += font_height;
2023-10-06 10:57:34 +00:00
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "$%I64u", player->coins);
next_pos.y += font_height;
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[H] Build Menu");
next_pos.y += font_height;
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[Shift+WASD] Strafe");
next_pos.y += font_height;
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[Ctrl+WASD] Dash");
next_pos.y += font_height;
TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[J|K] Melee/Range");
TELY_Render_PopFont(renderer);
2023-10-05 12:09:39 +00:00
}
if (!FP_Game_IsNilEntityHandle(game, game->clicked_entity) && game->active_menu == FP_GameActiveMenu_Build) {
2023-10-02 11:38:36 +00:00
// NOTE: Render building blueprint =========================================================
{
FP_GameEntity *entity = FP_Game_GetEntity(game, game->clicked_entity);
2023-10-05 12:09:39 +00:00
FP_GamePlaceableBuilding placeable_building = PLACEABLE_BUILDINGS[game->build_mode_building_index];
FP_EntityRenderData club_terry_render_data = FP_Entity_GetRenderData(game, placeable_building.type, placeable_building.state, entity->direction);
Dqn_Rect dest_rect = FP_Game_GetBuildingPlacementRectForEntity(game, placeable_building, entity->handle);
2023-10-02 11:38:36 +00:00
Dqn_V4 colour = game->build_mode_can_place_building ?
TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.5f) :
TELY_Colour_V4Alpha(TELY_COLOUR_RED_V4, 0.5f);
2023-10-02 11:38:36 +00:00
TELY_Render_RectColourV4(renderer, dest_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLUE_CADET_V4, 0.5f));
TELY_Render_TextureColourV4(renderer,
club_terry_render_data.sheet->tex_handle,
club_terry_render_data.sheet_rect,
dest_rect,
Dqn_V2_Zero /*rotate origin*/,
0.f /*rotation*/,
colour);
2023-10-02 11:38:36 +00:00
}
2023-10-05 12:09:39 +00:00
// NOTE: Render the building selector UI ===================================================
{
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); };
game->build_mode_building_index = DQN_CLAMP(game->build_mode_building_index, 0, DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) - 1);
Dqn_f32 building_ui_size = 150.f;
Dqn_f32 padding = 10.f;
Dqn_f32 total_size = DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) * building_ui_size + ((DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) - 1) * padding);
Dqn_f32 start_x = (platform->core.window_size.x * .5f) - (total_size * .5f);
DQN_FOR_UINDEX (building_index, DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS)) {
FP_GamePlaceableBuilding building = PLACEABLE_BUILDINGS[building_index];
FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, building.type, building.state, FP_GameDirection_Down);
Dqn_Rect rect = Dqn_Rect_InitNx4(start_x + (building_index * building_ui_size) + (padding * building_index),
ui_start_y,
building_ui_size,
building_ui_size);
2023-10-05 12:09:39 +00:00
Dqn_V4 texture_colour = TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .5f);
Dqn_V4 outline_colour = TELY_COLOUR_WHITE_PALE_GOLDENROD_V4;
if (game->build_mode_building_index == building_index) {
outline_colour = TELY_COLOUR_RED_TOMATO_V4;
texture_colour.a = 1.f;
}
TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.5f));
TELY_Render_TextureColourV4(renderer,
render_data.sheet->tex_handle,
render_data.sheet_rect,
rect,
Dqn_V2_Zero /*rotate origin*/,
0.f /*rotation*/,
texture_colour);
TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Line, outline_colour);
cmd->thickness = 2.f;
}
}
2023-09-16 02:21:24 +00:00
}
// NOTE: Add scanlines into the game for A E S T H E T I C S
Dqn_V2 screen_size = Dqn_V2_InitNx2(platform->core.window_size.w, platform->core.window_size.h);
Dqn_f32 scanline_gap = 4.0f;
Dqn_f32 scanline_thickness = 3.0f;
FP_GameRenderCameraFollowScanlines(renderer, screen_size, game->camera.world_pos, scanline_gap, scanline_thickness);
2023-09-16 07:32:25 +00:00
}
extern "C" __declspec(dllexport)
void TELY_DLL_FrameUpdate(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *) user_data;
TELY_PlatformInput *input = &platform->input;
TELY_Assets *assets = &platform->assets;
TELY_Renderer *renderer = &platform->renderer;
2023-09-17 10:24:07 +00:00
FP_Game *game = DQN_CAST(FP_Game *) platform->user_data;
2023-09-20 13:35:38 +00:00
TELY_RFui *rfui = &game->rfui;
2023-09-16 07:32:25 +00:00
TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb);
2023-09-17 10:24:07 +00:00
TELY_Render_PushFont(renderer, game->jetbrains_mono_font);
2023-09-16 07:32:25 +00:00
2023-09-20 13:35:38 +00:00
TELY_RFui_FrameSetup(rfui, &platform->frame_arena);
TELY_RFui_PushFont(rfui, game->jetbrains_mono_font);
TELY_RFui_PushLabelColourV4(rfui, TELY_COLOUR_BLACK_MIDNIGHT_V4);
2023-09-16 07:32:25 +00:00
// =============================================================================================
game->prev_clicked_entity = game->clicked_entity;
game->prev_hot_entity = game->hot_entity;
game->prev_active_entity = game->active_entity;
game->hot_entity = {};
game->active_entity = {};
Dqn_FArray_Clear(&game->parent_entity_stack);
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
2023-09-17 10:24:07 +00:00
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
TELY_Render_PushTransform(renderer, model_view);
2023-10-01 03:38:31 +00:00
Dqn_V2 world_mouse_p = input->mouse_p + game->camera.world_pos;
2023-09-16 07:32:25 +00:00
// =============================================================================================
TELY_Audio *audio = &platform->audio;
2023-09-30 09:09:15 +00:00
2023-09-30 10:14:42 +00:00
#if 0
2023-09-16 07:32:25 +00:00
if (audio->playback_size == 0) {
2023-09-30 09:09:15 +00:00
TELY_Audio_Play(audio, game->audio[FP_GameAudio_TestAudio], 1.f /*volume*/);
2023-09-16 07:32:25 +00:00
}
2023-09-30 10:14:42 +00:00
#endif
2023-09-16 07:32:25 +00:00
// =============================================================================================
if (TELY_Platform_InputKeyWasDown(input->mouse_left) && TELY_Platform_InputKeyIsDown(input->mouse_left)) {
if (game->prev_active_entity.id)
game->active_entity = game->prev_active_entity;
} else {
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
2023-09-16 07:32:25 +00:00
if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0)
continue;
2023-09-23 02:33:59 +00:00
if ((entity->flags & FP_GameEntityFlag_Clickable) == 0)
2023-09-16 07:32:25 +00:00
continue;
2023-09-17 10:24:07 +00:00
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
2023-09-16 07:32:25 +00:00
if (!Dqn_Rect_ContainsPoint(world_hit_box, world_mouse_p))
continue;
game->hot_entity = entity->handle;
if (TELY_Platform_InputKeyIsPressed(input->mouse_left)) {
game->active_entity = entity->handle;
game->clicked_entity = entity->handle;
}
}
}
2023-09-16 02:21:24 +00:00
for (game->delta_s_accumulator += DQN_CAST(Dqn_f32)input->delta_s;
2023-09-23 07:26:18 +00:00
game->delta_s_accumulator > PHYSICS_STEP;
game->delta_s_accumulator -= PHYSICS_STEP) {
FP_Update(platform, game, input);
2023-09-23 07:26:18 +00:00
}
2023-09-23 02:33:59 +00:00
FP_Render(game, platform, renderer);
// NOTE: UI ====================================================================================
2023-10-02 11:38:36 +00:00
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); };
2023-10-05 10:30:56 +00:00
if (game->debug_ui) {
// NOTE: Info bar ==========================================================================
{
TELY_RFuiResult info_bar = TELY_RFui_Row(rfui, DQN_STRING8("Info Bar"));
info_bar.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute;
info_bar.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f;
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute;
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].value = 10.f;
TELY_RFui_PushParent(rfui, info_bar.widget);
DQN_DEFER { TELY_RFui_PopParent(rfui); };
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
TELY_RFui_TextF(rfui, "TELY");
if (Dqn_String8_IsValid(platform->core.os_name)) {
TELY_RFui_TextF(rfui, " | %.*s", DQN_STRING_FMT(platform->core.os_name));
}
2023-09-20 13:35:38 +00:00
TELY_RFui_TextF(rfui,
" | %dx%d %.1fHz | TSC %.1f GHz",
platform->core.window_size.w,
platform->core.window_size.h,
platform->core.display.refresh_rate,
platform->core.tsc_per_second / 1'000'000'000.0);
if (platform->core.ram_mb)
TELY_RFui_TextF(rfui, " | RAM %.1fGB", platform->core.ram_mb / 1024.0);
TELY_RFui_TextF(rfui,
" | Work %04.1fms/f (%04.1f%%) | %05.1f FPS | Frame %'I64u | Timer %.1fs",
input->work_ms,
input->work_ms * 100.0 / input->delta_ms,
1000.0 / input->delta_ms,
input->frame_counter,
input->timer_s);
2023-09-20 13:35:38 +00:00
}
// NOTE: Other
{
TELY_RFuiResult profiler_layout = TELY_RFui_Column(rfui, DQN_STRING8("Profiler Bar"));
profiler_layout.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute;
profiler_layout.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f;
profiler_layout.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute;
profiler_layout.widget->semantic_position[TELY_RFuiAxis_Y].value = TELY_Asset_GetFont(assets, TELY_RFui_ActiveFont(rfui))->pixel_height * 1.5f;
TELY_RFui_PushParent(rfui, profiler_layout.widget);
DQN_DEFER { TELY_RFui_PopParent(rfui); };
TELY_RFui_TextF(rfui, "Camera: %.1f, %.1f", game->camera.world_pos.x, game->camera.world_pos.y);
Dqn_ProfilerAnchor *anchors = Dqn_Profiler_AnchorBuffer(Dqn_ProfilerAnchorBuffer_Back);
for (size_t anchor_index = 1; anchor_index < DQN_PROFILER_ANCHOR_BUFFER_SIZE; anchor_index++) {
Dqn_ProfilerAnchor const *anchor = anchors + anchor_index;
if (!anchor->hit_count)
continue;
2023-09-20 13:35:38 +00:00
uint64_t tsc_exclusive = anchor->tsc_exclusive;
uint64_t tsc_inclusive = anchor->tsc_inclusive;
Dqn_f64 tsc_exclusive_milliseconds = tsc_exclusive * 1000 / DQN_CAST(Dqn_f64)platform->core.tsc_per_second;
if (tsc_exclusive == tsc_inclusive) {
TELY_RFui_TextF(rfui,
"%.*s[%u]: %.1fms",
DQN_STRING_FMT(anchor->name),
anchor->hit_count,
tsc_exclusive_milliseconds);
} else {
Dqn_f64 tsc_inclusive_milliseconds = tsc_inclusive * 1000 / DQN_CAST(Dqn_f64)platform->core.tsc_per_second;
TELY_RFui_TextF(rfui,
"%.*s[%u]: %.1f/%.1fms",
DQN_STRING_FMT(anchor->name),
anchor->hit_count,
tsc_exclusive_milliseconds,
tsc_inclusive_milliseconds);
}
}
}
2023-09-20 13:35:38 +00:00
}
TELY_RFui_Flush(rfui, renderer, input, assets);
2023-09-30 09:09:15 +00:00
TELY_Audio_MixPlaybackSamples(audio, assets);
2023-09-16 02:21:24 +00:00
}