Entities or something

This commit is contained in:
Thomas Muller 2021-07-31 22:53:53 -04:00
parent 3029d22fd8
commit 1fd0d9656f
Signed by: thomas
GPG key ID: AF006EB730564952
17 changed files with 253 additions and 38 deletions

View file

@ -1,6 +1,7 @@
#include "app.h" #include "app.h"
#include "map/map.h" #include "map/map.h"
#include "tiles/rainbow.h"
#include "tiles/dirt.h" #include "tiles/dirt.h"
#include "tiles/grass.h" #include "tiles/grass.h"
#include "util/vector2.h" #include "util/vector2.h"
@ -20,8 +21,6 @@ bool App::OnUserCreate()
bool App::OnUserUpdate(float delta) bool App::OnUserUpdate(float delta)
{ {
(void)delta;
Vector2<int> screen = {ScreenWidth(), ScreenHeight()}; Vector2<int> screen = {ScreenWidth(), ScreenHeight()};
Vector2<int> mouse = {GetMouseX(), GetMouseY()}; Vector2<int> mouse = {GetMouseX(), GetMouseY()};
@ -37,7 +36,7 @@ bool App::OnUserUpdate(float delta)
auto tile_pos = screen_pos / (TILE_SIZE + 1); auto tile_pos = screen_pos / (TILE_SIZE + 1);
if(screen_pos.x() < 0) tile_pos -= {1, 0}; if(screen_pos.x() < 0) tile_pos -= {1, 0};
if(screen_pos.y() < 0) tile_pos -= {0, 1}; 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) { if(GetMouse(1).bPressed) {
auto screen_pos = mouse - m_camera_pos; 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 y = min.y() - 1; y < max.y() + 1; y++) {
for(int x = min.x() - 1; x < max.x() + 1; x++) { for(int x = min.x() - 1; x < max.x() + 1; x++) {
auto node = m_map.at(Vector2<int>(x, y)); auto &node = m_map.at(Vector2<int>(x, y));
node.update(delta);
node.render(m_render); node.render(m_render);
} }
} }

21
src/entities/entity.cpp Normal file
View file

@ -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<const char *>(&entity.m_id), sizeof(entity.m_id));
return os;
}
std::istream &operator>>(std::istream &is, Entity &entity) {
(void)entity;
return is;
}

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

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

18
src/entities/rainbow.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "entities/rainbow.h"
#include <iostream>
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

30
src/entities/rainbow.h Normal file
View file

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

View file

