summaryrefslogtreecommitdiff
path: root/kurator/src/Battle.cpp
blob: 8cced8b4ad8870cac2d36c3ee28b2d6fd80ec0f9 (plain)
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
#include "Battle.h"

#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <string>
#include <utility>

#include <raylib.h>
#include <imgui.h>

#include <kurator/campaign/scenarios.h>
#include <kurator/campaign/UniqueIdentifier.h>
#include <kurator/sim/Battle.h>
#include <kurator/sim/components.h>
#include <kurator/sim/events.h>
#include <kurator/sim/Point.h>
#include <kurator/stats/events.h>
#include <kurator/universe/ShipType.h>

#include "colors.h"
#include "components.h"
#include "Log.h"
#include "Session.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) :
	session {std::move(_session)},
	battle {sim::prepare(scenario)},
	time_factor {1.0}
{
	battle->dispatcher().sink<sim::End>().connect<&Battle::on_end>(*this);
	battle->dispatcher().sink<sim::Hit>().connect<&Battle::on_hit>(*this);
	battle->dispatcher().sink<stats::ShipLeft>().connect<&Battle::on_ship_left>(*this);
	auto& registry = battle->registry();
	auto ships = registry.view<sim::Team, universe::ShipType, campaign::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));
	}
}


Battle::~Battle()
{
	battle->dispatcher().sink<sim::End>().disconnect(*this);
	battle->dispatcher().sink<sim::Hit>().disconnect(*this);
	battle->dispatcher().sink<stats::ShipLeft>().disconnect(*this);
}


void
Battle::update(const float dt)
{
	battle->update(dt * time_factor);
	auto& registry = battle->registry();
	auto timers = registry.view<Timed>();
	for (auto&& [entity, timer] : timers.each()) {
		timer.left -= timer.scaled ? dt * time_factor : dt;
		if (timer.left < 0.0)
			registry.destroy(entity);
	}
	auto pops = registry.view<PopMove, UIOffset>();
	for (auto&& [entity, pop, offset] : pops.each()) {
		const auto speed = pop.speed.scale(dt);
		offset.x += speed.x;
		offset.y += speed.y;
		const auto damp = pop.speed.scale(pop.damp).scale(dt);
		pop.speed.x -= damp.x;
		pop.speed.y -= damp.y;
	}
	auto lines = registry.view<Line>();
	for (auto&& [entity, line] : lines.each())
		line.position += (1.0 + line.hlength) / line.duration * dt * time_factor;
	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)) {
		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::End();
	if (IsKeyPressed(KEY_SPACE))
		session->set(std::make_shared<Log>(session, log));
}


void
Battle::draw() const
{
	ClearBackground(BLACK);
	const int hwidth = GetScreenWidth() / 2;
	const int hheight = GetScreenHeight() / 2;
	const double scale = std::min(hwidth/15000.0, hheight/15000.0);
	auto& registry = battle->registry();
	auto lines = 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 = line.start + diff.scale(fstart > 0.0 ? fstart : 0.0);
		const auto end = line.start + diff.scale(fend > 1.0 ? 1.0 : fend);
		DrawLine(
			hwidth + start.x*scale,
			hheight + start.y*scale,
			hwidth + end.x*scale,
			hheight + end.y*scale,
			line.color);
	}
	auto view = registry.view<const Marker, const sim::Transform>();
	for (auto [entity, marker, transform] : view.each()) {
		const int x = hwidth + transform.position.x*scale;
		const int y = hheight + transform.position.y*scale;
		DrawCircle(x, y, marker.radius, marker.color);
		DrawLine(
			x,
			y,
			x + marker.radius*std::cos(transform.angle),
			y + marker.radius*std::sin(transform.angle),
			WHITE);
		DrawText(marker.name.c_str(), x+10, y-5, 10.0f, GRAY);
	}
	auto pops = registry.view<CenteredText, sim::Transform, UIOffset>();
	for (const auto& [entity, text, transform, offset] : pops.each()) {
		const int x = hwidth + transform.position.x*scale - text.width/2 + offset.x;
		const int y = hheight + transform.position.y*scale - text.font_size/2 + offset.y;
		DrawText(text.text.c_str(), x, y, text.font_size, text.color);
	}
	balance.draw();
}


void
Battle::on_end(const sim::End&)
{
	session->set(std::make_shared<Log>(session, log));
}


void
Battle::on_hit(const sim::Hit& hit)
{
	auto& registry = battle->registry();
	if (!registry.valid(hit.victim))
		return;
	const std::string text = TextFormat("%.1f", hit.damage);
	const auto& source = registry.get<sim::Transform>(hit.source);
	const auto& victim = registry.get<sim::Transform>(hit.victim);
	const auto popup = registry.create();
	registry.emplace<Timed>(popup, 1.2);
	registry.emplace<CenteredText>(popup, text, MeasureText(text.c_str(), 10), 10, RED);
	registry.emplace<sim::Transform>(popup, victim.position, 0.0);
	registry.emplace<UIOffset>(popup, 0, 0);
	registry.emplace<PopMove>(popup, sim::Point{0.0, -20}, 0.9998);
	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_ship_left(const stats::ShipLeft& event)
{
	log.push_back(event);
}


}  // namespace kurator