-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameInputHandler.cpp
131 lines (107 loc) · 2.55 KB
/
GameInputHandler.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
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
#include "GameInputHandler.h"
#include "SoundEngine.h"
#include "GameScreen.h"
class BulletSpawner;
void GameInputHandler::initialize()
{
m_PUC = static_pointer_cast<PlayerUpdateComponent>
(getPointerToScreenManagerRemoteControl()
->shareGameObjectSharer()
.findFirstObjectWithTag("Player")
.getComponentByTypeAndSpecificType(
"update", "player"));
m_PTC = getPointerToScreenManagerRemoteControl()->
shareGameObjectSharer().findFirstObjectWithTag(
"Player").getTransformComponent();
}
void GameInputHandler::handleGamepad()
{
float deadZone = 10.0f;
float x = Joystick::getAxisPosition(0, sf::Joystick::X);
float y = Joystick::getAxisPosition(0, sf::Joystick::Y);
if (x < deadZone && x > -deadZone)
{
x = 0;
}
if (y < deadZone && y > -deadZone)
{
y = 0;
}
m_PUC->updateShipTravelWithController(x, y);
// Has the player pressed the B button?
if (Joystick::isButtonPressed(0, 1))
{
mBButtonPressed = true;
}
// Has player just released the B button?
if (!Joystick::isButtonPressed(0, 1) && mBButtonPressed)
{
mBButtonPressed = false;
// Shoot a bullet
SoundEngine::playShoot();
Vector2f spawnLocation;
spawnLocation.x = m_PTC->getLocation().x +
m_PTC->getSize().x / 2;
spawnLocation.y = m_PTC->getLocation().y;
static_cast<GameScreen*>(getmParentScreen())->
getBulletSpawner()->spawnBullet(spawnLocation, true);
}
}
void GameInputHandler::handleKeyPressed(
Event& event, RenderWindow& window)
{
// Handle key presses
if (event.key.code == Keyboard::Escape)
{
SoundEngine::playClick();
getPointerToScreenManagerRemoteControl()->
SwitchScreens("Select");
}
if (event.key.code == Keyboard::Left)
{
m_PUC->moveLeft();
}
if (event.key.code == Keyboard::Right)
{
m_PUC->moveRight();
}
if (event.key.code == Keyboard::Up)
{
m_PUC->moveUp();
}
if (event.key.code == Keyboard::Down)
{
m_PUC->moveDown();
}
}
void GameInputHandler::handleKeyReleased(
Event& event, RenderWindow& window)
{
if (event.key.code == Keyboard::Left)
{
m_PUC->stopLeft();
}
else if (event.key.code == Keyboard::Right)
{
m_PUC->stopRight();
}
else if (event.key.code == Keyboard::Up)
{
m_PUC->stopUp();
}
else if (event.key.code == Keyboard::Down)
{
m_PUC->stopDown();
}
else if (event.key.code == Keyboard::Space)
{
// Shoot a bullet
SoundEngine::playShoot();
Vector2f spawnLocation;
spawnLocation.x = m_PTC->getLocation().x +
m_PTC->getSize().x / 2;
spawnLocation.y = m_PTC->getLocation().y;
static_cast<GameScreen*>(getmParentScreen())->
spawnBullet(spawnLocation, true);
}
}