Skip to content

Commit c38797d

Browse files
committed
added GameOverState and basic collision system in PlayState
1 parent ca94d9c commit c38797d

15 files changed

+259
-32
lines changed

AnimatedGraphic.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "AnimatedGraphic.h"
2+
3+
AnimatedGraphic::AnimatedGraphic(const LoaderParams* pParams, int animSpeed) : SDLGameObject(pParams), m_animSpeed(animSpeed)
4+
{
5+
//ctor
6+
}
7+
8+
void AnimatedGraphic::draw()
9+
{
10+
SDLGameObject::draw();
11+
}
12+
13+
void AnimatedGraphic::update()
14+
{
15+
m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % m_numFrames));
16+
}
17+
18+
void AnimatedGraphic::clean()
19+
{
20+
SDLGameObject::clean();
21+
}

AnimatedGraphic.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef ANIMATEDGRAPHIC_H
2+
#define ANIMATEDGRAPHIC_H
3+
4+
#include "SDLGameObject.h"
5+
6+
class AnimatedGraphic : public SDLGameObject
7+
{
8+
public:
9+
AnimatedGraphic(const LoaderParams* pParams, int animSpeed); //constructor
10+
11+
virtual void draw();
12+
virtual void update();
13+
virtual void clean();
14+
15+
protected:
16+
private:
17+
18+
int m_animSpeed;
19+
};
20+
21+
#endif // ANIMATEDGRAPHIC_H

Enemy.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams)
44
{
5-
//ctor
5+
m_velocity.setY(2);
6+
m_velocity.setX(0.001);
67
}
78

89

@@ -13,9 +14,16 @@ void Enemy::draw()
1314

1415
void Enemy::update()
1516
{
16-
m_acceleration.setX(1);
17-
m_acceleration.setY(1);
18-
m_currentFrame = int((SDL_GetTicks() / 100) % 6);
17+
m_currentFrame = int((SDL_GetTicks() / 100) % m_numFrames);
18+
19+
if(m_position.getY() < 0)
20+
{
21+
m_velocity.setY(2);
22+
}
23+
else if (m_position.getY() > 400)
24+
{
25+
m_velocity.setY(-2);
26+
}
1927

2028
SDLGameObject::update();
2129
}

Game.cpp

+1-12
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,11 @@ bool Game::init(char* title, int xpos, int ypos, int width, int height, Uint32 f
4949
cout << "incialitacio correcte " << endl;
5050
m_running = true;
5151

52-
//load images/sprites on database;
53-
TextureManager::Instance()->load("img/animate-alpha.png", "animate_dog", m_pRenderer);
54-
55-
//Initialize GameObjects optmized
56-
m_gameObjects.push_back(new Enemy(new LoaderParams(100, 100, 128, 82, "animate_dog")));
57-
m_gameObjects.push_back(new Player(new LoaderParams(300, 300, 128, 82, "animate_dog")));
58-
m_gameObjects.push_back(new Player(new LoaderParams(400, 0, 128, 82, "animate_dog")));
59-
60-
61-
// no necessari ara mateix // SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceRectangle.w, &m_sourceRectangle.h); //guardem a msourceRectangle les dimensions de m_pTexture
62-
6352
//joystick handling
6453
TheInputHandler::Instance()->initialiseJoysticks();
6554

6655
//
67-
SDL_SetRenderDrawColor(m_pRenderer, 255, 0, 0, 255);
56+
SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
6857

6958
//init of GameStateMachine
7059
m_pGameStateMachine = new GameStateMachine();

Game.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ class Game //SINGLETON
6060
bool m_running; //Atribut per mirar si continua el loop del joc.
6161
SDL_Window* m_pWindow; //Window del joc.
6262
SDL_Renderer* m_pRenderer; //Renderer del joc.
63-
int m_currentFrame; //NOTE: is it needed?
6463
GameStateMachine* m_pGameStateMachine; //ACTUAL GAMESTATEMACHINE
65-
66-
//POLYMORFISM OBJECTS (have it declared allow us to create them everywhere
67-
68-
//STL containers
69-
vector<GameObject*> m_gameObjects; //saves all gameObjects
64+
7065
};
7166

7267
typedef Game TheGame;

