Skip to content

Commit

Permalink
powerup fixes
Browse files Browse the repository at this point in the history
bug fixes
fullscreen support
API cleanups
Resource dirs
note how many asteroids are left
  • Loading branch information
JeffM2501 committed Jan 7, 2023
1 parent 86b0a80 commit 243c5f7
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 71 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ Pre-Release, still a work in progress.

## TODO
* UFO
* Powerups?
* Controller Support
* Tell them how many asteroids are left

* Code Comments
* Cleanup API to player
* Cleanup Camera Shake API (global shake state?)

* Menu for volume?
* Fullscreen Support
* Set working dir better

* Bins for major platforms

## Bugs
* Score goes up after you die
## Bugs
4 changes: 2 additions & 2 deletions game/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool Asteroid::Collide(const Entity& other)
newPos = Vector2Add(Position, Vector2Scale(newVector, split2Radius * 0.25f));
Create(split2Radius, newPos, Vector2Scale(newVector, GetRandomValueF(speed + 20, speed + 80)));

World::Instance->PlayerShip.Score += 1;
World::Instance->PlayerShip.AddScore(1);

Explosion::Create(Position, Radius);

Expand All @@ -68,7 +68,7 @@ void Asteroid::Create(float radius, const Vector2& pos, const Vector2& velocity)

if (radius < 40)
{
World::Instance->PlayerShip.Score += int(radius + 0.5f);
World::Instance->PlayerShip.AddScore(int(radius + 0.5f));
return;
}

Expand Down
78 changes: 70 additions & 8 deletions game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ double Time = 0;
int Level = 1;
float LevelChangeCountdown = LevelChangeTime;

Camera2D OverlayCamera = { 0 };

Vector2 GetDisplaySize()
{
if (IsWindowFullscreen())
return Vector2{ (float)GetMonitorWidth(GetCurrentMonitor()), (float)GetMonitorHeight(GetCurrentMonitor()) };
else
return Vector2{ (float)GetScreenWidth(), (float)GetScreenHeight() };
}

void ToggleFullscreenState()
{
if (IsWindowFullscreen())
{
ToggleFullscreen();
SetWindowSize(WindowWidth, WindowHeight);
CenterWindow();
}
else
{
int monitor = GetCurrentMonitor();
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
ToggleFullscreen();
}
}

void CenterWindow()
{
int monitor = GetCurrentMonitor();

int x = GetMonitorWidth(monitor) / 2 - WindowWidth / 2;
int y = GetMonitorHeight(monitor) / 2 - WindowHeight / 2;

SetWindowPosition(x, y);
}

double GetCurrentTime()
{
return Time;
Expand Down Expand Up @@ -65,7 +101,8 @@ void DrawCenteredText(const char* text, float textSize = 20, float yOffset = 0.5
{
Vector2 size = MeasureTextEx(GetFontDefault(), text, textSize, textSize / 10);

Vector2 pos = { GetScreenWidth() * xOffset - size.x / 2.0f, GetScreenHeight() * yOffset - size.y / 2.0f };

Vector2 pos = { GetDisplaySize().x * xOffset - size.x / 2.0f, GetDisplaySize().y * yOffset - size.y / 2.0f };
DrawText(text, int(pos.x), int(pos.y), int(textSize), WHITE);
}

Expand All @@ -75,7 +112,7 @@ void DrawMiniMap()

float rad = center.x * 0.5f - 10;

center.x = GetScreenWidth() - (center.x / 2.0f);
center.x = GetDisplaySize().x - (center.x / 2.0f);
center.y = center.y / 2.0f;

DrawCircleV(center, rad, ColorAlpha(BLACK, 0.75f));
Expand All @@ -96,23 +133,47 @@ void DrawMiniMap()

DrawCircleV(relPos, 2, BROWN);
}

for (const auto& powerup : World::Instance->PowerUps)
{
if (!powerup.Alive || Vector2DistanceSqr(World::Instance->PlayerShip.Position, powerup.Position) >= viewDist * viewDist)
continue;

Vector2 relPos = Vector2Subtract(powerup.Position, World::Instance->PlayerShip.Position);
relPos = Vector2Scale(relPos, viewScale);
relPos = Vector2Add(relPos, center);

DrawCircleV(relPos, 1, PURPLE);
}
}

