Krig Game Engine
Object.h
Go to the documentation of this file.
1 
7 #ifndef _OBJECT_H_
8 #define _OBJECT_H_
9 
10 #include <GL/glut.h>
11 #include <string>
12 #include <map>
13 
14 #include "ObjectNode.h"
15 #include "Sphere.h"
16 #include "Quaternion.h"
17 #include "ParticleSystem.h"
18 #include "constants.h"
19 
20 extern "C" {
21  #include "lua5.1/lua.h"
22  #include "lua5.1/lualib.h"
23  #include "lua5.1/lauxlib.h"
24 }
25 
26 // States
27 #define DEAD 0
28 #define NORMAL 1
29 
30 #define MAX_TEXTURES 32
31 
32 class Terrain;
33 class Camera;
34 
35 class Object : public ObjectNode {
36  public:
37  Object();
38  virtual ~Object();
39 
40  void processCollisions(Object*);
41 
42  // virtual functions ///////////////////////
43  virtual void draw(Object*) = 0;
44  virtual void drawOutline(Object*) = 0;
45  virtual void handleCollision(Object*) = 0;
46  virtual void update(Vector*) = 0;
47  virtual void printTypeName() = 0;
48  virtual void drawShadow(Vector*) {}
49  virtual void animate(const float&, Object*);
50  virtual void orientOnTerrain(Terrain *temp, const Quaternion &baseRotation) {}
51  virtual void setHeightFromTerrain(Terrain *temp, const float &offset){}
52  virtual void buildLuaObjectTable(lua_State *L);
53  virtual void transferLuaObjectTable(lua_State *L);
55 
56  void showCollisionBox();
57  void showControlTriangle();
58 
59  float calcTriangleCenter(const float&, const float&, const float&);
60 
61  void init();
62  void initSettings();
63  void cleanup();
64 
65  void loadScript(const std::string&, lua_State* luaState);
66  void animateScript(const float&);
67  void unloadScript();
68 
69  void setPosition(const GLfloat&, const GLfloat&, const GLfloat&);
70  void setPosition(const Vector&);
71  void setRotationAxis(const GLfloat&, const GLfloat&, const GLfloat&, const GLfloat&);
72  void setRotationAxis(const Vector&, const GLfloat&);
73  void setRotationEuler(const GLfloat&, const GLfloat&, const GLfloat&);
74  void setRotationEuler(const Vector&);
75  void setRotationQuaternion(const Quaternion&);
76  void setVelocity(const GLfloat&, const GLfloat&, const GLfloat&);
77  void setVelocity(const Vector&);
78  void setRotationVelocity(const GLfloat&, const GLfloat&, const GLfloat&);
79  void setRotationVelocity(const Vector&);
80 
81  void setScale(const GLfloat&, const GLfloat&, const GLfloat&);
82  void setScale(const Vector&);
83  void setScaleRate(const GLfloat&, const GLfloat&, const GLfloat&);
84  void setScaleRate(const Vector&);
85 
86  void setDrawEnabled(const bool &isDrawEnabled) { isDrawEnabled_ = isDrawEnabled; }
87  bool getDrawEnabled() { return isDrawEnabled_; }
88 
89  bool getInView() { return isInView_; }
90  void setParticleSystem(const int&);
91 
92  float getScriptValue(const char* s) {
93  float value = 0.0f;
94 
95  if (L_ != NULL) {
96  lua_getglobal(L_, s);
97  value = (float)lua_tonumber(L_, -1);
98  lua_pop(L_, 1);
99  }
100 
101  return value;
102  }
103 
104  void setScriptValue(const char* s, const float &value) {
105  if (L_ != NULL) {
106  lua_pushnumber(L_, value);
107  lua_setglobal(L_, s);
108  }
109  }
110 
111  void setState(const unsigned char&);
112  void setScript(const std::string&);
113 
114  void setSpeed(const GLfloat&, const GLfloat&, const GLfloat&);
115  void setSpeed(const Vector&);
116 
117  void setCollisionDetectionEnabled(const bool &isCollisionDetectionEnabled) {
118  isCollisionDetectionEnabled_ = isCollisionDetectionEnabled;
119  }
120 
121  bool getCollisionDetectionEnabled() { return isCollisionDetectionEnabled_; }
122 
123  void setActive(const bool &new_active) { active_ = new_active; }
124  bool getActive() { return active_; }
125 
126  Vector getPosition() { return position_; }
127  Vector getVelocity() { return velocity_; }
128  Vector getRotationVelocity() { return rotationVelocity_; }
129  Vector getScaleRate() { return scaleRate_; }
130  Vector getSpeed() { return speed_; }
131 
132  bool isRotationChanged() { return rotationChanged_; }
133  void setRotationChanged(const bool &rotationChanged_l) { rotationChanged_ = rotationChanged_l; }
134  Quaternion getRotation() { return rotation_; }
135  void setRotation(const Quaternion &rotation) { rotation_ = rotation; rotationChanged_ = true;}
136  Vector getDirection() { return direction_; }
137  Vector getUp() { return up_; }
138  Vector getScale() { return scale_; }
139 
140  Quaternion getRInterpStart() { return rInterpStart_; }
141  Quaternion getRInterpEnd() { return rInterpEnd_; }
142  void setRInterpStart(const Quaternion &rInterpStart_l) { rInterpStart_ = rInterpStart_l;}
143  void setRInterpEnd(const Quaternion &rInterpEnd_l) { rInterpEnd_= rInterpEnd_l;}
144 
145  bool isInterpolationEnabled() { return isInterpolationEnabled_; }
146  void setIsInterpolationEnabled(const bool &isInterpolationEnabled_l) {
147  isInterpolationEnabled_ = isInterpolationEnabled_l;
148  }
149 
150  float getValInterpBegin() { return valInterpBegin_; }
151  float getValInterpCurrent() { return valInterpCurrent_; }
152  float getValInterpEnd() { return valInterpEnd_; }
153 
154  void setValInterpBegin(const float &valInterpBegin) { valInterpBegin_ = valInterpBegin; }
155  void setValInterpCurrent(const float &valInterpCurrent) { valInterpCurrent_ = valInterpCurrent; }
156  void setValInterpEnd(const float &valInterpEnd) { valInterpEnd_ = valInterpEnd; }
157 
158  void setSuspendTime(const float &time) { suspendTime_ = time; }
159 
160  void setState(const int &state_l) {state_ = state_l; }
161  int getState() { return state_; }
162 
163  void setScaleChanged(const bool &scaleChanged_l) { scaleChanged_ = scaleChanged_l; }
164 
165  void setIsAlwaysLit(const bool &isAlwaysLit) { isAlwaysLit_ = isAlwaysLit; }
166  bool getIsAlwaysLit() { return isAlwaysLit_; }
167 
168  void setTypeId (const int &type_id) { typeId_ = type_id; }
169  int getTypeId() { return typeId_; }
170 
171  Sphere getBoundingSphere() { return boundingSphere_; }
172 
173  std::string getScriptName() { return scriptName_; }
174 
175  Vector getOrth() { return orth_; }
176 
177  bool getEnableSphereTest() { return enableSphereTest_; }
178 
179  bool isDrawable() {
180  return getActive() &&
181  getInView() &&
182  getDrawEnabled() &&
183  getState() != DEAD;
184  }
185 
186  void setGameLevelId(const unsigned int& gameLevelId) { gameLevelId_ = gameLevelId; }
187  unsigned int getGameLevelId() { return gameLevelId_; }
188 
189  static unsigned int textureIds[MAX_TEXTURES];
190  static std::map <std::string, unsigned int> textureHash;
191 
192  protected:
193  // orientation //
194  Vector position_; // x,y,z position of object
195  Quaternion rotation_;
196  Vector scale_; // x,y,z scale values
197 
198  Vector baseDirection_; // base direction of object
199  Vector direction_; // direction facing
200  Vector up_;
201  Vector orth_;
202 
203  // rate of change //
204  Vector velocity_;
205  Vector speed_;
206  Vector rotationVelocity_;
207  Vector scaleRate_;
208 
209  // collision detection //
210  Vector collisionBox_[2]; // 0 = min points, 1 = max points
211  Vector controlPoints_[3]; // used for orienting objects on surfaces
212  Sphere boundingSphere_;
213 
214  // state attributes //
215  unsigned char state_; // objects current state
216  bool active_; // is object active?
217  bool isDrawEnabled_;
218 
219  bool isInView_; // is the object within the camera's view
220 
221  bool isCollisionDetectionEnabled_;
222 
223  // used for interpolation between 2 orientations //
224  Quaternion rInterpStart_;
225  Quaternion rInterpEnd_;
226 
227  float valInterpBegin_, valInterpCurrent_, valInterpEnd_;
228  bool isInterpolationEnabled_;
229 
230  // Necessary for the Lua implementation
231  lua_State* L_;
232  std::string scriptName_;
233  int scriptIndex_;
234 
235  float suspendTime_;
236 
237  ParticleSystem *particleSystem_;
238 
239  int typeId_;
240  unsigned int gameLevelId_;
241 
242  Vector lastLight_;
243  bool scaleChanged_;
244  bool rotationChanged_;
245 
246  bool isAlwaysLit_;
247 
248  bool enableSphereTest_;
249 
250  private:
251  void traverseAndCopyLuaTable(lua_State*, lua_State*, const int&);
252  void copyLuaTableKey(lua_State*, lua_State*);
253 };
254 
255 #endif
Definition: Quaternion.h:14
Definition: Object.h:35
representation of a quaternion.
represent and operate on a sphere.
Definition: ParticleSystem.h:23
Definition: Vector.h:15
Definition: terrain.h:14
A game object link in the game list linked list.
Definition: ObjectNode.h:11
Definition: Sphere.h:13
Base class from which all concrete particle systems are derived.
Definition: Camera.h:13