SuperTuxKart
track_object.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2009-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_TRACK_OBJECT_HPP
20 #define HEADER_TRACK_OBJECT_HPP
21 
22 #include <vector3d.h>
23 
24 #include "physics/physical_object.hpp"
25 #include "scriptengine/scriptvec3.hpp"
26 #include "tracks/track_object_presentation.hpp"
27 #include "utils/cpp2011.hpp"
28 #include "utils/no_copy.hpp"
29 #include "utils/vec3.hpp"
30 #include <string>
31 #include "animations/three_d_animation.hpp"
32 
33 #include <memory>
34 
36 class RenderInfo;
37 class ThreeDAnimation;
38 class XMLNode;
39 
46 class TrackObject : public NoCopy
47 {
48 //public:
49  // The different type of track objects: physical objects, graphical
50  // objects (without a physical representation) - the latter might be
51  // eye candy (to reduce work for physics), ...
52  //enum TrackObjectType {TO_PHYSICAL, TO_GRAPHICAL};
53 
54 private:
56  bool m_enabled;
57 
58  TrackObjectPresentation* m_presentation;
59 
60  std::string m_name;
61 
62  std::string m_id;
63 
64  std::shared_ptr<RenderInfo> m_render_info;
65 
66 protected:
67 
69  core::vector3df m_init_xyz;
70 
72  core::vector3df m_init_hpr;
73 
75  core::vector3df m_init_scale;
76 
78  std::string m_lod_group;
79 
80  std::string m_interaction;
81 
82  std::string m_type;
83 
84  bool m_soccer_ball;
85 
88 
89  std::shared_ptr<PhysicalObject> m_physical_object;
90 
91  ThreeDAnimation* m_animator;
92 
93  TrackObject* m_parent_library;
94 
95  std::vector<TrackObject*> m_movable_children;
96  std::vector<TrackObject*> m_children;
97 
98  bool m_initially_visible;
99 
100  std::string m_visibility_condition;
101 
102  void init(const XMLNode &xml_node, scene::ISceneNode* parent,
103  ModelDefinitionLoader& model_def_loader,
104  TrackObject* parent_library);
105 
106 public:
107  TrackObject(const XMLNode &xml_node,
108  scene::ISceneNode* parent,
109  ModelDefinitionLoader& model_def_loader,
110  TrackObject* parent_library);
111 
112  TrackObject(const core::vector3df& xyz,
113  const core::vector3df& hpr,
114  const core::vector3df& scale,
115  const char* interaction,
116  TrackObjectPresentation* presentation,
117  bool is_dynamic,
118  const PhysicalObject::Settings* physicsSettings);
119  virtual ~TrackObject();
120  virtual void update(float dt);
121  virtual void updateGraphics(float dt);
122  virtual void resetAfterRewind();
123  void move(const core::vector3df& xyz, const core::vector3df& hpr,
124  const core::vector3df& scale, bool updateRigidBody,
125  bool isAbsoluteCoord);
126 
127  virtual void reset();
128  const core::vector3df& getPosition() const;
129  const core::vector3df getAbsolutePosition() const;
130  const core::vector3df getAbsoluteCenterPosition() const;
131  const core::vector3df& getRotation() const;
132  const core::vector3df& getScale() const;
133  bool castRay(const btVector3 &from,
134  const btVector3 &to, btVector3 *hit_point,
135  const Material **material, btVector3 *normal,
136  bool interpolate_normal) const;
137 
138  TrackObject* getParentLibrary()
139  {
140  return m_parent_library;
141  }
142 
143  // ------------------------------------------------------------------------
146  virtual void onWorldReady();
147  // ------------------------------------------------------------------------
150  virtual void handleExplosion(const Vec3& pos, bool directHit) {};
151  void setID(std::string obj_id) { m_id = obj_id; }
152 
153  // ------------------------------------------------------------------------
154  const std::string& getLodGroup() const { return m_lod_group; }
155  // ------------------------------------------------------------------------
156  const std::string& getType() const { return m_type; }
157  // ------------------------------------------------------------------------
158  const std::string getName() const { return m_name; }
159  // ------------------------------------------------------------------------
160  const std::string getID() const { return m_id; }
161  // ------------------------------------------------------------------------
162  const std::string getInteraction() const { return m_interaction; }
163  // ------------------------------------------------------------------------
164  bool isEnabled() const { return m_enabled; }
165  // ------------------------------------------------------------------------
166  bool isSoccerBall() const { return m_soccer_ball; }
167  // ------------------------------------------------------------------------
168  const PhysicalObject* getPhysicalObject() const
169  { return m_physical_object.get(); }
170  // ------------------------------------------------------------------------
171  PhysicalObject* getPhysicalObject() { return m_physical_object.get(); }
172  // ------------------------------------------------------------------------
173  const core::vector3df getInitXYZ() const { return m_init_xyz; }
174  // ------------------------------------------------------------------------
175  const core::vector3df getInitRotation() const { return m_init_hpr; }
176  // ------------------------------------------------------------------------
177  const core::vector3df getInitScale() const { return m_init_scale; }
178  // ------------------------------------------------------------------------
179  template<typename T>
180  T* getPresentation() { return dynamic_cast<T*>(m_presentation); }
181  // ------------------------------------------------------------------------
182  template<typename T>
183  const T* getPresentation() const { return dynamic_cast<T*>(m_presentation); }
184  // ------------------------------------------------------------------------
185  // Methods usable by scripts
197  scene::IAnimatedMeshSceneNode* getMesh();
201  TrackObjectPresentationParticles* getParticleEmitter() { return getPresentation<TrackObjectPresentationParticles>(); }
205  TrackObjectPresentationSound* getSoundEmitter(){ return getPresentation<TrackObjectPresentationSound>(); }
209  TrackObjectPresentationLight* getLight() { return getPresentation<TrackObjectPresentationLight>(); }
210  // For angelscript. Needs to be named something different than getAnimator since it's overloaded.
214  ThreeDAnimation* getIPOAnimator() { return m_animator; }
215  // For angelscript. Needs to be named something different than getPhysicalObject since it's overloaded.
219  PhysicalObject* getPhysics() { return m_physical_object.get(); }
221  void setEnabled(bool mode);
222 
223  void moveTo(const Scripting::SimpleVec3* pos, bool isAbsoluteCoord);
224  /* @} */
225  /* @} */
226  /* @} */
227 
228  void resetEnabled();
229  // ------------------------------------------------------------------------
230  ThreeDAnimation* getAnimator() { return m_animator; }
231  // ------------------------------------------------------------------------
232  const ThreeDAnimation* getAnimator() const { return m_animator; }
233  // ------------------------------------------------------------------------
234  /* Return true if it has animator or its parent library has */
235  bool hasAnimatorRecursively() const
236  {
237  if (m_animator)
238  return true;
239  if (!m_parent_library)
240  return false;
241  return m_parent_library->hasAnimatorRecursively();
242  }
243  // ------------------------------------------------------------------------
244  void setPaused(bool mode){ m_animator->setPaused(mode); }
245  // ------------------------------------------------------------------------
246  void setInitiallyVisible(bool val) { m_initially_visible = val; }
247  // ------------------------------------------------------------------------
249  bool isDriveable() const { return m_is_driveable; }
250  // ------------------------------------------------------------------------
254  void addMovableChild(TrackObject* child);
255  // ------------------------------------------------------------------------
256  void addChild(TrackObject* child);
257  // ------------------------------------------------------------------------
258  std::vector<TrackObject*>& getMovableChildren() { return m_movable_children; }
259  // ------------------------------------------------------------------------
260  std::vector<TrackObject*>& getChildren() { return m_children; }
261  // ------------------------------------------------------------------------
262  void movePhysicalBodyToGraphicalNode(const core::vector3df& xyz, const core::vector3df& hpr);
263  // ------------------------------------------------------------------------
264  bool joinToMainTrack();
265  LEAK_CHECK()
266 }; // TrackObject
267 
268 #endif
bool isDriveable() const
Returns if a kart can drive on this object.
Definition: track_object.hpp:249
void addMovableChild(TrackObject *child)
Used along the "extract movable nodes out of library objects" hack, used to still preserve the parent...
Definition: track_object.cpp:688
void init(const XMLNode &xml_node, scene::ISceneNode *parent, ModelDefinitionLoader &model_def_loader, TrackObject *parent_library)
Initialises the track object based on the specified XML data.
Definition: track_object.cpp:103
virtual void updateGraphics(float dt)
This updates all only graphical elements.
Definition: track_object.cpp:538
virtual ~TrackObject()
Destructor.
Definition: track_object.cpp:462
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
bool castRay(const btVector3 &from, const btVector3 &to, btVector3 *hit_point, const Material **material, btVector3 *normal, bool interpolate_normal) const
Does a raycast against the track object.
Definition: track_object.cpp:585
ThreeDAnimation * getIPOAnimator()
Should only be used on TrackObjects that use curve-based animation.
Definition: track_object.hpp:214
scene::IAnimatedMeshSceneNode * getMesh()
Should only be used on mesh track objects.
Definition: track_object.cpp:717
virtual void reset()
Initialises an object before a race starts.
Definition: track_object.cpp:471
virtual void onWorldReady()
To finish object constructions.
Definition: track_object.cpp:396
TrackObjectPresentationLight * getLight()
Should only be used on sound emitter track objects.
Definition: track_object.hpp:209
bool m_enabled
True if the object is currently being displayed.
Definition: track_object.hpp:56
A track object representation that consists of a sound emitter.
Definition: track_object_presentation.hpp:276
TrackObjectPresentationSound * getSoundEmitter()
Should only be used on sound emitter track objects.
Definition: track_object.hpp:205
bool m_is_driveable
True if a kart can drive on this object.
Definition: track_object.hpp:87
std::string m_lod_group
LOD group this object is part of, if it is LOD.
Definition: track_object.hpp:78
Base class for all track object presentation classes.
Definition: track_object_presentation.hpp:57
virtual void update(float dt)
This updates once per physics time step.
Definition: track_object.cpp:549
TrackObject(const XMLNode &xml_node, scene::ISceneNode *parent, ModelDefinitionLoader &model_def_loader, TrackObject *parent_library)
A track object: any additional object on the track.
Definition: track_object.cpp:54
Definition: physical_object.hpp:40
A track object representation that consists of a particle emitter.
Definition: track_object_presentation.hpp:336
A track object representation that consists of a light emitter.
Definition: track_object_presentation.hpp:364
virtual void handleExplosion(const Vec3 &pos, bool directHit)
Called when an explosion happens.
Definition: track_object.hpp:150
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
Definition: physical_object.hpp:50
core::vector3df m_init_hpr
The initial hpr of the object.
Definition: track_object.hpp:72
Definition: material.hpp:46
virtual void resetAfterRewind()
This reset all physical object moved by 3d animation back to current ticks.
Definition: track_object.cpp:560
Definition: scriptvec3.hpp:28
utility class used to parse XML files
Definition: xml_node.hpp:47
core::vector3df m_init_scale
The initial scale of the object.
Definition: track_object.hpp:75
Definition: render_info.hpp:27
core::vector3df m_init_xyz
The initial XYZ position of the object.
Definition: track_object.hpp:69
A virtual base class for all animations.
Definition: three_d_animation.hpp:44
This is a base object for any separate object on the track, which might also have a skeletal animatio...
Definition: track_object.hpp:46
void setEnabled(bool mode)
Hide or show the object.
Definition: track_object.cpp:483
TrackObjectPresentationParticles * getParticleEmitter()
Should only be used on particle emitter track objects.
Definition: track_object.hpp:201
Utility class to load level-of-detail nodes and library nodes.
Definition: model_definition_loader.hpp:81
PhysicalObject * getPhysics()
Get the physics representation of an object.
Definition: track_object.hpp:219