NWengine 0.9
Loading...
Searching...
No Matches
GameObject.h
Go to the documentation of this file.
1
6#pragma once
7#include "Globals.h"
8#include "Serialization.h"
9
10#include <map>
11
17#define ADD_COMPONENT(str, type) if (type == #str ) return this->AddComponent<str>();
18
24typedef int (*DrawCallback)(void* data);
25
30public:
35 static std::string GetType() { return "GameComponent"; }
36
40 virtual void Update() {};
41
45 virtual void Start() {};
46
51 virtual void SetGameObject(void* go) {};
52
57 virtual void* GetGameObject() { return nullptr; };
58
62 virtual ~GameComponent() {};
63};
64
68class GameObject : public Serialized {
69private:
70 static int numberOfGameObjects;
71
72public:
73 std::map<std::string, GameComponent*> components;
74 std::string name = "new GameObject";
75 uint32 id = 0;
81 GameObject();
82
87 GameObject(const GameObject& other);
88
93
98 int Draw();
99
104 void SetDrawCallback(DrawCallback callback);
105
110 void DeleteComponent(std::string typeName);
111
115 void DeleteComponents();
116
123 int Serialize(std::fstream* data, int offset);
124
131 int Deserialize(std::fstream* data, int offset);
132
138 template<typename T>
140 T* component = nullptr;
141 if (this->components.find(T::GetType()) == this->components.end()) return component;
142
143 component = (T*)this->components[T::GetType()];
144 return component;
145 };
146
152 template<typename T>
154 std::map<std::string, GameComponent*>::iterator temp = components.find(T::GetType());
155 if (temp != components.end()) return (T*)(*&temp)->second;
156 T* ptr = new T(this);
157 components.insert(std::pair<std::string, GameComponent*>( T::GetType(), ptr ));
158 return ptr;
159 };
160
161 GameComponent* AddComponent(std::string type);
167 GameComponent* GetComponent(std::string type);
168
173 template<typename T>
175 if (components.find(T::GetType()) == components.end()) return;
176 delete components[T::GetType()];
177 components.erase(T::GetType());
178 }
179};
int(* DrawCallback)(void *data)
Function pointer type for draw callbacks.
Definition GameObject.h:24
Base class for game components.
Definition GameObject.h:29
virtual void * GetGameObject()
Get the GameObject associated with the game component.
Definition GameObject.h:57
static std::string GetType()
Get the type of the game component.
Definition GameObject.h:35
virtual void Start()
Start the game component.
Definition GameObject.h:45
virtual void Update()
Update the game component.
Definition GameObject.h:40
virtual ~GameComponent()
Destructor for the game component.
Definition GameObject.h:62
virtual void SetGameObject(void *go)
Set the GameObject associated with the game component.
Definition GameObject.h:51
Class representing a game object.
Definition GameObject.h:68
GameObject()
Default constructor for GameObject.
Definition GameObject.cpp:27
int Serialize(std::fstream *data, int offset)
Serialize the game object.
Definition GameObject.cpp:74
std::map< std::string, GameComponent * > components
Definition GameObject.h:73
DrawCallback _drawProc
Definition GameObject.h:76
T * GetComponent()
Get a component of the specified type.
Definition GameObject.h:139
void DeleteComponent()
Delete a component of the specified type.
Definition GameObject.h:174
T * AddComponent()
Add a component of the specified type to the game object.
Definition GameObject.h:153
int Deserialize(std::fstream *data, int offset)
Deserialize the game object.
Definition GameObject.cpp:85
int Draw()
Draw the game object.
Definition GameObject.cpp:16
void SetDrawCallback(DrawCallback callback)
Set the draw callback for the game object.
Definition GameObject.cpp:22
~GameObject()
Destructor for GameObject.
Definition GameObject.cpp:40
std::string name
Definition GameObject.h:74
void DeleteComponents()
Delete all components from the game object.
Definition GameObject.cpp:48
Base class for serialization and deserialization.
Definition Serialization.h:23