-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
199 lines (178 loc) · 5.3 KB
/
input.cpp
File metadata and controls
199 lines (178 loc) · 5.3 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: input.cpp
* Author: ross
*
* Created on 03 October 2018, 03:29
*/
#include "input.h"
#include "game.h"
Input::Input()
{
}
Input::~Input()
{
}
void Input::handleInput(SDL_Event &event, Input &input, Player &player, Game *game)
{
//Poll event to see if any changes have occurred
if (SDL_PollEvent(&event))
{
//If a key has been pressed...
if(event.type == SDL_KEYDOWN)
{
//If the key isn't repeating...
if(event.key.repeat == 0)
{
//Register as a key down event
keyDownEvent(event);
}
}
//If a key has been released...
else if (event.type == SDL_KEYUP)
{
//Register as a key up event
keyUpEvent(event);
}
//If quit event is triggered...
else if (event.type == SDL_QUIT)
{
//Exit program
game->setRunning(false);
}
//If escape key is pressed...
if (wasKeyPressed(SDL_SCANCODE_ESCAPE))
{
//Exit program
game->setRunning(false);
}
}
//if (isKeyHeld(_lastState.key1) && isKeyHeld(_lastState.key2))
//{
//do nothing - keep things the same
//}
if (isKeyHeld(SDL_SCANCODE_W) && isKeyHeld(SDL_SCANCODE_D))
{
//Clear other held keys
//this->_heldKeys.clear();
//this->_heldKeys.
player.moveUpRight();
_lastState.key1 = SDL_SCANCODE_UP;
_lastState.key2 = SDL_SCANCODE_RIGHT;
}
else if (isKeyHeld(SDL_SCANCODE_W) && isKeyHeld(SDL_SCANCODE_A))
{
player.moveUpLeft();
_lastState.key1 = SDL_SCANCODE_UP;
_lastState.key2 = SDL_SCANCODE_LEFT;
}
else if (isKeyHeld(SDL_SCANCODE_S) && isKeyHeld(SDL_SCANCODE_D))
{
player.moveDownRight();
_lastState.key1 = SDL_SCANCODE_DOWN;
_lastState.key2 = SDL_SCANCODE_RIGHT;
}
else if (isKeyHeld(SDL_SCANCODE_S) && isKeyHeld(SDL_SCANCODE_A))
{
player.moveDownLeft();
_lastState.key1 = SDL_SCANCODE_DOWN;
_lastState.key2 = SDL_SCANCODE_LEFT;
}
else if (isKeyHeld(SDL_SCANCODE_W))
{
player.moveUp();
_lastState.key1 = SDL_SCANCODE_UP;
_lastState.key2 = SDL_SCANCODE_0;
}
else if (isKeyHeld(SDL_SCANCODE_S))
{
player.moveDown();
_lastState.key1 = SDL_SCANCODE_DOWN;
_lastState.key2 = SDL_SCANCODE_0;
}
else if (isKeyHeld(SDL_SCANCODE_D))
{
player.moveRight();
_lastState.key1 = SDL_SCANCODE_RIGHT;
_lastState.key2 = SDL_SCANCODE_0;
}
else if (isKeyHeld(SDL_SCANCODE_A))
{
player.moveLeft();
_lastState.key1 = SDL_SCANCODE_LEFT;
_lastState.key2 = SDL_SCANCODE_0;
}
else if (!isKeyHeld(SDL_SCANCODE_W) && !isKeyHeld(SDL_SCANCODE_S) && !isKeyHeld(SDL_SCANCODE_D) && !isKeyHeld(SDL_SCANCODE_A))
{
if(player.isJumping())
{
player.stop(false, false, false, false);
}
else
{
player.stop(true, true, false, false);
}
//clear last state
_lastState.key1 = SDL_SCANCODE_0;
_lastState.key2 = SDL_SCANCODE_0;
}
if(isKeyHeld(SDL_SCANCODE_J))
{
player.setJumping(false);
}
//jump + fall code;
if(isKeyHeld(SDL_SCANCODE_SPACE))
{
player.jump(true);
}
else if(wasKeyReleased(SDL_SCANCODE_SPACE))
{
player.jump(false);
}
else if(player.isJumping())
{
player.jump(false);
}
}
//Clears input state every new frame
void Input::beginNewFrame()
{
this->_pressedKeys.clear();
this->_releasedKeys.clear();
}
//Called when a key is pressed
void Input::keyDownEvent(const SDL_Event &event)
{
//Key added to pressed and held arrays as true when key is initially pressed
this->_pressedKeys[event.key.keysym.scancode] = true;
this->_heldKeys[event.key.keysym.scancode] = true;
}
//Called when a key is released
void Input::keyUpEvent(const SDL_Event &event)
{
//Key added to released array as true but removed from held array as it has been released
this->_releasedKeys[event.key.keysym.scancode] = true;
this->_heldKeys[event.key.keysym.scancode] = false;
}
//Called to check whether a given key has been pressed
bool Input::wasKeyPressed(SDL_Scancode key)
{
//Returns boolean value of given key
return this->_pressedKeys[key];
}
//Called to check whether a given key has been released
bool Input::wasKeyReleased(SDL_Scancode key)
{
//Returns boolean value of given key
return this->_releasedKeys[key];
}
//Called to check whether a given key is being held
bool Input::isKeyHeld(SDL_Scancode key)
{
//Returns boolean value of given key
return this->_heldKeys[key];
}