-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPlayState.cpp
101 lines (84 loc) · 2.53 KB
/
PlayState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "PlayState.h"
#include <iostream>
#include "TextureManager.h"
#include "Game.h"
#include "Player.h"
#include "Enemy.h"
#include "InputHandler.h"
#include "PauseState.h"
#include "GameOverState.h"
const std::string PlayState::s_playID = "PLAY";
void PlayState::update()
{
//listen for ESC key to pause the game
if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_ESCAPE))
{
TheGame::Instance()->getStateMachine()->pushState(new PauseState());
}
for (int i = 0; i < (int)m_gameObjects.size(); ++i)
{
m_gameObjects[i]->update();
}
if (checkCollision(dynamic_cast<SDLGameObject*>(m_gameObjects[0]), dynamic_cast<SDLGameObject*>(m_gameObjects[1])))
{
TheGame::Instance()->getStateMachine()->pushState(new GameOverState());
}
}
void PlayState::render()
{
for (int i = 0; i < (int)m_gameObjects.size(); ++i)
{
m_gameObjects[i]->draw();
}
}
bool PlayState::onEnter()
{
if (!TheTextureManager::Instance()->load("assets/helicopter.png", "helicopter", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/helicopter2.png", "helicopter2", TheGame::Instance()->getRenderer()))
{
return false;
}
GameObject* player = new Player(new LoaderParams(500, 100, 128, 55, 5, "helicopter"));
GameObject* enemy = new Enemy(new LoaderParams(100, 100, 128, 55, 5, "helicopter2"));
m_gameObjects.push_back(player);
m_gameObjects.push_back(enemy);
std::cout << "entering PlayState\n";
return true;
}
bool PlayState::onExit()
{
for (int i = 0; i < (int)m_gameObjects.size(); ++i)
{
m_gameObjects[i]->clean();
}
m_gameObjects.clear();
TheTextureManager::Instance()->clearFromTextureMap("helicopter");
std::cout << "exiting PlayState\n";
return true;
}
//collision detection, will trigger the GameOverState
bool PlayState::checkCollision(SDLGameObject* p1, SDLGameObject* p2)
{
float leftA, leftB;
float rightA, rightB;
float topA, topB;
float bottomA, bottomB;
leftA = p1->getPosition().getX();
rightA = p1->getPosition().getX() + p1->getWidth();
topA = p1->getPosition().getY();
bottomA = p1->getPosition().getY() + p1->getHeight();
//Calculate the sides of rect B
leftB = p2->getPosition().getX();
rightB = p2->getPosition().getX() + p2->getWidth();
topB = p2->getPosition().getY();
bottomB = p2->getPosition().getY() + p2->getHeight();
//If any of the sides from A are outside of B
if (bottomA <= topB) { return false; }
if (topA >= bottomB) { return false; }
if (rightA <= leftB) { return false; }
if (leftA >= rightB) { return false; }
return true;
}