/
Player.cpp
196 lines (168 loc) · 4.9 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
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
#include <Book/Player.hpp>
#include <Book/CommandQueue.hpp>
#include <Book/Aircraft.hpp>
#include <Book/Foreach.hpp>
#include <Book/NetworkProtocol.hpp>
#include <SFML/Network/Packet.hpp>
#include <map>
#include <string>
#include <algorithm>
using namespace std::placeholders;
struct AircraftMover
{
AircraftMover(float vx, float vy, int identifier)
: velocity(vx, vy)
, aircraftID(identifier)
{
}
void operator() (Aircraft& aircraft, sf::Time) const
{
if (aircraft.getIdentifier() == aircraftID)
aircraft.accelerate(velocity * aircraft.getMaxSpeed());
}
sf::Vector2f velocity;
int aircraftID;
};
struct AircraftFireTrigger
{
AircraftFireTrigger(int identifier)
: aircraftID(identifier)
{
}
void operator() (Aircraft& aircraft, sf::Time) const
{
if (aircraft.getIdentifier() == aircraftID)
aircraft.fire();
}
int aircraftID;
};
struct AircraftMissileTrigger
{
AircraftMissileTrigger(int identifier)
: aircraftID(identifier)
{
}
void operator() (Aircraft& aircraft, sf::Time) const
{
if (aircraft.getIdentifier() == aircraftID)
aircraft.launchMissile();
}
int aircraftID;
};
Player::Player(sf::TcpSocket* socket, sf::Int32 identifier, const KeyBinding* binding)
: mKeyBinding(binding)
, mCurrentMissionStatus(MissionRunning)
, mIdentifier(identifier)
, mSocket(socket)
{
// Set initial action bindings
initializeActions();
// Assign all categories to player's aircraft
FOREACH(auto& pair, mActionBinding)
pair.second.category = Category::PlayerAircraft;
}
void Player::handleEvent(const sf::Event& event, CommandQueue& commands)
{
// Event
if (event.type == sf::Event::KeyPressed)
{
Action action;
if (mKeyBinding && mKeyBinding->checkAction(event.key.code, action) && !isRealtimeAction(action))
{
// Network connected -> send event over network
if (mSocket)
{
sf::Packet packet;
packet << static_cast<sf::Int32>(Client::PlayerEvent);
packet << mIdentifier;
packet << static_cast<sf::Int32>(action);
mSocket->send(packet);
}
// Network disconnected -> local event
else
{
commands.push(mActionBinding[action]);
}
}
}
// Realtime change (network connected)
if ((event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased) && mSocket)
{
Action action;
if (mKeyBinding && mKeyBinding->checkAction(event.key.code, action) && isRealtimeAction(action))
{
// Send realtime change over network
sf::Packet packet;
packet << static_cast<sf::Int32>(Client::PlayerRealtimeChange);
packet << mIdentifier;
packet << static_cast<sf::Int32>(action);
packet << (event.type == sf::Event::KeyPressed);
mSocket->send(packet);
}
}
}
bool Player::isLocal() const
{
// No key binding means this player is remote
return mKeyBinding != nullptr;
}
void Player::disableAllRealtimeActions()
{
FOREACH(auto& action, mActionProxies)
{
sf::Packet packet;
packet << static_cast<sf::Int32>(Client::PlayerRealtimeChange);
packet << mIdentifier;
packet << static_cast<sf::Int32>(action.first);
packet << false;
mSocket->send(packet);
}
}
void Player::handleRealtimeInput(CommandQueue& commands)
{
// Check if this is a networked game and local player or just a single player game
if ((mSocket && isLocal()) || !mSocket)
{
// Lookup all actions and push corresponding commands to queue
std::vector<Action> activeActions = mKeyBinding->getRealtimeActions();
FOREACH(Action action, activeActions)
commands.push(mActionBinding[action]);
}
}
void Player::handleRealtimeNetworkInput(CommandQueue& commands)
{
if (mSocket && !isLocal())
{
// Traverse all realtime input proxies. Because this is a networked game, the input isn't handled directly
FOREACH(auto pair, mActionProxies)
{
if (pair.second && isRealtimeAction(pair.first))
commands.push(mActionBinding[pair.first]);
}
}
}
void Player::handleNetworkEvent(Action action, CommandQueue& commands)
{
commands.push(mActionBinding[action]);
}
void Player::handleNetworkRealtimeChange(Action action, bool actionEnabled)
{
mActionProxies[action] = actionEnabled;
}
void Player::setMissionStatus(MissionStatus status)
{
mCurrentMissionStatus = status;
}
Player::MissionStatus Player::getMissionStatus() const
{
return mCurrentMissionStatus;
}
void Player::initializeActions()
{
mActionBinding[PlayerAction::MoveLeft].action = derivedAction<Aircraft>(AircraftMover(-1, 0, mIdentifier));
mActionBinding[PlayerAction::MoveRight].action = derivedAction<Aircraft>(AircraftMover(+1, 0, mIdentifier));
mActionBinding[PlayerAction::MoveUp].action = derivedAction<Aircraft>(AircraftMover( 0, -1, mIdentifier));
mActionBinding[PlayerAction::MoveDown].action = derivedAction<Aircraft>(AircraftMover( 0, +1, mIdentifier));
mActionBinding[PlayerAction::Fire].action = derivedAction<Aircraft>(AircraftFireTrigger(mIdentifier));
mActionBinding[PlayerAction::LaunchMissile].action = derivedAction<Aircraft>(AircraftMissileTrigger(mIdentifier));
}