A few things

Make child entites work.

z-sort entity list before rendering.

entites have a creation timestamp.
This commit is contained in:
2025-01-02 21:44:14 -05:00
parent 3349474e3a
commit 25d080ff2c
11 changed files with 90 additions and 25 deletions

View File

@@ -9,5 +9,5 @@ class Engine::Camera : public Renderable {
public:
void Render() override {};
public:
explicit Camera(const Vector2& position) : Renderable(position) {}
explicit Camera(const Vector2& position) : Renderable(position, 0) {}
};

View File

@@ -1,6 +1,7 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <vector>
#include <chrono>
namespace Engine {
class Entity;
@@ -8,15 +9,21 @@ namespace Engine {
class Engine::Entity {
protected:
// nullptr means no parent.
Entity* parent = nullptr;
// Epoch micro-seconds.
long creation_time = 0.0f;
std::vector<Entity*> children{};
Vector2 position = {0, 0};
// Assuming 0 radians is up.
float face_angle = 0.0f;
void UpdateChildren();
public:
// Movements independent of the rotation.
// Movements independent of the face_angle.
void MoveX(float speed);
void MoveY(float speed);
void Move(float angle_rad, float speed);
// Movements dependent on face angle.
void MoveForward(float speed);
@@ -24,20 +31,29 @@ public:
void MoveLeft(float speed);
void MoveRight(float speed);
void Move(float angle_rad, float speed);
void Rotate(float speed);
void SetRotation(float new_rotation);
// Parent child structure.
[[nodiscard]] bool AppendChild(Entity* entity);
void DestroyChild(Entity* entity);
void RemoveChild(Entity* entity);
public:
[[nodiscard]] std::vector<Entity*> GetChildren() const;
[[nodiscard]] Entity* GetParent() const;
// Microseconds.
[[nodiscard]] long GetCreationTime() const;
[[nodiscard]] long GetAge() const;
[[nodiscard]] float GetRotation() const;
[[nodiscard]] Vector2 GetPosition() const;
public:
virtual void Update() {}
public:
explicit Entity(const Vector2& position, float rotation = 0.0f) : position(position), face_angle(rotation) {}
virtual ~Entity();
explicit Entity(const Vector2& position, float rotation = 0.0f) :
creation_time(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()),
position(position), face_angle(rotation) {}
};

View File

@@ -7,5 +7,5 @@ namespace Engine {
class Engine::Hud : public Engine::Renderable {
public:
Hud() : Renderable({0, 0}, 0) {};
Hud() : Renderable({0, 0}, 0, 0) {};
};

View File

@@ -8,9 +8,15 @@ namespace Engine {
}
class Engine::Renderable : public Entity {
protected:
// Higher numbers are farther to the back.
unsigned int depth = 0;
public:
[[nodiscard]] unsigned int GetDepth() const { return depth; }
void SetDepth(unsigned int new_depth) { depth = new_depth; }
public:
// *must* be overridden.
virtual void Render() = 0;
public:
explicit Renderable(const Vector2& position, float rotation = 0.0f) : Entity(position, rotation) {}
explicit Renderable(const Vector2& position, unsigned int depth = 0, float rotation = 0.0f) : Entity(position, rotation), depth(depth) {}
};

View File

@@ -25,6 +25,6 @@ public:
void Render() override;
public:
~Sprite() override;
explicit Sprite(const Vector2& pos, const float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* texture = nullptr,
Texture* alpha_mask = nullptr) : Renderable(pos, face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {}
explicit Sprite(const Vector2& pos, unsigned int depth = 0, const float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* texture = nullptr,
Texture* alpha_mask = nullptr) : Renderable(pos, depth,face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {}
};

View File

@@ -18,6 +18,8 @@ protected:
Camera* active_camera = nullptr;
std::vector<Fixed*> fixed_list{};
std::vector<Entity*> entity_list{};
protected:
[[nodiscard]] std::vector<Entity*> GetFlatEntityList(const std::vector<Entity*>& entity_list) const;
public:
[[nodiscard]] bool EntityListContains(const Entity* entity) const;
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const;