fp: Move spawning to entity.cpp

This commit is contained in:
doyle 2023-09-25 22:13:32 +10:00
parent e1a9c21174
commit 1c8b6c8e1f
5 changed files with 153 additions and 130 deletions

View File

@ -181,34 +181,12 @@ void TELY_DLL_Init(void *user_data)
// NOTE: Hero
{
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Terry");
entity->type = FP_EntityType_Terry;
entity->local_pos = Dqn_V2_InitNx2(1334, 396);
entity->size_scale = Dqn_V2_InitNx1(0.25f);
entity->local_hit_box_size = Dqn_V2_InitNx2(428, 471) * entity->size_scale;
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
entity->flags |= FP_GameEntityFlag_NonTraversable;
entity->direction = FP_GameDirection_Left;
game->clicked_entity = entity->handle;
game->player = entity->handle;
FP_GameEntityHandle terry = FP_Entity_CreateTerry(game, Dqn_V2_InitNx2(1334, 396), "Terry");
game->clicked_entity = terry;
game->player = terry;
}
// NOTE: Merchant
{
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Merchant");
entity->type = FP_EntityType_Merchant;
entity->local_pos = Dqn_V2_InitNx2(1000, 124);
entity->local_hit_box_size = Dqn_V2_InitNx2(50, 50);
entity->size_scale = Dqn_V2_InitNx1(0.25f);
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
entity->flags |= FP_GameEntityFlag_NonTraversable;
}
FP_Entity_CreateMerchant(game, Dqn_V2_InitNx2(1000, 124), "Merchant");
game->tile_size = 37;
Dqn_V2I max_tile = platform->core.window_size / game->tile_size;
@ -224,13 +202,13 @@ void TELY_DLL_Init(void *user_data)
{
Dqn_V2I const vert_wall_part_tile_size = Dqn_V2I_InitNx2(1, (vert_wall_tile_size.y / 2) - 2);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base left-top wall"), left_wall_top_tile, vert_wall_part_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base left-top wall"), left_wall_top_tile, vert_wall_part_tile_size);
Dqn_V2I bottom_part_tile = Dqn_V2I_InitNx2(left_wall_top_tile.x, left_wall_top_tile.y + vert_wall_tile_size.y - vert_wall_part_tile_size.y);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base left-bottom wall"), bottom_part_tile, vert_wall_part_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base left-bottom wall"), bottom_part_tile, vert_wall_part_tile_size);
}
FP_GameEntityHandle right_wall = FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base right wall"), right_wall_tile_pos, vert_wall_tile_size);
FP_GameEntityHandle right_wall = FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base right wall"), right_wall_tile_pos, vert_wall_tile_size);
Dqn_Rect right_wall_box = FP_Game_CalcEntityWorldHitBox(game, right_wall);
Dqn_V2I right_wall_bottom_tile = FP_Game_WorldPosToTilePos(game, right_wall_box.pos + Dqn_V2_InitNx2(0, right_wall_box.size.y));
Dqn_V2I right_wall_top_left_tile = FP_Game_WorldPosToTilePos(game, right_wall_box.pos);
@ -239,81 +217,31 @@ void TELY_DLL_Init(void *user_data)
Dqn_V2I hori_wall_tile_size = Dqn_V2I_InitNx2((right_wall_tile_pos.x - left_wall_top_tile.x - 1) / 2 - 1, 1);
{
Dqn_V2I bottom_left_wall_tile_pos = Dqn_V2I_InitNx2(left_wall_bottom_tile.x + 1, left_wall_bottom_tile.y - 1);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base bottom-left wall"), bottom_left_wall_tile_pos, hori_wall_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base bottom-left wall"), bottom_left_wall_tile_pos, hori_wall_tile_size);
Dqn_V2I bottom_right_wall_tile_pos = Dqn_V2I_InitNx2(right_wall_bottom_tile.x - hori_wall_tile_size.x, right_wall_bottom_tile.y - 1);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base bottom-right wall"), bottom_right_wall_tile_pos, hori_wall_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base bottom-right wall"), bottom_right_wall_tile_pos, hori_wall_tile_size);
}
{
Dqn_V2I top_left_wall_tile_pos = Dqn_V2I_InitNx2(left_wall_top_tile.x + 1, left_wall_top_tile.y);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base top-left wall"), top_left_wall_tile_pos, hori_wall_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base top-left wall"), top_left_wall_tile_pos, hori_wall_tile_size);
Dqn_V2I top_right_wall_tile_pos = Dqn_V2I_InitNx2(right_wall_top_left_tile.x - hori_wall_tile_size.x, right_wall_top_left_tile.y);
FP_Game_EntityAddWallAtTile(game, DQN_STRING8("Base top-right wall"), top_right_wall_tile_pos, hori_wall_tile_size);
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base top-right wall"), top_right_wall_tile_pos, hori_wall_tile_size);
}
}
}
// NOTE: Mob spawner ===========================================================================
{
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Mob spawner");
{
entity->local_pos = Dqn_V2_InitNx2(50.f, platform->core.window_size.y * .5f);
entity->local_hit_box_size = Dqn_V2_InitNx1(32);
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
entity->flags |= FP_GameEntityFlag_MobSpawner;
entity->spawn_cap = 16;
entity->spawn_list = FP_SentinelList_Init<FP_GameEntityHandle>(game->chunk_pool);
FP_GameShape *shape = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
shape->type = FP_GameShapeType_Rect;
shape->p1 = {};
shape->p2 = entity->local_hit_box_size;
shape->render_mode = TELY_RenderShapeMode_Line;
shape->colour = TELY_COLOUR_BLUE_CADET_V4;
}
FP_Game_PushParentEntity(game, entity->handle);
{
{
FP_GameEntity *waypoint = FP_Game_MakeEntityPointerF(game, "Waypoint");
waypoint->local_pos = Dqn_V2_InitNx2(800.f, -200.f);
waypoint->local_hit_box_size = Dqn_V2_InitNx1(32);
waypoint->flags |= FP_GameEntityFlag_Clickable;
waypoint->flags |= FP_GameEntityFlag_MoveByKeyboard;
waypoint->flags |= FP_GameEntityFlag_MoveByMouse;
waypoint->flags |= FP_GameEntityFlag_MoveByGamepad;
waypoint->flags |= FP_GameEntityFlag_MobSpawnerWaypoint;
FP_GameShape *shape = Dqn_FArray_Make(&waypoint->shapes, Dqn_ZeroMem_Yes);
shape->type = FP_GameShapeType_Circle;
shape->circle_radius = 16.f;
shape->render_mode = TELY_RenderShapeMode_Line;
shape->colour = TELY_COLOUR_BLUE_CADET_V4;
}
{
FP_GameEntity *waypoint = FP_Game_MakeEntityPointerF(game, "Waypoint");
waypoint->local_pos = Dqn_V2_InitNx2(932.f, 200.f);
waypoint->local_hit_box_size = Dqn_V2_InitNx1(32);
waypoint->flags |= FP_GameEntityFlag_Clickable;
waypoint->flags |= FP_GameEntityFlag_MoveByKeyboard;
waypoint->flags |= FP_GameEntityFlag_MoveByMouse;
waypoint->flags |= FP_GameEntityFlag_MoveByGamepad;
waypoint->flags |= FP_GameEntityFlag_MobSpawnerWaypoint;
FP_GameShape *shape = Dqn_FArray_Make(&waypoint->shapes, Dqn_ZeroMem_Yes);
shape->type = FP_GameShapeType_Circle;
shape->circle_radius = 16.f;
shape->render_mode = TELY_RenderShapeMode_Line;
shape->colour = TELY_COLOUR_BLUE_CADET_V4;
}
}
FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game,
Dqn_V2_InitNx2(50.f, platform->core.window_size.y * .5f),
16 /*spawn_cap*/,
"Mob spawner");
FP_Game_PushParentEntity(game, mob_spawner);
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(800.f, -200.f), "Waypoint");
FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(932.f, 200.f), "Waypoint");
FP_Game_PopParentEntity(game);
}
@ -911,7 +839,7 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
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);
link->data = FP_Game_EntityAddMob(game, entity_world_pos);
link->data = FP_Entity_CreateSmoochie(game, entity_world_pos, "Smoochie");
// NOTE: Setup the mob with waypoints
FP_GameEntity *mob = FP_Game_GetEntity(game, link->data);