@ -3,10 +3,19 @@
#include "tiles/dirt.h" #include "tiles/dirt.h"
#include "tiles/grass.h" #include "tiles/grass.h"
#include "tiles/stone.h" #include "tiles/stone.h"
#include "tiles/rainbow.h"
Node::Node(const Node &other) { Node::Node(const Node &other) {
if(other.m_tile) m_tile = other.m_tile->clone(); if(other.m_tile) {
// if(other.m_entity) m_entity = other.m_entity->clone(); 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_pos = other.m_pos;
m_size = other.m_size; m_size = other.m_size;
@ -14,12 +23,51 @@ Node::Node(const Node &other) {
Node::~Node() { Node::~Node() {
if(m_tile) delete m_tile; 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) { void Node::render(Render r) {
if(m_tile) if(m_tile)
m_tile->render(r, pos(), size()); m_tile->render(r);
} }
std::ostream &operator<<(std::ostream &os, const Node &node) { std::ostream &operator<<(std::ostream &os, const Node &node) {
@ -57,7 +105,12 @@ std::istream &operator>>(std::istream &is, Node &node) {
break; break;
case 3: case 3:
tile = new Stone(); tile = new Stone();
break;
case 4:
tile = new Rainbow();
break;
default: default:
std::cout << "Invalid tile" << std::endl;
break; break;
} }
if(tile) node.set_tile(tile); if(tile) node.set_tile(tile);

View file

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include "tiles/tile.h" #include "tiles/tile.h"
#include "entities/entity.h"
#include "util/render.h" #include "util/render.h"
#include "util/vector2.h" #include "util/vector2.h"
@ -20,16 +21,12 @@ class Node {
~Node(); ~Node();
// Get a reference to the tile associated with this node // Get a reference to the tile associated with this node
Tile *tile() { return m_tile; } Tile &tile() { return *m_tile; }
void set_tile(Tile *tile) { void set_tile(Tile *tile);
if(m_tile)
delete m_tile;
m_tile = tile;
}
// Get a reference to the tile entity associated with this node // 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 // Get/Set the position in world coordinates
Vector2<int> pos() { return m_pos; } Vector2<int> pos() { return m_pos; }
@ -39,6 +36,7 @@ class Node {
Vector2<int> size() { return m_size; } Vector2<int> size() { return m_size; }
void set_size(Vector2<int> size) { m_size = size; } void set_size(Vector2<int> size) { m_size = size; }
void update(double dt);
void render(Render r); void render(Render r);
friend std::ostream &operator<<(std::ostream &os, const Node &node); friend std::ostream &operator<<(std::ostream &os, const Node &node);
@ -47,7 +45,7 @@ class Node {
private: private:
Tile *m_tile = 0; Tile *m_tile = 0;
// Entity *m_entity; Entity *m_entity = 0;
Vector2<int> m_pos; Vector2<int> m_pos;
Vector2<int> m_size; Vector2<int> m_size;

View file

@ -1,9 +1,9 @@
#include "tiles/dirt.h" #include "tiles/dirt.h"
void Dirt::render(Render r, Vector2<int>pos, Vector2<int>size) { void Dirt::render(Render r) {
r.fill_rectangle_sz( r.fill_rectangle_sz(
pos * (TILE_SIZE + 1), node().pos() * (TILE_SIZE + 1),
size * TILE_SIZE, node().size() * TILE_SIZE,
{100, 50, 10} {100, 50, 10}
); );
} }

View file

@ -1,4 +1,5 @@
#include "tiles/tile.h" #include "tiles/tile.h"
#include "map/node.h"
class Dirt : public Tile { class Dirt : public Tile {
public: public:
@ -11,7 +12,7 @@ class Dirt : public Tile {
virtual Dirt *clone() { return new Dirt(*this); } virtual Dirt *clone() { return new Dirt(*this); }
void render(Render r, Vector2<int>pos, Vector2<int>size) override; void render(Render r) override;
private: private:

View file

@ -1,9 +1,9 @@
#include "tiles/grass.h" #include "tiles/grass.h"
void Grass::render(Render r, Vector2<int>pos, Vector2<int>size) { void Grass::render(Render r) {
r.fill_rectangle_sz( r.fill_rectangle_sz(
pos * (TILE_SIZE + 1), node().pos() * (TILE_SIZE + 1),
size * TILE_SIZE, node().size() * TILE_SIZE,
{7, 176, 52} {7, 176, 52}
); );
} }

View file

@ -1,4 +1,5 @@
#include "tiles/tile.h" #include "tiles/tile.h"
#include "map/node.h"
class Grass : public Tile { class Grass : public Tile {
public: public:
@ -11,7 +12,7 @@ class Grass : public Tile {
virtual Grass *clone() { return new Grass(*this); } virtual Grass *clone() { return new Grass(*this); }
void render(Render r, Vector2<int>pos, Vector2<int>size) override; void render(Render r) override;
private: private:

17
src/tiles/rainbow.cpp Normal file
View file

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

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

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

View file

@ -1,9 +1,9 @@
#include "tiles/stone.h" #include "tiles/stone.h"
void Stone::render(Render r, Vector2<int>pos, Vector2<int>size) { void Stone::render(Render r) {
r.fill_rectangle_sz( r.fill_rectangle_sz(
pos * (TILE_SIZE + 1), node().pos() * (TILE_SIZE + 1),
size * TILE_SIZE, node().size() * TILE_SIZE,
{42, 42, 42} {42, 42, 42}
); );
} }

View file

@ -1,4 +1,5 @@
#include "tiles/tile.h" #include "tiles/tile.h"
#include "map/node.h"
class Stone : public Tile { class Stone : public Tile {
public: public:
@ -11,7 +12,7 @@ class Stone : public Tile {
virtual Stone *clone() { return new Stone(*this); } virtual Stone *clone() { return new Stone(*this); }
void render(Render r, Vector2<int>pos, Vector2<int>size) override; void render(Render r) override;
private: private:

View file

@ -1,22 +1,32 @@
#include "tiles/tile.h" #include "tiles/tile.h"
#include "map/node.h"
#include "entities/rainbow.h"
void Tile::render(Render r, Vector2<int>pos, Vector2<int>size) { 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( r.rectangle_sz(
pos * (TILE_SIZE + 1), node().pos() * (TILE_SIZE + 1),
size * TILE_SIZE, node().size() * TILE_SIZE,
m_color {255, 0, 0}
); );
} }
std::ostream &operator<<(std::ostream &os, const Tile &tile) { std::ostream &operator<<(std::ostream &os, const Tile &tile) {
Color tmp;
os.write(reinterpret_cast<const char *>(&tile.m_id), sizeof(tile.m_id)); os.write(reinterpret_cast<const char *>(&tile.m_id), sizeof(tile.m_id));
os.write(reinterpret_cast<const char *>(&tile.m_color), sizeof(tile.m_color)); os.write(reinterpret_cast<const char *>(&tmp), sizeof(tmp));
return os; return os;
} }
std::istream &operator>>(std::istream &is, Tile &tile) { std::istream &operator>>(std::istream &is, Tile &tile) {
is.read(reinterpret_cast<char *>(&tile.m_color), sizeof(tile.m_color)); (void)tile;
Color tmp;
is.read(reinterpret_cast<char *>(&tmp), sizeof(tmp));
return is; return is;
} }

View file

@ -2,10 +2,13 @@
#include <iostream> #include <iostream>
#include "entities/entity.h"
#include "util/render.h" #include "util/render.h"
#include "util/vector2.h" #include "util/vector2.h"
#define TILE_SIZE 16 #define TILE_SIZE 8
class Node;
class Tile { class Tile {
public: public:
@ -19,16 +22,22 @@ class Tile {
virtual Tile *clone() { return new Tile(*this); } 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 // Get the tile's ID
int id() { return m_id; } int id() { return m_id; }
// void set_id(int id) { m_id = id; } // void set_id(int id) { m_id = id; }
virtual void render(Render r, Vector2<int>pos, Vector2<int>size); virtual void render(Render r);
friend std::ostream &operator<<(std::ostream &os, const Tile &tile); friend std::ostream &operator<<(std::ostream &os, const Tile &tile);
friend std::istream &operator>>(std::istream &is, Tile &tile); friend std::istream &operator>>(std::istream &is, Tile &tile);
private: private:
Node *m_node;
int m_id; int m_id;
Color m_color = Color::from_hsv(rand() % 255, 255, 255);
}; };