Helps to commit *most* of the code

This commit is contained in:
Thomas Muller 2021-08-07 02:11:28 -04:00
parent b3f1071a1d
commit 7b4352de0f
Signed by: thomas
GPG key ID: AF006EB730564952
9 changed files with 218 additions and 0 deletions

47
src/entities/conveyor.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "entities/conveyor.h"
#include "items/item.h"
#include "map/map.h"
#include <iostream>
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<int>(1, 0));
auto *item_sink = dynamic_cast<ItemSink *>(&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

35
src/entities/conveyor.h Normal file
View file

@ -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

View file

@ -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;
};

10
src/items/item.cpp Normal file
View file

@ -0,0 +1,10 @@
#include "items/item.h"
void Item::render(Render r) {
r.fill_rectangle_sz(
{10, 10},
{255, 0, 0}
);
}

29
src/items/item.h Normal file
View file

@ -0,0 +1,29 @@
#pragma once
#include <iostream>
#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;
};

23
src/items/wrench.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "wrench.h"
std::vector<Vector2<float>> 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);
}

19
src/items/wrench.h Normal file
View file

@ -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:
};

25
src/tiles/conveyor.cpp Normal file
View file

@ -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<float>(entity.progress() - 1.0, 0) * (TILE_SIZE + 1);
r.translate(pos);
entity.item().render(r);
}
r.pop_matrix();
}

21
src/tiles/conveyor.h Normal file
View file

@ -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:
};