Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux update and new asteroids #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Chapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand Down Expand Up @@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand Down Expand Up @@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All @@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand Down Expand Up @@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file added Chapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets