summaryrefslogtreecommitdiff
path: root/kurator/src/inspect.cpp
blob: d49a917afb0925e343511e2ac9054a841318f925 (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
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
#include "inspect.h"

#include <utility>

#include <entt/entt.hpp>
#include <imgui.h>
#include <raylib.h>

#include <kurator/sim/ai.h>
#include <kurator/sim/components.h>
#include <kurator/sim/FloatingMovement.h>
#include <kurator/sim/HitPoints.h>
#include <kurator/sim/weapons.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/UniqueIdentifier.h>

#include "Controller.h"


namespace kurator
{


template <typename Type> void inspect(entt::handle& entity);
template <typename Type> void inspect(entt::handle& entity, Type& component);


template <typename Type>
void
inspect(entt::handle& entity)
{
	if (entity.all_of<Type>()) {
		inspect(entity, entity.get<Type>());
		ImGui::Separator();
	}
}


void
InspectionWindow::show()
{
	if (!open)
		return;
	if (ImGui::Begin("Inspect", &open)) {
		auto it = selected.begin();
		while (it != selected.end()) {
			if (!it->valid()) {
				it = selected.erase(it);
				continue;
			}
			ImGui::PushID(it - selected.begin());
			inspect<universe::UniqueIdentifier>(*it);
			inspect<universe::ShipType>(*it);
			inspect<sim::FloatingMovement>(*it);
			inspect<sim::HitPoints>(*it);
			inspect<sim::AIShip>(*it);
			inspect<sim::KeepAtRange>(*it);
			inspect<sim::Turret>(*it);
			inspect<TurretVisuals>(*it);
			inspect<AIVisuals>(*it);
			it = std::next(it);
			ImGui::PopID();
		}
		if (selected.empty())
			ImGui::Text("Nothing selected");
	}
	ImGui::End();
	if (!open)
		deselect();
}


void
InspectionWindow::select(entt::handle entity)
{
	if (!entity)
		return;
	entity.get<Selectable>().active = true;
	auto* registry = entity.registry();
	auto turrets = registry->view<sim::Turret>();
	entity.emplace<AIVisuals>();
	selected.push_back(std::move(entity));
	for (auto&& [entity_, turret] : turrets.each()) {
		if (turret.owner == entity) {
			registry->emplace<TurretVisuals>(entity_);
			selected.push_back(entt::handle{*registry, entity_});
		}
	}
	open = true;
}


void
InspectionWindow::deselect()
{
	for (auto&& entity_ : selected) {
		if (!entity_.valid())
			continue;
		if (entity_.all_of<Selectable>())
			entity_.get<Selectable>().active = false;
		if (entity_.all_of<TurretVisuals>())
			entity_.remove<TurretVisuals>();
		if (entity_.all_of<AIVisuals>())
			entity_.remove<AIVisuals>();
	}
	selected.clear();
}


template <>
void
inspect(entt::handle&, universe::UniqueIdentifier& identifier)
{
	ImGui::Text("Selected: %d", identifier.id);
}


template <>
void
inspect(entt::handle&, universe::ShipType& type)
{
	ImGui::Text("Type: %s", type.name.c_str());
}


static bool
slider_double(const char* label, double* value, double minimum, double maximum, const char* fmt)
{
	return ImGui::SliderScalar(label, ImGuiDataType_Double, value, &minimum, &maximum, fmt);
}


template <>
void
inspect(entt::handle&, sim::Turret& turret)
{
	ImGui::Text("Turret: %s", turret.type.name.c_str());
	ImGui::Text("DPS: %.1f", turret.type.damage_per_second());
	ImGui::Text("Eff. Range: %.1f", turret.type.effective_range());
	ImGui::Text("Current");
	ImGui::SliderInt("Rounds##current", &turret.rounds, 0, turret.type.rounds);
	slider_double("Delay", &turret.delay, 0, turret.type.rate_of_fire, "%.3f");
	slider_double("Reload##current", &turret.reload, 0, turret.type.reload, "%.3f");
	ImGui::Text("Base");
	ImGui::SliderInt("Rounds##base", &turret.type.rounds, 0, 50);
	slider_double("Base Damage", &turret.type.base_damage, 0, 1000.0, "%.1f");
	slider_double("Rate of Fire", &turret.type.rate_of_fire, 0, 60.0, "%.3f");
	slider_double("Reload##base", &turret.type.reload, 0, 60.0, "%.3f");
	slider_double("Optimal Range", &turret.type.optimal_range, 0, 30000.0, "%.1f");
	slider_double("Falloff Modifier", &turret.type.falloff_modifier, 0, 5.0, "%.3f");
	slider_double("Falloff Intensity", &turret.type.falloff_intensity, 0, 1.0, "%.3f");
	slider_double("Tracking", &turret.type.tracking, 0, 1000.0, "%.1f");
	slider_double("Tracking Factor", &turret.type.tracking_factor, 0, 10.0, "%.3f");
	slider_double("Tracking Exponent", &turret.type.tracking_exponent, 0, 2.0, "%.3f");
}


template <>
void
inspect(entt::handle&, sim::FloatingMovement& movement)
{
	ImGui::Text("Speed: %.2f", movement.speed.magnitude());
	ImGui::InputDouble("Maximum Speed", &movement.max_speed, 0, 0, "%.1f");
}


template <>
void
inspect(entt::handle&, sim::HitPoints& points)
{
	ImGui::Text("Total: %.2f", points.total());
	ImGui::InputDouble("Shield", &points.shield.points, 0, 0, "%.1f");
	ImGui::InputDouble("Armour", &points.armour.points, 0, 0, "%.1f");
	ImGui::InputDouble("Structure", &points.structure.points, 0, 0, "%.1f");
}


template <>
void
inspect(entt::handle&, sim::AIShip& ai)
{
	ImGui::Text("Nothing to inspect in AIShip for now");
}


template <>
void
inspect(entt::handle&, sim::KeepAtRange& action)
{
	ImGui::InputDouble("Keep At Range", &action.distance, 0, 0, "%.1f");
}


template <>
void
inspect(entt::handle&, AIVisuals& visuals)
{
	ImGui::Checkbox("Show Target", &visuals.show_target);
	ImGui::Checkbox("Show Destination", &visuals.show_destination);
}


template <>
void
inspect(entt::handle&, TurretVisuals& visuals)
{
	ImGui::Checkbox("Show Turret Ranges", &visuals.visible);
}


static constexpr Color OPTIMAL {0x88, 0x22, 0x22, 0xff};
static constexpr Color EFFECTIVE {0x77, 0x50, 0x22, 0xff};


void
draw_turret_visuals(const sim::State& ctx)
{
	const auto helpers = ctx.registry.view<TurretVisuals, sim::Turret>();
	for (const auto& [entity, visuals, turret] : helpers.each()) {
		if (!visuals.visible || !ctx.registry.valid(turret.owner))
			continue;
		const auto& transform = ctx.registry.get<sim::Transform>(turret.owner);
		const auto center = ctx.camera.to_screen(transform.position);
		const auto optimal = turret.type.optimal_range * ctx.camera.scale;
		const auto effective = turret.type.effective_range() * ctx.camera.scale;
		DrawCircleLines(center.x, center.y, optimal, OPTIMAL);
		DrawCircleLines(center.x, center.y, effective, EFFECTIVE);
	}
}


static constexpr Color DESTINATION {0x22, 0x22, 0x88, 0xff};
static constexpr Color TARGET {0x44, 0x22, 0x66, 0xff};


void
draw_ai_visuals(const sim::State& ctx)
{
	const auto visuals = ctx.registry.view<AIVisuals, sim::AIShip, sim::Transform>();
	for (const auto& [entity, visuals, ai, transform] : visuals.each()) {
		const auto start = ctx.camera.to_screen(transform.position);
		if (visuals.show_target && ctx.registry.valid(ai.target)) {
			const auto& target = ctx.registry.get<sim::Transform>(ai.target);
			const auto end = ctx.camera.to_screen(target.position);
			DrawLine(start.x, start.y, end.x, end.y, TARGET);
		}
		if (!visuals.show_destination)
			continue;
		static constexpr double radius = 8.0;
		const auto dest = ctx.camera.to_screen(ai.destination);
		const auto diff = dest - start;
		const auto length = diff.magnitude();
		if (length > radius) {
			const auto end = start + diff.normalized().scale(length - radius);
			DrawLine(start.x, start.y, end.x, end.y, DESTINATION);
		}
		DrawCircleLines(dest.x, dest.y, radius, DESTINATION);
	}
}


}  // namespace kurator