diff --git a/src/entities/conveyor.cpp b/src/entities/conveyor.cpp new file mode 100644 index 0000000..308b95e --- /dev/null +++ b/src/entities/conveyor.cpp @@ -0,0 +1,47 @@ +#include "entities/conveyor.h" + +#include "items/item.h" +#include "map/map.h" + +#include + +namespace entity { + +void Conveyor::update(double dt) { + // std::cout << Map::instance().at({0, 0}).pos() << std::endl; + if(m_has_item) { + if(m_progress >= 1.0) { + m_progress = 1.0; + auto pos = node().pos(); + auto &next_belt_node = Map::instance().at(pos + Vector2(1, 0)); + auto *item_sink = dynamic_cast(&next_belt_node.entity()); + + if(item_sink && item_sink->can_accept(*m_item)) { + std::cout << "accepted!" << std::endl; + item_sink->insert(*m_item); + m_has_item = false; + } + // std::cout << item_sink << ", " << item_sink->can_accept(m_item); + } else { + m_progress += 1.0 * dt; + } + + std::cout << "progress: " << m_progress << std::endl; + } +} + +bool Conveyor::can_accept(Item &item) { + // We dont care what item yet + (void)item; + + return !m_has_item; +} + +void Conveyor::insert(Item &item) { + m_has_item = true; + m_progress = 0.0; + m_item = &item; +} + +} // End namespace entity + diff --git a/src/entities/conveyor.h b/src/entities/conveyor.h new file mode 100644 index 0000000..b5c46c4 --- /dev/null +++ b/src/entities/conveyor.h @@ -0,0 +1,35 @@ +#include "entities/entity.h" +#include "interfaces/itemsink.h" +#include "map/node.h" + +namespace entity { + +class Conveyor : public Entity, public ItemSink { + public: + Conveyor() : Entity(5) { std::cout << "Conveyor construct" << std::endl; } + + Conveyor(const Conveyor &other) = default; + Conveyor &operator=(const Conveyor &other) = default; + + virtual ~Conveyor() = default; //{ std::cout << "Conveyor destruct" << std::endl; } + + virtual Conveyor *clone() { return new Conveyor(*this); } + + void update(double dt) override; + + bool can_accept(Item &item) override; + void insert(Item &item) override; + + bool has_item() { return m_has_item; } + float progress() { return m_progress; } + Item &item() { return *m_item; } + + private: + bool m_has_item = false; + float m_progress = 0.0; + + Item *m_item; +}; + +} // End namespace entity + diff --git a/src/interfaces/itemsink.h b/src/interfaces/itemsink.h new file mode 100644 index 0000000..3cb97b3 --- /dev/null +++ b/src/interfaces/itemsink.h @@ -0,0 +1,9 @@ +#pragma once + +#include "items/item.h" + +class ItemSink { + public: + virtual bool can_accept(Item &item) = 0; + virtual void insert(Item &item) = 0; +}; diff --git a/src/items/item.cpp b/src/items/item.cpp new file mode 100644 index 0000000..8c8ff5c --- /dev/null +++ b/src/items/item.cpp @@ -0,0 +1,10 @@ +#include "items/item.h" + +void Item::render(Render r) { + r.fill_rectangle_sz( + {10, 10}, + {255, 0, 0} + ); +} + + diff --git a/src/items/item.h b/src/items/item.h new file mode 100644 index 0000000..c4e0681 --- /dev/null +++ b/src/items/item.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "util/render.h" + +class Item { + public: + Item() : Item(-1) {} + Item(int id) : m_id(id) {} + + Item(const Item &other) = default; + Item &operator=(const Item &other) = default; + + virtual ~Item() = default; + + // virtual Item *clone() { return new Item(*this); } + + int id() { return m_id; } + + virtual void render(Render r); + + friend std::ostream &operator<<(std::ostream &os, const Item &item); + friend std::istream &operator>>(std::istream &is, Item &item); + + private: + int m_id; +}; + diff --git a/src/items/wrench.cpp b/src/items/wrench.cpp new file mode 100644 index 0000000..99c901c --- /dev/null +++ b/src/items/wrench.cpp @@ -0,0 +1,23 @@ +#include "wrench.h" + +std::vector> points = { + {75.0, 53.5}, + {65.5, 22.0}, + {85.0, 0.0}, + {122.5, 0.0}, + {103.5, 27.5}, + {127.0, 47.0}, + {149.5, 18.5}, + {168.5, 57.0}, + {149.5, 79.0}, + {114.0, 79.0}, + {35.5, 191.0}, + {0.0, 193.5}, + {0.0, 165.5}, + {75.0, 53.5} +}; + +void Wrench::render(Render r) { + r.scale({0.0949554, 0.08268733}); + r.draw_lines(points); +} diff --git a/src/items/wrench.h b/src/items/wrench.h new file mode 100644 index 0000000..31dc181 --- /dev/null +++ b/src/items/wrench.h @@ -0,0 +1,19 @@ +#include "item.h" + +class Wrench : public Item { + public: + Wrench() : Item(1) {} + + Wrench(const Wrench &other) = default; + Wrench &operator=(const Wrench &other) = delete; + + virtual ~Wrench() = default; + + virtual Wrench *clone() { return new Wrench(*this); } + + void render(Render r) override; + + private: + +}; + diff --git a/src/tiles/conveyor.cpp b/src/tiles/conveyor.cpp new file mode 100644 index 0000000..9d83106 --- /dev/null +++ b/src/tiles/conveyor.cpp @@ -0,0 +1,25 @@ +#include "tiles/conveyor.h" +#include "items/wrench.h" +#include "entities/conveyor.h" + +Entity *Conveyor::create_entity() { + return new entity::Conveyor(); +} + +void Conveyor::render(Render r) { + auto &entity = (entity::Conveyor &)node().entity(); + r.push_matrix(); + r.translate(node().pos() * (TILE_SIZE + 1)); + r.fill_rectangle_sz( + node().size() * TILE_SIZE, + {0, (int)(entity.progress() * 255.0), 255 - (int)((entity.progress() * 255.0))} + ); + + if(entity.has_item()) { + auto pos = Vector2(entity.progress() - 1.0, 0) * (TILE_SIZE + 1); + r.translate(pos); + entity.item().render(r); + } + r.pop_matrix(); +} + diff --git a/src/tiles/conveyor.h b/src/tiles/conveyor.h new file mode 100644 index 0000000..1ab9a53 --- /dev/null +++ b/src/tiles/conveyor.h @@ -0,0 +1,21 @@ +#include "tiles/tile.h" +#include "map/node.h" + +class Conveyor : public Tile { + public: + Conveyor() : Tile(5) {} + + Conveyor(const Conveyor &other) = default; + Conveyor &operator=(const Conveyor &other) = delete; + + virtual ~Conveyor() = default; + + virtual Conveyor *clone() { return new Conveyor(*this); } + + Entity *create_entity() override; + + void render(Render r) override; + + private: + +};