souleng
Game engine providing full Python scripting support
Loading...
Searching...
No Matches
Component.hpp
1#pragma once
2
3#include <memory>
4
5#include "ComponentType.hpp"
6
7// forward declaration to set up circular reference.
8// should NOT include the file otherwise we end up with include "infinite loop"
9struct GameObject;
10
11// Components must know about the GameObject associated with
12// Components must have a type
13
17struct Component {
18 Component() {}
19
20 virtual ~Component() {}
21
23 virtual void Input() {}
24
28 virtual void Update(float deltaTime) {}
29
32 virtual void Render() {}
33
34 // Must be implemented - how to do 'abstract' in C++
36 static ComponentType GetType() { return ComponentType::DEFAULT; }
37
39 inline void SetGameObject(std::weak_ptr<GameObject> ent) {
40 mGameObject = ent;
41 }
42
44 inline std::shared_ptr<GameObject> GetGameObject() {
45 return mGameObject.lock();
46 }
47
49 template <typename T> std::shared_ptr<T> GetComponent();
50
51 // to think about:
52 // sending/receiving messages? (does godot do something like this?)
53
54protected:
55 // Each component must be associated with a single game entity.
56 // non-owning, so should be a weak ptr. could possibly be a raw pointer, unless
57 // components end up being reused across entity boundaries.
58 std::weak_ptr<GameObject> mGameObject;
59};
60
61#include "GameObject.hpp"
62template <typename T> std::shared_ptr<T> Component::GetComponent() {
63 return mGameObject.lock()->GetComponent<T>();
64}
Definition Component.hpp:17
virtual void Update(float deltaTime)
Definition Component.hpp:28
void SetGameObject(std::weak_ptr< GameObject > ent)
Sets the GameObject this component is attached to.
Definition Component.hpp:39
virtual void Render()
Definition Component.hpp:32
virtual void Input()
Called once per frame to handle any new input.
Definition Component.hpp:23
static ComponentType GetType()
Returns the type of this component to make querying GetComponent easier.
Definition Component.hpp:36
std::shared_ptr< T > GetComponent()
Alias for GetGameObject()->GetComponent<T>(), for easier scripting.
Definition Component.hpp:62
std::shared_ptr< GameObject > GetGameObject()
Retrieves the GameObject this component is attached to, behind a shared pointer.
Definition Component.hpp:44
Main unit of gameplay which contains and updated components.
Definition GameObject.hpp:13