mirror of
https://gitlab.com/voidframe/voidframe-cpp.git
synced 2026-06-10 08:25:11 -04:00
Compare commits
7 Commits
650925b4e1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f753f0c47 | ||
|
|
e1fce167e2 | ||
|
|
70df396822 | ||
|
|
f22b25642b | ||
|
|
27eb6ff7bb | ||
|
|
a730e82247 | ||
|
|
af7adaa6f2 |
@@ -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
|
||||||
|
|||||||
@@ -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
19
include/sysreg.h
Normal 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
20
include/world.h
Normal 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
|
||||||
@@ -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); }
|
||||||
|
|||||||
@@ -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
26
src/sysreg.cpp
Normal 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
26
src/world.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user