mirror of
https://github.com/NickTheFox99/RaylibGame.git
synced 2026-06-28 09:55:11 -04:00
Begin engine development
This commit is contained in:
12
src/engine/component.h
Normal file
12
src/engine/component.h
Normal 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
13
src/engine/entity.cpp
Normal 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
21
src/engine/entity.h
Normal 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
36
src/engine/entity.ipp
Normal 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
0
src/engine/world.cpp
Normal file
11
src/engine/world.h
Normal file
11
src/engine/world.h
Normal 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;
|
||||
};
|
||||
@@ -1,9 +1,4 @@
|
||||
#include "../data/cube.png.h"
|
||||
#include "Functions.hpp"
|
||||
#include "RenderTexture.hpp"
|
||||
#include "Texture.hpp"
|
||||
#include "Vector3.hpp"
|
||||
#include "raylib.h"
|
||||
#include <cmath>
|
||||
#include <raylib-cpp.hpp>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user