133
feely_pona_entity.cpp Normal file
View File

@ -0,0 +1,133 @@
#if defined(__clang__)
#pragma once
#include "feely_pona_unity.h"
#endif
static void FP_Entity_AddDebugEditorFlags(FP_Game *game, FP_GameEntityHandle handle)
{
FP_GameEntity *entity = FP_Game_GetEntity(game, handle);
if (!FP_Game_IsNilEntity(entity)) {
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
}
}
static FP_GameEntityHandle FP_Entity_CreateWaypointF(FP_Game *game, Dqn_V2 pos, DQN_FMT_STRING_ANNOTATE char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
FP_GameEntity *entity = FP_Game_MakeEntityPointerFV(game, fmt, args);
FP_GameEntityHandle result = entity->handle;
va_end(args);
entity->local_pos = pos;
entity->local_hit_box_size = Dqn_V2_InitNx1(32);
FP_Entity_AddDebugEditorFlags(game, entity->handle);
entity->flags |= FP_GameEntityFlag_MobSpawnerWaypoint;
FP_GameShape *shape = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
shape->type = FP_GameShapeType_Circle;
shape->circle_radius = 16.f;
shape->render_mode = TELY_RenderShapeMode_Line;
shape->colour = TELY_COLOUR_BLUE_CADET_V4;
return result;
}
static FP_GameEntityHandle FP_Entity_CreateSmoochie(FP_Game *game, Dqn_V2 pos, DQN_FMT_STRING_ANNOTATE char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
FP_GameEntity *entity = FP_Game_MakeEntityPointerFV(game, fmt, args);
FP_GameEntityHandle result = entity->handle;
va_end(args);
entity->type = FP_EntityType_Smoochie;
entity->local_pos = pos;
entity->size_scale = Dqn_V2_InitNx1(.25f);
entity->local_hit_box_size = Dqn_V2_InitNx2(428, 471) * entity->size_scale;
FP_Entity_AddDebugEditorFlags(game, entity->handle);
entity->flags |= FP_GameEntityFlag_NonTraversable;
return result;
}
static FP_GameEntityHandle FP_Entity_CreateWallAtTile(FP_Game *game, Dqn_String8 name, Dqn_V2I tile_pos, Dqn_V2I size_in_tiles)
{
Dqn_V2 size = Dqn_V2_InitV2I(size_in_tiles * DQN_CAST(int32_t) game->tile_size);
Dqn_V2 world_pos = FP_Game_TilePosToWorldPos(game, tile_pos);
world_pos += size * .5f;
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, name.data);
FP_GameEntityHandle result = entity->handle;
entity->local_pos = world_pos;
entity->local_hit_box_size = Dqn_V2_InitV2I(size_in_tiles * DQN_CAST(int32_t)game->tile_size);
FP_Entity_AddDebugEditorFlags(game, entity->handle);
entity->flags |= FP_GameEntityFlag_NonTraversable;
FP_GameShape *wall = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
wall->type = FP_GameShapeType_Rect;
wall->p2 = entity->local_hit_box_size;
wall->colour = TELY_COLOUR_GREEN_DARK_KHAKI_V4;
return result;
}
static FP_GameEntityHandle FP_Entity_CreateMobSpawner(FP_Game *game, Dqn_V2 pos, Dqn_usize spawn_cap, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
FP_GameEntity *entity = FP_Game_MakeEntityPointerFV(game, fmt, args);
FP_GameEntityHandle result = entity->handle;
va_end(args);
entity->local_pos = pos;
entity->local_hit_box_size = Dqn_V2_InitNx1(32);
FP_Entity_AddDebugEditorFlags(game, result);
entity->flags |= FP_GameEntityFlag_MobSpawner;
entity->spawn_cap = spawn_cap;
entity->spawn_list = FP_SentinelList_Init<FP_GameEntityHandle>(game->chunk_pool);
FP_GameShape *shape = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
shape->type = FP_GameShapeType_Rect;
shape->p1 = {};
shape->p2 = entity->local_hit_box_size;
shape->render_mode = TELY_RenderShapeMode_Line;
shape->colour = TELY_COLOUR_BLUE_CADET_V4;
return result;
}
static FP_GameEntityHandle FP_Entity_CreateTerry(FP_Game *game, Dqn_V2 pos, DQN_FMT_STRING_ANNOTATE char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
FP_GameEntity *entity = FP_Game_MakeEntityPointerFV(game, fmt, args);
FP_GameEntityHandle result = entity->handle;
va_end(args);
entity->type = FP_EntityType_Terry;
entity->local_pos = pos;
entity->size_scale = Dqn_V2_InitNx1(0.25f);
entity->local_hit_box_size = Dqn_V2_InitNx2(428, 471) * entity->size_scale;
FP_Entity_AddDebugEditorFlags(game, result);
entity->flags |= FP_GameEntityFlag_NonTraversable;
return result;
}
static FP_GameEntityHandle FP_Entity_CreateMerchant(FP_Game *game, Dqn_V2 pos, DQN_FMT_STRING_ANNOTATE char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
FP_GameEntity *entity = FP_Game_MakeEntityPointerFV(game, fmt, args);
FP_GameEntityHandle result = entity->handle;
va_end(args);
entity->type = FP_EntityType_Merchant;
entity->local_pos = pos;
entity->local_hit_box_size = Dqn_V2_InitNx2(50, 50);
entity->size_scale = Dqn_V2_InitNx1(0.25f);
FP_Entity_AddDebugEditorFlags(game, result);
entity->flags |= FP_GameEntityFlag_NonTraversable;
return result;
}

