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

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

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

#include <kurator/campaign/Scenario.h>
#include <kurator/campaign/scenarios.h>
#include <kurator/campaign/ShipConfig.h>
#include <kurator/stats/EventLog.h>
#include <kurator/stats/events.h>

#include "Battle.h"
#include "Session.h"


namespace kurator
{


class Reporter
{
public:
	Reporter(std::vector<campaign::ShipConfig>& _ships) : ships {_ships} {}
	void operator()(const stats::ShipLeft& event);
private:
	std::vector<campaign::ShipConfig>& ships;
};


Campaign::Campaign(std::shared_ptr<Session> _session) :
	session {std::move(_session)},
	ships {},
	team {0},
	level {0},
	id {0}
{
	auto scenario = campaign::scenarios::example();
	for (auto& ship : scenario.ships) {
		if (ship.team == team) {
			ship.identifier.id = id++;
			ships.push_back(std::move(ship));
		}
	}
}


void
Campaign::update(const float)
{
	if (ships.empty())
		return session->pop();
	if (ImGui::Begin("Campaign")) {
		ImGui::Text("Level %d, ships left:", level);
		for (const auto& ship : ships) {
			if (ship.team == team)
				ImGui::Text("%s", ship.type.c_str());
		}
		if (ImGui::Button("Start")) {
			campaign::Scenario scenario {"encounter", 12000, {}};
			scenario.ships.insert(scenario.ships.cend(), ships.begin(), ships.end());
			auto generator = campaign::scenarios::example();
			for (auto& ship : generator.ships) {
				if (ship.team != team) {
					ship.identifier.id = id++;
					scenario.ships.push_back(std::move(ship));
				}
			}
			auto destroy = [&](const stats::EventLog& log) { log.for_each(Reporter{ships}); };
			session->push(std::make_shared<Battle>(session, std::move(scenario), destroy));
			level++;
		}
		if (ImGui::Button("Back"))
			session->pop();
	}
	ImGui::End();
}


void
Campaign::draw() const
{
	ClearBackground(BLACK);
}


void
Reporter::operator()(const stats::ShipLeft& event)
{
	using campaign::ShipConfig;
	auto was_destroyed = [&event](const ShipConfig& ship){ return ship.identifier.id == event.ship.id; };
	ships.erase(std::remove_if(ships.begin(), ships.end(), was_destroyed), ships.end());
}


}  // namespace kurator