Cafu Engine
CompPlayerPhysics.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GAMESYS_COMPONENT_PLAYER_PHYSICS_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_PLAYER_PHYSICS_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 #include "ClipSys/TraceSolid.hpp"
12 
13 
14 namespace cf { namespace ClipSys { class ClipModelT; } }
15 namespace cf { namespace ClipSys { class ClipWorldT; } }
16 
17 
18 namespace cf
19 {
20  namespace GameSys
21  {
22  /// This component implements human player physics for its entity.
23  /// It updates the entity's origin according to the laws of simple physics
24  /// that are appropriate for player movement.
25  /// The component does not act on its own in a server's Think() step, but is
26  /// only a helper to other C++ or script code that must drive it explicitly.
28  {
29  public:
30 
31  /// The constructor.
33 
34  /// The copy constructor.
35  /// @param Comp The component to create a copy of.
37 
38  /// This is the main method of this component: It advances the entity's origin
39  /// according to the laws of simple physics and the given state and parameters.
40  /// Other C++ or script code of the entity typically calls this method on each
41  /// clock-tick (frame) of the server.
42  /// @param FrameTime The time across which the entity is to be advanced.
43  /// @param WishVelocity The desired velocity of the entity as per user input.
44  /// @param WishVelLadder The desired velocity on a ladder as per user input.
45  /// @param WishJump Does the user want the entity to jump?
46  void MoveHuman(float FrameTime, const Vector3fT& WishVelocity, const Vector3fT& WishVelLadder, bool WishJump);
47 
48  /// Returns the current velocity.
49  const Vector3dT& GetVelocity() const { return m_Velocity.Get(); }
50 
51  // Base class overrides.
53  const char* GetName() const { return "PlayerPhysics"; }
54  void UpdateDependencies(EntityT* Entity);
55 
56 
57  // The TypeSys related declarations for this class.
58  const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
59  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
60  static const cf::TypeSys::TypeInfoT TypeInfo;
61 
62 
63  protected:
64 
65  // The Lua API methods of this class.
66  static int MoveHuman(lua_State* LuaState);
67  static int toString(lua_State* LuaState);
68 
69  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
70  static const char* DocClass;
71  static const cf::TypeSys::MethsDocT DocMethods[];
72  static const cf::TypeSys::VarsDocT DocVars[];
73 
74 
75  private:
76 
77  enum PosCatT { InAir, OnSolid };
78 
79  PosCatT CategorizePosition() const;
80  void ApplyFriction(double FrameTime, PosCatT PosCat);
81  void ApplyAcceleration(double FrameTime, PosCatT PosCat, const Vector3dT& WishVelocity);
82  void ApplyGravity(double FrameTime, PosCatT PosCat);
83  void FlyMove(double TimeLeft);
84  void GroundMove(double FrameTime);
85  void MoveHuman(float FrameTime, const Vector3dT& WishVelocity, const VectorT& WishVelLadder, bool WishJump);
86 
87  TypeSys::VarT<Vector3dT> m_Velocity; ///< The current velocity of the entity.
88  TypeSys::VarT<BoundingBox3dT> m_Dimensions; ///< The bounding box of the entity (relative to the origin).
89  TypeSys::VarT<double> m_StepHeight; ///< The maximum height that the entity can climb in one step.
90  TypeSys::VarT<bool> m_OldWishJump; ///< Only jump if the jump key was *not* pressed in the previous frame.
91 
92  const cf::ClipSys::ClipWorldT* m_ClipWorld;
93  const cf::ClipSys::ClipModelT* m_IgnoreClipModel;
94  Vector3dT m_Origin;
95  Vector3dT m_Vel;
96  cf::ClipSys::TraceBoxT m_DimSolid;
97  };
98  }
99 }
100 
101 #endif
void MoveHuman(float FrameTime, const Vector3fT &WishVelocity, const Vector3fT &WishVelLadder, bool WishJump)
This is the main method of this component: It advances the entity's origin according to the laws of s...
Definition: CompPlayerPhysics.cpp:84
A clip model represents an object in the world against which clipping queries can be performed...
Definition: ClipModel.hpp:31
const Vector3dT & GetVelocity() const
Returns the current velocity.
Definition: CompPlayerPhysics.hpp:49
ComponentPlayerPhysicsT()
The constructor.
Definition: CompPlayerPhysics.cpp:46
const char * GetName() const
Returns the name of this component.
Definition: CompPlayerPhysics.hpp:53
This class represents game entities, which are the basic elements of a world.
Definition: Entity.hpp:53
This is a "wrapper" around a normal C++ variable.
Definition: SetCompVar.hpp:15
This class represents a convex solid in the shape of a (bounding-)box.
Definition: TraceSolid.hpp:105
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompPlayerPhysics.hpp:69
The clip world manages all the clip models that exist in a world (their "union"). ...
Definition: ClipWorld.hpp:27
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
void UpdateDependencies(EntityT *Entity)
This method is called whenever something "external" to this component has changed: ...
Definition: CompPlayerPhysics.cpp:112
ComponentPlayerPhysicsT * Clone() const
The virtual copy constructor.
Definition: CompPlayerPhysics.cpp:106
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54
This component implements human player physics for its entity.
Definition: CompPlayerPhysics.hpp:27