16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 12:29:18 +02:00

ECS System WIP

This commit is contained in:
2026-04-07 23:40:03 +02:00
parent f6db9978d2
commit 8e9fd9225f
30 changed files with 1133 additions and 242 deletions

View File

@@ -0,0 +1,5 @@
//
// Created by Daniu
//
#include "BaseSystem.h"

View File

@@ -0,0 +1,25 @@
//
// Created by Daniu
//
#ifndef EU07_BASICSYSTEM_H
#define EU07_BASICSYSTEM_H
#pragma once
class ECWorld;
class BaseSystem
{
public:
virtual ~BaseSystem() = default;
virtual void OnCreate(ECWorld& world) {}
virtual void OnDestroy(ECWorld& world) {}
virtual void Update(ECWorld& world, float dt) {}
};
#endif //EU07_BASICSYSTEM_H

View File

@@ -0,0 +1,29 @@
//
// Created by Daniu
//
#include "MovementSystem.h"
#include "entitysystem/ECWorld.h"
#include "entitysystem/components/BasicComponents.h"
#include "utilities/Logs.h"
void MovementSystem::Update(ECWorld& world, float dt)
{
constexpr double damping = 0.90; // 0.0 = instant stop, 1.0 = without damping
world.Each<ECSComponent::Transform, ECSComponent::Velocity>(
[dt](auto entity, auto& transform, auto& velocity)
{
transform.Position += velocity.Value * dt;
velocity.Value *= damping;
if (glm::length(velocity.Value) < 0.001)
{
velocity.Value = glm::dvec3(0.0);
}
}
);
}

View File

@@ -0,0 +1,25 @@
//
// Created by Daniu
//
#ifndef EU07_MOVEMENTSYSTEM_H
#define EU07_MOVEMENTSYSTEM_H
#pragma once
#include "BaseSystem.h"
namespace ECSComponent
{
struct Transform;
struct Velocity;
}
class MovementSystem : public BaseSystem
{
public:
void Update(ECWorld& world, float dt) override;
};
#endif //EU07_MOVEMENTSYSTEM_H

View File

@@ -0,0 +1,33 @@
//
// Created by Daniu
//
#include "SystemManager.h"
#include "entitysystem/systems/BaseSystem.h"
#include "entitysystem/ECWorld.h"
#include "utilities/Logs.h"
SystemManager::~SystemManager() = default;
void SystemManager::Create(ECWorld& world)
{
WriteLog("[SystemManager] Creating systems");
for (auto& system : m_systems){
system->OnCreate(world);
}
}
void SystemManager::Destroy(ECWorld& world)
{
WriteLog("[SystemManager] Destroying systems");
for (auto& system : m_systems)
system->OnDestroy(world);
}
void SystemManager::Update(ECWorld& world, float dt)
{
for (auto& system : m_systems)
system->Update(world, dt);
}

View File

@@ -0,0 +1,46 @@
//
// Created by Daniu
//
#ifndef EU07_SYSTEMMANAGER_H
#define EU07_SYSTEMMANAGER_H
#pragma once
#include "BaseSystem.h"
#include <vector>
#include <memory>
class ECWorld;
class SystemManager
{
public:
SystemManager() = default;
~SystemManager();
template<typename T, typename... Args>
T& AddSystem(Args&&... args)
{
static_assert(std::is_base_of_v<BaseSystem, T>);
auto system = std::make_unique<T>(std::forward<Args>(args)...);
T& ref = *system;
m_systems.emplace_back(std::move(system));
return ref;
}
void Create(ECWorld& world);
void Destroy(ECWorld& world);
void Update(ECWorld& world, float dt);
private:
std::vector<std::unique_ptr<BaseSystem>> m_systems;
};
#endif //EU07_SYSTEMMANAGER_H