1212#include " Actor.h"
1313#include " SpriteComponent.h"
1414#include " Ship.h"
15+ #include " Asteroid.h"
16+ #include " Random.h"
1517
1618Game::Game ()
1719:mWindow(nullptr )
@@ -50,6 +52,8 @@ bool Game::Initialize()
5052 return false ;
5153 }
5254
55+ Random::Init ();
56+
5357 LoadData ();
5458
5559 mTicksCount = SDL_GetTicks ();
@@ -80,14 +84,18 @@ void Game::ProcessInput()
8084 }
8185 }
8286
83- const Uint8* state = SDL_GetKeyboardState (NULL );
84- if (state [SDL_SCANCODE_ESCAPE ])
87+ const Uint8* keyState = SDL_GetKeyboardState (NULL );
88+ if (keyState [SDL_SCANCODE_ESCAPE ])
8589 {
8690 mIsRunning = false ;
8791 }
8892
89- // Process ship input
90- mShip ->ProcessKeyboard (state);
93+ mUpdatingActors = true ;
94+ for (auto actor : mActors )
95+ {
96+ actor->ProcessInput (keyState);
97+ }
98+ mUpdatingActors = false ;
9199}
92100
93101void Game::UpdateGame ()
@@ -138,7 +146,7 @@ void Game::UpdateGame()
138146
139147void Game::GenerateOutput ()
140148{
141- SDL_SetRenderDrawColor (mRenderer , 0 , 0 , 0 , 255 );
149+ SDL_SetRenderDrawColor (mRenderer , 220 , 220 , 220 , 255 );
142150 SDL_RenderClear (mRenderer );
143151
144152 // Draw all sprite components
@@ -154,7 +162,15 @@ void Game::LoadData()
154162{
155163 // Create player's ship
156164 mShip = new Ship (this );
157- mShip ->SetPosition (Vector2 (100 .0f , 384 .0f ));
165+ mShip ->SetPosition (Vector2 (512 .0f , 384 .0f ));
166+ mShip ->SetRotation (Math::PiOver2);
167+
168+ // Create asteroids
169+ const int numAsteroids = 20 ;
170+ for (int i = 0 ; i < numAsteroids; i++)
171+ {
172+ new Asteroid (this );
173+ }
158174}
159175
160176void Game::UnloadData ()
@@ -207,6 +223,21 @@ SDL_Texture* Game::GetTexture(const std::string& fileName)
207223 return tex;
208224}
209225
226+ void Game::AddAsteroid (Asteroid* ast)
227+ {
228+ mAsteroids .emplace_back (ast);
229+ }
230+
231+ void Game::RemoveAsteroid (Asteroid* ast)
232+ {
233+ auto iter = std::find (mAsteroids .begin (),
234+ mAsteroids .end (), ast);
235+ if (iter != mAsteroids .end ())
236+ {
237+ mAsteroids .erase (iter);
238+ }
239+ }
240+
210241void Game::Shutdown ()
211242{
212243 UnloadData ();
0 commit comments