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

#include <limits>
#include <memory>
#include <utility>

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

#include <kurator/campaign/Scenario.h>
#include <kurator/universe.h>
#include <kurator/universe/Repository.h>

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


namespace kurator
{


bool groups_editor(std::shared_ptr<universe::Repository> repo, std::vector<Group>& groups);
bool loadout_editor(std::shared_ptr<universe::Repository> repo, campaign::Loadout& loadout);


ScenarioEditor::ScenarioEditor(std::shared_ptr<Session> _session) :
	session {std::move(_session)},
	repository {universe::load_json("resources/universe")},
	battle {},
	frame {},
	radius {12000.0f}
{
	regenerate();
}


void
ScenarioEditor::update(float)
{
	frame.update();
	if (ImGui::Begin("Scenario Editor")) {
		bool changed = false;
		changed |= ImGui::SliderFloat("Distance", &radius, 8000.0f, 20000.0f);
		changed |= groups_editor(repository, groups);
		if (changed)
			regenerate();
		if (ImGui::Button("Start"))
			start_battle();
		ImGui::SameLine();
		if (ImGui::Button("Back"))
			session->pop();
	}
	ImGui::End();
	if (IsKeyPressed(KEY_SPACE))
		start_battle();
}


void
ScenarioEditor::draw() const
{
	ClearBackground(BLACK);
	frame.draw();
}


void
ScenarioEditor::start_battle()
{
	session->push(std::move(battle));
	regenerate();
}


void
ScenarioEditor::regenerate()
{
	int id = 0;
	campaign::Scenario scenario {"Player Encounter", radius, {}};
	for (const auto& group : groups) {
		for (auto it = group.counts.begin(); it != group.counts.end(); ++it) {
			const int team = it - group.counts.begin();
			for (int n = 0; n < *it; ++n)
				scenario.ships.push_back(campaign::ShipConfig{{id++}, team, group.loadout});
		}
	}
	battle = std::make_shared<Battle>(session, std::move(scenario));
	frame.reset(battle);
}


bool
groups_editor(std::shared_ptr<universe::Repository> repo, std::vector<Group>& groups)
{
	bool changed = false;
	if (ImGui::BeginTable("Groups", 1 + 6)) {
		ImGui::TableSetupColumn("Group", ImGuiTableColumnFlags_WidthFixed);
		for (int n = 1; n <= 6; ++n)
			ImGui::TableSetupColumn(TextFormat("Team %d", n));
		ImGui::TableHeadersRow();
		auto it = groups.begin();
		while (it != groups.end()) {
			ImGui::PushID(it - groups.begin());
			ImGui::TableNextColumn();
			const bool removed = ImGui::Button("-");
			changed |= removed;
			ImGui::SameLine();
			if (ImGui::Button("Edit"))
				ImGui::OpenPopup("Layout Editor");
			if (ImGui::BeginPopup("Layout Editor")) {
				changed |= loadout_editor(repo, it->loadout);
				ImGui::EndPopup();
			}
			for (auto jt = it->counts.begin(); jt != it->counts.end(); ++jt) {
				ImGui::TableNextColumn();
				ImGui::PushID(jt - it->counts.begin());
				ImGui::PushItemWidth(-std::numeric_limits<float>::min());
				changed |= ImGui::DragInt("##Count", &(*jt), 0.2f, 0, 20);
				ImGui::PopItemWidth();
				ImGui::PopID();
			}
			it = removed ? groups.erase(it) : std::next(it);
			ImGui::PopID();
		}
		ImGui::EndTable();
	}
	if (ImGui::Button("Add Group")) {
		groups.push_back(Group{{repo->ship_type()}});
		changed = true;
	}
	return changed;
}


bool
loadout_editor(std::shared_ptr<universe::Repository> repo, campaign::Loadout& loadout)
{
	bool changed = false;
	if (ImGui::BeginCombo("Ship Type", loadout.type.name.c_str())) {
		repo->for_ship_types([&](const auto& type){
			if (ImGui::Selectable(type.name.c_str(), loadout.type.name == type.name)) {
				loadout.type = type;
				changed = true;
			}
		});
		ImGui::EndCombo();
	}
	ImGui::Indent();
	auto it = loadout.turrets.begin();
	while (it != loadout.turrets.end()) {
		ImGui::PushID(it - loadout.turrets.begin());
		const bool removed = ImGui::Button("-");
		ImGui::SameLine();
		if (ImGui::BeginCombo("Turret Type", it->name.c_str())) {
			repo->for_turret_types([&](const auto& type){
				if (ImGui::Selectable(type.name.c_str(), it->name == type.name)) {
					*it = type;
					changed = true;
				}
			});
			ImGui::EndCombo();
		}
		it = removed ? loadout.turrets.erase(it) : std::next(it);
		changed |= removed;
		ImGui::PopID();
	}
	if (ImGui::Button("Add Turret")) {
		loadout.turrets.push_back(repo->turret_type());
		changed = true;
	}
	ImGui::Unindent();
	return changed;
}


}  // namespace kurator