-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerUpdateComponent.cpp
More file actions
111 lines (95 loc) · 2.23 KB
/
Copy pathPlayerUpdateComponent.cpp
File metadata and controls
111 lines (95 loc) · 2.23 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
#include "PlayerUpdateComponent.h"
#include "WorldState.h"
using namespace std;
using namespace sf;
void PlayerUpdateComponent::update(float dt)
{
if (sf::Joystick::isConnected(0))
{
m_TC->getLocation().x += ((m_Speed / 100) * m_XExtent) * dt;
m_TC->getLocation().y += ((m_Speed / 100) * m_YExtent) * dt;
}
// Left and right
if (m_IsHoldingLeft)
{
m_TC->getLocation().x -= m_Speed * dt;
}
else if (m_IsHoldingRight)
{
m_TC->getLocation().x += m_Speed * dt;
}
// Up and down
if (m_IsHoldingUp)
{
m_TC->getLocation().y -= m_Speed * dt;
}
else if (m_IsHoldingDown)
{
m_TC->getLocation().y += m_Speed * dt;
}
// Update collider
m_RCC->setOrMoveCollider(
m_TC->getLocation().x,
m_TC->getLocation().y,
m_TC->getSize().x,
m_TC->getSize().y
);
// Make sure the ship doesn't go outside the allowed area
if (m_TC->getLocation().x > WorldState::WORLD_WIDTH - m_TC->getSize().x)
{
m_TC->getLocation().x = WorldState::WORLD_WIDTH - m_TC->getSize().x;
}
else if (m_TC->getLocation().x < 0)
{
m_TC->getLocation().x = 0;
}
if (m_TC->getLocation().y > WorldState::WORLD_HEIGHT - m_TC->getSize().y)
{
m_TC->getLocation().y = WorldState::WORLD_HEIGHT - m_TC->getSize().y;
}
else if (m_TC->getLocation().y < WorldState::WORLD_HEIGHT / 2)
{
m_TC->getLocation().y = WorldState::WORLD_HEIGHT / 2;
}
}
void PlayerUpdateComponent::updateShipTravelWithController(float x, float y)
{
m_XExtent = x;
m_YExtent = y;
}
void PlayerUpdateComponent::moveLeft()
{
m_IsHoldingLeft = true;
stopRight();
}
void PlayerUpdateComponent::moveRight()
{
m_IsHoldingRight = true;
stopLeft();
}
void PlayerUpdateComponent::moveUp()
{
m_IsHoldingUp = true;
stopDown();
}
void PlayerUpdateComponent::moveDown()
{
m_IsHoldingDown = true;
stopUp();
}
void PlayerUpdateComponent::stopLeft()
{
m_IsHoldingLeft = false;
}
void PlayerUpdateComponent::stopRight()
{
m_IsHoldingRight = false;
}
void PlayerUpdateComponent::stopUp()
{
m_IsHoldingUp = false;
}
void PlayerUpdateComponent::stopDown()
{
m_IsHoldingDown = false;
}