feat: add player gold

This commit is contained in:
2026-07-05 20:57:17 +02:00
parent 354c8b8a5d
commit a167e38d3c
5 changed files with 87 additions and 102 deletions
+1
View File
@@ -1 +1,2 @@
HEALTH_MAX = 100 HEALTH_MAX = 100
START_GOLD = 0
+3 -1
View File
@@ -101,6 +101,7 @@ typedef struct {
Physics physics; Physics physics;
float rotation; float rotation;
int health; int health;
int gold;
} Player; } Player;
typedef enum { BOAT, ENEMIES_TEX_COUNT } enemiesTextureID; typedef enum { BOAT, ENEMIES_TEX_COUNT } enemiesTextureID;
@@ -250,7 +251,8 @@ void RenderProjectiles(Projectiles *projectiles);
void CheckPlayerCollisions(Player *player, Enemies *enemies); void CheckPlayerCollisions(Player *player, Enemies *enemies);
void CheckEnemyCollisions(Enemies *enemies); void CheckEnemyCollisions(Enemies *enemies);
void CheckProjectileCollisions(Projectiles *projectiles, Enemies *enemies, Player *player); void CheckProjectileCollisions(Projectiles *projectiles, Enemies *enemies,
Player *player);
void InitGame(App *app); void InitGame(App *app);
void UpdateGame(App *app); void UpdateGame(App *app);
+1
View File
@@ -82,6 +82,7 @@ void CheckProjectileCollisions(Projectiles *projectiles, Enemies *enemies,
if (CheckCollisionCircles(projectilePos, projectilesCollisionRadius, if (CheckCollisionCircles(projectilePos, projectilesCollisionRadius,
enemyPos, enemiesCollisionRadius)) { enemyPos, enemiesCollisionRadius)) {
player->gold += 20;
Rectangle cameraRec = GetCameraRectangle(); Rectangle cameraRec = GetCameraRectangle();
enemies->physics[j].position = enemies->physics[j].position =
GetPositionOutsideRectangle(cameraRec, 100, 1000); GetPositionOutsideRectangle(cameraRec, 100, 1000);
+4 -2
View File
@@ -6,8 +6,10 @@ void RenderOverlay(Mouse *mouse) {
// Health // Health
Player *player = GetPlayer(); Player *player = GetPlayer();
const char *healthText = TextFormat("%d", player->health); const char *text = TextFormat("Health: %i\n"
DrawText(healthText, 0, 0, 25, WHITE); "Gold: %i\n",
player->health, player->gold);
DrawText(text, 0, 0, 25, WHITE);
// Mouse // Mouse
Rectangle mouseRectangle = (Rectangle){0, 0, mouse->cursorTexture.width, Rectangle mouseRectangle = (Rectangle){0, 0, mouse->cursorTexture.width,
+29 -50
View File
@@ -1,14 +1,10 @@
#include "app.h" #include "app.h"
#include "engine.h"
const TextureAsset playerTextureAssets[PLAYER_TEX_COUNT] = { 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, 5.0f, 10.0f};
0.0f,
5.0f,
10.0f
};
Config config; Config config;
@@ -21,17 +17,16 @@ playerSpeedID GetPlayerSpeedID(float speed) {
return ANCHOR; return ANCHOR;
} }
Player* GetPlayer(void) Player *GetPlayer(void) {
{
static Player player; static Player player;
return &player; return &player;
} }
void CreatePlayer(void) void CreatePlayer(void) {
{
InitConfig(&config, "config/player.ini"); InitConfig(&config, "config/player.ini");
int PLAYER_HEALTH_MAX = GetConfigInt(&config, "HEALTH_MAX"); int PLAYER_HEALTH_MAX = GetConfigInt(&config, "HEALTH_MAX");
int START_GOLD = GetConfigInt(&config, "START_GOLD");
TextureAsset startShipAsset = playerTextureAssets[START_SHIP]; TextureAsset startShipAsset = playerTextureAssets[START_SHIP];
@@ -39,76 +34,62 @@ void CreatePlayer(void)
float frameWidth = startShipAsset.frameWidth; float frameWidth = startShipAsset.frameWidth;
float rotation = startShipAsset.rotation; float rotation = startShipAsset.rotation;
Sprite playerSprite = { Sprite playerSprite = {.texture = texture,
.texture = texture,
.frameSize = {frameWidth, frameWidth}, .frameSize = {frameWidth, frameWidth},
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
.destRec = {0.0f, 0.0f, frameWidth, frameWidth}, .destRec = {0.0f, 0.0f, frameWidth, frameWidth},
.origin = {frameWidth / 2, frameWidth / 2}, .origin = {frameWidth / 2, frameWidth / 2},
.rotation = rotation, .rotation = rotation,
.color = WHITE .color = WHITE};
};
Animation playerAnimation = { Animation playerAnimation = {.state = 1,
.state = 1,
.currentFrame = 0, .currentFrame = 0,
.maxFrame = 3, .maxFrame = 3,
.framesCounter = 0, .framesCounter = 0,
.framesSpeed = 0.0f, .framesSpeed = 0.0f,
.animTimer = 0.0f .animTimer = 0.0f};
};
Physics playerPhysics = {.position = (Vector2){0.0f, 0.0f},
Physics playerPhysics = {
.position = (Vector2){0.0f,0.0f},
.direction = (Vector2){0.0f, 0.0f}, .direction = (Vector2){0.0f, 0.0f},
.speed = playerSpeeds[ANCHOR], .speed = playerSpeeds[ANCHOR],
.weight = 10.0f .weight = 10.0f};
};
Player* player = GetPlayer(); Player *player = GetPlayer();
*player = (Player){ *player = (Player){.sprite = playerSprite,
.sprite = playerSprite,
.animation = playerAnimation, .animation = playerAnimation,
.physics = playerPhysics, .physics = playerPhysics,
.rotation = rotation, .rotation = rotation,
.health = PLAYER_HEALTH_MAX .health = PLAYER_HEALTH_MAX,
}; .gold = START_GOLD};
} }
void UpdatePlayer(void) void UpdatePlayer(void) {
{ Player *player = GetPlayer();
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]){
player->physics.speed = playerSpeeds[SLOW_AHEAD]; player->physics.speed = playerSpeeds[SLOW_AHEAD];
} } else {
else {
player->physics.speed = playerSpeeds[FAST_AHEAD]; player->physics.speed = playerSpeeds[FAST_AHEAD];
} }
} }
} }
if(IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)){ if (IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)) {
if(player->physics.speed > playerSpeeds[ANCHOR]){ if (player->physics.speed > playerSpeeds[ANCHOR]) {
if(player->physics.speed > playerSpeeds[SLOW_AHEAD]){ if (player->physics.speed > playerSpeeds[SLOW_AHEAD]) {
player->physics.speed = playerSpeeds[SLOW_AHEAD]; player->physics.speed = playerSpeeds[SLOW_AHEAD];
} } else {
else {
player->physics.speed = playerSpeeds[ANCHOR]; player->physics.speed = playerSpeeds[ANCHOR];
} }
} }
} }
player->sprite.sourceRec.x = 16.0f * GetPlayerSpeedID(player->physics.speed); player->sprite.sourceRec.x = 16.0f * GetPlayerSpeedID(player->physics.speed);
float rotationSpeed = 50.0f; float rotationSpeed = 50.0f;
if (player->physics.speed > playerSpeeds[ANCHOR]){ if (player->physics.speed > playerSpeeds[ANCHOR]) {
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) { if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {
player->rotation -= rotationSpeed * GetFrameTime(); player->rotation -= rotationSpeed * GetFrameTime();
@@ -123,13 +104,11 @@ void UpdatePlayer(void)
Vector2 dir = RotationToVector2(player->rotation - 90.0f); Vector2 dir = RotationToVector2(player->rotation - 90.0f);
UpdatePhysics(&player->physics, dir); UpdatePhysics(&player->physics, dir);
UpdateSpriteDestRec(&player->sprite, &player->physics.position); UpdateSpriteDestRec(&player->sprite, &player->physics.position);
} }
void RenderPlayer(void){ void RenderPlayer(void) {
Player* player = GetPlayer(); Player *player = GetPlayer();
RenderSprite(&player->sprite); RenderSprite(&player->sprite);
} }