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
|
#include "Battle.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <raylib.h>
#include <imgui.h>
#include <kurator/engine/Context.h>
#include <kurator/engine/Point.h>
#include <kurator/campaign/scenarios.h>
#include <kurator/sim/Battle.h>
#include <kurator/sim/components.h>
#include <kurator/sim/events.h>
#include <kurator/sim/FloatingMovement.h>
#include <kurator/stats/events.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/UniqueIdentifier.h>
#include "colors.h"
#include "components.h"
#include "Grid.h"
#include "Pause.h"
#include "PopupEmitter.h"
#include "Session.h"
#include "Summary.h"
namespace kurator
{
Battle::Battle(std::shared_ptr<Session> _session) :
Battle(_session, campaign::scenarios::example())
{
}
Battle::Battle(std::shared_ptr<Session> _session, campaign::Scenario scenario, Battle::Callback _report) :
session {std::move(_session)},
battle {sim::prepare(scenario)},
report {std::move(_report)}
{
battle->dispatcher().sink<sim::End>().connect<&Battle::on_end>(*this);
battle->dispatcher().sink<sim::Hit>().connect<&Battle::on_hit>(*this);
battle->dispatcher().sink<sim::Destroyed>().connect<&Battle::on_destroyed>(*this);
battle->dispatcher().sink<stats::ShipLeft>().connect<&Battle::on_ship_left>(*this);
auto& registry = battle->registry();
auto ships = registry.view<sim::Team, universe::ShipType, universe::UniqueIdentifier>();
for (const auto& [entity, team, type, identifier] : ships.each()) {
std::string label = TextFormat("%s (%d)", type.name.c_str(), identifier.id);
registry.emplace<Marker>(entity, 5.0, team_color(team.id), std::move(label));
registry.emplace<PopupEmitter>(entity);
}
camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0);
}
Battle::~Battle()
{
battle->dispatcher().sink<sim::End>().disconnect(*this);
battle->dispatcher().sink<sim::Hit>().disconnect(*this);
battle->dispatcher().sink<sim::Destroyed>().disconnect(*this);
battle->dispatcher().sink<stats::ShipLeft>().disconnect(*this);
}
void
time_controls(const char* id, float& time_factor)
{
ImGui::PushID(id);
ImGui::PushItemWidth(-std::numeric_limits<float>::min());
if (ImGui::Button("1x"))
time_factor = 1.0f;
ImGui::SameLine();
ImGui::SliderFloat("##time_factor", &time_factor, 0.1f, 10.0f);
ImGui::PopItemWidth();
ImGui::PopID();
}
static void progress_timers(engine::Context& ctx);
static void move_ui_pops(engine::Context& ctx);
static void blink_crosses(engine::Context& ctx);
static void animate_lines(engine::Context& ctx);
void
Battle::update()
{
if (IsKeyPressed(KEY_ESCAPE))
return session->set(std::make_shared<Pause>(session, session->current()));
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
const auto delta = GetMouseDelta();
camera.offset.x -= delta.x / camera.scale;
camera.offset.y -= delta.y / camera.scale;
}
if (IsWindowResized())
camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); // won't work in frame
auto& registry = battle->registry();
clock.update();
engine::Context ctx {registry, battle->dispatcher(), clock, camera};
battle->update(ctx);
progress_timers(ctx);
move_ui_pops(ctx);
blink_crosses(ctx);
animate_lines(ctx);
balance.update(registry);
ImGui::SetNextWindowPos({GetScreenWidth()/2.0f, GetScreenHeight()-100.0f}, ImGuiCond_Once, {0.5f, 0.5f});
ImGui::SetNextWindowSize({240.0f, 0.0f}, ImGuiCond_Once);
if (ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoFocusOnAppearing))
time_controls("TimeControls", clock.time_factor);
ImGui::End();
}
void
progress_timers(engine::Context& ctx)
{
auto timers = ctx.registry.view<Timed>();
for (auto&& [entity, timer] : timers.each()) {
timer.left -= timer.scaled ? ctx.clock.dt : ctx.clock.ui;
if (timer.left < 0.0)
ctx.registry.destroy(entity);
}
}
void
move_ui_pops(engine::Context& ctx)
{
auto pops = ctx.registry.view<PopMove, UIOffset>();
for (auto&& [entity, pop, offset] : pops.each()) {
const auto speed = pop.speed.scale(ctx.clock.ui);
offset.x += speed.x;
offset.y += speed.y;
const auto damp = pop.speed.scale(pop.damp).scale(ctx.clock.ui);
pop.speed.x -= damp.x;
pop.speed.y -= damp.y;
}
}
void
blink_crosses(engine::Context& ctx)
{
auto crosses = ctx.registry.view<Cross>();
for (auto&& [entity, cross] : crosses.each()) {
cross.timer += ctx.clock.ui;
const auto dphase = cross.phase * 2;
if (cross.timer > dphase)
cross.timer -= dphase;
}
}
void
animate_lines(engine::Context& ctx)
{
auto lines = ctx.registry.view<Line>();
for (auto&& [entity, line] : lines.each())
line.position += (1.0 + line.hlength) / line.duration * ctx.clock.dt;
}
static void draw_crosses(engine::ConstContext& ctx);
static void draw_lines(engine::ConstContext& ctx);
static void draw_markers(engine::ConstContext& ctx);
static void draw_pops(engine::ConstContext& ctx);
void
Battle::draw() const
{
ClearBackground(BLACK);
Grid().draw(camera);
engine::ConstContext ctx {battle->registry(), battle->dispatcher(), clock, camera};
draw_crosses(ctx);
draw_lines(ctx);
draw_markers(ctx);
draw_pops(ctx);
balance.draw();
}
void
draw_crosses(engine::ConstContext& ctx)
{
auto crosses = ctx.registry.view<sim::Transform, Cross>();
for (const auto& [entity, transform, cross] : crosses.each()) {
if (cross.timer > cross.phase)
continue;
const auto pos = ctx.camera.to_screen(transform.position);
DrawLine(
pos.x - cross.hlength,
pos.y - cross.hlength,
pos.x + cross.hlength,
pos.y + cross.hlength,
cross.color);
DrawLine(
pos.x + cross.hlength,
pos.y - cross.hlength,
pos.x - cross.hlength,
pos.y + cross.hlength,
cross.color);
}
}
void
draw_lines(engine::ConstContext& ctx)
{
auto lines = ctx.registry.view<Line>();
for (const auto& [entity, line] : lines.each()) {
const auto diff = line.end - line.start;
const auto fstart = line.position - line.hlength;
const auto fend = line.position + line.hlength;
const auto start = ctx.camera.to_screen(line.start + diff.scale(fstart > 0.0 ? fstart : 0.0));
const auto end = ctx.camera.to_screen(line.start + diff.scale(fend > 1.0 ? 1.0 : fend));
DrawLine(start.x, start.y, end.x, end.y, line.color);
}
}
void
draw_markers(engine::ConstContext& ctx)
{
auto view = ctx.registry.view<const Marker, const sim::Transform>();
for (auto [entity, marker, transform] : view.each()) {
const auto pos = ctx.camera.to_screen(transform.position);
if (ctx.registry.all_of<sim::FloatingMovement>(entity)) {
const auto& movement = ctx.registry.get<sim::FloatingMovement>(entity);
const auto& velocity = movement.speed;
const auto edge = pos + velocity.normalized().scale(marker.radius);
const auto tip = edge + velocity.scale(2.0 * ctx.camera.scale);
DrawLine(edge.x, edge.y, tip.x, tip.y, DARKGREEN);
}
const engine::Point direction {std::cos(transform.angle), std::sin(transform.angle)};
const auto edge = pos + direction.scale(marker.radius);
DrawCircle(pos.x, pos.y, marker.radius, marker.color);
DrawLine(pos.x, pos.y, edge.x, edge.y, WHITE);
DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, GRAY);
}
}
void
draw_pops(engine::ConstContext& ctx)
{
auto pops = ctx.registry.view<CenteredText, sim::Transform, UIOffset>();
for (const auto& [entity, text, transform, offset] : pops.each()) {
const auto screen = ctx.camera.to_screen(transform.position);
const auto pos = screen.subtract(text.width/2, text.font_size/2) + offset;
DrawText(text.text.c_str(), pos.x, pos.y, text.font_size, text.color);
}
}
void
Battle::on_end(const sim::End&)
{
if (report)
report(log);
session->set(std::make_shared<Summary>(session, log));
}
void
Battle::on_hit(const sim::Hit& hit)
{
auto& registry = battle->registry();
if (!registry.valid(hit.victim))
return;
const auto& source = registry.get<sim::Transform>(hit.source);
const auto& victim = registry.get<sim::Transform>(hit.victim);
auto& popup = registry.get<PopupEmitter>(hit.victim);
popup.emit(registry, victim, hit.damage);
const auto line = registry.create();
registry.emplace<Timed>(line, 0.2, true);
registry.emplace<Line>(
line,
RED,
source.position,
victim.position,
1000.0 / (victim.position - source.position).magnitude(),
0.2);
}
void
Battle::on_destroyed(const sim::Destroyed& event)
{
auto& registry = battle->registry();
if (!registry.valid(event.victim))
return;
const auto& victim = registry.get<sim::Transform>(event.victim);
const auto cross = registry.create();
registry.emplace<Timed>(cross, 1.6);
registry.emplace<Cross>(cross, 0.2, 6.0);
registry.emplace<sim::Transform>(cross, victim.position, 0.0);
}
void
Battle::on_ship_left(const stats::ShipLeft& event)
{
log.push_back(event);
}
} // namespace kurator
|