void DrawGameHud()
{
DrawText(TextFormat("Score %d", World::Instance->PlayerShip.Score), 0, 0, 40, BLUE);
OverlayCamera.zoom = 1.0f;
if (World::Instance->Shake())
OverlayCamera.offset = Vector2{ sinf(float(GetCurrentTime() * 90)) * 3, sinf(float(GetCurrentTime() * 180)) * 3 };
else
OverlayCamera.offset = Vector2Zero();

BeginMode2D(OverlayCamera);
DrawText(TextFormat("Level %d | Score %d", Level, World::Instance->PlayerShip.GetScore()), 0, 0, 40, BLUE);

DrawMiniMap();
Vector2 upperRight = { float(GetScreenWidth()),0 };
if (World::Instance->GetActiveAsteroidCount() < 10)
{
DrawCenteredText(TextFormat("Asteroids Left : %d", World::Instance->GetActiveAsteroidCount()), 20, 0.125f);
}

Vector2 upperRight = { float(GetDisplaySize().x),0 };
Sprites::DrawJustfied(Sprites::MiniMapSprite, upperRight, Sprites::Justifications::Max, Sprites::Justifications::Min);

float topBarWidth = 222+33;
float center = GetScreenWidth()/ 2.0f - topBarWidth / 2.0f;
float center = GetDisplaySize().x / 2.0f - topBarWidth / 2.0f;

Sprites::DrawJustfied(Sprites::BoostIcon, Vector2{ center, 3 }, Sprites::Justifications::Min, Sprites::Justifications::Min);
Sprites::DrawJustfied(Sprites::ShieldIcon, Vector2{ center, 43 }, Sprites::Justifications::Min, Sprites::Justifications::Min);

center = GetScreenWidth() / 2.0f + topBarWidth / 2.0f;
center = GetDisplaySize().x / 2.0f + topBarWidth / 2.0f;

float boostFactor = World::Instance->PlayerShip.Power / 1000.0f;
float shieldFactor = World::Instance->PlayerShip.Shield / 1000.0f;
Expand Down Expand Up @@ -142,6 +203,7 @@ void DrawGameHud()

Sprites::DrawJustfied(Sprites::ShieldProgress, Vector2{ center, 43 }, Sprites::Justifications::Max, Sprites::Justifications::Min, Vector2{ shieldFactor * 222, 33 }, c);
}
EndMode2D();
}

void DrawLevelChangeCountdown()
Expand All @@ -151,7 +213,7 @@ void DrawLevelChangeCountdown()

void DrawGameOver()
{
DrawCenteredText(TextFormat("Game Over, your score was %d, good job!", World::Instance->PlayerShip.Score), 20, 0.25f);
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))
{
Expand Down Expand Up @@ -207,5 +269,5 @@ void DrawOverlay()
break;
}

DrawFPS(0, GetScreenHeight() - 20);
DrawFPS(0, (int)GetDisplaySize().y - 20);
}
11 changes: 10 additions & 1 deletion game/include/game.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "raylib.h"

enum class GameStates
{
Menu,
Expand All @@ -15,4 +17,11 @@ double GetCurrentTime();
float GetDeltaTime();

void UpdateGame();
void DrawOverlay();
void DrawOverlay();

Vector2 GetDisplaySize();
void ToggleFullscreenState();
void CenterWindow();

constexpr int WindowWidth = 1280;
constexpr int WindowHeight = 800;
7 changes: 6 additions & 1 deletion game/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Player : public Entity
static constexpr float NominalPower = 1000;

float Reload = 0;
int Score = 0;

bool Thrusting = false;
bool Boost = false;

Expand All @@ -30,7 +30,12 @@ class Player : public Entity
void Reset();
void Respawn();

void AddScore(int scoreDelta);
inline int GetScore() const { return Score; }

protected:
float ShieldHitAngle = 0;
float ShieldHitLifetime = -1;

int Score = 0;
};
1 change: 1 addition & 0 deletions game/include/powerup.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PowerUp : public Entity

PowerType Type = PowerType::Shot;

void Update() override;
void Draw() const override;

bool Collide(const Entity& other) override;
Expand Down
95 changes: 95 additions & 0 deletions game/include/resource_dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**********************************************************************************************
*
* raylibExtras * Utilities and Shared Components for Raylib
*
* Resource Dir * function to help find resource dir in common locations
*
* LICENSE: MIT
*
* Copyright (c) 2022 Jeffery Myers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************/

#pragma once

#include "raylib.h"

#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
#endif
/// <summary>
/// Looks for the specified resource dir in several common locations
/// The working dir
/// The app dir
/// Up to 3 levels above the app dir
/// When found the dir will be set as the working dir so that assets can be loaded relative to that.
/// </summary>
/// <param name="folderName">The name of the resources dir to look for</param>
/// <returns>True if a dir with the name was found, false if no change was made to the working dir</returns>
inline bool SearchAndSetResourceDir(const char* folderName)
{
// check the working dir
if (DirectoryExists(folderName))
{
ChangeDirectory(TextFormat("%s/%s", GetWorkingDirectory(), folderName));
return true;
}

const char* appDir = GetApplicationDirectory();

// check the applicationDir
const char* dir = TextFormat("%s%s", appDir, folderName);
if (DirectoryExists(dir))
{
ChangeDirectory(dir);
return true;
}

// check one up from the app dir
dir = TextFormat("%s../%s", appDir, folderName);
if (DirectoryExists(dir))
{
ChangeDirectory(dir);
return true;
}

// check two up from the app dir
dir = TextFormat("%s../../%s", appDir, folderName);
if (DirectoryExists(dir))
{
ChangeDirectory(dir);
return true;
}

// check three up from the app dir
dir = TextFormat("%s../../../%s", appDir, folderName);
if (DirectoryExists(dir))
{
ChangeDirectory(dir);
return true;
}

return false;
}

#if defined(__cplusplus)
}
#endif
5 changes: 5 additions & 0 deletions game/include/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ class World

bool IsLevelClear() const { return LevelClear; }

size_t GetActiveAsteroidCount() const { return ActiveAsteroidCount; }

bool BounceBounds(Entity& entity);

bool Shake() const { return PlayerShip.Alive && PlayerShip.Boost; }

protected:
bool LevelClear = false;
size_t ActiveAsteroidCount = 0;
};
Loading

0 comments on commit 243c5f7

Please sign in to comment.