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

#include <algorithm>
#include <cmath>
#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) :
	session {std::move(_session)},
	battle {sim::prepare(campaign::scenarios::example())},
	time_factor {1.0}
{
	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::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 -= 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;
	}
	balance.update(registry);
	ImGui::Begin("Controls");
	ImGui::SliderFloat("Time Factor", &time_factor, 0.1f, 3.0f);
	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()) {
		DrawLine(
			hwidth + line.start.x*scale,
			hheight + line.start.y*scale,
			hwidth + line.end.x*scale,
			hheight + line.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_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);
	registry.emplace<Line>(line, RED, source.position, victim.position);
}


void
Battle::on_ship_left(const stats::ShipLeft& event)
{
	log.push_back(event);
}


}  // namespace kurator