/
InputHandler.cpp
75 lines (69 loc) · 1.74 KB
/
InputHandler.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
//
// InputHandler.cpp
// Disker
//
// Created by Joram Wilander on 2014-03-26.
// Copyright (c) 2014 Slaughter Games. All rights reserved.
//
#include "InputHandler.h"
InputHandler::InputHandler(CoreInput *coreInput, Player *player)
{
this->coreInput = coreInput;
this->player = player;
}
void InputHandler::handleEvent(Event *e)
{
/* make sure we are getting events from the correct place */
if (e->getDispatcher() == coreInput)
{
InputEvent *ie = (InputEvent*) e;
switch (ie->getEventCode()) {
case InputEvent::EVENT_KEYDOWN:
HandleKeyDown(ie->getKey());
break;
case InputEvent::EVENT_KEYUP:
HandleKeyUp(ie->getKey());
break;
}
}
}
/* handles all user input that happens on a key press */
void InputHandler::HandleKeyDown(PolyKEY key)
{
switch (key) {
case Polycode::KEY_w:
player->MoveForward();
break;
case Polycode::KEY_s:
player->MoveBackward();
break;
case Polycode::KEY_a:
player->TurnAntiClockwise();
break;
case Polycode::KEY_d:
player->TurnClockwise();
break;
default:
break;
}
}
/* handles all user input on a key release */
void InputHandler::HandleKeyUp(PolyKEY key)
{
switch (key) {
case Polycode::KEY_w:
player->StopMove();
break;
case Polycode::KEY_s:
player->StopMove();
break;
case Polycode::KEY_a:
player->StopTurn();
break;
case Polycode::KEY_d:
player->StopTurn();
break;
default:
break;
}
}