Refactor & More.

Changed the entity system to utilize mix-ins.

Added instanced sprites.

Texture support.
This commit is contained in:
2025-01-03 19:28:58 -05:00
parent c9daae8d25
commit 6664925621
39 changed files with 503 additions and 310 deletions

View File

@@ -1,5 +1,5 @@
#include "Engine/Entity/Animation.h"
#include "jlog/Logger.hpp"
#include <Engine/types/entity/Animation.h>
#include <jlog/Logger.hpp>
float Animation::GetMsBetweenFrames() const {
return ms_between_frames;

View File

@@ -1,109 +0,0 @@
#include <J3ML/J3ML.hpp>
#include <Engine/Entity/Entity.h>
#include <Engine/Globals.h>
using namespace J3ML;
void Engine::Entity::MoveX(float speed) {
position.x = position.x + (speed * Globals::Window->GetDeltaTime());
}
void Engine::Entity::MoveY(float speed) {
position.y = position.y + (speed * Globals::DeltaTime());
}
void Engine::Entity::Move(float angle_rad, float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(angle_rad);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(angle_rad);
}
void Engine::Entity::MoveForward(float speed) {
Move(face_angle, speed);
}
void Engine::Entity::MoveBackward(float speed) {
speed = -speed;
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Engine::Entity::MoveLeft(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * -Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
}
void Engine::Entity::MoveRight(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * -Math::Cos(face_angle);
}
void Engine::Entity::SetRotation(float new_face_angle) {
face_angle = new_face_angle;
if (face_angle < 0)
face_angle = fmod(face_angle + Math::Pi * 2, Math::Pi * 2);
else if (face_angle >= 2 * M_PI)
face_angle = fmod(face_angle, Math::Pi * 2);
}
void Engine::Entity::Rotate(float speed) {
SetRotation(face_angle + speed * Globals::DeltaTime());
}
float Engine::Entity::GetRotation() const {
return face_angle;
}
Vector2 Engine::Entity::GetPosition() const {
return position;
}
bool Engine::Entity::AppendChild(Entity* entity) {
bool success = false;
if (!Globals::CurrentScene->EntityListContains(entity)) {
entity->parent = this;
children.push_back(entity);
success = true;
}
return success;
}
Engine::Entity::~Entity() {
for (auto* e : children)
delete e;
}
void Engine::Entity::UpdateChildren() {
for (auto& e : children) {
e->Update();
if (!e->children.empty())
e->UpdateChildren();
}
}
void Engine::Entity::DestroyChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
delete *it, children.erase(it);
}
void Engine::Entity::RemoveChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
children.erase(it);
}
long Engine::Entity::GetCreationTime() const {
return creation_time;
}
long Engine::Entity::GetAge() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - creation_time;
}
Engine::Entity* Engine::Entity::GetParent() const {
return parent;
}
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
return children;
}

View File

@@ -1,21 +0,0 @@
#include <Engine/Entity/Sprite.h>
Engine::Sprite::~Sprite() {
delete texture;
delete alpha_mask;
}
void Engine::Sprite::Render() {
if (texture && !alpha_mask)
J2D::DrawSprite(texture, position, face_angle, origin, scale, base_color);
if (texture && alpha_mask)
J2D::DrawSprite(texture, alpha_mask, position, face_angle, origin, scale, base_color);
}
AABB2D Engine::Sprite::GetBounds() {
return { position, texture->GetDimensions() + position };
}
Texture* Engine::Sprite::GetTexture() {
return texture;
}

View File

@@ -1,6 +1,5 @@
#include <Engine/GameWindow.h>
#include <Engine/Globals.h>
#include <Engine/Entity/Camera.h>
#include <JGL/JGL.h>

View File

