Cafu Engine
GameEntity.hpp
1 /*
2 =================================================================================
3 This file is part of Cafu, the open-source game engine and graphics engine
4 for multiplayer, cross-platform, real-time 3D action.
5 Copyright (C) 2002-2013 Carsten Fuchs Software.
6 
7 Cafu is free software: you can redistribute it and/or modify it under the terms
8 of the GNU General Public License as published by the Free Software Foundation,
9 either version 3 of the License, or (at your option) any later version.
10 
11 Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17 
18 For support and more information about Cafu, visit us at <http://www.cafu.de>.
19 =================================================================================
20 */
21 
22 #ifndef CAFU_GAME_ENTITY_HPP_INCLUDED
23 #define CAFU_GAME_ENTITY_HPP_INCLUDED
24 
25 #include "Math3D/BoundingBox.hpp"
26 #include "Templates/Pointer.hpp"
27 
28 
29 namespace cf { namespace GameSys { class GameWorldI; } }
30 namespace cf { namespace Network { class InStreamT; } }
31 namespace cf { namespace Network { class OutStreamT; } }
32 namespace cf { namespace TypeSys { class TypeInfoT; } }
33 
34 
35 /// This is the interface that the client and server use to access and work with game entities.
36 /// It is the only means by which the Cafu engine "knows" the game entities.
37 ///
38 /// When the Cafu engine (client or server) needs a game entity, it uses the GameI interface to ask the game
39 /// implementation to create one: it is solely up to the game implementation to create concrete instances.
40 class GameEntityI : public RefCountedT
41 {
42  public:
43 
44  /// The virtual destructor.
45  virtual ~GameEntityI() { }
46 
47  /// Returns the proper type info for this entity.
48  virtual const cf::TypeSys::TypeInfoT* GetType() const=0;
49 
50  /// Let the entity know that it is about to be removed from the map.
51  /// At this time, the only purpose of this method is to give the entity a chance to break cycles
52  /// of IntrusivePtrT<> to itself that would otherwise prevent it from being destructed.
53  virtual void NotifyLeaveMap()=0;
54 
55  /// Returns the dimensions of this entity.
56  virtual const BoundingBox3dT& GetDimensions() const=0;
57 
58  /// Returns the orientation angles of the entity itself.
59  /// Used for computing the light source and eye positions in entity (model) space.
60  /// TODO: Both the signature as well as the implementation of this method are temporary,
61  /// and fully expected to change later.
62  virtual void GetBodyOrientation(unsigned short& h, unsigned short& p, unsigned short& b) const=0;
63 
64  /// Writes the current state of this entity into the given stream.
65  /// This method is called to send the state of the entity over the network or to save it to disk.
66  ///
67  /// The implementation calls DoSerialize(), that derived classes override to add their own data.
68  ///
69  /// Note that this method is the twin of Deserialize(), whose implementation it must match.
70  virtual void Serialize(cf::Network::OutStreamT& Stream) const=0;
71 
72  /// Reads the state of this entity from the given stream, and updates the entity accordingly.
73  /// This method is called after the state of the entity has been received over the network,
74  /// has been loaded from disk, or must be "reset" for the purpose of (re-)prediction.
75  ///
76  /// The implementation calls DoDeserialize(), that derived classes override to read their own data.
77  /// It also calls ProcessEvent() (overridden by derived classes) for any received events.
78  ///
79  /// Note that this method is the twin of Serialize(), whose implementation it must match.
80  ///
81  /// @param Stream
82  /// The stream to read the state data from.
83  ///
84  /// @param IsIniting
85  /// Used to indicate that the call is part of the construction / first-time initialization of the entity.
86  /// The implementation will use this to not wrongly process the event counters, interpolation, etc.
87  virtual void Deserialize(cf::Network::InStreamT& Stream, bool IsIniting)=0;
88 
89 
90  // /// This SERVER-SIDE function is called by the server in order to advance the world one clock-tick.
91  // /// That is, basing on the present (old) state, it is called for computing the next (new) state.
92  // /// 'FrameTime' is the time of the clock-tick, in seconds.
93  // /// 'ServerFrameNr' is the number of the current server frame.
94  // /// >>> IMPORTANT NOTE: In truth, also the CLIENT-SIDE calls this function for the purpose of predicting the local human player entity! <<<
95  // /// >>> As a consequence, special rules apply when this function is called for predicted entities (that is, human player entities). <<<
96  // /// >>> For further details and examples, please refer to the EntHumanPlayerT::Think() function in HumanPlayer.cpp. <<<
97  // virtual void Think(float FrameTime, unsigned long ServerFrameNr)=0;
98 
99 
100  /// This CLIENT-SIDE function is called in order to retrieve light source information about this entity.
101  /// Returns 'true' if this entity is a light source and 'DiffuseColor', 'SpecularColor',
102  /// 'Position' (in world space!), 'Radius' and 'CastsShadows' have been set.
103  /// Returns 'false' if this entity is no light source.
104  /// (In theory, this function might also be called on server-side, from within Think().)
105  virtual bool GetLightSourceInfo(unsigned long& DiffuseColor, unsigned long& SpecularColor, VectorT& Position, float& Radius, bool& CastsShadows) const=0;
106 
107  /// This CLIENT-SIDE function is called by the client in order to get this entity drawn.
108  ///
109  /// Note that it is usually called several times per frame, in order to gather the individual terms of the
110  /// lighting equation: there is a call for the ambient contribution (everything that is independent of a
111  /// lightsource) and for each light source there is a call for the shadows, followed by another call for
112  /// adding the lightsource-dependent contribution (diffuse and specular terms etc.).
113  ///
114  /// Also note that the calling code has properly set up the Cafu Material Systems global lighting parameters
115  /// before calling. That is, the ambient light color, light source position (in model space), radius, diff+spec
116  /// color and the eye position (in model space) are set. However, normally only those parameters that are relevant
117  /// for the current Material Systems rendering action are set: In the AMBIENT rendering action,
118  /// only the ambient colors are set, in the STENCILSHADOW action only the light source position may be set,
119  /// and in the LIGHTING action all parameters except for the ambient light color are set.
120  ///
121  /// @param FirstPersonView is true when the engine has rendered the world from this entities viewpoint,
122  /// e.g. because this is the local players entity.
123  /// @param LodDist is the world-space distance of the entity to the viewer,
124  /// supposed to be used for level-of-detail reductions.
125  virtual void Draw(bool FirstPersonView, float LodDist) const=0;
126 
127  /// This CLIENT-SIDE function is called by the client in order to advance all values of this entity that have
128  /// been registered for interpolation.
129  /// Each concrete entity class determines itself which values are interpolated (e.g. the m_Origin) by calling
130  /// Register() in its constructor: see Register() for details.
131  /// Interpolation is used to "smooth" values in the client video frames between server updates.
132  virtual void Interpolate(float FrameTime)=0;
133 
134  /// This CLIENT-SIDE function is called once per frame, for each entity, after the regular rendering
135  /// (calls to 'Draw()') is completed, in order to provide entities an opportunity to render the HUD,
136  /// employ simple "mini-prediction", triggers sounds, register particles, do other server-independent eye-candy,
137  /// and so on.
138  ///
139  /// @param FrameTime is the time in seconds that it took to render the last (previous) frame.
140  /// @param FirstPersonView is true when the engine has rendered the world from this entities viewpoint,
141  /// e.g. because this is the local players entity.
142  ///
143  /// As it is convenient for current code (dealing with the particle system...), it is guaranteed that there is
144  /// exactly one call to this function with 'FirstPersonView==true', which occurs after the appropriate calls
145  /// for all other entities. This behaviour may change in the future.
146  virtual void PostDraw(float FrameTime, bool FirstPersonView)=0;
147 };
148 
149 #endif
virtual void Serialize(cf::Network::OutStreamT &Stream) const =0
Writes the current state of this entity into the given stream.
virtual void Draw(bool FirstPersonView, float LodDist) const =0
This CLIENT-SIDE function is called by the client in order to get this entity drawn.
This is the interface that the client and server use to access and work with game entities...
Definition: GameEntity.hpp:40
virtual void GetBodyOrientation(unsigned short &h, unsigned short &p, unsigned short &b) const =0
Returns the orientation angles of the entity itself.
virtual void Interpolate(float FrameTime)=0
This CLIENT-SIDE function is called by the client in order to advance all values of this entity that ...
virtual ~GameEntityI()
The virtual destructor.
Definition: GameEntity.hpp:45
virtual bool GetLightSourceInfo(unsigned long &DiffuseColor, unsigned long &SpecularColor, VectorT &Position, float &Radius, bool &CastsShadows) const =0
This CLIENT-SIDE function is called in order to retrieve light source information about this entity...
This class is used for reading data from a StateT instance (deserialization).
Definition: State.hpp:222
virtual const cf::TypeSys::TypeInfoT * GetType() const =0
Returns the proper type info for this entity.
virtual void NotifyLeaveMap()=0
Let the entity know that it is about to be removed from the map.
This class is used for writing data into a StateT instance (serialization).
Definition: State.hpp:96
virtual void Deserialize(cf::Network::InStreamT &Stream, bool IsIniting)=0
Reads the state of this entity from the given stream, and updates the entity accordingly.
virtual void PostDraw(float FrameTime, bool FirstPersonView)=0
This CLIENT-SIDE function is called once per frame, for each entity, after the regular rendering (cal...
virtual const BoundingBox3dT & GetDimensions() const =0
Returns the dimensions of this entity.
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:94
A base class for objects that are reference-counted with IntrusivePtrTs.
Definition: Pointer.hpp:28