From b3f1071a1d553af3ce65d831f0d6df5460fedb65 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 7 Aug 2021 02:06:35 -0400 Subject: [PATCH] We have items and a conveyor belt! Map is now a singleton so nodes can look up blocks that are next to them Conveyor block/entity now exists and can move an item to the right Added a wrench item for conveyor belts to move, it has the best texture ever --- src/app.cpp | 22 +++++++++++++++++----- src/app.h | 1 - src/map/map.h | 8 ++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index dcdf3c2..17963f8 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,7 +1,10 @@ #include "app.h" #include "map/map.h" +#include "interfaces/itemsink.h" +#include "items/wrench.h" #include "tiles/rainbow.h" +#include "tiles/conveyor.h" #include "tiles/dirt.h" #include "tiles/grass.h" #include "util/vector2.h" @@ -23,14 +26,16 @@ bool App::OnUserCreate() bool App::OnUserUpdate(float delta) { + auto &map = Map::instance(); + Vector2 screen = {ScreenWidth(), ScreenHeight()}; Vector2 mouse = {GetMouseX(), GetMouseY()}; // Save and load keys if(GetKey(olc::Key::S).bReleased) - m_map.save(); + map.save(); if(GetKey(olc::Key::L).bReleased) - m_map.load(); + map.load(); // Basic block placing if(GetMouse(0).bPressed) { @@ -38,14 +43,21 @@ bool App::OnUserUpdate(float delta) auto tile_pos = screen_pos / (TILE_SIZE + 1); if(screen_pos.x() < 0) tile_pos -= {1, 0}; if(screen_pos.y() < 0) tile_pos -= {0, 1}; - m_map.set_tile(tile_pos, new Rainbow()); + map.set_tile(tile_pos, new Conveyor()); } if(GetMouse(1).bPressed) { auto screen_pos = mouse - m_camera_pos; auto tile_pos = screen_pos / (TILE_SIZE + 1); if(screen_pos.x() < 0) tile_pos -= {1, 0}; if(screen_pos.y() < 0) tile_pos -= {0, 1}; - m_map.set_tile(tile_pos, new Dirt()); + // map.set_tile(tile_pos, new Dirt()); + auto sink = dynamic_cast(&map.at(tile_pos).entity()); + std::cout << "Insert " << sink << std::endl; + if(sink) { + std::cout << sink->can_accept(*(new Wrench())) << std::endl; + sink->insert(*(new Wrench())); + std::cout << sink->can_accept(*(new Wrench())) << std::endl; + } } // Pan camera with middle mouse @@ -68,7 +80,7 @@ bool App::OnUserUpdate(float delta) for(int y = min.y() - 1; y < max.y() + 1; y++) { for(int x = min.x() - 1; x < max.x() + 1; x++) { - auto &node = m_map.at(Vector2(x, y)); + auto &node = map.at(Vector2(x, y)); node.update(delta); node.render(m_render); } diff --git a/src/app.h b/src/app.h index e3ac11a..52fa4be 100644 --- a/src/app.h +++ b/src/app.h @@ -17,7 +17,6 @@ public: private: Render m_render; - Map m_map; Vector2 m_drag_start; Vector2 m_camera_pos; diff --git a/src/map/map.h b/src/map/map.h index 08e770a..c3b6dfc 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -10,6 +10,14 @@ class Map { public: Map() = default; + Map(Map const &) = delete; + void operator=(const Map &) = delete; + + static Map &instance() { + static Map s_instance; + return s_instance; + } + Node &at(Vector2 pos); void set_tile(Vector2 pos, Tile *tile);