@@ -0,0 +1,28 @@
#include <Engine/types/InstancedTexture.h>
#include <Engine/types/entity/InstancedSprite.h>
using namespace Engine;
void InstancedTexture::AddUser(const InstancedSprite* user) {
// Prevent double references.
if (!InUseBy(user))
users.push_back(user);
}
void InstancedTexture::RemoveUser(const InstancedSprite* user) {
auto it = std::find(users.begin(), users.end(), user);
if (it != users.end())
users.erase(it);
}
bool InstancedTexture::InUseBy(const InstancedSprite* rhs) {
for (const auto* u : users)
if (u == rhs)
return true;
return false;
}
InstancedTexture::~InstancedTexture() {
delete texture;
for (const auto* is: users)
delete is;
}

View File

@@ -0,0 +1,53 @@
#include <J3ML/J3ML.hpp>
#include <Engine/types/entity/Entity.h>
#include <Engine/Globals.h>
bool Engine::Entity::AppendChild(Entity* entity) {
if (!Globals::CurrentScene->EntityListContains(entity)) {
entity->parent = this;
children.push_back(entity);
return true;
}
return false;
}
Engine::Entity::~Entity() {
for (auto* e : children)
delete e;
}
void Engine::Entity::UpdateChildren() {
for (auto& e : children) {
e->Update();
if (!e->children.empty())
e->UpdateChildren();
}
}
void Engine::Entity::DestroyChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
delete *it, children.erase(it);
}
void Engine::Entity::RemoveChild(Entity* entity) {
auto it = std::find(children.begin(), children.end(), entity);
if (it != children.end())
children.erase(it);
}
long Engine::Entity::GetCreationTime() const {
return creation_time;
}
long Engine::Entity::GetAge() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - creation_time;
}
Engine::Entity* Engine::Entity::GetParent() const {
return parent;
}
std::vector<Engine::Entity*> Engine::Entity::GetChildren() const {
return children;
}

View File

@@ -0,0 +1,13 @@
#include <Engine/types/entity/InstancedSprite.h>
#include <Engine/Globals.h>
using namespace Engine;
Texture* InstancedSprite::GetTexture() {
return Globals::CurrentScene->GetInstancedTexture(this);
}
Texture* InstancedSprite::GetAlphaMask() {
if (Sprite::GetAlphaMask())
return Sprite::GetAlphaMask();
return Globals::CurrentScene->GetInstancedAlphaMask(this);
}

View File

@@ -0,0 +1,57 @@
#include "Engine/types/entity/mixins/Movable.h"
#include "Engine/Globals.h"
using namespace J3ML;
void Engine::Movable::MoveX(float speed) {
position.x = position.x + (speed * Globals::Window->GetDeltaTime());
}
void Engine::Movable::MoveY(float speed) {
position.y = position.y + (speed * Globals::DeltaTime());
}
void Engine::Movable::Move(float angle_rad, float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(angle_rad);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(angle_rad);
}
void Engine::Movable::MoveForward(float speed) {
Move(face_angle, speed);
}
void Engine::Movable::MoveBackward(float speed) {
speed = -speed;
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
}
void Engine::Movable::MoveLeft(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * -Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
}
void Engine::Movable::MoveRight(float speed) {
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
position.y = position.y + (speed * Globals::DeltaTime()) * -Math::Cos(face_angle);
}
void Engine::Movable::SetRotation(float new_face_angle) {
face_angle = new_face_angle;
if (face_angle < 0)
face_angle = fmod(face_angle + Math::Pi * 2, Math::Pi * 2);
else if (face_angle >= 2 * M_PI)
face_angle = fmod(face_angle, Math::Pi * 2);
}
void Engine::Movable::Rotate(float speed) {
SetRotation(face_angle + speed * Globals::DeltaTime());
}
float Engine::Movable::GetRotation() const {
return face_angle;
}
Vector2 Engine::Movable::GetPosition() const {
return position;
}

View File

