Skip to content

Commit

Permalink
controller support.
Browse files Browse the repository at this point in the history
owner draw cursor
  • Loading branch information
JeffM2501 committed Jan 8, 2023
1 parent 8d3f420 commit c976e6a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions game/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions game/include/sprites.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Sprites
extern size_t BoostPowerup;
extern size_t ShotPowerup;

extern size_t Cursor;

void Init();
void Shutdown();

Expand Down
10 changes: 6 additions & 4 deletions game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -109,6 +109,8 @@ int main ()

DrawOverlay();

Sprites::Draw(Sprites::Cursor, GetMousePosition(), 0);

EndDrawing();
}

Expand Down
60 changes: 44 additions & 16 deletions game/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -95,31 +121,22 @@ 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();

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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion game/powerup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions game/sprites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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} });
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion game/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c976e6a

Please sign in to comment.