Make child entites work. z-sort entity list before rendering. entites have a creation timestamp.
59 lines
1.6 KiB
C++
59 lines
1.6 KiB
C++
#pragma once
|
|
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
|
#include <vector>
|
|
#include <chrono>
|
|
|
|
namespace Engine {
|
|
class Entity;
|
|
}
|
|
|
|
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 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);
|
|
void MoveBackward(float speed);
|
|
void MoveLeft(float speed);
|
|
void MoveRight(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:
|
|
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) {}
|
|
}; |