Skip to content

Commit

Permalink
Make powerups do more
Browse files Browse the repository at this point in the history
more audio
  • Loading branch information
JeffM2501 committed Jan 7, 2023
1 parent 243c5f7 commit 8d3f420
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 23 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Asteroids Example
An example game of asteroids for raylib
An example game of asteroids for raylib written in C++.

## building
Uses game-premake
https://github.com/raylib-extras/game-premake


## Pre-Release
Pre-Release, still a work in progress.

## Goals


## Design


## TODO
* UFO
* Controller Support
Expand Down
10 changes: 9 additions & 1 deletion game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#include "common.h"
#include "world.h"
#include "sprites.h"
#include "sounds.h"

#include <cmath>

constexpr char Version[] = "Ver 0.0.2.01072023a Copyright 2023 Jeffery Myers";

constexpr float LevelChangeTime = 10;

GameStates GameState = GameStates::Menu;
Expand Down Expand Up @@ -86,7 +89,9 @@ void UpdateGame()
{
if (!World::Instance->PlayerShip.Alive)
{
Sounds::PlaySoundEffect(Sounds::GameOver);
GameState = GameStates::GameOver;
Sounds::SetThrustState(false, false);
}
else if(World::Instance->IsLevelClear())
{
Expand All @@ -101,7 +106,6 @@ void DrawCenteredText(const char* text, float textSize = 20, float yOffset = 0.5
{
Vector2 size = MeasureTextEx(GetFontDefault(), text, textSize, textSize / 10);


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 Down Expand Up @@ -239,7 +243,11 @@ void DrawMenu()
GameState = GameStates::Playing;
World::Instance->PlayerShip.Reset();
World::Instance->Reset(Level);

Sounds::PlaySoundEffect(Sounds::Begin);
}

DrawText(Version, 0, 0, 20, GRAY);
}

void DrawOverlay()
Expand Down
8 changes: 8 additions & 0 deletions game/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class Player : public Entity
{
public:
static constexpr float NominalShield = 1000;
static constexpr float NominalShieldRecharge = 2;
static constexpr float NominalPower = 1000;
static constexpr float NominalThrust = 400;
static constexpr float NominalBoostMultiplyer = 3;

float Reload = 0;

Expand All @@ -21,6 +24,11 @@ class Player : public Entity
float MaxPower = NominalPower;
float Power = MaxPower;

float MaxThrust = NominalThrust;
float BoostMultiplyer = NominalBoostMultiplyer;

float ShieldRecharge = NominalShieldRecharge;

Player();
void Draw() const override;
void Update() override;
Expand Down
4 changes: 4 additions & 0 deletions game/include/sounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Sounds
extern size_t Destoryed;
extern size_t SheldHit;
extern size_t Shot;
extern size_t Upgrade;

extern size_t GameOver;
extern size_t Begin;

void Init();
void Shutdown();
Expand Down
4 changes: 2 additions & 2 deletions game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ int main ()

Rectangle screen = { 0,0, center.x*2, center.y*2 };
Vector2 screenOriginInWorld = GetScreenToWorld2D(Vector2Zero(), worldCamera);
Vector2 screenEdgeInWorld = GetScreenToWorld2D(Vector2{ screen.width,screen.height }, worldCamera);
Vector2 screenEdgeInWorld = GetScreenToWorld2D(Vector2{ screen.width, screen.height }, worldCamera);
Rectangle screenInWorld = Rectangle{ screenOriginInWorld.x, screenOriginInWorld.y, screenEdgeInWorld.x - screenOriginInWorld.x,screenEdgeInWorld.y - screenOriginInWorld.y };

float bgScale = 0.5f;
Rectangle sourceRect = Rectangle{ screenInWorld.x * bgScale, screenInWorld.y * bgScale, screenInWorld.width * bgScale, screenInWorld.width * bgScale };
Rectangle sourceRect = Rectangle{ screenInWorld.x * bgScale, screenInWorld.y * bgScale, screenInWorld.width * bgScale, screenInWorld.height * bgScale };
DrawTexturePro(Background, sourceRect, screenInWorld, Vector2Zero(), 0, WHITE);

world.Draw(screenInWorld);
Expand Down
30 changes: 19 additions & 11 deletions game/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

constexpr float BaseReloadTime = 0.5f;
constexpr float ShieldHitMaxLife = 0.35f;
constexpr float BreakingFriction = 10;

Player::Player()
{
Expand Down Expand Up @@ -61,9 +62,9 @@ void Player::Update()
if (!Alive)
return;

Shield += GetDeltaTime() * 2;
if (Shield > 1000)
Shield = 1000;
Shield += GetDeltaTime() * ShieldRecharge;
if (Shield > MaxShield)
Shield = MaxShield;

// TODO, game pad support

Expand All @@ -74,7 +75,7 @@ void Player::Update()

if (!wantBoost)
Boost = false;
else if (wantBoost && Power > 250)
else if (wantBoost && Power > MaxPower / 4)
Boost = true;
else if (Power <= 1)
Boost = false;
Expand All @@ -83,15 +84,15 @@ void Player::Update()
{
Power -= GetDeltaTime() * 400;
}
else if (Power < 1000)
else if (Power < MaxPower)
{
Power += GetDeltaTime() * 20;
}

if (Power < 0)
Power = 0;
if (Power > 1000)
Power = 1000;
if (Power > MaxPower)
Power = MaxPower;

Reload -= GetDeltaTime() * ShotSpeedMultiplyer;

Expand All @@ -106,10 +107,10 @@ void Player::Update()

Vector2 shipVector = Vector2{ sinf(Orientation * DEG2RAD), -cosf(Orientation * DEG2RAD) };

float speed = 30 + GetDeltaTime();
float speed = MaxThrust * GetDeltaTime();

if (Boost)
speed *= 3;
speed *= BoostMultiplyer;

if (wantThrust)
{
Expand All @@ -119,7 +120,7 @@ void Player::Update()

float frictionScale = 1;
if (IsKeyDown(KEY_S))
frictionScale = 30;
frictionScale *= BreakingFriction;

Vector2 normVel = Vector2Normalize(Velocity);
Vector2 friction = Vector2Scale(normVel, -90.0f * frictionScale * GetFrameTime());
Expand Down Expand Up @@ -166,7 +167,11 @@ bool Player::Collide(const Entity& other)
{
Explosion::Create(other.Position, other.Radius);

Shield -= other.Radius / 1.0f;
float damageFactor = 1.0f;
if (Boost)
damageFactor = 3.0f;

Shield -= other.Radius * damageFactor;

Sounds::PlaySoundEffect(Sounds::SheldHit);

Expand Down Expand Up @@ -195,6 +200,9 @@ void Player::Reset()
Score = 0;
Power = MaxPower;
Shield = MaxShield;
BoostMultiplyer = NominalBoostMultiplyer;
MaxThrust = NominalThrust;
ShieldRecharge = NominalShieldRecharge;
Respawn();
}

Expand Down
14 changes: 11 additions & 3 deletions game/powerup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "world.h"
#include "common.h"
#include "sprites.h"
#include "sounds.h"

constexpr float PowerupRadius = 55;

Expand Down Expand Up @@ -34,18 +35,25 @@ bool PowerUp::Collide(const Entity& other)
{
if (Entity::Collide(other))
{
Sounds::PlaySoundEffect(Sounds::Upgrade);

Player& player = World::Instance->PlayerShip;
switch (Type)
{
case PowerUp::PowerType::Shot:
World::Instance->PlayerShip.ShotSpeedMultiplyer += 0.25f;
player.ShotSpeedMultiplyer += 0.10f;
break;

case PowerUp::PowerType::Shield:
World::Instance->PlayerShip.Shield += World::Instance->PlayerShip.MaxShield / 8.0f;
player.Shield += World::Instance->PlayerShip.MaxShield / 8.0f;
player.ShieldRecharge += player.NominalShieldRecharge;

break;

case PowerUp::PowerType::Boost:
World::Instance->PlayerShip.Power += World::Instance->PlayerShip.MaxPower / 4.0f;
player.Power += World::Instance->PlayerShip.MaxPower / 4.0f;
player.BoostMultiplyer += 0.1;
player.MaxThrust += player.NominalThrust * 0.25f;
break;
}

Expand Down
13 changes: 10 additions & 3 deletions game/sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <vector>



namespace Sounds
{
std::vector<Sound> Effects;
Expand All @@ -16,6 +14,10 @@ namespace Sounds
size_t Destoryed = 0;
size_t SheldHit = 0;
size_t Shot = 0;
size_t Upgrade = 0;

size_t GameOver = 0;
size_t Begin = 0;

size_t AddEffect(const char* name)
{
Expand All @@ -28,9 +30,11 @@ namespace Sounds
InitAudioDevice();
BGM = LoadMusicStream("bgm.ogg");
BGM.looping = true;
SetMusicVolume(BGM, 0.25f);

Thrust = LoadMusicStream("thruster.ogg");
Thrust.looping = true;
SetMusicVolume(Thrust, 1.0f);

Boost = LoadMusicStream("boost.ogg");
Boost.looping = true;
Expand All @@ -39,6 +43,9 @@ namespace Sounds
Destoryed = AddEffect("destoryed.ogg");
SheldHit = AddEffect("shield_hit.ogg");
Shot = AddEffect("shot.ogg");
Upgrade = AddEffect("upgrade.ogg");
GameOver = AddEffect("game_over.ogg");
Begin = AddEffect("begin.ogg");
}

void Shutdown()
Expand Down Expand Up @@ -115,6 +122,6 @@ namespace Sounds
void PlaySoundEffect(size_t effect)
{
if (effect <= Effects.size())
PlaySound(Effects[effect]);
PlaySoundMulti(Effects[effect]);
}
}
5 changes: 4 additions & 1 deletion game/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ void World::Update()

void World::Draw(Rectangle& screenInWorld) const
{
DrawRectangleLinesEx(Bounds, 40, SKYBLUE);
float thickness = 30 + sinf(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);

float viewRadSq = powf(screenInWorld.width * 0.65f, 2) + powf(screenInWorld.height * 0.65f, 2);

Expand Down
Binary file added resources/begin.ogg
Binary file not shown.
Binary file modified resources/boost.ogg
Binary file not shown.
Binary file added resources/game_over.ogg
Binary file not shown.
Binary file added resources/upgrade.ogg
Binary file not shown.

0 comments on commit 8d3f420

Please sign in to comment.