-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototypeGame.cpp
More file actions
393 lines (327 loc) · 11.9 KB
/
PrototypeGame.cpp
File metadata and controls
393 lines (327 loc) · 11.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "game/PrototypeGame.h"
#include "input/InputBindingParse.h"
#include "input/InputMapper.h"
#include "loop/DebugCamera.h"
#include "platform/win/LauncherLogSingletonWin.h"
#include "platform/win/WinFiles.h"
#include <chrono>
#include <cmath>
#include <filesystem>
#include <new>
#include <string>
#include <vector>
namespace fs = std::filesystem;
namespace {
// Best-effort logging to the same process-wide log stream used by AppMain/launcher.
static void LogLine(const std::wstring& line) noexcept
{
try
{
auto& log = LauncherLog();
WriteLog(log, line);
}
catch (...)
{
// Logging must never take down the game.
}
}
static void AddUnique(std::vector<fs::path>& out, const fs::path& p)
{
const auto ps = p.generic_wstring();
for (const auto& e : out)
{
if (e.generic_wstring() == ps)
return;
}
out.push_back(p);
}
static std::vector<fs::path> BuildBindingsCandidates()
{
std::vector<fs::path> out;
// 0) User override (per-machine/per-user) in LocalAppData\ColonyGame.
// This lets players change bindings without touching the install folder
// (and avoids write permissions issues in Program Files).
const fs::path saveDir = platform::win::GetSaveDir();
if (!saveDir.empty())
{
AddUnique(out, saveDir / L"input_bindings.json");
AddUnique(out, saveDir / L"input_bindings.ini");
}
// 1) Dev-friendly search: walk up from the current working directory.
// This mirrors InputMapper::LoadFromDefaultPaths(), but we need the
// successful path for logging + hot-reload.
constexpr int kMaxParents = 5;
std::error_code ec;
fs::path base = fs::current_path(ec);
if (ec || base.empty())
base = fs::path(L".");
for (int depth = 0; depth <= kMaxParents; ++depth)
{
AddUnique(out, base / L"assets" / L"config" / L"input_bindings.json");
AddUnique(out, base / L"assets" / L"config" / L"input_bindings.ini");
AddUnique(out, base / L"input_bindings.json");
AddUnique(out, base / L"input_bindings.ini");
if (!base.has_parent_path())
break;
const auto parent = base.parent_path();
if (parent == base)
break;
base = parent;
}
// 2) Shipping-friendly search: paths relative to the executable.
const fs::path exeDir = platform::win::GetExeDir();
if (!exeDir.empty())
{
AddUnique(out, exeDir / L"assets" / L"config" / L"input_bindings.json");
AddUnique(out, exeDir / L"assets" / L"config" / L"input_bindings.ini");
AddUnique(out, exeDir / L"input_bindings.json");
AddUnique(out, exeDir / L"input_bindings.ini");
// Common "bin next to repo" layouts.
AddUnique(out, exeDir.parent_path() / L"assets" / L"config" / L"input_bindings.json");
AddUnique(out, exeDir.parent_path() / L"assets" / L"config" / L"input_bindings.ini");
}
return out;
}
static bool TryGetLastWriteTime(const fs::path& p, fs::file_time_type& outTime) noexcept
{
std::error_code ec;
outTime = fs::last_write_time(p, ec);
return !ec;
}
static bool TryLoadBindings(colony::input::InputMapper& mapper,
const std::vector<fs::path>& candidates,
fs::path& outLoadedPath) noexcept
{
std::error_code ec;
for (const auto& p : candidates)
{
ec.clear();
if (!fs::exists(p, ec) || ec)
continue;
if (mapper.LoadFromFile(p))
{
outLoadedPath = p;
return true;
}
// File exists but did not parse.
LogLine(L"[Input] Failed to parse bindings file: " + p.wstring());
}
return false;
}
} // namespace
namespace colony::game {
struct PrototypeGame::Impl {
colony::appwin::DebugCameraController camera;
colony::input::InputMapper mapper;
// Input bindings hot-reload
std::vector<fs::path> bindingsCandidates;
fs::path bindingsPath;
fs::file_time_type bindingsWriteTime{};
fs::file_time_type lastFailedWriteTime{};
bool hasBindingsPath = false;
bool hasBindingsWriteTime = false;
bool hasLastFailedWriteTime = false;
bool loggedMissing = false;
float bindingsPollAccum = 0.f; // seconds
float bindingsSearchAccum = 0.f; // seconds
};
static void HotReloadBindings(PrototypeGame::Impl& impl, float dtSeconds, bool force) noexcept
{
// Drive the timers from the fixed-step simulation dt. This is good enough
// for a prototype and keeps the logic simple.
if (dtSeconds > 0.0f)
{
impl.bindingsPollAccum += dtSeconds;
impl.bindingsSearchAccum += dtSeconds;
}
constexpr float kPollPeriod = 0.5f; // seconds
constexpr float kSearchPeriod = 2.0f; // seconds
const bool shouldPoll = force || (impl.bindingsPollAccum >= kPollPeriod);
const bool shouldSearch = force || (!impl.hasBindingsPath && (impl.bindingsSearchAccum >= kSearchPeriod));
if (!shouldPoll && !shouldSearch)
return;
if (shouldPoll)
impl.bindingsPollAccum = 0.0f;
if (shouldSearch)
impl.bindingsSearchAccum = 0.0f;
if (force)
colony::LogLine(L"[Input] Reload bindings requested");
// Poll current file if we have one.
if (impl.hasBindingsPath && shouldPoll)
{
if (auto now = TryGetLastWriteTime(impl.bindingsPath))
{
if (!impl.hasBindingsWriteTime || (*now != impl.bindingsWriteTime))
{
if (TryLoadBindings(impl.bindingsPath, impl.mapper))
{
impl.bindingsWriteTime = *now;
impl.hasBindingsWriteTime = true;
}
}
}
return;
}
// If we don't have a bindings path yet, periodically search candidates.
if (!impl.hasBindingsPath && shouldSearch)
{
for (const auto& p : impl.bindingsCandidates)
{
if (TryLoadBindings(p, impl.mapper))
{
impl.bindingsPath = p;
impl.hasBindingsPath = true;
if (auto ft = TryGetLastWriteTime(p))
{
impl.bindingsWriteTime = *ft;
impl.hasBindingsWriteTime = true;
}
break;
}
}
}
}
PrototypeGame::PrototypeGame()
{
m_impl.reset(new (std::nothrow) Impl{});
// Optional: allow developers to override bindings without recompiling.
// If no config file is found, defaults remain.
if (!m_impl)
return;
m_impl->bindingsCandidates = BuildBindingsCandidates();
fs::path loaded;
if (TryLoadBindings(m_impl->mapper, m_impl->bindingsCandidates, loaded))
{
m_impl->bindingsPath = loaded;
m_impl->hasBindingsPath = true;
fs::file_time_type ft{};
if (TryGetLastWriteTime(loaded, ft))
{
m_impl->bindingsWriteTime = ft;
m_impl->hasBindingsWriteTime = true;
}
LogLine(L"[Input] Loaded bindings: " + loaded.wstring());
}
else
{
LogLine(L"[Input] No input_bindings.json/.ini found (using compiled defaults)."
L" Expected e.g. assets\\config\\input_bindings.json");
}
}
PrototypeGame::~PrototypeGame()
= default;
bool PrototypeGame::OnInput(std::span<const colony::input::InputEvent> events) noexcept
{
if (!m_impl)
return false;
bool changed = false;
bool actionsChanged = false;
// Process events in order so action-chords + mouse-drag decisions are made
// against the *current* button state (not just the final state for the frame).
m_impl->mapper.BeginFrame();
for (const auto& ev : events)
{
if (m_impl->mapper.ConsumeEvent(ev))
actionsChanged = true;
using colony::input::InputEventType;
switch (ev.type)
{
case InputEventType::MouseDelta:
{
// Orbit/Pan are action-driven (mouse buttons are bound through InputMapper).
// If both actions are down (e.g., due to an overlapping bind), prefer pan.
const bool pan = m_impl->mapper.IsDown(colony::input::Action::CameraPan);
const bool orbit = m_impl->mapper.IsDown(colony::input::Action::CameraOrbit) && !pan;
if (m_impl->camera.ApplyDrag(static_cast<long>(ev.dx), static_cast<long>(ev.dy), orbit, pan))
changed = true;
break;
}
case InputEventType::MouseWheel:
if (m_impl->camera.ApplyWheelDetents(static_cast<int>(ev.wheelDetents)))
changed = true;
break;
case InputEventType::FocusLost:
// Drop all held actions when we lose focus to avoid "stuck key" symptoms.
m_impl->mapper.ClearState();
actionsChanged = true;
changed = true;
break;
default:
break;
}
}
if (actionsChanged)
changed = true;
// Manual input bindings hot-reload (defaults to F5). Automatic polling is
// handled in UpdateFixed().
bool reloadRequested = false;
for (const auto& ae : m_impl->mapper.ActionEvents())
{
if (ae.action == colony::input::Action::ReloadBindings &&
ae.type == colony::input::ActionEventType::Pressed)
{
reloadRequested = true;
break;
}
}
HotReloadBindings(*m_impl, 0.f, reloadRequested);
return changed;
}
bool PrototypeGame::UpdateFixed(float dtSeconds) noexcept
{
if (!m_impl)
return false;
// Automatic hot-reload polling (filesystem timestamps).
HotReloadBindings(*m_impl, dtSeconds, false);
// Continuous keyboard movement (WASD + QE) in camera-relative space.
const auto axes = m_impl->mapper.GetMovementAxes();
const bool anyMove = (axes.x != 0.f) || (axes.y != 0.f) || (axes.z != 0.f);
if (!anyMove || dtSeconds <= 0.f)
return false;
const auto& s = m_impl->camera.State();
constexpr float kPi = 3.14159265358979323846f;
const float yawRad = s.yaw * (kPi / 180.f);
const float sinY = std::sin(yawRad);
const float cosY = std::cos(yawRad);
// Forward when yaw==0 is +Y. Right is +X.
const float fwdX = sinY;
const float fwdY = cosY;
const float rightX = cosY;
const float rightY = -sinY;
// Boost can be a modifier action, or it can be implied by chord actions.
const bool boost = m_impl->mapper.IsDown(colony::input::Action::SpeedBoost) ||
m_impl->mapper.IsDown(colony::input::Action::MoveForwardFast);
const float speedMul = boost ? 3.0f : 1.0f;
// Pan speed is "world" units per second. Tune later.
constexpr float kPanSpeed = 3.0f;
const float panSpeed = kPanSpeed * speedMul;
const float worldX = (rightX * axes.x + fwdX * axes.y) * (panSpeed * dtSeconds);
const float worldY = (rightY * axes.x + fwdY * axes.y) * (panSpeed * dtSeconds);
bool moved = false;
if (m_impl->camera.ApplyPan(worldX, worldY))
moved = true;
if (axes.z != 0.f)
{
// Exponential zoom is stable (always positive) and feels consistent.
constexpr float kZoomSpeed = 1.5f; // per second
const float zoomSpeed = kZoomSpeed * (boost ? 2.0f : 1.0f);
const float factor = std::exp(axes.z * zoomSpeed * dtSeconds);
if (m_impl->camera.ApplyZoomFactor(factor))
moved = true;
}
return moved;
}
DebugCameraInfo PrototypeGame::GetDebugCameraInfo() const noexcept
{
DebugCameraInfo out{};
if (!m_impl)
return out;
const auto& s = m_impl->camera.State();
out.yaw = s.yaw;
out.pitch = s.pitch;
out.panX = s.panX;
out.panY = s.panY;
out.zoom = s.zoom;
return out;
}
} // namespace colony::game