Anvil Engine
Loading...
Searching...
No Matches
AEntity.h
Go to the documentation of this file.
1#pragma once
2#include "AMath.h"
3#include "IComponent.h"
4#include <string>
5#include <vector>
6
16{
17 public:
18
19 // Basic transform properties of the entity
20 std::string name; // Name of the entity
21 glm::vec3 position = glm::vec3(0.0f); // Position in 3D space
22 glm::vec3 rotation = glm::vec3(0.0f); // Rotation in Euler angles
23 glm::vec3 scale = glm::vec3(1.0f); // Scale factor in each axis
24
30 {
31
32 comp->OnInit(this); // Initialize the component with this entity
33 m_components.push_back(comp); // Add component to the component list
34 }
35
41 template <typename T> T* GetComponent()
42 {
43 for (auto* c : m_components)
44 {
45 T* target = dynamic_cast<T*>(c); // Try to cast component to desired type
46 if (target)
47 return target; // Return component if cast was successful
48 }
49 return nullptr; // Return nullptr if component of desired type not found
50 }
51
56 const std::vector<IComponent*>& GetComponents() const
57 {
58 return m_components;
59 }
60
65 void Update(float dt)
66 {
67 for (auto* c : m_components)
68 c->OnUpdate(dt); // Update each component with delta time
69 }
70
75 {
76 for (auto* c : m_components)
77 delete c; // Delete each component to prevent memory leaks
78 }
79
80 private:
81 std::vector<IComponent*> m_components; // Container for all attached components
82};
float dt
Definition Main.cpp:10
#define ANVIL_API
Definition ACore.h:6
Represents an entity in the game engine with position, rotation, scale and components.
Definition AEntity.h:16
void Update(float dt)
Updates all components attached to this entity.
Definition AEntity.h:65
glm::vec3 scale
Definition AEntity.h:23
~AEntity()
Destructor that cleans up all attached components.
Definition AEntity.h:74
glm::vec3 rotation
Definition AEntity.h:22
std::string name
Definition AEntity.h:20
T * GetComponent()
Gets a specific component by type.
Definition AEntity.h:41
const std::vector< IComponent * > & GetComponents() const
Gets all components attached to this entity.
Definition AEntity.h:56
glm::vec3 position
Definition AEntity.h:21
void AddComponent(IComponent *comp)
Adds a component to the entity and initializes it.
Definition AEntity.h:29
The IComponent class is an abstract base class that defines the interface for all components in the A...
Definition IComponent.h:12
virtual void OnInit(AEntity *owner)=0
Called when the component is initialized.