refactor: player as singleton

This commit is contained in:
huaqo-games
2025-11-06 11:50:20 +01:00
parent dbac58eda8
commit cc53a19fd5
3 changed files with 25 additions and 17 deletions
+4 -4
View File
@@ -171,7 +171,6 @@ typedef struct
Soundtrack soundtracks[SOUNDTRACK_COUNT];
RenderTexture2D target;
Floor floor;
Player player;
Birds birds;
Islands islands;
Mouse mouse;
@@ -230,9 +229,10 @@ void RenderFloor(Floor *floor);
void UnloadFloor(Floor *floor);
playerSpeedID GetPlayerSpeedID(float speed);
Player CreatePlayer(void);
void UpdatePlayer(Player *player);
void RenderPlayer(Player *player);
Player* GetPlayer(void);
void CreatePlayer(void);
void UpdatePlayer(void);
void RenderPlayer(void);
Birds CreateBirds(void);
void UpdateBirds(Birds *birds);
+7 -5
View File
@@ -33,8 +33,9 @@ void InitGame(App *app)
g->target = LoadRenderTexture(app->window.width, app->window.height);
g->player = CreatePlayer();
CreateCamera(g->player.physics.position, 10.0f);
CreatePlayer();
Player* player = GetPlayer();
CreateCamera(player->physics.position, 10.0f);
g->birds = CreateBirds();
g->islands = CreateIslands();
g->floor = CreateFloor();
@@ -72,9 +73,10 @@ void UpdateGame(App *app)
}
UpdateMouse(&g->mouse);
UpdateCamera2D(&g->player.physics.position, &g->mouse);
Player* player = GetPlayer();
UpdateCamera2D(&player->physics.position, &g->mouse);
UpdateFloor(&g->floor);
UpdatePlayer(&g->player);
UpdatePlayer();
UpdateBirds(&g->birds);
UpdateIslands(&g->islands);
}
@@ -83,7 +85,7 @@ void RenderComponents(App *app)
{
GameState *g = &app->game;
RenderFloor(&g->floor);
RenderPlayer(&g->player);
RenderPlayer();
RenderBirds(&g->birds);
RenderIslands(&g->islands);
}
+14 -8
View File
@@ -4,8 +4,6 @@ const TextureAsset playerTextureAssets[PLAYER_TEX_COUNT] = {
{"assets/ship_start.png", 16.0f, 16.0f, 0.0f}
};
float playerSpeeds[SPEED_COUNT] = {
0.0f,
5.0f,
@@ -21,9 +19,14 @@ playerSpeedID GetPlayerSpeedID(float speed) {
return ANCHOR;
}
Player CreatePlayer(void)
Player* GetPlayer(void)
{
static Player player;
return &player;
}
void CreatePlayer(void)
{
TextureAsset startShipAsset = playerTextureAssets[START_SHIP];
Texture2D texture = LoadTexture(startShipAsset.path);
@@ -50,24 +53,26 @@ Player CreatePlayer(void)
.animTimer = 0.0f
};
Physics playerPhysics = {
.position = (Vector2){0.0f,0.0f},
.direction = (Vector2){0.0f, 0.0f},
.speed = playerSpeeds[ANCHOR]
};
return (Player){
Player* player = GetPlayer();
*player = (Player){
.sprite = playerSprite,
.animation = playerAnimation,
.physics = playerPhysics,
.rotation = rotation
};
}
void UpdatePlayer(Player *player)
void UpdatePlayer(void)
{
Player* player = GetPlayer();
if(IsKeyPressed(KEY_W) || IsKeyPressed(KEY_UP)){
if(player->physics.speed < playerSpeeds[FAST_AHEAD]){
if(player->physics.speed < playerSpeeds[SLOW_AHEAD]){
@@ -116,7 +121,8 @@ void UpdatePlayer(Player *player)
UpdateSpriteDestRec(&player->sprite, &player->physics.position);
}
void RenderPlayer(Player *player){
void RenderPlayer(void){
Player* player = GetPlayer();
RenderSprite(&player->sprite);
}