Bubba-3D  0.9.0
Awesome game engine!
GameObject.h
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #pragma once
18 
19 #include "IDrawable.h"
20 #include "AABB2.h"
21 #include "linmath/float4x4.h"
22 #include <vector>
23 #include <linmath/Quaternion.h>
24 #include <Sphere.h>
25 #include "ShaderProgram.h"
26 
27 enum EventType {BeforeCollision, DuringCollision, AfterCollision};
28 
29 typedef int TypeIdentifier;
30 
31 class Mesh;
32 class Triangle;
33 class IRenderComponent;
34 class IComponent;
35 class Octree;
36 
67 class GameObject : public IDrawable {
68 public:
69  GameObject();
70  GameObject(GameObject* parent);
71 
77  explicit GameObject(Mesh *mesh);
84  GameObject(Mesh *mesh, Mesh *colliderMesh);
85 
86  GameObject(Mesh *mesh, GameObject* parent);
87  GameObject(Mesh *mesh, Mesh *colliderMesh, GameObject* parent);
88 
89  virtual ~GameObject();
90 
94  virtual void render();
98  virtual void renderShadow(ShaderProgram* shaderProgram);
99 
100  void addRenderComponent(IRenderComponent* renderer);
101  void addComponent(IComponent* newComponent);
107  void update(float dt);
114  void callEvent(EventType type, GameObject* data);
115 
116 
117  chag::float4x4 getModelMatrix();
123  void move(chag::float4x4 model_matrix);
129  void update(chag::float4x4 update_matrix);
130 
131  chag::float3 getRelativeScale();
132  chag::float3 getAbsoluteScale();
133  void setScale(chag::float3 s);
134 
135  chag::Quaternion getRelativeRotation();
136  chag::Quaternion getAbsoluteRotation();
137  void setRotation(chag::Quaternion r);
138  void updateRotation(chag::Quaternion r);
139 
140  chag::float3 getRelativeLocation();
141  chag::float3 getAbsoluteLocation();
142  void setLocation(chag::float3 l);
143 
144  void addChild(GameObject* child);
145 
146  TypeIdentifier getIdentifier();
147  void setIdentifier(TypeIdentifier identifier);
148  void addCollidesWith(TypeIdentifier colliderID);
149  void addCollidesWith(std::initializer_list<TypeIdentifier> colliderIDs);
150  void clearCollidesWithList();
151  bool collidesWith(TypeIdentifier id);
152 
158  Octree* getOctree();
164  std::vector<Triangle*> getTriangles();
165 
166  AABB getTransformedAABB();
167  Sphere getTransformedSphere();
168 
169  bool isDynamicObject();
170  void setDynamic(bool isDynamic);
171 
172  int getId();
173 
178  void makeDirty();
179  bool isDirty();
180 
181 
182 private:
183  void initGameObject(Mesh *mesh, Mesh *colliderMesh);
184 
185  chag::float4x4 getFullMatrix();
186 
190  int getUniqueId();
191 
197  Octree* createOctree(Mesh* mesh);
198 
199  static int uniqueId;
200  int id;
201 
202  /* Mesh */
203  Mesh *mesh;
204  chag::float4x4 m_modelMatrix;
205  chag::float3 scale = chag::make_vector(1.0f, 1.0f, 1.0f);
206  chag::Quaternion rotation;
207  chag::Quaternion rotationPointReference;
208  bool hasRotation = false;
209 
210  chag::float3 location = chag::make_vector(0.0f, 0.0f, 0.0f);
211 
212  bool changed = false;
213 
214  /* Hierarchy */
215  GameObject* parent = nullptr;
216  std::vector<GameObject*> children;
217 
218  /* Collision */
219  Mesh *collisionMesh;
220  AABB aabb;
221  Sphere sphere;
222  Octree *octree;
223  TypeIdentifier typeIdentifier = -1;
224  std::vector<TypeIdentifier> canCollideWith;
225  bool dynamicObject = false;
226 
227  /* Components */
228  IRenderComponent* renderComponent = nullptr;
229  std::vector<IComponent*> components;
230 
231  bool dirty = false;
232 };
virtual void render()
Definition: GameObject.cpp:137
virtual void renderShadow(ShaderProgram *shaderProgram)
Definition: GameObject.cpp:157
void update(float dt)
Definition: GameObject.cpp:192
Definition: Mesh.h:45
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
Definition: Octree.h:32
Octree * getOctree()
Definition: GameObject.cpp:274
void move(chag::float4x4 model_matrix)
Definition: GameObject.cpp:129
void makeDirty()
Definition: GameObject.cpp:118
A class for containing all information about a object in the game world.
Definition: GameObject.h:67
void callEvent(EventType type, GameObject *data)
Definition: GameObject.cpp:207
Definition: Sphere.h:19
Definition: AABB2.h:23
std::vector< Triangle * > getTriangles()
Definition: GameObject.cpp:148