Compare commits

..

9 Commits

Author SHA1 Message Date
Maple Redleaf
650925b4e1 Add Entity::HasComponents() wrapper 2025-11-10 12:19:38 -06:00
Maple Redleaf
1166bc8911 Fix Entity::GetComponent() 2025-11-10 12:16:34 -06:00
Maple Redleaf
4afd985a50 Add attempt for Entity::GetComponent 2025-11-10 12:14:13 -06:00
Maple Redleaf
45c7b2412c Add a barebones Entity::Update() 2025-11-10 12:04:31 -06:00
Maple Redleaf
03c3a75232 Make the AddComponent function safe 2025-11-10 11:48:33 -06:00
Maple Redleaf
f99daa2f9d Fix .gitignore 2025-11-10 11:25:25 -06:00
Maple Redleaf
3c309da421 Continue implementation of Entity::AddComponent() 2025-11-10 11:17:47 -06:00
Maple Redleaf
9d9a574b87 Begin implementation of Entities 2025-11-10 10:31:25 -06:00
Maple Redleaf
63477b20f5 Add base Component class 2025-11-10 07:52:35 -06:00
8 changed files with 69 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build*/

View File

@@ -4,10 +4,10 @@ project(VoidEngine VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp" "include/*.h")
add_library(${PROJECT_NAME} add_library(${PROJECT_NAME}
STATIC STATIC ${SOURCE_FILES}
src/*.cpp
include/*.hpp
) )
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}

14
include/component.h Normal file
View File

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

21
include/entity.h Normal file
View File

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

View File

@@ -1 +0,0 @@
// Public code time!

7
src/entity.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include "../include/entity.h"
void Entity::Update() {
for (auto &comp : components) {
comp.get()->Update();
}
}

23
src/entity_i.cpp Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#ifndef VOID_ENTITY_H
#include "../include/entity.h"
#endif
#include <memory>
#include <utility>
#include <vector>
template <typename CompT, typename... Args>
Component *Entity::AddComponent(Args &&...args) {
components.push_back(std::make_unique<CompT>(std::forward<Args>(args)...));
return components.back().get();
}
template <typename CompT> CompT *Entity::GetComponent() {
for (const auto &comp_ptr : components) {
return dynamic_cast<CompT *>(comp_ptr.get());
}
}
template <typename CompT> bool Entity::HasComponent() {
return GetComponent<CompT>() != nullptr;
}

View File

@@ -1 +0,0 @@
// Code time!