-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.cpp
More file actions
153 lines (143 loc) · 4.38 KB
/
Player.cpp
File metadata and controls
153 lines (143 loc) · 4.38 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "stdafx.h"
#include "Game.h"
#include "Player.h"
Player::Player(float newX, float newY): speed (4.f),
MovingEntity(newX, newY, true, true, "Sprites/Player.png", dlb::PLAYER)
{}
// TODO make update use elapsed time so always move the same speed
int Player::Update()
{
#ifdef DEBUG
std::cout << Game::GetMainWindow().GetFrameTime() << std::endl;
#endif
//Check for input and set speed. Make sure always on 32 x 32 grid.
if (Game::GetInput().IsKeyDown(sf::Key::Down))
{
if ( static_cast<int>(m_position.y) % 32 == 0)
if (m_hSpeed == 0)
m_vSpeed = speed ;//* Game::GetMainWindow().GetFrameTime();
}
else if (Game::GetInput().IsKeyDown(sf::Key::Up))
{
if ( static_cast<int>(m_position.y) % 32 == 0)
if (m_hSpeed == 0)
m_vSpeed = -speed ;//* Game::GetMainWindow().GetFrameTime();
}
else if ( static_cast<int>(m_position.y) % 32 == 0)
{
m_vSpeed = 0;
}
if (Game::GetInput().IsKeyDown(sf::Key::Left))
{
if ( static_cast<int>(m_position.x) % 32 == 0)
if (m_vSpeed == 0)
m_hSpeed = -speed ;//* Game::GetMainWindow().GetFrameTime();
}
else if (Game::GetInput().IsKeyDown(sf::Key::Right))
{
if (static_cast<int>(m_position.x) % 32 == 0)
if (m_vSpeed == 0)
m_hSpeed = speed ;//* Game::GetMainWindow().GetFrameTime();
}
else if ( static_cast<int>(m_position.x) % 32 == 0)
{
m_hSpeed = 0;
}
//Stop if no arrow key is down
if (!(Game::GetInput().IsKeyDown(sf::Key::Right)) &&
!(Game::GetInput().IsKeyDown(sf::Key::Left)) &&
!(Game::GetInput().IsKeyDown(sf::Key::Up)) &&
!(Game::GetInput().IsKeyDown(sf::Key::Down)))
{
if ((static_cast<int>(m_position.x) % 32 == 0) &&
(static_cast<int>(m_position.y) % 32 == 0))
{
m_hSpeed = 0;
m_vSpeed = 0;
}
}
//Check for collisions
CollisionWithWalls();
//Update Position
UpdatePosition();
#ifdef DEBUG
std::cout << "PlayerX: " << m_position.x << " PlayerY: " << m_position.y <<std::endl;
#endif
return 1;
}
//Check for collisions going each direction and stopping in grid if true
void Player::CollisionWithWalls()
{
if (m_hSpeed > 0)
{
for (std::vector<BaseObject*>::const_iterator wallIterator = Game::GetGameObjectManager().WallsForCollision().begin();
wallIterator != Game::GetGameObjectManager().WallsForCollision().end(); ++wallIterator)
{
if ( (((*wallIterator)->GetPositionX() - m_position.x) <= 63) &&
((abs((*wallIterator)->GetPositionY() - m_position.y)) < 32) &&
(((*wallIterator)->GetPositionX() - m_position.x) >= 0) )
{
if ((static_cast<int>(m_position.x) % 32 == 0) &&
(static_cast<int>(m_position.y) % 32 == 0))
{
m_vSpeed = 0;
m_hSpeed = 0;
}
}
}
}
else if (m_hSpeed < 0)
{
for (std::vector<BaseObject*>::const_iterator wallIterator = Game::GetGameObjectManager().WallsForCollision().begin();
wallIterator != Game::GetGameObjectManager().WallsForCollision().end(); ++wallIterator)
{
if ( ((m_position.x - (*wallIterator)->GetPositionX()) <= 36) &&
((abs((*wallIterator)->GetPositionY() - m_position.y)) < 32) &&
((m_position.x - (*wallIterator)->GetPositionX()) > 0) )
{
if ((static_cast<int>(m_position.x) % 32 == 0) &&
(static_cast<int>(m_position.y) % 32 == 0))
{
m_vSpeed = 0;
m_hSpeed = 0;
}
}
}
}
else if (m_vSpeed > 0)
{
for (std::vector<BaseObject*>::const_iterator wallIterator = Game::GetGameObjectManager().WallsForCollision().begin();
wallIterator != Game::GetGameObjectManager().WallsForCollision().end(); ++wallIterator)
{
if ( (((*wallIterator)->GetPositionY() - m_position.y) <= 45) &&
((abs((*wallIterator)->GetPositionX() - m_position.x)) < 32) &&
(((*wallIterator)->GetPositionY() - m_position.y) >= 0) )
{
if ((static_cast<int>(m_position.x) % 32 == 0) &&
(static_cast<int>(m_position.y) % 32 == 0))
{
m_vSpeed = 0;
m_hSpeed = 0;
}
}
}
}
else if (m_vSpeed < 0)
{
for (std::vector<BaseObject*>::const_iterator wallIterator = Game::GetGameObjectManager().WallsForCollision().begin();
wallIterator != Game::GetGameObjectManager().WallsForCollision().end(); ++wallIterator)
{
if ( ((m_position.y - (*wallIterator)->GetPositionY()) <= 45) &&
((abs((*wallIterator)->GetPositionX() - m_position.x)) < 32) &&
((m_position.y - (*wallIterator)->GetPositionY()) >= 0) )
{
if ((static_cast<int>(m_position.x) % 32 == 0) &&
(static_cast<int>(m_position.y) % 32 == 0))
{
m_vSpeed = 0;
m_hSpeed = 0;
}
}
}
}
}