GameOverState.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "GameOverState.h"
2+
#include "Game.h"
3+
#include "TextureManager.h"
4+
5+
const string GameOverState::s_gameOverID = "GAMEOVER";
6+
7+
GameOverState::GameOverState()
8+
{
9+
10+
}
11+
12+
void GameOverState::s_gameOverToMain()
13+
{
14+
TheGame::Instance()->getStateMachine()->changeState(new MenuState());
15+
}
16+
17+
void GameOverState::s_restartPlay()
18+
{
19+
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
20+
}
21+
22+
void GameOverState::update()
23+
{
24+
for (int i = 0; i < m_gameObjects.size(); i++)
25+
{
26+
m_gameObjects[i]->update();
27+
}
28+
}
29+
30+
void GameOverState::render()
31+
{
32+
for (int i = 0; i < m_gameObjects.size(); i++)
33+
{
34+
m_gameObjects[i]->draw();
35+
}
36+
}
37+
38+
bool GameOverState::onEnter()
39+
{
40+
if(!TheTextureManager::Instance()->load("img/gameover.png", "gameovertext", TheGame::Instance()->getRenderer()))
41+
{
42+
return false; //can't load texutre, error
43+
}
44+
if (!TheTextureManager::Instance()->load("img/main.png", "mainbutton", TheGame::Instance()->getRenderer()))
45+
{
46+
return false; //can't load texture, error
47+
}
48+
if(!TheTextureManager::Instance()->load("img/restart.png", "restartbutton", TheGame::Instance()->getRenderer()))
49+
{
50+
return false;
51+
}
52+
53+
GameObject* gameOverText = new AnimatedGraphic(new LoaderParams(200, 100, 190, 30, 2, "gameovertext"), 2);
54+
GameObject* button1 = new MenuButton(new LoaderParams(200, 200, 200, 80, 3, "mainbutton"), s_gameOverToMain);
55+
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, 3, "restartbutton"), s_restartPlay);
56+
57+
m_gameObjects.push_back(gameOverText);
58+
m_gameObjects.push_back(button1);
59+
m_gameObjects.push_back(button2);
60+
61+
cout << "entering GameOverState" << endl;
62+
return true;
63+
}
64+
65+
bool GameOverState::onExit()
66+
{
67+
for (int i = 0; i < m_gameObjects.size(); i++)
68+
{
69+
m_gameObjects[i]->clean();
70+
}
71+
m_gameObjects.clear();
72+
73+
TheTextureManager::Instance()->clearFromTextureMap("resumebutton");
74+
TheTextureManager::Instance()->clearFromTextureMap("mainbutton");
75+
76+
cout << "exiting GameOverState" << endl;
77+
return true;
78+
}
79+
80+
string GameOverState::getStateID() const
81+
{
82+
return s_gameOverID;
83+
}
84+

GameOverState.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef GAMEOVERSTATE_H
2+
#define GAMEOVERSTATE_H
3+
4+
#include "GameState.h"
5+
#include "AnimatedGraphic.h"
6+
#include <vector>
7+
#include <iostream>
8+
9+
using namespace std;
10+
11+
class GameOverState : public GameState
12+
{
13+
public:
14+
GameOverState();
15+
16+
virtual void update();
17+
virtual void render();
18+
19+
virtual bool onEnter();
20+
virtual bool onExit();
21+
22+
//GETs
23+
virtual string getStateID() const;
24+
25+
protected:
26+
private:
27+
28+
//private methods:
29+
static void s_gameOverToMain(); //changes GameState to Main Menu
30+
static void s_restartPlay(); //Reset PlayState
31+
32+
static const string s_gameOverID; //string identificator ID of the state
33+
34+
vector<GameObject*> m_gameObjects; //container with all the gameObjects
35+
};
36+
37+
#endif // GAMEOVERSTATE_H

LoaderParams.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ using namespace std;
88
class LoaderParams //This class load to abstract classes the right information
99
{
1010
public:
11-
LoaderParams(int x, int y, int width, int height, string textureID) : m_x(x),
12-
m_y(y), m_width(width), m_height(height), m_textureID(textureID)
11+
LoaderParams(int x, int y, int width, int height, int numFrames, string textureID) : m_x(x),
12+
m_y(y), m_width(width), m_height(height), m_numFrames(numFrames), m_textureID(textureID)
1313
{
1414

1515
}
@@ -18,6 +18,7 @@ class LoaderParams //This class load to abstract classes the right information
1818
int getY() const { return m_y; }
1919
int getWidth() const { return m_width; }
2020
int getHeight() const { return m_height; }
21+
int getnFrames() const { return m_numFrames; }
2122
string getTextureID() const { return m_textureID; }
2223

2324

@@ -28,6 +29,7 @@ class LoaderParams //This class load to abstract classes the right information
2829

2930
int m_width;
3031
int m_height;
32+
int m_numFrames;
3133

3234
string m_textureID;
3335
};

MenuState.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ bool MenuState::onEnter()
4343
return false;
4444
}
4545

46-
GameObject* button1 = new MenuButton(new LoaderParams(100, 100, 400, 100, "playbutton"), s_menuToPlay);
47-
GameObject* button2 = new MenuButton(new LoaderParams(100,300, 400, 100, "exitbutton"), s_exitFromMenu);
46+
GameObject* button1 = new MenuButton(new LoaderParams(100, 100, 400, 100, 3, "playbutton"), s_menuToPlay);
47+
GameObject* button2 = new MenuButton(new LoaderParams(100,300, 400, 100, 3, "exitbutton"), s_exitFromMenu);
4848

