Frapper  1.0a
S3DGameEngine.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of FRAPPER
4 research.animationsinstitut.de
5 sourceforge.net/projects/frapper
6 
7 Copyright (c) 2008-2009 Filmakademie Baden-Wuerttemberg, Institute of Animation
8 
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; version 2.1 of the License.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
21 -----------------------------------------------------------------------------
22 */
23 
32 
33 #ifndef S3DGAMEENGINE_H
34 #define S3DGAMEENGINE_H
35 
36 // Ogre
37 #include "OgreContainer.h"
38 #include "OgreTools.h"
39 
40 // Game paths
41 #include "S3DGamePaths.h"
42 
43 
44 namespace S3DGameNode {
45 using namespace Frapper;
46 
47 // forward declaration;
48 class SceneObject;
49 class EventMarker;
50 class Enemy;
51 class Obstacle;
52 
53 // This class stores a game events.
54 // An event consists of strings which define the actions of players
55 class GameEvent
56 {
57 public:
58  GameEvent( Ogre::SceneNode* eventSceneNode)
59  : mSceneNode(eventSceneNode),
60  mJump(NULL),
61  mMove(NULL),
62  mMoveJump(NULL),
63  mPlatform(""),
64  mCheckpoint("")
65  {
66  // check valid pointer
67  assert( mSceneNode );
68  // Update the bounds of the event scene node
69  mSceneNode->_updateBounds();
70  };
71 
72  Path* Jump() const { return mJump; }
73  void Jump(Path* val) { mJump = val; }
74  Path* Move() const { return mMove; }
75  void Move(Path* val) { mMove = val; }
76  Path * MoveJump() const { return mMoveJump; }
77  void MoveJump(Path * val) { mMoveJump = val; }
78  QString Platform() const { return mPlatform; }
79  void Platform( QString val) { mPlatform = val; }
80  QString Checkpoint() const { return mCheckpoint; }
81  void Checkpoint(QString val) { mCheckpoint = val; }
82  Ogre::SceneNode* SceneNode() const { return mSceneNode; }
83 
84  // Check if this event is active for the given position
85  bool isActive( Vec3 position )
86  {
87  return ( mSceneNode && mSceneNode->_getWorldAABB().contains(position) );
88  }
89 
90 private:
91  Path *mJump, *mMove, *mMoveJump;
92  QString mPlatform;
94  QString mCheckpoint;
95 };
96 
97 
98 
100 {
101 public:
102  AnimatedObjectMarker( SceneNode* markerGeometry, QString markerName )
103  : mMarkerGeometry(markerGeometry),
104  mObjectGeometry(NULL),
105  mVisibleTo(""),
106  mMarkerName(markerName) {}
107 
109  {
110  };
111 
112  void MarkerName(QString val) { mMarkerName = val; }
113  void VisibleTo( QString val) { mVisibleTo = val; }
114  void MarkerGeometry( SceneNode* val) { mMarkerGeometry = val; }
115  void ObjectGeometry( SceneNode* val) { mObjectGeometry = val; }
116 
117  QString MarkerName() const { return mMarkerName; }
118  QString VisibleTo() const { return mVisibleTo; }
119  SceneNode* MarkerGeometry() const { return mMarkerGeometry; }
120  SceneNode* ObjectGeometry() const { return mObjectGeometry; }
121 
122 protected:
125  QString mMarkerName;
126  QString mVisibleTo;
127 };
128 
129 
130 
131 // Scene object that also updates its copies via ogre container
133 {
134 public:
135  SceneObject( SceneNode* sceneNode)
136  : mGeometry(sceneNode), mContainer(NULL)
137  {
138  assert(mGeometry);
139  mContainer = OgreTools::getOgreContainer(sceneNode);
140  assert(mContainer);
141  }
143 
144  virtual void Animate()
145  {
146  UpdateCopies();
147  };
148 
149  void UpdateCopies()
150  {
151  if( mContainer )
152  mContainer->updateCopies();
153  }
154  SceneNode* Geometry() const { return mGeometry; }
155  void Geometry(SceneNode* val) { mGeometry = val; }
156 
157 protected:
160 };
161 
162 // Visual Marker for an event in the scene
163 class EventMarker : public SceneObject
164 {
165 public:
166  EventMarker( SceneNode* sceneNode)
167  : SceneObject(sceneNode)
168  {
169  }
171 
172  void Animate()
173  {
174  if( mGeometry )
175  this->mGeometry->yaw( Ogre::Radian( Ogre::Degree( 5 )), Ogre::Node::TS_LOCAL );
176  UpdateCopies();
177  }
178 
179 private:
180 };
181 
182 class Enemy : public SceneObject
183 {
184 public:
185  Enemy( Ogre::SceneNode* geometry )
186  : SceneObject( geometry )
187  {
188  assert( geometry );
189  mDestroyed = false;
190  mScale = geometry->getScale().x;
191  mSpeed = Ogre::Math::RangeRandom(5.0f, 20.0f);
192  }
193  ~Enemy() {};
194 
195  void Destroy()
196  {
197  mDestroyed = true;
198  };
199  void Animate()
200  {
201  if( mGeometry )
202  {
203  mGeometry->yaw( Ogre::Radian( Ogre::Degree(mSpeed)), Ogre::Node::TS_LOCAL);
204  if( mDestroyed )
205  {
206  float scale = mGeometry->getScale().x;
207  if ( scale > 0.05f)
208  {
209  scale -= 0.05f;
210  CLAMP_LOWER( scale, 0.0f );
211  mGeometry->setScale(Vec3(scale));
212  }
213  }
214  UpdateCopies();
215  }
216  };
217  void Reset()
218  {
219  if( mGeometry )
220  mGeometry->setScale( Vec3(mScale));
221  mDestroyed = false;
222  UpdateCopies();
223  }
224 
225  bool Destroyed() const { return mDestroyed; }
226  void Destroyed(bool val) { mDestroyed = val; }
227 
228 protected:
230  float mSpeed;
231  float mScale;
232 };
233 
234 class Obstacle : public SceneObject
235 {
236 public:
237 
238  Obstacle( SceneNode* sceneNode )
239  : SceneObject(sceneNode),
240  mOpen(false),
241  mOpenState(0.0f)
242  {}
244  {};
245 
246  void Animate()
247  {
248  // animate opening
249  if( mOpen && mOpenState < 1.0f )
250  mOpenState += 0.1f;
251  else if (!mOpen && mOpenState > 0.0f )
252  mOpenState -= 0.1f;
253  }
254 
255  bool Open() const { return mOpen; }
256  void Open(bool val) { mOpen = val; }
257  void Reset()
258  {
259  mOpen = false;
260  mOpenState = 0.0f;
261  UpdateCopies();
262  }
263 
264 private:
265  bool mOpen;
266  float mOpenState;
267 };
268 
270 {
271 public:
272 
273  AnimatedDoor( NumberParameter* param, float stepSize = 0.1f )
274  : mParam(param), mOpen(false), mOpenState(0.0f), mStepSize( stepSize )
275  {}
277  {};
278 
279  void Animate( float animScale = 100.0f )
280  {
281  // animate opening
282  if( mOpen && mOpenState < 1.0f )
283  mOpenState += mStepSize;
284  else if (!mOpen && mOpenState > 0.0f )
285  mOpenState -= mStepSize;
286 
287  CLAMP( mOpenState, 0.0f, 1.0f);
288  if( mParam ) mParam->setValue( QVariant( mOpenState * animScale ), true );
289  }
290 
291  void SetOpen( bool open ) { mOpen = open; }
292 
293 private:
295  bool mOpen;
296  float mOpenState, mStepSize;
297 };
298 
303 {
304 public:
305 
306 public: // typedefs
307 
311  typedef QHash<QString, Path*> PathMap;
312  typedef QHash<QString, Ogre::SceneNode*> MarkerMap;
313  typedef QHash<QString, Enemy*> EnemyMap;
314  typedef QHash<QString, Obstacle*> ObstacleMap;
315 
316  typedef QMap<QString, AnimatedObjectMarker*> AnimatedObjectsMap;
317  static const QString PathPrefix;
318  static const QString StartPath;
319 
320 public: // constructors and destructors
321 
328  S3DGameEngine ();
329 
337  virtual ~S3DGameEngine ();
338 
339 
340 public: // functions
341 
342  PathMap Paths() const { return mPaths; }
343  MarkerMap Marker() const { return mMarker; }
344  EnemyMap Enemies() const { return mEnemies; }
345  ObstacleMap Obstacles() const { return mObstacles; }
346 
347  QList<GameEvent*> GameEvents() const { return mGameEvents; }
348  QList<EventMarker*> EventMarkers() const { return mEventMarkers; }
349  AnimatedObjectsMap AnimatedObjectMarkers() const { return mAnimatedObjectMarkers; }
350 
351 
352 private:
353 
359  QList<GameEvent*> mGameEvents;
360  QList<EventMarker*> mEventMarkers;
361 
362 public: //functions
363 
364  void ParseLogicSceneNode( SceneNode * inputLogic );
365  void ParseEnemies( SceneNode* sceneNode );
366 
367  void parseObjectMarker( Ogre::SceneNode * inputObjectMarker, QList<QString>& objectNames );
368  void createAnimatedObjects( QString objectName, Ogre::SceneNode* objectGeometry, Ogre::SceneNode* player1, Ogre::SceneNode* player2, Ogre::SceneNode* both );
369 
370  void CreateObstacles( SceneNode * inputLogic, SceneNode* inputGeometry, SceneNode* parent );
371  void CreateEventMarker( Ogre::SceneNode* sceneNode, SceneNode* parent );
372 
373  void processInputGeometryP1( Ogre::SceneNode * inputGeometryP1 );
374  void processInputGeometryP2( Ogre::SceneNode * inputGeometryP2 );
375  void processInputGeometryBoth( Ogre::SceneNode * inputGeometryBoth );
376 
377  Path* GetPathByName( const QString &action );
378 
379 private:
380 
381  void ParseMarkers(Ogre::SceneNode* sceneNode);
382  void ParsePaths( Ogre::SceneNode* sceneNode);
383  void ParseEvents(Ogre::SceneNode* sceneNode);
384 
385  Path* CreatePath( QString startMarkerName, QString endMarkerName, QString type );
386 };
387 
388 } // namespace S3DGameNode
389 #endif