/
MapPositionManager.cpp
218 lines (182 loc) · 5.74 KB
/
MapPositionManager.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "MapPositionManager.h"
#include "maplib.h"
#include "ientity.h"
#include "gamelib.h"
#include "ieventmanager.h"
#include "iregistry.h"
#include "itextstream.h"
#include "icommandsystem.h"
#include "string/string.h"
#include "entitylib.h"
#include <functional>
#include "camera/GlobalCamera.h"
#include "xyview/GlobalXYWnd.h"
namespace map
{
namespace
{
const std::string SAVE_COMMAND_ROOT = "SavePosition";
const std::string LOAD_COMMAND_ROOT = "LoadPosition";
const char* const GKEY_LAST_CAM_POSITION = "/mapFormat/lastCameraPositionKey";
const char* const GKEY_LAST_CAM_ANGLE = "/mapFormat/lastCameraAngleKey";
const char* const GKEY_PLAYER_START_ECLASS = "/mapFormat/playerStartPoint";
const char* const GKEY_PLAYER_HEIGHT = "/defaults/playerHeight";
unsigned int MAX_POSITIONS = 10;
}
// Constructor
MapPositionManager::MapPositionManager()
{
GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &MapPositionManager::onMapEvent)
);
// Create the MapPosition objects and add the commands to the eventmanager
for (unsigned int i = 1; i <= MAX_POSITIONS; i++)
{
// Allocate a new MapPosition object and store the shared_ptr
_positions[i] = std::make_shared<MapPosition>(i);
// Add the load/save commands to the eventmanager and point it to the member
GlobalCommandSystem().addCommand(
SAVE_COMMAND_ROOT + string::to_string(i),
std::bind(&MapPosition::store, _positions[i].get(), std::placeholders::_1)
);
GlobalCommandSystem().addCommand(
LOAD_COMMAND_ROOT + string::to_string(i),
std::bind(&MapPosition::recall, _positions[i].get(), std::placeholders::_1)
);
GlobalEventManager().addCommand(
SAVE_COMMAND_ROOT + string::to_string(i),
SAVE_COMMAND_ROOT + string::to_string(i)
);
GlobalEventManager().addCommand(
LOAD_COMMAND_ROOT + string::to_string(i),
LOAD_COMMAND_ROOT + string::to_string(i)
);
}
}
void MapPositionManager::loadPositions()
{
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
for (unsigned int i = 1; i <= MAX_POSITIONS; ++i)
{
if (_positions[i])
{
_positions[i]->load(worldspawn);
}
}
}
else
{
rError() << "MapPositionManager: Could not locate worldspawn entity.\n";
}
}
void MapPositionManager::removeLastCameraPosition()
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
worldspawn->setKeyValue(keyLastCamPos, "");
worldspawn->setKeyValue(keyLastCamAngle, "");
}
}
void MapPositionManager::saveLastCameraPosition()
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
ui::CamWndPtr camWnd = GlobalCamera().getActiveCamWnd();
if (camWnd == NULL) return;
worldspawn->setKeyValue(keyLastCamPos, string::to_string(camWnd->getCameraOrigin()));
worldspawn->setKeyValue(keyLastCamAngle, string::to_string(camWnd->getCameraAngles()));
}
}
void MapPositionManager::gotoLastCameraPosition()
{
const std::string keyLastCamPos = game::current::getValue<std::string>(GKEY_LAST_CAM_POSITION);
const std::string keyLastCamAngle = game::current::getValue<std::string>(GKEY_LAST_CAM_ANGLE);
const std::string eClassPlayerStart = game::current::getValue<std::string>(GKEY_PLAYER_START_ECLASS);
Vector3 angles(0, 0, 0);
Vector3 origin(0, 0, 0);
Entity* worldspawn = map::current::getWorldspawn();
if (worldspawn != nullptr)
{
// Try to find a saved "last camera position"
const std::string savedOrigin = worldspawn->getKeyValue(keyLastCamPos);
if (!savedOrigin.empty())
{
// Construct the vector out of the std::string
origin = string::convert<Vector3>(savedOrigin);
angles = string::convert<Vector3>(worldspawn->getKeyValue(keyLastCamAngle));
}
else
{
// Get the player start entity
Entity* playerStart = Scene_FindEntityByClass(eClassPlayerStart);
if (playerStart != NULL)
{
// Get the entity origin
origin = string::convert<Vector3>(playerStart->getKeyValue("origin"));
// angua: move the camera upwards a bit
origin.z() += game::current::getValue<float>(GKEY_PLAYER_HEIGHT);
// Check for an angle key, and use it if present
angles[ui::CAMERA_YAW] = string::convert<float>(playerStart->getKeyValue("angle"), 0);
}
}
}
// Focus the view with the given parameters
GlobalCamera().focusCamera(origin, angles);
GlobalXYWnd().setOrigin(origin);
}
void MapPositionManager::savePositions()
{
Entity* worldspawn = map::current::getWorldspawn();
for (unsigned int i = 1; i <= MAX_POSITIONS; ++i)
{
if (_positions[i])
{
_positions[i]->save(worldspawn);
}
}
}
void MapPositionManager::removePositions()
{
// Find the worldspawn node
Entity* worldspawn = map::current::getWorldspawn();
for (unsigned int i = 1; i <= MAX_POSITIONS; ++i)
{
if (_positions[i])
{
_positions[i]->remove(worldspawn);
}
}
}
void MapPositionManager::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapSaving)
{
// Store the map positions into the worldspawn spawnargs
savePositions();
saveLastCameraPosition();
}
else if (ev == IMap::MapSaved)
{
// Remove the map positions again after saving
removeLastCameraPosition();
removePositions();
}
else if (ev == IMap::MapLoaded)
{
// Load the stored map positions from the worldspawn entity
loadPositions();
// Remove them, so that the user doesn't get bothered with them
removePositions();
gotoLastCameraPosition();
removeLastCameraPosition();
}
}
} // namespace map