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