-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
129 lines (107 loc) · 2.99 KB
/
Player.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
#include "Player.hpp"
#include <iostream>
struct AircraftMover
{
AircraftMover(float vx, float vy)
: velocity(vx, vy)
{}
void operator() (Aircraft &aircraft, sf::Time) const
{
aircraft.accelerate(velocity);
}
sf::Vector2f velocity;
};
Player::Player()
: mKeyBinding()
, mActionBinding()
, mMissionStatus(MissionStatus::MissionRunning)
{
mKeyBinding[sf::Keyboard::Left] = MoveLeft;
mKeyBinding[sf::Keyboard::Right] = MoveRight;
mKeyBinding[sf::Keyboard::Up] = MoveUp;
mKeyBinding[sf::Keyboard::Down] = MoveDown;
mKeyBinding[sf::Keyboard::M] = LaunchMissile;
mKeyBinding[sf::Keyboard::Space] = Fire;
initializeActions();
for(auto &pair : mActionBinding)
pair.second.category = Category::PlayerAircraft;
}
void Player::handleEvent(const sf::Event &event, CommandQueue &commands)
{
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::P)
{
Command output;
output.category = Category::PlayerAircraft;
output.action = [] (SceneNode &s, sf::Time)
{
std::cout << s.getPosition().x << ", " << s.getPosition().y << std::endl;
};
commands.push(output);
}
if(event.type == sf::Event::KeyPressed)
{
auto found = mKeyBinding.find(event.key.code);
if(found != mKeyBinding.end() && !isRealtimeAction(found->second))
commands.push(mActionBinding[found->second]);
}
}
void Player::handleRealtimeInput(CommandQueue &commands)
{
for(auto pair : mKeyBinding)
{
if(sf::Keyboard::isKeyPressed(pair.first) && isRealtimeAction(pair.second))
{
commands.push(mActionBinding[pair.second]);
}
}
}
void Player::assignKey(Action action, sf::Keyboard::Key key)
{
auto itr = mKeyBinding.begin();
for(; itr!=mKeyBinding.end(); ++itr)
{
if(itr->second == action) // find action, requires only 1 key binds to 1 action
break;
}
mKeyBinding.erase(itr);
mKeyBinding[key] = action;
}
sf::Keyboard::Key Player::getAssignedKey(Action action)
{
for(auto &pair : mKeyBinding)
if(pair.second == action)
return pair.first;
return sf::Keyboard::Unknown;
}
void Player::setMissionStatus(MissionStatus status)
{
mMissionStatus = status;
}
Player::MissionStatus Player::getMissionStatus() const
{
return mMissionStatus;
}
void Player::initializeActions()
{
const float playerSpeed = 200.f;
mActionBinding[MoveLeft].action = derivedAction<Aircraft>(AircraftMover(-playerSpeed, 0.f));
mActionBinding[MoveRight].action = derivedAction<Aircraft>(AircraftMover(+playerSpeed, 0.f));
mActionBinding[MoveUp].action = derivedAction<Aircraft>(AircraftMover(0.f, -playerSpeed));
mActionBinding[MoveDown].action = derivedAction<Aircraft>(AircraftMover(0.f, +playerSpeed));
mActionBinding[Fire].action = derivedAction<Aircraft>([] (Aircraft &a, sf::Time){ a.fire();});
mActionBinding[LaunchMissile].action = derivedAction<Aircraft>([](Aircraft &a, sf::Time){a.launchMissile();});
}
bool Player::isRealtimeAction(Action action)
{
switch(action)
{
case MoveUp:
case MoveDown:
case MoveLeft:
case MoveRight:
case Fire:
return true;
default:
return false;
}
}