Begin engine development

This commit is contained in:
maple
2025-11-10 01:58:47 +00:00
parent 99275870eb
commit f97e8fe9d8
7 changed files with 93 additions and 5 deletions

12
src/engine/component.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
class Entity;
class Component {
public:
Entity *owner = nullptr;
virtual ~Component() = default;
virtual void Update(float dt) {}
virtual void Draw() {}
};

13
src/engine/entity.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "entity.h"
void Entity::Update(float dt) {
for (auto &comp : components) {
comp->Update(dt);
}
}
void Entity::Draw() {
for (auto &comp : components) {
comp->Draw();
}
}

21
src/engine/entity.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#define ENTITY_H_INCLUDED
#include "component.h"
#include <memory>
#include <vector>
class Entity {
public:
template <typename T, typename... Args> T *AddComponent(Args &&...args);
template <typename T> T *GetComponent();
template <typename T> bool HasComponent();
template <typename T> void RemoveComponent();
void Update(float dt);
void Draw();
private:
std::vector<std::unique_ptr<Component>> components;
};
#include "entity.ipp"

36
src/engine/entity.ipp Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include <algorithm>
#ifndef ENTITY_H_INCLUDED
#include "entity.h"
#endif
template <typename T, typename... Args>
T *Entity::AddComponent(Args &&...args) {
T *component = new T(std::forward<Args>(args)...);
component->owner = this;
components.emplace_back(component);
return component;
}
template <typename T> T *Entity::GetComponent() {
for (auto &comp : components) {
if (T *casted = dynamic_cast<T *>(comp.get())) {
return casted;
}
}
return nullptr;
}
template <typename T> bool Entity::HasComponent() {
return GetComponent<T>() != nullptr;
}
template <typename T> void Entity::RemoveComponent() {
components.erase(std::remove_if(components.begin(), components.end(),
[](const std::unique_ptr<Component> &comp) {
return dynamic_cast<T *>(comp.get()) !=
nullptr;
}),
components.end());
}

0
src/engine/world.cpp Normal file
View File

11
src/engine/world.h Normal file
View File

@@ -0,0 +1,11 @@
#include "entity.h"
#include <vector>
class World {
public:
void Update(float dt);
void Draw();
void AddEntity(Entity entity) {}
private:
std::vector<Entity> entities;
};

View File

@@ -1,9 +1,4 @@
#include "../data/cube.png.h" #include "../data/cube.png.h"
#include "Functions.hpp"
#include "RenderTexture.hpp"
#include "Texture.hpp"
#include "Vector3.hpp"
#include "raylib.h"
#include <cmath> #include <cmath>
#include <raylib-cpp.hpp> #include <raylib-cpp.hpp>