dEngine
Simple 2D C++ game engine
GameObject.h
Go to the documentation of this file.
1 #ifndef DENGINE_GAMEOBJECT_H
2 #define DENGINE_GAMEOBJECT_H
3 
4 
5 #include <string>
6 #include <vector>
7 #include<bits/stdc++.h>
8 #include <SDL_rect.h>
9 #include "../Universal.h"
10 
11 namespace dengine {
12  class Component;
13 
19  class GameObject {
20  public:
21  GameObject(std::string objectName);
22 
27  std::string GetName();
28 
34  void AddComponent(Component *component);
35 
41  void RemoveComponent(Component *component);
42 
49  bool HasComponent(std::string name);
50 
54  virtual void Start();
55 
60  virtual void NotifyCollision(GameObject& other);
61 
65  virtual void Update();
66 
70  virtual void Render();
71 
75  SDL_Rect box;
76 
83  void SetPos(float xPos, float yPos);
84 
95  std::weak_ptr<Component> GetComponentByName(std::string name);
96 
100  void AddTag(std::string tag){
101  auto it = std::find(tags.begin(), tags.end(), tag);
102  if(it == tags.end()) {
103  tags.push_back(tag);
104  }
105  }
106 
111  bool HasTag(std::string tag){
112  auto it = std::find(tags.begin(), tags.end(), tag);
113  return it == tags.end();
114  }
115 
119  void RemoveTag(std::string tag){
120  auto it = std::find(tags.begin(), tags.end(), tag);
121  if(it == tags.end()) {
122  tags.erase(std::remove(tags.begin(),tags.end(),tag),tags.end());
123  }
124  }
125 
126  private:
130  std::vector<class Component *> components;
134  std::string name;
135  std::vector<std::string> tags;
136  };
137 }
138 
139 #endif //DENGINE_GAMEOBJECT_H
std::vector< std::string > tags
Definition: GameObject.h:135
A base object for a game The gameobject can be considered the most basic building block of a game...
Definition: GameObject.h:19
SDL_Rect box
A rect denoting the object&#39;s position in space.
Definition: GameObject.h:75
std::string GetName()
Returns the object&#39;s name.
Definition: GameObject.cpp:12
GameObject(std::string objectName)
Definition: GameObject.cpp:8
virtual void Render()
Performs render on all attached components.
Definition: GameObject.cpp:54
virtual void NotifyCollision(GameObject &other)
Definition: GameObject.cpp:66
void RemoveTag(std::string tag)
Remove tag from game object.
Definition: GameObject.h:119
bool HasTag(std::string tag)
Check if game object has a tag.
Definition: GameObject.h:111
bool HasComponent(std::string name)
Does it have a component?
Definition: GameObject.cpp:16
void RemoveComponent(Component *component)
Removes a component from the GameObject.
Definition: GameObject.cpp:26
Gameobject component that asssists in adding additional functionality.
Definition: Component.h:14
virtual void Start()
Called on object instantiation.
Definition: GameObject.cpp:30
std::vector< class Component * > components
A vector containing any components that may be on the GameObject.
Definition: GameObject.h:130
std::string name
A string containing the object name.
Definition: GameObject.h:134
void AddTag(std::string tag)
Add a tag to the game object.
Definition: GameObject.h:100
void SetPos(float xPos, float yPos)
Set position.
Definition: GameObject.cpp:61
std::weak_ptr< Component > GetComponentByName(std::string name)
Get a reference to a component.
Definition: GameObject.cpp:44
The namespace containing the engine&#39;s code.
Definition: Collider.h:9
virtual void Update()
Runs every frame performing any required logic.
Definition: GameObject.cpp:37
void AddComponent(Component *component)
Adds a component to the GameObject.
Definition: GameObject.cpp:22