Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpMotionBall.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
21 #include "ICmpMotion.h"
22 
23 #include "ICmpPosition.h"
25 
26 #include "graphics/Terrain.h"
27 
28 class CCmpMotionBall : public ICmpMotion
29 {
30 public:
31  static void ClassInit(CComponentManager& componentManager)
32  {
33  componentManager.SubscribeToMessageType(MT_Update);
34  }
35 
37 
38  // Current speed in metres per second
40 
41  static std::string GetSchema()
42  {
43  return "<a:component type='test'/><ref name='anything'/>";
44  }
45 
46  virtual void Init(const CParamNode& UNUSED(paramNode))
47  {
48  m_SpeedX = 0;
49  m_SpeedZ = 0;
50  }
51 
52  virtual void Deinit()
53  {
54  }
55 
56  virtual void Serialize(ISerializer& serialize)
57  {
58  serialize.NumberFloat_Unbounded("speed x", m_SpeedX);
59  serialize.NumberFloat_Unbounded("speed z", m_SpeedZ);
60  }
61 
62  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
63  {
64  Init(paramNode);
65  deserialize.NumberFloat_Unbounded("speed x", m_SpeedX);
66  deserialize.NumberFloat_Unbounded("speed z", m_SpeedZ);
67  }
68 
69  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
70  {
71  switch (msg.GetType())
72  {
73  case MT_Update:
74  {
75  fixed dt = static_cast<const CMessageUpdate&> (msg).turnLength;
76  Move(dt);
77  break;
78  }
79  }
80  }
81 
82  void Move(fixed dt);
83 };
84 
86 {
88  if (!cmpPosition)
89  return;
90 
91  // TODO: this is all FP-unsafe
92 
93  CFixedVector3D pos = cmpPosition->GetPosition();
94  float x = pos.X.ToFloat();
95  float z = pos.Z.ToFloat();
96 
98  // Flatten the vector, to get the downhill force
99  float g = 10.f;
100  CVector3D force(g * normal.X, 0.f, g * normal.Z);
101 
102  m_SpeedX += force.X;
103  m_SpeedZ += force.Z;
104 
105  float drag = 0.5f; // fractional decay per second
106 
107  float dt_ = dt.ToFloat();
108 
109  m_SpeedX *= powf(drag, dt_);
110  m_SpeedZ *= powf(drag, dt_);
111 
112  cmpPosition->MoveTo(entity_pos_t::FromFloat(x + m_SpeedX * dt_), entity_pos_t::FromFloat(z + m_SpeedZ * dt_));
113 }
114 
115 REGISTER_COMPONENT_TYPE(MotionBall)
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
A simple fixed-point number class.
Definition: Fixed.h:115
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
Generic per-turn update message, for things that don&#39;t care much about ordering.
Definition: MessageTypes.h:57
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
static std::string GetSchema()
Generic motion interface for entities with entirely self-contained motion e.g.
Definition: ICmpMotion.h:28
static void ClassInit(CComponentManager &componentManager)
CEntityHandle GetEntityHandle() const
Definition: IComponent.h:45
virtual void HandleMessage(const CMessage &msg, bool global)
void NumberFloat_Unbounded(const char *name, float value)
Serialize a number.
Definition: ISerializer.h:181
virtual void Serialize(ISerializer &serialize)
virtual int GetType() const =0
float X
Definition: Vector3D.h:31
float ToFloat() const
Convert to float. May be lossy - float can&#39;t represent all values.
Definition: Fixed.h:161
void Move(fixed dt)
virtual void Deinit()
virtual void MoveTo(entity_pos_t x, entity_pos_t z)=0
Move smoothly to the given location.
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
virtual void Init(const CParamNode &paramNode)
virtual CFixedVector3D GetPosition()=0
Returns the current x,y,z position (no interpolation).
virtual void NumberFloat_Unbounded(const char *name, float &out)
static CFixed FromFloat(float n)
Definition: Fixed.h:141
float Z
Definition: Vector3D.h:31
CVector3D CalcExactNormal(float x, float z) const
Definition: Terrain.cpp:235
CTerrain & GetTerrain() const
Definition: SimContext.cpp:51
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34