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
This commit is contained in:
Thomas Muller 2021-08-07 02:06:35 -04:00
parent 9633b155bd
commit b3f1071a1d
Signed by: thomas
GPG key ID: AF006EB730564952
3 changed files with 25 additions and 6 deletions

View file

@ -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<int> screen = {ScreenWidth(), ScreenHeight()};
Vector2<int> 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<ItemSink *>(&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<int>(x, y));
auto &node = map.at(Vector2<int>(x, y));
node.update(delta);
node.render(m_render);
}

View file

@ -17,7 +17,6 @@ public:
private:
Render m_render;
Map m_map;
Vector2<int> m_drag_start;
Vector2<int> m_camera_pos;

View file

@ -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<int> pos);
void set_tile(Vector2<int> pos, Tile *tile);