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
+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);
}