From 1fd0d9656fe5e263453920dc52b8b0ea43134349 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 31 Jul 2021 22:53:53 -0400 Subject: [PATCH] Entities or something --- src/app.cpp | 8 +++--- src/entities/entity.cpp | 21 ++++++++++++++ src/entities/entity.h | 35 +++++++++++++++++++++++ src/entities/rainbow.cpp | 18 ++++++++++++ src/entities/rainbow.h | 30 ++++++++++++++++++++ src/map/node.cpp | 61 +++++++++++++++++++++++++++++++++++++--- src/map/node.h | 16 +++++------ src/tiles/dirt.cpp | 6 ++-- src/tiles/dirt.h | 3 +- src/tiles/grass.cpp | 6 ++-- src/tiles/grass.h | 3 +- src/tiles/rainbow.cpp | 17 +++++++++++ src/tiles/rainbow.h | 21 ++++++++++++++ src/tiles/stone.cpp | 6 ++-- src/tiles/stone.h | 3 +- src/tiles/tile.cpp | 22 +++++++++++---- src/tiles/tile.h | 15 ++++++++-- 17 files changed, 253 insertions(+), 38 deletions(-) create mode 100644 src/entities/entity.cpp create mode 100644 src/entities/entity.h create mode 100644 src/entities/rainbow.cpp create mode 100644 src/entities/rainbow.h create mode 100644 src/tiles/rainbow.cpp create mode 100644 src/tiles/rainbow.h diff --git a/src/app.cpp b/src/app.cpp index e376b05..da68615 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,6 +1,7 @@ #include "app.h" #include "map/map.h" +#include "tiles/rainbow.h" #include "tiles/dirt.h" #include "tiles/grass.h" #include "util/vector2.h" @@ -20,8 +21,6 @@ bool App::OnUserCreate() bool App::OnUserUpdate(float delta) { - (void)delta; - Vector2 screen = {ScreenWidth(), ScreenHeight()}; Vector2 mouse = {GetMouseX(), GetMouseY()}; @@ -37,7 +36,7 @@ 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 Tile()); + m_map.set_tile(tile_pos, new Rainbow()); } if(GetMouse(1).bPressed) { auto screen_pos = mouse - m_camera_pos; @@ -67,7 +66,8 @@ 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 = m_map.at(Vector2(x, y)); + node.update(delta); node.render(m_render); } } diff --git a/src/entities/entity.cpp b/src/entities/entity.cpp new file mode 100644 index 0000000..62c1d0f --- /dev/null +++ b/src/entities/entity.cpp @@ -0,0 +1,21 @@ +#include "entities/entity.h" +#include "map/node.h" + +void Entity::update(double dt) { + (void)dt; + std::cout << "Entity " << m_id << " did not override update, you fucked up!" << std::endl; +} + +std::ostream &operator<<(std::ostream &os, const Entity &entity) { + os.write(reinterpret_cast(&entity.m_id), sizeof(entity.m_id)); + + return os; +} + +std::istream &operator>>(std::istream &is, Entity &entity) { + (void)entity; + + return is; +} + + diff --git a/src/entities/entity.h b/src/entities/entity.h new file mode 100644 index 0000000..05ef9ff --- /dev/null +++ b/src/entities/entity.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +class Node; + +class Entity { + public: + Entity() : Entity(-1) {} + Entity(int id) : m_id(id) {} + + Entity(const Entity &other) = default; + Entity &operator=(const Entity &other) = delete; + + virtual ~Entity() = default; + + virtual Entity *clone() { return new Entity(*this); } + + void set_node(Node *node) { m_node = node; } + Node &node() { return *m_node; } + + // Get the tile's ID + int id() { return m_id; } + // void set_id(int id) { m_id = id; } + + virtual void update(double dt); + + friend std::ostream &operator<<(std::ostream &os, const Entity &tile); + friend std::istream &operator>>(std::istream &is, Entity &tile); + + private: + Node *m_node; + + int m_id; +}; diff --git a/src/entities/rainbow.cpp b/src/entities/rainbow.cpp new file mode 100644 index 0000000..63e2b2d --- /dev/null +++ b/src/entities/rainbow.cpp @@ -0,0 +1,18 @@ +#include "entities/rainbow.h" + +#include + +namespace entity { + +void Rainbow::update(double dt) { + m_time += dt; + + if(m_time - m_prev_time > 0.1) { + std::cout << "Entity tick 1 second" << std::endl; + m_prev_time = m_time; + m_color = Color::from_hsv(rand() % 255, 255, 255); + } +} + +} // End namespace entity + diff --git a/src/entities/rainbow.h b/src/entities/rainbow.h new file mode 100644 index 0000000..6ba6142 --- /dev/null +++ b/src/entities/rainbow.h @@ -0,0 +1,30 @@ +#include "entities/entity.h" +#include "map/node.h" + +namespace entity { + +class Rainbow : public Entity { + public: + Rainbow() : Entity(4) { std::cout << "Rainbow construct" << std::endl; } + + Rainbow(const Rainbow &other) = default; + Rainbow &operator=(const Rainbow &other) = default; + + virtual ~Rainbow() = default; //{ std::cout << "Rainbow destruct" << std::endl; } + + virtual Rainbow *clone() { return new Rainbow(*this); } + + void update(double dt) override; + + void set_color(Color color) { m_color = color; } + Color color() { return m_color; } + + private: + double m_time = 0; + double m_prev_time = 0; + + Color m_color = Color::from_hsv(rand() % 255, 255, 255); +}; + +} // End namespace entity + diff --git a/src/map/node.cpp b/src/map/node.cpp index a7800d4..bd87b68 100644 --- a/src/map/node.cpp +++ b/src/map/node.cpp @@ -3,10 +3,19 @@ #include "tiles/dirt.h" #include "tiles/grass.h" #include "tiles/stone.h" +#include "tiles/rainbow.h" Node::Node(const Node &other) { - if(other.m_tile) m_tile = other.m_tile->clone(); - // if(other.m_entity) m_entity = other.m_entity->clone(); + if(other.m_tile) { + m_tile = other.m_tile->clone(); + m_tile->set_node(this); + } + + if(other.m_entity) { + std::cout << "Entity being cloned" << std::endl; + m_entity = other.m_entity->clone(); + m_entity->set_node(this); + } m_pos = other.m_pos; m_size = other.m_size; @@ -14,12 +23,51 @@ Node::Node(const Node &other) { Node::~Node() { if(m_tile) delete m_tile; - // if(m_entity) delete(m_entity); + if(m_entity) delete m_entity; +} + +void Node::set_tile(Tile *tile) { + if(!tile) { + std::cout << "Tried to set null tile!" << std::endl; + return; + } + if(m_tile) { + delete m_tile; + m_tile = nullptr; + } + if(m_entity) { + delete m_entity; + m_entity = nullptr; + } + + m_tile = tile; + m_tile->set_node(this); + + set_entity(m_tile->create_entity()); +} + +void Node::set_entity(Entity *entity) { + std::cout << "Entity set!@#?! " << entity << std::endl; + if(!entity) + return; + + if(m_entity) { + delete m_entity; + m_entity = nullptr; + } + + m_entity = entity; + m_entity->set_node(this); +} + +void Node::update(double dt) { + if(m_entity) + m_entity->update(dt); } void Node::render(Render r) { if(m_tile) - m_tile->render(r, pos(), size()); + m_tile->render(r); } std::ostream &operator<<(std::ostream &os, const Node &node) { @@ -57,7 +105,12 @@ std::istream &operator>>(std::istream &is, Node &node) { break; case 3: tile = new Stone(); + break; + case 4: + tile = new Rainbow(); + break; default: + std::cout << "Invalid tile" << std::endl; break; } if(tile) node.set_tile(tile); diff --git a/src/map/node.h b/src/map/node.h index 8bda56c..1cb283a 100644 --- a/src/map/node.h +++ b/src/map/node.h @@ -3,6 +3,7 @@ #include #include "tiles/tile.h" +#include "entities/entity.h" #include "util/render.h" #include "util/vector2.h" @@ -20,16 +21,12 @@ class Node { ~Node(); // Get a reference to the tile associated with this node - Tile *tile() { return m_tile; } - void set_tile(Tile *tile) { - if(m_tile) - delete m_tile; - - m_tile = tile; - } + Tile &tile() { return *m_tile; } + void set_tile(Tile *tile); // Get a reference to the tile entity associated with this node - // Entity &entity() { return *m_entity; } + Entity &entity() { return *m_entity; } + void set_entity(Entity *tile); // Get/Set the position in world coordinates Vector2 pos() { return m_pos; } @@ -39,6 +36,7 @@ class Node { Vector2 size() { return m_size; } void set_size(Vector2 size) { m_size = size; } + void update(double dt); void render(Render r); friend std::ostream &operator<<(std::ostream &os, const Node &node); @@ -47,7 +45,7 @@ class Node { private: Tile *m_tile = 0; - // Entity *m_entity; + Entity *m_entity = 0; Vector2 m_pos; Vector2 m_size; diff --git a/src/tiles/dirt.cpp b/src/tiles/dirt.cpp index 8aa7f08..97d8e03 100644 --- a/src/tiles/dirt.cpp +++ b/src/tiles/dirt.cpp @@ -1,9 +1,9 @@ #include "tiles/dirt.h" -void Dirt::render(Render r, Vector2pos, Vector2size) { +void Dirt::render(Render r) { r.fill_rectangle_sz( - pos * (TILE_SIZE + 1), - size * TILE_SIZE, + node().pos() * (TILE_SIZE + 1), + node().size() * TILE_SIZE, {100, 50, 10} ); } diff --git a/src/tiles/dirt.h b/src/tiles/dirt.h index 6d629ef..0540cbf 100644 --- a/src/tiles/dirt.h +++ b/src/tiles/dirt.h @@ -1,4 +1,5 @@ #include "tiles/tile.h" +#include "map/node.h" class Dirt : public Tile { public: @@ -11,7 +12,7 @@ class Dirt : public Tile { virtual Dirt *clone() { return new Dirt(*this); } - void render(Render r, Vector2pos, Vector2size) override; + void render(Render r) override; private: diff --git a/src/tiles/grass.cpp b/src/tiles/grass.cpp index 7f71c3b..d5b7ca5 100644 --- a/src/tiles/grass.cpp +++ b/src/tiles/grass.cpp @@ -1,9 +1,9 @@ #include "tiles/grass.h" -void Grass::render(Render r, Vector2pos, Vector2size) { +void Grass::render(Render r) { r.fill_rectangle_sz( - pos * (TILE_SIZE + 1), - size * TILE_SIZE, + node().pos() * (TILE_SIZE + 1), + node().size() * TILE_SIZE, {7, 176, 52} ); } diff --git a/src/tiles/grass.h b/src/tiles/grass.h index c154d18..f98db8c 100644 --- a/src/tiles/grass.h +++ b/src/tiles/grass.h @@ -1,4 +1,5 @@ #include "tiles/tile.h" +#include "map/node.h" class Grass : public Tile { public: @@ -11,7 +12,7 @@ class Grass : public Tile { virtual Grass *clone() { return new Grass(*this); } - void render(Render r, Vector2pos, Vector2size) override; + void render(Render r) override; private: diff --git a/src/tiles/rainbow.cpp b/src/tiles/rainbow.cpp new file mode 100644 index 0000000..289e526 --- /dev/null +++ b/src/tiles/rainbow.cpp @@ -0,0 +1,17 @@ +#include "tiles/rainbow.h" +#include "entities/rainbow.h" + +Entity *Rainbow::create_entity() { + std::cout << "Create called" << std::endl; + return new entity::Rainbow(); +} + +void Rainbow::render(Render r) { + auto &entity = (entity::Rainbow &)node().entity(); + r.fill_rectangle_sz( + node().pos() * (TILE_SIZE + 1), + node().size() * TILE_SIZE, + entity.color() + ); +} + diff --git a/src/tiles/rainbow.h b/src/tiles/rainbow.h new file mode 100644 index 0000000..0b4d47d --- /dev/null +++ b/src/tiles/rainbow.h @@ -0,0 +1,21 @@ +#include "tiles/tile.h" +#include "map/node.h" + +class Rainbow : public Tile { + public: + Rainbow() : Tile(4) {} + + Rainbow(const Rainbow &other) = default; + Rainbow &operator=(const Rainbow &other) = delete; + + virtual ~Rainbow() = default; + + virtual Rainbow *clone() { return new Rainbow(*this); } + + Entity *create_entity() override; + + void render(Render r) override; + + private: + +}; diff --git a/src/tiles/stone.cpp b/src/tiles/stone.cpp index 8737328..fa90dd3 100644 --- a/src/tiles/stone.cpp +++ b/src/tiles/stone.cpp @@ -1,9 +1,9 @@ #include "tiles/stone.h" -void Stone::render(Render r, Vector2pos, Vector2size) { +void Stone::render(Render r) { r.fill_rectangle_sz( - pos * (TILE_SIZE + 1), - size * TILE_SIZE, + node().pos() * (TILE_SIZE + 1), + node().size() * TILE_SIZE, {42, 42, 42} ); } diff --git a/src/tiles/stone.h b/src/tiles/stone.h index 7f2a4f1..71bd602 100644 --- a/src/tiles/stone.h +++ b/src/tiles/stone.h @@ -1,4 +1,5 @@ #include "tiles/tile.h" +#include "map/node.h" class Stone : public Tile { public: @@ -11,7 +12,7 @@ class Stone : public Tile { virtual Stone *clone() { return new Stone(*this); } - void render(Render r, Vector2pos, Vector2size) override; + void render(Render r) override; private: diff --git a/src/tiles/tile.cpp b/src/tiles/tile.cpp index ae2eccc..d611556 100644 --- a/src/tiles/tile.cpp +++ b/src/tiles/tile.cpp @@ -1,22 +1,32 @@ #include "tiles/tile.h" +#include "map/node.h" +#include "entities/rainbow.h" -void Tile::render(Render r, Vector2pos, Vector2size) { +Entity *Tile::create_entity() { + // std::cout << "Tile " << id() << " did not override create_entity, it is boring..." << std::endl; + return nullptr; +} + +void Tile::render(Render r) { r.rectangle_sz( - pos * (TILE_SIZE + 1), - size * TILE_SIZE, - m_color + node().pos() * (TILE_SIZE + 1), + node().size() * TILE_SIZE, + {255, 0, 0} ); } std::ostream &operator<<(std::ostream &os, const Tile &tile) { + Color tmp; os.write(reinterpret_cast(&tile.m_id), sizeof(tile.m_id)); - os.write(reinterpret_cast(&tile.m_color), sizeof(tile.m_color)); + os.write(reinterpret_cast(&tmp), sizeof(tmp)); return os; } std::istream &operator>>(std::istream &is, Tile &tile) { - is.read(reinterpret_cast(&tile.m_color), sizeof(tile.m_color)); + (void)tile; + Color tmp; + is.read(reinterpret_cast(&tmp), sizeof(tmp)); return is; } diff --git a/src/tiles/tile.h b/src/tiles/tile.h index 4b5297b..a24d408 100644 --- a/src/tiles/tile.h +++ b/src/tiles/tile.h @@ -2,10 +2,13 @@ #include +#include "entities/entity.h" #include "util/render.h" #include "util/vector2.h" -#define TILE_SIZE 16 +#define TILE_SIZE 8 + +class Node; class Tile { public: @@ -19,16 +22,22 @@ class Tile { virtual Tile *clone() { return new Tile(*this); } + void set_node(Node *node) { m_node = node; } + Node &node() { return *m_node; } + + virtual Entity *create_entity(); + // Get the tile's ID int id() { return m_id; } // void set_id(int id) { m_id = id; } - virtual void render(Render r, Vector2pos, Vector2size); + virtual void render(Render r); friend std::ostream &operator<<(std::ostream &os, const Tile &tile); friend std::istream &operator>>(std::istream &is, Tile &tile); private: + Node *m_node; + int m_id; - Color m_color = Color::from_hsv(rand() % 255, 255, 255); };