diff --git a/Chapter03/Assets/NewAsteroid.png b/Chapter03/Assets/NewAsteroid.png new file mode 100644 index 00000000..ac5af17c Binary files /dev/null and b/Chapter03/Assets/NewAsteroid.png differ diff --git a/Chapter03/Assets/StartScreen.png b/Chapter03/Assets/StartScreen.png new file mode 100644 index 00000000..67e7eac7 Binary files /dev/null and b/Chapter03/Assets/StartScreen.png differ diff --git a/Chapter03/Game.cpp b/Chapter03/Game.cpp index ed758b2a..4f657880 100644 --- a/Chapter03/Game.cpp +++ b/Chapter03/Game.cpp @@ -7,14 +7,14 @@ // ---------------------------------------------------------------- #include "Game.h" -#include "SDL/SDL_image.h" +#include "SDL_image.h" #include #include "Actor.h" #include "SpriteComponent.h" #include "Ship.h" #include "Asteroid.h" #include "Random.h" - +#include "NewAsteroid.h" Game::Game() :mWindow(nullptr) ,mRenderer(nullptr) @@ -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() @@ -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(); diff --git a/Chapter03/Game.h b/Chapter03/Game.h index 5a7b8261..fc607e31 100644 --- a/Chapter03/Game.h +++ b/Chapter03/Game.h @@ -7,11 +7,10 @@ // ---------------------------------------------------------------- #pragma once -#include "SDL/SDL.h" +#include "SDL.h" #include #include #include - class Game { public: @@ -32,6 +31,10 @@ class Game void AddAsteroid(class Asteroid* ast); void RemoveAsteroid(class Asteroid* ast); std::vector& GetAsteroids() { return mAsteroids; } + + void AddNewAsteroid(class NewAsteroid* ast); + void RemoveNewAsteroid(class NewAsteroid* ast); + std::vector& GetNewAsteroids() { return mNewAsteroids; } private: void ProcessInput(); void UpdateGame(); @@ -60,4 +63,6 @@ class Game // Game-specific class Ship* mShip; // Player's ship std::vector mAsteroids; + std::vector mNewAsteroids; + }; diff --git a/Chapter03/Laser.cpp b/Chapter03/Laser.cpp index a03c1981..5c94beb6 100644 --- a/Chapter03/Laser.cpp +++ b/Chapter03/Laser.cpp @@ -1,10 +1,4 @@ -// ---------------------------------------------------------------- -// 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" @@ -12,45 +6,62 @@ #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; + } + } + } } diff --git a/Chapter03/NewAsteroid.cpp b/Chapter03/NewAsteroid.cpp new file mode 100644 index 00000000..fdcd7786 --- /dev/null +++ b/Chapter03/NewAsteroid.cpp @@ -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); +} \ No newline at end of file diff --git a/Chapter03/NewAsteroid.h b/Chapter03/NewAsteroid.h new file mode 100644 index 00000000..80897d5d --- /dev/null +++ b/Chapter03/NewAsteroid.h @@ -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; +}; diff --git a/Chapter03/SpriteComponent.h b/Chapter03/SpriteComponent.h index c430e888..4f49bd0c 100644 --- a/Chapter03/SpriteComponent.h +++ b/Chapter03/SpriteComponent.h @@ -8,7 +8,7 @@ #pragma once #include "Component.h" -#include "SDL/SDL.h" +#include "SDL.h" class SpriteComponent : public Component { public: diff --git a/Chapter03/build/Assets/Asteroid.png b/Chapter03/build/Assets/Asteroid.png new file mode 100644 index 00000000..98e831e9 Binary files /dev/null and b/Chapter03/build/Assets/Asteroid.png differ diff --git a/Chapter03/build/Assets/Laser.png b/Chapter03/build/Assets/Laser.png new file mode 100644 index 00000000..95352d27 Binary files /dev/null and b/Chapter03/build/Assets/Laser.png differ diff --git a/Chapter03/build/Assets/NewAsteroid.png b/Chapter03/build/Assets/NewAsteroid.png new file mode 100644 index 00000000..ac5af17c Binary files /dev/null and b/Chapter03/build/Assets/NewAsteroid.png differ diff --git a/Chapter03/build/Assets/Ship.png b/Chapter03/build/Assets/Ship.png new file mode 100644 index 00000000..96514436 Binary files /dev/null and b/Chapter03/build/Assets/Ship.png differ diff --git a/Chapter03/build/Assets/ShipWithThrust.png b/Chapter03/build/Assets/ShipWithThrust.png new file mode 100644 index 00000000..ed9a43e2 Binary files /dev/null and b/Chapter03/build/Assets/ShipWithThrust.png differ diff --git a/Chapter03/build/Assets/StartScreen.png b/Chapter03/build/Assets/StartScreen.png new file mode 100644 index 00000000..67e7eac7 Binary files /dev/null and b/Chapter03/build/Assets/StartScreen.png differ diff --git a/Chapter03/makefile b/Chapter03/makefile new file mode 100644 index 00000000..29c77830 --- /dev/null +++ b/Chapter03/makefile @@ -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