View File

@ -624,42 +624,3 @@ static Dqn_Slice<Dqn_V2I> FP_Game_AStarPathFind(FP_Game *game,
DQN_ASSERT(result.size == slice_size);
return result;
}
// NOTE: Spawning code =============================================================================
static FP_GameEntityHandle FP_Game_EntityAddWallAtTile(FP_Game *game, Dqn_String8 name, Dqn_V2I tile_pos, Dqn_V2I size_in_tiles)
{
Dqn_V2 size = Dqn_V2_InitV2I(size_in_tiles * DQN_CAST(int32_t) game->tile_size);
Dqn_V2 world_pos = FP_Game_TilePosToWorldPos(game, tile_pos);
world_pos += size * .5f;
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, name.data);
entity->local_pos = world_pos;
entity->local_hit_box_size = Dqn_V2_InitV2I(size_in_tiles * DQN_CAST(int32_t)game->tile_size);
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_NonTraversable;
FP_GameShape *wall = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
wall->type = FP_GameShapeType_Rect;
wall->p2 = entity->local_hit_box_size;
wall->colour = TELY_COLOUR_GREEN_DARK_KHAKI_V4;
FP_GameEntityHandle result = entity->handle;
return result;
}
static FP_GameEntityHandle FP_Game_EntityAddMob(FP_Game *game, Dqn_V2 pos)
{
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Mob");
entity->type = FP_EntityType_Smoochie;
entity->local_pos = pos;
entity->size_scale = Dqn_V2_InitNx1(.25f);
entity->local_hit_box_size = Dqn_V2_InitNx2(428, 471) * entity->size_scale;
entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_NonTraversable;
FP_GameEntityHandle result = entity->handle;
return result;
}

View File

@ -67,5 +67,6 @@ DQN_MSVC_WARNING_DISABLE(4505) // warning C4505: unreferenced function with inte
#include "feely_pona.h"
#include "feely_pona_game.cpp"
#include "feely_pona_entity.cpp"
#include "feely_pona_misc.cpp"
#include "feely_pona.cpp"

Binary file not shown.