4949
m_gameObjects.push_back(button1);
5050
m_gameObjects.push_back(button2);

PauseState.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ bool PauseState::onEnter()
4141
return false; //can't load texture, error
4242
}
4343

44-
GameObject* button1 = new MenuButton(new LoaderParams(200, 100, 200, 80, "mainbutton"), s_pauseToMain);
45-
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, "resumebutton"), s_resumePlay);
44+
GameObject* button1 = new MenuButton(new LoaderParams(200, 100, 200, 80, 3, "mainbutton"), s_pauseToMain);
45+
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, 3, "resumebutton"), s_resumePlay);
4646

4747
m_gameObjects.push_back(button1);
4848
m_gameObjects.push_back(button2);

PlayState.cpp

+43-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ PlayState::PlayState()
1515

1616
void PlayState::update()
1717
{
18+
//input behaviour
1819
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_ESCAPE))
1920
{
2021
TheGame::Instance()->getStateMachine()->pushState(new PauseState());
2122
}
23+
24+
//update GameObjects
2225
for(int i = 0; i < m_gameObjects.size(); i++)
2326
{
2427
m_gameObjects[i]->update();
2528
}
29+
30+
//Collision detection
31+
if(checkCollision(dynamic_cast<SDLGameObject*>(m_gameObjects[0]), dynamic_cast<SDLGameObject*>(m_gameObjects[1])))
32+
{
33+
TheGame::Instance()->getStateMachine()->pushState(new GameOverState());
34+
}
2635
}
2736

2837
void PlayState::render()
@@ -39,8 +48,14 @@ bool PlayState::onEnter()
3948
{
4049
return false;
4150
}
42-
GameObject* player = new Player (new LoaderParams(100, 100, 128, 55, "helicopter"));
51+
if(!TheTextureManager::Instance()->load("img/helicopter2.png", "helicopter2", TheGame::Instance()->getRenderer()))
52+
{
53+
return false;
54+
}
55+
GameObject* player = new Player (new LoaderParams(100, 100, 128, 55, 5, "helicopter"));
56+
GameObject* enemy = new Enemy (new LoaderParams(500, 100, 128, 55, 5, "helicopter2"));
4357
m_gameObjects.push_back(player);
58+
m_gameObjects.push_back(enemy);
4459

4560
cout << "Entering PlayState" << endl;
4661
return true;
@@ -65,3 +80,30 @@ string PlayState::getStateID() const
6580
{
6681
return s_playID;
6782
}
83+
84+
bool PlayState::checkCollision(SDLGameObject* p1, SDLGameObject* p2)
85+
{
86+
int leftA, leftB;
87+
int rightA, rightB;
88+
int topA, topB;
89+
int bottomA, bottomB;
90+
91+
//Calculate sides of rect b
92+
leftA = p1->getPosition().getX();
93+
rightA = p1->getPosition().getX() + p1->getWidth();
94+
topA = p1->getPosition().getY();
95+
bottomA = p1->getPosition().getY() + p1->getHeight();
96+
97+
//Calculate sides of rect b
98+
leftB = p2->getPosition().getX();
99+
rightB = p2->getPosition().getX() + p2->getWidth();
100+
topB = p2->getPosition().getY();
101+
bottomB = p2->getPosition().getY() + p2->getHeight();
102+
103+
//check collision
104+
if (bottomA <= topB or topA >= bottomB or rightA <= leftB or leftA >= rightB) //no collision
105+
return false;
106+
else
107+
return true;
108+
109+
}

PlayState.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ using namespace std;
77
#include <string>
88
#include <vector>
99
#include "GameObject.h"
10-
#include "PauseState.h"
10+
#include "PauseState.h"
11+
#include "SDLGameObject.h"
12+
#include "GameOverState.h"
1113

1214

1315
class PlayState : public GameState
@@ -24,6 +26,10 @@ class PlayState : public GameState
2426
protected:
2527
private:
2628

29+
//Private methods:
30+
bool checkCollision(SDLGameObject* p1, SDLGameObject* p2); //checks collision between p1 and p2
31+
32+
//Atributes:
2733
static const string s_playID; //string ID identificator
2834
vector<GameObject*> m_gameObjects; //vector with all the gameObjects
2935
};

Player.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Player::update()
1818

1919
handleInput();
2020

21-
m_currentFrame = int((SDL_GetTicks() / 100) % 5);
21+
m_currentFrame = int((SDL_GetTicks() / 100) % m_numFrames);
2222

2323
SDLGameObject::update(); //call the up-class
2424
}

0 commit comments

Comments
 (0)