@@ -0,0 +1,29 @@
#include <Engine/types/entity/Sprite.h>
#include <JGL/JGL.h>
Engine::Sprite::~Sprite() {
delete texture;
delete alpha_mask;
}
void Engine::Sprite::Render() {
auto* t = GetTexture();
auto* a = GetAlphaMask();
if (t && !a)
return J2D::DrawSprite(t, position, face_angle, origin, scale, base_color);
if (t && a)
return J2D::DrawSprite(t, a, position, face_angle, origin, scale, base_color);
}
Texture* Engine::Sprite::GetTexture() {
return texture;
}
Texture* Engine::Sprite::GetAlphaMask() {
return alpha_mask;
}
void Engine::Sprite::SetAlphaMask(Texture* new_alpha_mask) {
alpha_mask = new_alpha_mask;
}

View File

@@ -0,0 +1,14 @@
#include "Engine/types/entity/mixins/BoxCollider.h"
using namespace Engine;
bool BoxCollider::BoxCollides(BoxCollider* rhs) {
if (GetBounds().Intersects(rhs->GetBounds()))
return true;
if (GetBounds().Contains(rhs->GetBounds()))
return true;
return false;
}
bool BoxCollider::BoxCollides(const Vector2& rhs) {
return GetBounds().Contains(rhs);
}

View File

