11#include " chomper/runtimes/game.hpp"
2- #include " chomper/collectible.hpp"
32#include " chomper/im_util.hpp"
43#include " chomper/runtimes/entrypoint.hpp"
5- #include " chomper/world_size.hpp"
64#include " chomper/world_space.hpp"
75#include < le2d/random.hpp>
86#include < algorithm>
@@ -12,17 +10,16 @@ namespace {
1210constexpr auto countdownParams_v = le::drawable::Text::Params{
1311 .height = le::TextHeight{60 },
1412};
15- constexpr auto collectibleAmount_v = 10 ;
1613} // namespace
1714using ActionValue = le::input::action::Value;
1815
1916Game::Game (gsl::not_null<Engine*> engine) : m_engine(engine), m_mapping(&engine->getInputRouter ()) {
2017 createPlayer ();
2118 m_world = std::make_unique<World>(m_engine);
2219
23- createCollectibleTexture ();
20+ createCollectibles ();
2421
25- spawnCollectibles ( );
22+ m_collectibles-> spawn (*m_player );
2623
2724 m_countdownText.set_string (engine->getResources ().getMainFont (), " 3" , countdownParams_v);
2825}
@@ -52,9 +49,7 @@ void Game::tick(kvf::Seconds const dt) {
5249
5350void Game::render (le::IRenderer& renderer) const {
5451 m_world->draw (renderer);
55- for (auto const & collectible : m_collectibles) {
56- collectible.draw (renderer);
57- }
52+ m_collectibles->draw (renderer);
5853 m_player->draw (renderer);
5954 if (m_countdown.count () > 0 ) {
6055 m_countdownText.draw (renderer);
@@ -89,67 +84,22 @@ void Game::createPlayer() {
8984 m_player = std::make_unique<Player>(m_mapping, m_engine);
9085}
9186
92- void Game::createCollectibleTexture () {
87+ void Game::createCollectibles () {
9388 m_collectibleTexture = m_engine->getResources ().load <le::ITexture>(" images/apple.png" );
94- }
95-
96- void Game::findEmptyTiles () {
97- m_emptyTiles.clear ();
98- m_emptyTiles.reserve (static_cast <int >(worldSize_v.x * worldSize_v.y ));
99- for (auto i = 0 ; i < static_cast <int >(worldSize_v.x * worldSize_v.y ); i++) {
100- m_emptyTiles.push_back (i);
101- }
102-
103- auto const removeTile = [this ](int tile) {
104- auto it = std::ranges::find (m_emptyTiles, tile);
105- if (it != m_emptyTiles.end ()) {
106- *it = m_emptyTiles.back ();
107- m_emptyTiles.pop_back ();
108- }
109- };
110-
111- for (auto const & seg : m_player->getSegments ()) {
112- auto p = worldSpace::worldToGrid (seg.transform .position );
113- removeTile (static_cast <int >((p.y * worldSize_v.x ) + p.x ));
114- }
115-
116- for (auto const & c : m_collectibles) {
117- auto p = c.getGridPosition ();
118- removeTile (static_cast <int >((p.y * worldSize_v.x ) + p.x ));
119- }
120- }
121-
122- void Game::spawnCollectibles () {
123- findEmptyTiles ();
124-
125- for (auto i = m_collectibles.size (); i < collectibleAmount_v; i++) {
126- if (m_emptyTiles.empty ()) {
127- return ;
128- }
129- // find a random tile
130- auto random = m_random.next_index (m_emptyTiles.size ());
131- auto tile = m_emptyTiles[random];
132- // remove said tile from the vector
133- std::erase_if (m_emptyTiles, [&](auto const & v) {
134- return v == m_emptyTiles[random];
135- });
136- // place the collectible on the tile
137- auto width = static_cast <int >(worldSize_v.x );
138- m_collectibles.emplace_back (*m_collectibleTexture, worldSpace::gridToWorld ({tile % width, tile / width}));
139- }
89+ m_collectibles = std::make_unique<Collectibles>(*m_collectibleTexture);
14090}
14191
14292void Game::collideCollectibles () {
143- auto it = std::ranges::find_if (m_collectibles, [&](auto const & collectible) {
144- return collectible.getGridPosition ( ) == worldSpace::worldToGrid (m_player->getSegments ().back ().transform .position );
93+ auto it = std::ranges::find_if (m_collectibles-> getInstances () , [&](auto const & collectible) {
94+ return worldSpace::worldToGrid ( collectible.transform . position ) == worldSpace::worldToGrid (m_player->getSegments ().back ().transform .position );
14595 });
146- if (it == m_collectibles.end ()) {
96+ if (it == m_collectibles-> getInstances () .end ()) {
14797 return ;
14898 }
14999
150- m_collectibles.erase (it);
151100 m_player->grow ();
152- spawnCollectibles ();
101+ m_collectibles->eraseInstance (static_cast <std::size_t >(std::distance (m_collectibles->getInstances ().begin (), it)));
102+ m_collectibles->spawn (*m_player);
153103}
154104
155105void Game::onGoBack () {
0 commit comments