-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.cpp
More file actions
158 lines (133 loc) · 4.3 KB
/
Copy pathgame.cpp
File metadata and controls
158 lines (133 loc) · 4.3 KB
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
#include "chomper/runtimes/game.hpp"
#include "chomper/collectible.hpp"
#include "chomper/im_util.hpp"
#include "chomper/runtimes/entrypoint.hpp"
#include "chomper/world_size.hpp"
#include "chomper/world_space.hpp"
#include <le2d/random.hpp>
#include <algorithm>
namespace chomper::runtime {
namespace {
constexpr auto collectibleAmount_v = 10;
} // namespace
using ActionValue = le::input::action::Value;
Game::Game(gsl::not_null<Engine*> engine) : m_engine(engine), m_mapping(&engine->getInputRouter()) {
createPlayer();
m_world = std::make_unique<World>(m_engine);
createCollectibleTexture();
spawnCollectibles();
m_countdown.emplace(&engine->getResources().getMainFont());
}
void Game::tick(kvf::Seconds const dt) {
ImGui::SetNextWindowSize({300.0f, 300.0f}, ImGuiCond_Once);
if (ImGui::Begin("Debug Inspect")) {
debugInspectWindow();
}
ImGui::End();
if (m_countdown) {
m_countdown->tick(dt);
if (m_countdown->getRemain() <= 0s) {
m_countdown.reset();
}
return;
}
m_player->tick(dt);
collideCollectibles();
// On death
if (!m_player->getInfo().alive) {
m_engine->setNextRuntime<runtime::Entrypoint>();
}
}
void Game::render(le::IRenderer& renderer) const {
m_world->draw(renderer);
for (auto const& collectible : m_collectibles) {
collectible.draw(renderer);
}
m_player->draw(renderer);
if (m_countdown) {
m_countdown->draw(renderer);
}
}
void Game::debugInspectWindow() {
auto const inspectItems = std::array{
InspectItem{.inspector = m_player.get(), .label = "Player"},
InspectItem{.inspector = &m_engine->getResources(), .label = "Resources"},
InspectItem{.inspector = m_engine, .label = "Engine"},
};
im_util::inspectAsTabs(inspectItems);
}
void Game::bindActions() {
// goBackKey is separated from PlayerController so that it works regardless of the type of controller in use.
// this implies that all actions must share the same mapping to be active simultaneously.
m_mapping.bind_action(&m_actions.goBackKey, [this](ActionValue const v) {
if (v.get<bool>()) {
onGoBack();
}
});
}
void Game::createPlayer() {
// clear bindings that may point to dangling actions after existing player gets destroyed.
m_mapping.clear_bindings();
// rebind game actions.
bindActions();
// create the player, passing a reference of the logger and a reference of the input mapping to create its PlayerController.
m_player = std::make_unique<Player>(m_mapping, m_engine);
}
void Game::createCollectibleTexture() {
m_collectibleTexture = m_engine->getResources().load<le::ITexture>("images/apple.png");
}
void Game::findEmptyTiles() {
m_emptyTiles.clear();
m_emptyTiles.reserve(static_cast<int>(worldSize_v.x * worldSize_v.y));
for (auto i = 0; i < static_cast<int>(worldSize_v.x * worldSize_v.y); i++) {
m_emptyTiles.push_back(i);
}
auto const removeTile = [this](int tile) {
auto it = std::ranges::find(m_emptyTiles, tile);
if (it != m_emptyTiles.end()) {
*it = m_emptyTiles.back();
m_emptyTiles.pop_back();
}
};
for (auto const& seg : m_player->getSegments()) {
auto p = worldSpace::worldToGrid(seg.transform.position);
removeTile(static_cast<int>((p.y * worldSize_v.x) + p.x));
}
for (auto const& c : m_collectibles) {
auto p = c.getGridPosition();
removeTile(static_cast<int>((p.y * worldSize_v.x) + p.x));
}
}
void Game::spawnCollectibles() {
findEmptyTiles();
for (auto i = m_collectibles.size(); i < collectibleAmount_v; i++) {
if (m_emptyTiles.empty()) {
return;
}
// find a random tile
auto random = m_random.next_index(m_emptyTiles.size());
auto tile = m_emptyTiles[random];
// remove said tile from the vector
std::erase_if(m_emptyTiles, [&](auto const& v) {
return v == m_emptyTiles[random];
});
// place the collectible on the tile
auto width = static_cast<int>(worldSize_v.x);
m_collectibles.emplace_back(*m_collectibleTexture, worldSpace::gridToWorld({tile % width, tile / width}));
}
}
void Game::collideCollectibles() {
auto it = std::ranges::find_if(m_collectibles, [&](auto const& collectible) {
return collectible.getGridPosition() == worldSpace::worldToGrid(m_player->getSegments().back().transform.position);
});
if (it == m_collectibles.end()) {
return;
}
m_collectibles.erase(it);
m_player->grow();
spawnCollectibles();
}
void Game::onGoBack() {
m_log.debug("execute 'go back' action here");
}
} // namespace chomper::runtime