From c976e6acb7e9aece22e935771001803c86ec16c4 Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Sat, 7 Jan 2023 16:10:02 -0800 Subject: [PATCH] controller support. owner draw cursor --- README.md | 2 -- game/game.cpp | 4 +-- game/include/player.h | 2 ++ game/include/sprites.h | 2 ++ game/main.cpp | 10 ++++--- game/player.cpp | 60 +++++++++++++++++++++++++++++++----------- game/powerup.cpp | 2 +- game/sprites.cpp | 4 +++ game/world.cpp | 2 +- 9 files changed, 62 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 791a4e5..98d93f7 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,8 @@ Pre-Release, still a work in progress. ## TODO * UFO -* Controller Support * Code Comments -* Cleanup API to player * Menu for volume? * Bins for major platforms diff --git a/game/game.cpp b/game/game.cpp index 951c302..ebb32d7 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -219,7 +219,7 @@ void DrawGameOver() { DrawCenteredText(TextFormat("Game Over, your score was %d, good job!", World::Instance->PlayerShip.GetScore()), 20, 0.25f); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_SPACE)) + if (World::Instance->PlayerShip.AcceptPressed()) { GameState = GameStates::Menu; World::Instance->Reset(50); @@ -237,7 +237,7 @@ void DrawMenu() DrawCenteredText("Left Click or space to fire", 20, 0.6f); DrawCenteredText("Shift + Thrust to Boost", 20, 0.65f); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_SPACE)) + if (World::Instance->PlayerShip.AcceptPressed()) { Level = 1; GameState = GameStates::Playing; diff --git a/game/include/player.h b/game/include/player.h index 79f5966..dc74854 100644 --- a/game/include/player.h +++ b/game/include/player.h @@ -41,6 +41,8 @@ class Player : public Entity void AddScore(int scoreDelta); inline int GetScore() const { return Score; } + bool AcceptPressed(); + protected: float ShieldHitAngle = 0; float ShieldHitLifetime = -1; diff --git a/game/include/sprites.h b/game/include/sprites.h index fbaeee7..17ce166 100644 --- a/game/include/sprites.h +++ b/game/include/sprites.h @@ -39,6 +39,8 @@ namespace Sprites extern size_t BoostPowerup; extern size_t ShotPowerup; + extern size_t Cursor; + void Init(); void Shutdown(); diff --git a/game/main.cpp b/game/main.cpp index 81bedee..e231baa 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -32,14 +32,14 @@ int main () SearchAndSetResourceDir("resources"); - SetMouseCursor(MOUSE_CURSOR_CROSSHAIR); + HideCursor(); + //SetMouseCursor(MOUSE_CURSOR_CROSSHAIR); Sounds::Init(); SetMasterVolume(0.5f); Sprites::Init(); - Camera2D worldCamera = { 0 }; worldCamera.zoom = 0.5f; @@ -60,9 +60,9 @@ int main () UpdateGame(); Sounds::Update(); - if (IsKeyPressed(KEY_ENTER) && (IsKeyDown(KEY_LEFT_ALT)|| IsKeyDown(KEY_RIGHT_ALT))) + if (IsKeyPressed(KEY_ENTER) && (IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT))) ToggleFullscreenState(); - + Vector2 center = Vector2Scale(GetDisplaySize(), 0.5f); worldCamera.offset = center; @@ -109,6 +109,8 @@ int main () DrawOverlay(); + Sprites::Draw(Sprites::Cursor, GetMousePosition(), 0); + EndDrawing(); } diff --git a/game/player.cpp b/game/player.cpp index e192937..8dda4b5 100644 --- a/game/player.cpp +++ b/game/player.cpp @@ -66,13 +66,39 @@ void Player::Update() if (Shield > MaxShield) Shield = MaxShield; - // TODO, game pad support - bool wantThrust = IsKeyDown(KEY_W) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT); Thrusting = false; bool wantBoost = (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT)) && wantThrust; + bool wantShoot = IsKeyDown(KEY_SPACE) || IsMouseButtonDown(MOUSE_BUTTON_LEFT); + + bool wantBreak = IsKeyDown(KEY_S); + + if (IsGamepadAvailable(0)) + { + wantThrust = wantThrust || GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER) > 0.125f; + wantBoost = wantBoost || (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN) && wantThrust > 0); + wantShoot = wantShoot || GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER) > 0.125f; + wantBreak = wantBreak || IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT); + } + + if (Vector2LengthSqr(GetMouseDelta()) > 0) + { + Vector2 mouseVec = Vector2Normalize(Vector2Subtract(GetMousePosition(), Vector2Scale(GetDisplaySize(), 0.5f))); + + if (Vector2LengthSqr(mouseVec) > 0) + { + Orientation = atan2f(mouseVec.y, mouseVec.x) * RAD2DEG + 90; + } + } + else if(IsGamepadAvailable(0)) + { + float rotation = 360 * GetDeltaTime(); + + Orientation += GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X) * rotation; + } + if (!wantBoost) Boost = false; else if (wantBoost && Power > MaxPower / 4) @@ -95,16 +121,7 @@ void Player::Update() Power = MaxPower; Reload -= GetDeltaTime() * ShotSpeedMultiplyer; - - float rotation = 270 * GetDeltaTime(); - - Vector2 mouseVec = Vector2Normalize(Vector2Subtract(GetMousePosition(), Vector2Scale(GetDisplaySize(),0.5f))); - - if (Vector2LengthSqr(mouseVec) > 0) - { - Orientation = atan2f(mouseVec.y, mouseVec.x) * RAD2DEG + 90; - } - + Vector2 shipVector = Vector2{ sinf(Orientation * DEG2RAD), -cosf(Orientation * DEG2RAD) }; float speed = MaxThrust * GetDeltaTime(); @@ -112,14 +129,14 @@ void Player::Update() if (Boost) speed *= BoostMultiplyer; - if (wantThrust) + if (wantThrust > 0) { Velocity = Vector2Add(Velocity, Vector2Scale(shipVector, speed)); Thrusting = true; } float frictionScale = 1; - if (IsKeyDown(KEY_S)) + if (wantBreak) frictionScale *= BreakingFriction; Vector2 normVel = Vector2Normalize(Velocity); @@ -143,8 +160,6 @@ void Player::Update() while (Orientation < -180) Orientation += 360; - bool wantShoot = IsKeyDown(KEY_SPACE) || IsMouseButtonDown(MOUSE_BUTTON_LEFT); - if (wantShoot && Reload <= 0) { // fire @@ -225,3 +240,16 @@ void Player::AddScore(int scoreDelta) Score += scoreDelta; } + +bool Player::AcceptPressed() +{ + if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + return true; + + if (IsGamepadAvailable(0)) + { + return IsGamepadButtonPressed(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN); + } + + return false; +} diff --git a/game/powerup.cpp b/game/powerup.cpp index ea245f6..403e8fc 100644 --- a/game/powerup.cpp +++ b/game/powerup.cpp @@ -52,7 +52,7 @@ bool PowerUp::Collide(const Entity& other) case PowerUp::PowerType::Boost: player.Power += World::Instance->PlayerShip.MaxPower / 4.0f; - player.BoostMultiplyer += 0.1; + player.BoostMultiplyer += 0.1f; player.MaxThrust += player.NominalThrust * 0.25f; break; } diff --git a/game/sprites.cpp b/game/sprites.cpp index 17b63c8..64914d9 100644 --- a/game/sprites.cpp +++ b/game/sprites.cpp @@ -33,6 +33,8 @@ namespace Sprites size_t BoostPowerup = 0; size_t ShotPowerup = 0; + size_t Cursor = 0; + size_t AddFrame(size_t sheetId, float x, float y, float w, float h) { Frames.emplace_back(SpriteFrame{ sheetId, Rectangle{x,y,w,h} }); @@ -82,6 +84,8 @@ namespace Sprites BoostPowerup = AddFrame(0, 674, 262, 22, 21); ShotPowerup = AddFrame(0, 222, 108, 22, 21); + Cursor = AddFrame(0, 382, 814, 17, 17); + AsteroidSprites.push_back(AddFrame(0, 224, 664, 101, 84)); AsteroidSprites.push_back(AddFrame(0, 0, 520, 120, 98)); AsteroidSprites.push_back(AddFrame(0, 518, 810, 89, 82)); diff --git a/game/world.cpp b/game/world.cpp index 6e373bf..e78dcdc 100644 --- a/game/world.cpp +++ b/game/world.cpp @@ -88,7 +88,7 @@ void World::Update() void World::Draw(Rectangle& screenInWorld) const { - float thickness = 30 + sinf(GetTime() * 10) * 10; + float thickness = 30 + sinf((float)GetTime() * 10) * 10; Rectangle bounds = { Bounds.x - thickness, Bounds.y - thickness, Bounds.width + thickness * 2, Bounds.height + thickness + 2 }; DrawRectangleLinesEx(bounds, thickness, SKYBLUE); DrawRectangleLinesEx(bounds, thickness/3, WHITE);