Line data Source code
1 : /**
2 : * @file Object.h
3 : * @brief Base game object from which all concrete types are defined.
4 : *
5 : * Base game object.
6 : */
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 "luajit-2.1/lua.h"
22 : #include "luajit-2.1/lualib.h"
23 : #include "luajit-2.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 0 : virtual void drawShadow(Vector*) {}
46 :
47 : virtual void handleCollision(Object*) = 0;
48 :
49 : virtual void update(Vector*) = 0;
50 : virtual void animate(const float&, Object*);
51 :
52 0 : virtual void orientOnTerrain(Terrain *temp, const Quaternion &baseRotation) {}
53 0 : virtual void setHeightFromTerrain(Terrain *temp, const float &offset){}
54 :
55 : virtual void buildLuaObjectTable(lua_State *L);
56 : virtual void transferLuaObjectTable(lua_State *L);
57 :
58 : virtual void printTypeName() = 0;
59 : ////////////////////////////////////////////
60 :
61 : void showCollisionBox();
62 : void showControlTriangle();
63 :
64 : float calcTriangleCenter(const float& p1, const float& p2, const float& p3);
65 :
66 : void init();
67 : void initSettings();
68 : void cleanup();
69 :
70 : void loadScript(const std::string&, lua_State* luaState);
71 : void animateScript(const float&);
72 : void unloadScript();
73 :
74 : void setPosition(const GLfloat&, const GLfloat&, const GLfloat&);
75 : void setPosition(const Vector&);
76 : void setRotationAxis(const GLfloat&, const GLfloat&, const GLfloat&, const GLfloat&);
77 : void setRotationAxis(const Vector&, const GLfloat&);
78 : void setRotationEuler(const GLfloat&, const GLfloat&, const GLfloat&);
79 : void setRotationEuler(const Vector&);
80 : void setRotationQuaternion(const Quaternion&);
81 : void setVelocity(const GLfloat&, const GLfloat&, const GLfloat&);
82 : void setVelocity(const Vector&);
83 : void setRotationVelocity(const GLfloat&, const GLfloat&, const GLfloat&);
84 : void setRotationVelocity(const Vector&);
85 :
86 : void setScale(const GLfloat&, const GLfloat&, const GLfloat&);
87 : void setScale(const Vector&);
88 : void setScaleRate(const GLfloat&, const GLfloat&, const GLfloat&);
89 : void setScaleRate(const Vector&);
90 :
91 1 : void setDrawEnabled(const bool &isDrawEnabled) { isDrawEnabled_ = isDrawEnabled; }
92 4 : bool getDrawEnabled() { return isDrawEnabled_; }
93 :
94 4 : bool getInView() { return isInView_; }
95 : void setParticleSystem(const int&);
96 :
97 0 : float getScriptValue(const char* s) {
98 0 : float value = 0.0f;
99 :
100 0 : if (L_ != NULL) {
101 0 : lua_getglobal(L_, s);
102 0 : value = (float)lua_tonumber(L_, -1);
103 0 : lua_pop(L_, 1);
104 0 : }
105 :
106 0 : return value;
107 : }
108 :
109 0 : void setScriptValue(const char* s, const float &value) {
110 0 : if (L_ != NULL) {
111 0 : lua_pushnumber(L_, value);
112 0 : lua_setglobal(L_, s);
113 0 : }
114 0 : }
115 :
116 : /** Sets gameplay state_ only; does not change active_. Pair with setActive(false) if "dead" should stop updates. */
117 : void setState(const unsigned char&);
118 : void setScript(const std::string&);
119 :
120 : void setSpeed(const GLfloat&, const GLfloat&, const GLfloat&);
121 : void setSpeed(const Vector&);
122 :
123 1 : void setCollisionDetectionEnabled(const bool &isCollisionDetectionEnabled) {
124 1 : isCollisionDetectionEnabled_ = isCollisionDetectionEnabled;
125 1 : }
126 :
127 2 : bool getCollisionDetectionEnabled() { return isCollisionDetectionEnabled_; }
128 :
129 2 : void setActive(const bool &new_active) { active_ = new_active; }
130 7 : bool getActive() { return active_; }
131 :
132 6 : Vector getPosition() { return position_; }
133 4 : Vector getVelocity() { return velocity_; }
134 2 : Vector getRotationVelocity() { return rotationVelocity_; }
135 1 : Vector getScaleRate() { return scaleRate_; }
136 2 : Vector getSpeed() { return speed_; }
137 :
138 6 : bool isRotationChanged() { return rotationChanged_; }
139 8 : void setRotationChanged(const bool &rotationChanged_l) { rotationChanged_ = rotationChanged_l; }
140 5 : Quaternion getRotation() { return rotation_; }
141 1 : void setRotation(const Quaternion &rotation) { rotation_ = rotation; rotationChanged_ = true;}
142 1 : Vector getDirection() { return direction_; }
143 0 : Vector getUp() { return up_; }
144 2 : Vector getScale() { return scale_; }
145 :
146 : Quaternion getRInterpStart() { return rInterpStart_; }
147 : Quaternion getRInterpEnd() { return rInterpEnd_; }
148 0 : void setRInterpStart(const Quaternion &rInterpStart_l) { rInterpStart_ = rInterpStart_l;}
149 0 : void setRInterpEnd(const Quaternion &rInterpEnd_l) { rInterpEnd_= rInterpEnd_l;}
150 :
151 2 : bool isInterpolationEnabled() { return isInterpolationEnabled_; }
152 2 : void setIsInterpolationEnabled(const bool &isInterpolationEnabled_l) {
153 2 : isInterpolationEnabled_ = isInterpolationEnabled_l;
154 2 : }
155 :
156 : float getValInterpBegin() { return valInterpBegin_; }
157 1 : float getValInterpCurrent() { return valInterpCurrent_; }
158 1 : float getValInterpEnd() { return valInterpEnd_; }
159 :
160 1 : void setValInterpBegin(const float &valInterpBegin) { valInterpBegin_ = valInterpBegin; }
161 1 : void setValInterpCurrent(const float &valInterpCurrent) { valInterpCurrent_ = valInterpCurrent; }
162 1 : void setValInterpEnd(const float &valInterpEnd) { valInterpEnd_ = valInterpEnd; }
163 :
164 0 : void setSuspendTime(const float &time) { suspendTime_ = time; }
165 :
166 : /** Same storage as setState(unsigned char); overload exists for legacy call sites. */
167 1 : void setState(const int &state_l) {state_ = state_l; }
168 3 : int getState() { return state_; }
169 :
170 : void setScaleChanged(const bool &scaleChanged_l) { scaleChanged_ = scaleChanged_l; }
171 :
172 1 : void setIsAlwaysLit(const bool &isAlwaysLit) { isAlwaysLit_ = isAlwaysLit; }
173 1 : bool getIsAlwaysLit() { return isAlwaysLit_; }
174 :
175 1 : void setTypeId (const int &type_id) { typeId_ = type_id; }
176 2 : int getTypeId() { return typeId_; }
177 :
178 0 : Sphere getBoundingSphere() { return boundingSphere_; }
179 :
180 1 : std::string getScriptName() { return scriptName_; }
181 :
182 0 : Vector getOrth() { return orth_; }
183 :
184 1 : bool getEnableSphereTest() { return enableSphereTest_; }
185 :
186 4 : bool isDrawable() {
187 7 : return getActive() &&
188 3 : getInView() &&
189 3 : getDrawEnabled() &&
190 2 : getState() != DEAD;
191 : }
192 :
193 30 : void setGameLevelId(const unsigned int& gameLevelId) { gameLevelId_ = gameLevelId; }
194 1 : unsigned int getGameLevelId() { return gameLevelId_; }
195 :
196 : static unsigned int textureIds[MAX_TEXTURES];
197 : static std::map <std::string, unsigned int> textureHash;
198 :
199 : protected:
200 : // orientation //
201 : Vector position_; // x,y,z position of object
202 : Quaternion rotation_;
203 : Vector scale_; // x,y,z scale values
204 :
205 : Vector baseDirection_; // base direction of object
206 : Vector direction_; // direction facing
207 : Vector up_;
208 : Vector orth_;
209 :
210 : // rate of change //
211 : Vector velocity_;
212 : Vector speed_;
213 : Vector rotationVelocity_;
214 : Vector scaleRate_;
215 :
216 : // collision detection //
217 : Vector collisionBox_[2]; // 0 = min points, 1 = max points
218 : Vector controlPoints_[3]; // used for orienting objects on surfaces
219 : Sphere boundingSphere_;
220 :
221 : // state attributes //
222 : unsigned char state_; // objects current state
223 : bool active_; // is object active?
224 : bool isDrawEnabled_;
225 :
226 : bool isInView_; // is the object within the camera's view
227 :
228 : bool isCollisionDetectionEnabled_;
229 :
230 : // used for interpolation between 2 orientations //
231 : Quaternion rInterpStart_;
232 : Quaternion rInterpEnd_;
233 :
234 : float valInterpBegin_, valInterpCurrent_, valInterpEnd_;
235 : bool isInterpolationEnabled_;
236 :
237 : // Necessary for the Lua implementation
238 : lua_State* L_;
239 : std::string scriptName_;
240 : int scriptIndex_;
241 :
242 : float suspendTime_;
243 :
244 : ParticleSystem *particleSystem_;
245 :
246 : int typeId_;
247 : unsigned int gameLevelId_;
248 :
249 : Vector lastLight_;
250 : bool scaleChanged_;
251 : bool rotationChanged_;
252 :
253 : bool isAlwaysLit_;
254 :
255 : bool enableSphereTest_;
256 :
257 : private:
258 : void traverseAndCopyLuaTable(lua_State*, lua_State*, const int&);
259 : void copyLuaTableKey(lua_State*, lua_State*);
260 : };
261 :
262 : #endif
|