-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayState.cpp
86 lines (71 loc) · 1.91 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
#include "PlayState.h"
const std::string PlayState::s_playID = "PLAY";
void PlayState::update()
{
if(TheInputHandler::instance()->isKeyDown(SDL_SCANCODE_ESCAPE))
{
TheGame::instance()->getStateMachine()->pushState(new PauseState());
}
for(int i = 0; i < 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()
{
pLevel->render();
for(int i = 0; i < m_gameObjects.size(); i++)
{
m_gameObjects[i]->draw();
}
}
bool PlayState::onEnter()
{
LevelParser levelParser;
pLevel = levelParser.parseLevel("../assets/book_example.tmx");
StateParser stateParser;
stateParser.parseState("../conf/play.xml", s_playID, &m_gameObjects, &m_textureIDList);
std::cout << "entering PlayState" << std::endl;
return true;
}
bool PlayState::onExit()
{
for(int i = 0; i < m_gameObjects.size(); i++ )
{
m_gameObjects[i]->clean();
}
m_gameObjects.clear();
//clear texture manager.
for(int i = 0; i < m_textureIDList.size(); i++)
{
TheTextureManager::instance()->clearFromTextureMap(m_textureIDList[i]);
}
std::cout << "exiting PlayState" << std::endl;
return true;
}
bool PlayState::checkCollision(SDLGameObject* p1, SDLGameObject* p2)
{
int leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB;
leftA = p1->getPosition().getX();
rightA = p1->getPosition().getX() + p1->getWidth();
topA = p1->getPosition().getY();
bottomA = p1->getPosition().getY() + p1->getHeight();
leftB = p2->getPosition().getX();
rightB = p2->getPosition().getX() + p2->getWidth();
topB = p2->getPosition().getY();
bottomB = p2->getPosition().getY() + p2->getHeight();
if(
bottomA <= topB ||
topA >= bottomB ||
rightA <= leftB ||
leftA >= rightB
)
{
return false;
}
return true;
}