diff --git a/config/player.ini b/config/player.ini index 6b06718..9ad7fc6 100644 --- a/config/player.ini +++ b/config/player.ini @@ -1 +1,2 @@ HEALTH_MAX = 100 +START_GOLD = 0 diff --git a/include/app.h b/include/app.h index e413cea..1065b2e 100644 --- a/include/app.h +++ b/include/app.h @@ -101,6 +101,7 @@ typedef struct { Physics physics; float rotation; int health; + int gold; } Player; typedef enum { BOAT, ENEMIES_TEX_COUNT } enemiesTextureID; @@ -250,7 +251,8 @@ void RenderProjectiles(Projectiles *projectiles); void CheckPlayerCollisions(Player *player, 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 UpdateGame(App *app); diff --git a/src/appGameCollisions.c b/src/appGameCollisions.c index dde1db2..d42b723 100644 --- a/src/appGameCollisions.c +++ b/src/appGameCollisions.c @@ -82,6 +82,7 @@ void CheckProjectileCollisions(Projectiles *projectiles, Enemies *enemies, if (CheckCollisionCircles(projectilePos, projectilesCollisionRadius, enemyPos, enemiesCollisionRadius)) { + player->gold += 20; Rectangle cameraRec = GetCameraRectangle(); enemies->physics[j].position = GetPositionOutsideRectangle(cameraRec, 100, 1000); diff --git a/src/appGameOverlay.c b/src/appGameOverlay.c index bfd2843..b5b87f1 100644 --- a/src/appGameOverlay.c +++ b/src/appGameOverlay.c @@ -6,8 +6,10 @@ void RenderOverlay(Mouse *mouse) { // Health Player *player = GetPlayer(); - const char *healthText = TextFormat("%d", player->health); - DrawText(healthText, 0, 0, 25, WHITE); + const char *text = TextFormat("Health: %i\n" + "Gold: %i\n", + player->health, player->gold); + DrawText(text, 0, 0, 25, WHITE); // Mouse Rectangle mouseRectangle = (Rectangle){0, 0, mouse->cursorTexture.width, diff --git a/src/appGamePlayer.c b/src/appGamePlayer.c index a3d07ed..de376c5 100644 --- a/src/appGamePlayer.c +++ b/src/appGamePlayer.c @@ -1,135 +1,114 @@ #include "app.h" +#include "engine.h" 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] = { - 0.0f, - 5.0f, - 10.0f -}; +float playerSpeeds[SPEED_COUNT] = {0.0f, 5.0f, 10.0f}; Config config; playerSpeedID GetPlayerSpeedID(float speed) { - for (int i = 0; i < SPEED_COUNT; i++) { - if (playerSpeeds[i] == speed) { - return (playerSpeedID)i; - } + for (int i = 0; i < SPEED_COUNT; i++) { + if (playerSpeeds[i] == speed) { + return (playerSpeedID)i; } - return ANCHOR; + } + return ANCHOR; } -Player* GetPlayer(void) -{ - static Player player; - return &player; +Player *GetPlayer(void) { + static Player player; + return &player; } -void CreatePlayer(void) -{ +void CreatePlayer(void) { InitConfig(&config, "config/player.ini"); int PLAYER_HEALTH_MAX = GetConfigInt(&config, "HEALTH_MAX"); + int START_GOLD = GetConfigInt(&config, "START_GOLD"); - TextureAsset startShipAsset = playerTextureAssets[START_SHIP]; - - Texture2D texture = LoadTexture(startShipAsset.path); - float frameWidth = startShipAsset.frameWidth; - float rotation = startShipAsset.rotation; + TextureAsset startShipAsset = playerTextureAssets[START_SHIP]; - Sprite playerSprite = { - .texture = texture, - .frameSize = {frameWidth, frameWidth}, - .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .destRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .origin = {frameWidth / 2, frameWidth / 2}, - .rotation = rotation, - .color = WHITE - }; + Texture2D texture = LoadTexture(startShipAsset.path); + float frameWidth = startShipAsset.frameWidth; + float rotation = startShipAsset.rotation; - Animation playerAnimation = { - .state = 1, - .currentFrame = 0, - .maxFrame = 3, - .framesCounter = 0, - .framesSpeed = 0.0f, - .animTimer = 0.0f - }; + Sprite playerSprite = {.texture = texture, + .frameSize = {frameWidth, frameWidth}, + .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, + .destRec = {0.0f, 0.0f, frameWidth, frameWidth}, + .origin = {frameWidth / 2, frameWidth / 2}, + .rotation = rotation, + .color = WHITE}; + Animation playerAnimation = {.state = 1, + .currentFrame = 0, + .maxFrame = 3, + .framesCounter = 0, + .framesSpeed = 0.0f, + .animTimer = 0.0f}; - Physics playerPhysics = { - .position = (Vector2){0.0f,0.0f}, - .direction = (Vector2){0.0f, 0.0f}, - .speed = playerSpeeds[ANCHOR], - .weight = 10.0f - }; - - Player* player = GetPlayer(); - *player = (Player){ - .sprite = playerSprite, - .animation = playerAnimation, - .physics = playerPhysics, - .rotation = rotation, - .health = PLAYER_HEALTH_MAX - }; + Physics playerPhysics = {.position = (Vector2){0.0f, 0.0f}, + .direction = (Vector2){0.0f, 0.0f}, + .speed = playerSpeeds[ANCHOR], + .weight = 10.0f}; + Player *player = GetPlayer(); + *player = (Player){.sprite = playerSprite, + .animation = playerAnimation, + .physics = playerPhysics, + .rotation = rotation, + .health = PLAYER_HEALTH_MAX, + .gold = START_GOLD}; } -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]){ - player->physics.speed = playerSpeeds[SLOW_AHEAD]; - } - else { - player->physics.speed = playerSpeeds[FAST_AHEAD]; - } - } - } +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]) { + player->physics.speed = playerSpeeds[SLOW_AHEAD]; + } else { + player->physics.speed = playerSpeeds[FAST_AHEAD]; + } + } + } - if(IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)){ - if(player->physics.speed > playerSpeeds[ANCHOR]){ - if(player->physics.speed > playerSpeeds[SLOW_AHEAD]){ - player->physics.speed = playerSpeeds[SLOW_AHEAD]; - } - else { - player->physics.speed = playerSpeeds[ANCHOR]; + if (IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)) { + if (player->physics.speed > playerSpeeds[ANCHOR]) { + if (player->physics.speed > playerSpeeds[SLOW_AHEAD]) { + player->physics.speed = playerSpeeds[SLOW_AHEAD]; + } else { + 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; + if (player->physics.speed > playerSpeeds[ANCHOR]) { - float rotationSpeed = 50.0f; + if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) { + player->rotation -= rotationSpeed * GetFrameTime(); + } - if (player->physics.speed > playerSpeeds[ANCHOR]){ + if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) { + player->rotation += rotationSpeed * GetFrameTime(); + } + } - if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) { - player->rotation -= rotationSpeed * GetFrameTime(); - } + player->sprite.rotation = player->rotation; - if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) { - player->rotation += rotationSpeed * GetFrameTime(); - } - } + Vector2 dir = RotationToVector2(player->rotation - 90.0f); - player->sprite.rotation = player->rotation; - - Vector2 dir = RotationToVector2(player->rotation - 90.0f); - - - UpdatePhysics(&player->physics, dir); - UpdateSpriteDestRec(&player->sprite, &player->physics.position); + UpdatePhysics(&player->physics, dir); + UpdateSpriteDestRec(&player->sprite, &player->physics.position); } -void RenderPlayer(void){ - Player* player = GetPlayer(); - RenderSprite(&player->sprite); +void RenderPlayer(void) { + Player *player = GetPlayer(); + RenderSprite(&player->sprite); } -