Compare commits

...

7 Commits

Author SHA1 Message Date
Maple Redleaf
2f753f0c47 Add SysReg::NewCallback for future use 2025-11-12 11:31:22 -06:00
Maple Redleaf
e1fce167e2 Implement a simple function register manager (SysReg) 2025-11-12 11:22:01 -06:00
Maple Redleaf
70df396822 Add World::Update() 2025-11-11 12:37:42 -06:00
Maple Redleaf
f22b25642b Add worlds 2025-11-11 12:32:14 -06:00
Maple Redleaf
27eb6ff7bb Cleanup component.h 2025-11-11 11:20:15 -06:00
Maple Redleaf
a730e82247 Namespace project & add RemoveComponent 2025-11-11 11:19:31 -06:00
maple
af7adaa6f2 Fix Entity functions sofar 2025-11-10 21:16:06 +00:00
8 changed files with 134 additions and 13 deletions

View File

@@ -1,14 +1,13 @@
#pragma once #pragma once
namespace VoidFrame {
class Entity; class Entity;
class Component { class Component {
public: public:
virtual void Init();
virtual void Draw();
virtual void Update(); virtual void Update();
virtual void FixedUpdate();
virtual void Destroy();
private: private:
Entity *owner; Entity *owner;
}; };
} // namespace VoidFrame

View File

@@ -1,21 +1,36 @@
#ifndef VOID_ENTITY_H #ifndef VOID_ENTITY_H
#define VOID_ENTITY_H #define VOID_ENTITY_H
#include "component.h" #include "component.h"
#include "world.h"
#include <algorithm>
#include <cstdint>
#include <memory> #include <memory>
#include <utility>
#include <vector> #include <vector>
namespace VoidFrame {
class World;
class Entity { class Entity {
public: public:
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
Component *AddComponent(Args &&...args); Entity *AddComponent(Args &&...args);
template <typename CompT> CompT *GetComponent(); template <typename CompT> CompT *GetComponent();
template <typename CompT> bool HasComponent(); template <typename CompT> bool HasComponent();
template <typename CompT> Entity *RemoveComponent();
void Destroy();
void Update(); void Update();
private: private:
Entity();
std::vector<std::unique_ptr<Component>> components; std::vector<std::unique_ptr<Component>> components;
World *world;
friend class World;
}; };
#include "../src/entity_i.cpp" #include "../src/entity_i.cpp"
} // namespace VoidFrame
#endif // VOID_ENTITY_H #endif // VOID_ENTITY_H

19
include/sysreg.h Normal file
View File

@@ -0,0 +1,19 @@
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace VoidFrame {
class SysReg {
public:
const std::function<void(void)> *
RegisterCallback(const std::string &&name,
std::function<void(void)> &callback);
const std::function<void(void)> *GetCallback(std::string &&name);
void DeleteCallback(std::string &&name);
const std::function<void(void)> *NewCallback(const std::string &&name);
private:
std::unordered_map<std::string, std::unique_ptr<std::function<void(void)>>>
callbacks;
};
} // namespace VoidFrame

20
include/world.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include "entity.h"
#include <cstdint>
#include <memory>
#include <vector>
namespace VoidFrame {
class World {
public:
Entity *Spawn();
void Destroy(Entity *entity);
void Update();
private:
std::vector<std::unique_ptr<Entity>> entities;
};
} // namespace VoidFrame

View File

@@ -1,7 +1,11 @@
#include "../include/entity.h" #include "../include/entity.h"
using namespace VoidFrame;
void Entity::Update() { void Entity::Update() {
for (auto &comp : components) { for (auto &comp : components) {
comp.get()->Update(); comp.get()->Update();
} }
} }
void Entity::Destroy() { world->Destroy(this); }

View File

@@ -2,22 +2,34 @@
#ifndef VOID_ENTITY_H #ifndef VOID_ENTITY_H
#include "../include/entity.h" #include "../include/entity.h"
#endif #endif
#include <memory>
#include <utility> using namespace VoidFrame;
#include <vector>
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
Component *Entity::AddComponent(Args &&...args) { Entity *Entity::AddComponent(Args &&...args) {
components.push_back(std::make_unique<CompT>(std::forward<Args>(args)...)); CompT *component = new CompT(std::forward<Args>(args)...);
return components.back().get(); component->owner = this;
components.emplace_back(component);
return this;
} }
template <typename CompT> CompT *Entity::GetComponent() { template <typename CompT> CompT *Entity::GetComponent() {
for (const auto &comp_ptr : components) { for (auto &comp_ptr : components) {
return dynamic_cast<CompT *>(comp_ptr.get()); if (CompT *casted = dynamic_cast<CompT *>(comp_ptr.get()))
return casted;
} }
} }
template <typename CompT> bool Entity::HasComponent() { template <typename CompT> bool Entity::HasComponent() {
return GetComponent<CompT>() != nullptr; return GetComponent<CompT>() != nullptr;
} }
template <typename CompT> Entity *Entity::RemoveComponent() {
components.erase(std::remove_if(components.begin(), components.end(),
[&](const std::unique_ptr<Component> &comp) {
return dynamic_cast<CompT *>(comp.get()) !=
nullptr;
}),
components.end());
return this;
}

26
src/sysreg.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "../include/sysreg.h"
using namespace VoidFrame;
const std::function<void(void)> *
SysReg::RegisterCallback(const std::string &&name,
std::function<void(void)> &callback) {
callbacks[name] =
std::make_unique<std::function<void(void)>>(std::move(callback));
return callbacks[name].get();
}
const std::function<void(void)> *SysReg::GetCallback(std::string &&name) {
return callbacks[name].get();
}
void SysReg::DeleteCallback(std::string &&name) { callbacks.erase(name); }
const std::function<void(void)> *SysReg::NewCallback(const std::string &&name) {
if (callbacks.count(name) == 0) {
callbacks[name] = std::make_unique<std::function<void(void)>>([]() {});
}
return callbacks[name].get();
}

26
src/world.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "../include/world.h"
#include <algorithm>
#include <memory>
#include <vector>
using namespace VoidFrame;
Entity *World::Spawn() {
Entity *entity = new Entity;
entity->world = this;
entities.emplace_back(entity);
return entity;
}
void World::Destroy(Entity *entity) {
entities.erase(std::remove_if(entities.begin(), entities.end(),
[&](const std::unique_ptr<Entity> &entityPtr) {
return entityPtr.get() == entity;
}));
}
void World::Update() {
for (auto &entity : entities) {
entity->Update();
}
}