souleng
Game engine providing full Python scripting support
Loading...
Searching...
No Matches
GameObject.hpp
1#pragma once
2
3#include <SDL3/SDL.h>
4#include <map>
5#include <memory>
6#include <vector>
7
8#include "components/ComponentType.hpp"
9
10struct Component;
11
13struct GameObject : public std::enable_shared_from_this<GameObject> {
14 GameObject();
15
16 virtual ~GameObject();
17
18 std::weak_ptr<GameObject> GetThisPtr() { return weak_from_this(); }
19
21 virtual void Input();
22
24 virtual void Update(float dt);
25
27 virtual void Render();
28
30 // @param component The component being added
31 template <typename T> void AddComponent(std::shared_ptr<T> component);
32
34 template <typename T> std::shared_ptr<T> GetComponent();
35
37 void AddChild(std::shared_ptr<GameObject> child);
38
41 std::shared_ptr<GameObject> GetChildAtIndex(size_t idx);
42
45 void SetRenderable(bool renderable);
46
48 bool IsRenderable() const;
49
52 bool Intersects(std::shared_ptr<GameObject> e);
53
54protected:
55 // may want to use unordered_map or multi_map
56 // at the moment, assume one component per entity per type
57 std::map<ComponentType, std::shared_ptr<Component>> mComponents;
58 bool mRenderable{true};
59 std::vector<std::shared_ptr<GameObject>> mChildren;
60};
Definition Component.hpp:17
Main unit of gameplay which contains and updated components.
Definition GameObject.hpp:13
std::shared_ptr< T > GetComponent()
Retrieves an existing component of the specified type, or nullptr if there is none.
Definition GameObject.cpp:61
std::shared_ptr< GameObject > GetChildAtIndex(size_t idx)
Definition GameObject.cpp:82
void AddComponent(std::shared_ptr< T > component)
Adds a component of the specified type.
Definition GameObject.cpp:54
virtual void Input()
Refreshes input for all components and children each frame.
Definition GameObject.cpp:21
virtual void Render()
Renders all components and children.
Definition GameObject.cpp:41
bool Intersects(std::shared_ptr< GameObject > e)
Definition GameObject.cpp:90
virtual void Update(float dt)
Runs Update on all components and children.
Definition GameObject.cpp:31
void SetRenderable(bool renderable)
Definition GameObject.cpp:86
bool IsRenderable() const
Queries if this GameObject can be rendered.
Definition GameObject.cpp:88
void AddChild(std::shared_ptr< GameObject > child)
Adds a child GameObject to this GameObject.
Definition GameObject.cpp:78