@@ -1,4 +1,4 @@
#include <Engine/Level/Fixed.h>
#include "Engine/types/scene/Fixed.h"
bool Engine::Fixed::Collidable() const {
return collidable;

View File

@@ -1,32 +1,35 @@
#include <Engine/Level/Scene.h>
#include <Engine/Entity/Camera.h>
#include <Engine/types/scene/Scene.h>
#include <Engine/types/entity/Camera.h>
#include <Engine/types/entity/InstancedSprite.h>
#include <JGL/JGL.h>
#include <algorithm>
bool Engine::Scene::EntityListContains(const Entity* entity) const {
using namespace Engine;
bool Scene::EntityListContains(const Entity* entity) const {
auto flat = GetFlatEntityList(entity_list);
for (auto* e : flat)
if (e == entity)
return true;
return false;
}
bool Engine::Scene::FixedListContains(const Fixed* fixed) const {
bool Scene::FixedListContains(const Fixed* fixed) const {
for (auto* f : fixed_list)
if (f == fixed)
return true;
return false;
}
size_t Engine::Scene::FixedCount() const {
size_t Scene::FixedCount() const {
return fixed_list.size();
}
size_t Engine::Scene::EntityCount() const {
size_t Scene::EntityCount() const {
return GetFlatEntityList(entity_list).size();
}
void Engine::Scene::Update() {
void Scene::Update() {
if (active_camera)
active_camera->Update();
@@ -35,7 +38,7 @@ void Engine::Scene::Update() {
e->Update();
}
void Engine::Scene::Render(RenderTarget* render_target) {
void Scene::Render(RenderTarget* render_target) {
// The Camera and the HUD are ignored here because they're special cases.
std::vector<Renderable*> display_list{};
for (auto* e : GetFlatEntityList(entity_list))
@@ -45,7 +48,7 @@ void Engine::Scene::Render(RenderTarget* render_target) {
display_list.push_back(r);
std::sort(display_list.begin(), display_list.end(), [](Renderable* a, Renderable* b) {
return a->GetDepth() > b->GetDepth();
return a->GetLayer() > b->GetLayer();
});
J2D::Begin( render_target, true);
@@ -66,7 +69,7 @@ void Engine::Scene::Render(RenderTarget* render_target) {
J2D::End();
}
Engine::Scene::~Scene() {
Scene::~Scene() {
for (auto* f : fixed_list)
delete f;
@@ -77,7 +80,7 @@ Engine::Scene::~Scene() {
delete active_camera;
}
void Engine::Scene::Unload() {
void Scene::Unload() {
for (auto* f : fixed_list)
delete f;
@@ -87,50 +90,50 @@ void Engine::Scene::Unload() {
delete heads_up_display;
}
void Engine::Scene::AppendEntity(Entity* entity) {
void Scene::AppendEntity(Entity* entity) {
if (!EntityListContains(entity))
entity_list.push_back(entity);
}
void Engine::Scene::AppendFixed(Fixed* fixed) {
void Scene::AppendFixed(Fixed* fixed) {
if (!FixedListContains(fixed))
fixed_list.push_back(fixed);
}
void Engine::Scene::DestroyEntity(Entity *entity) {
void Scene::DestroyEntity(Entity *entity) {
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != entity_list.end())
delete *it, entity_list.erase(it);
}
void Engine::Scene::DestroyFixed(Fixed* fixed) {
void Scene::DestroyFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end())
delete *it, fixed_list.erase(it);
}
void Engine::Scene::RemoveEntity(Entity* entity) {
void Scene::RemoveEntity(Entity* entity) {
auto it = std::find(entity_list.begin(), entity_list.end(), entity);
if (it != entity_list.end())
entity_list.erase(it);
}
void Engine::Scene::RemoveFixed(Fixed* fixed) {
void Scene::RemoveFixed(Fixed* fixed) {
auto it = std::find(fixed_list.begin(), fixed_list.end(), fixed);
if (it != fixed_list.end())
fixed_list.erase(it);
}
std::string Engine::Scene::GetName() const {
std::string Scene::GetName() const {
return name;
}
Engine::Camera* Engine::Scene::GetActiveCamera() const {
Camera* Scene::GetActiveCamera() const {
return active_camera;
}
std::vector<Engine::Entity*> Engine::Scene::GetFlatEntityList(const std::vector<Entity*>& ent_list) const {
std::vector<Entity*> Scene::GetFlatEntityList(const std::vector<Entity*>& ent_list) const {
std::vector<Entity*> result{};
for (auto* e : ent_list) {
@@ -142,3 +145,28 @@ std::vector<Engine::Entity*> Engine::Scene::GetFlatEntityList(const std::vector<
return result;
}
Texture* Scene::GetInstancedTexture(const InstancedSprite* user) {
for (auto* itx : instanced_textures)
if (itx->InUseBy(user))
return itx->GetTexture();
auto* t = new Texture(user->GetTextureFilesystemPath());
auto* itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx);
return t;
}
Texture* Scene::GetInstancedAlphaMask(const InstancedSprite* user) {
for (auto* itx : instanced_alpha_masks)
if (itx->InUseBy(user))
return itx->GetTexture();
if (!user->GetAlphaMaskFilesystemPath().empty()) {
auto *t = new Texture(user->GetTextureFilesystemPath());
auto *itx = new InstancedTexture(t, user);
instanced_textures.push_back(itx);
return t;
}
return nullptr;
}

View File

@@ -1,4 +1,6 @@
#include <Game/Entity/Box.h>
#include <Game/entity/Box.h>
#include <Engine/Globals.h>
#include <JGL/JGL.h>
void Game::Box::Render() {
J2D::FillRect(Colors::Red, Vector2(position), {20, 20});
@@ -13,4 +15,4 @@ void Game::Box::Update() {
MoveX(-500);
if (Globals::Window->IsKeyDown(Keys::D))
MoveX(500);
}
}

View File

@@ -1,5 +1,6 @@
#include <Game/Entity/DemoGameHud.h>
#include <Game/entity/DemoGameHud.h>
#include <Engine/Globals.h>
#include <JGL/JGL.h>
void Game::DemoGameHud::Render() {
J2D::DrawString(Colors::Whites::Ivory, "Framerate: " + std::to_string((int) Globals::Window->GetRefreshRate()), 0, 0, 1, 16);

View File

@@ -1,6 +1,6 @@
#include <Game/Scene/ControllableBox.h>
#include <Game/Entity/DemoGameHud.h>
#include <Game/Entity/Box.h>
#include <Game/scene/ControllableBox.h>
#include <Game/entity/DemoGameHud.h>
#include <Game/entity/Box.h>
void ControllableBox::Init() {
auto* hud = new Game::DemoGameHud();

View File

@@ -1,5 +1,6 @@
#include <Game/Scene/Loading.h>
#include <Game/scene/Loading.h>
#include <Engine/Globals.h>
#include <JGL/JGL.h>
void LoadingScreen::Init() {
RedactedSoftware = new JGL::Texture("assets/sprites/Re3D.png");