Cafu Engine
CompScript.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_SCRIPT_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_SCRIPT_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 
12 
13 namespace cf
14 {
15  namespace GameSys
16  {
17  /// This component runs custom Lua script code, implementing the behaviour of the entity in the game world.
18  /// The script code can be loaded from a separate file, or it can be entered and kept directly in the component.
19  ///
20  /// Keeping the script code in a separate file is useful when it is re-used with several entity instances
21  /// or in several maps.
22  /// Keeping the script code directly in the component is useful for short scripts that are unique to a single
23  /// map and entity instance.
24  /// Note that both options can also be combined: The script code from a file is loaded first, and immediate
25  /// code can be used to augment it (for example to "configure" it).
27  {
28  public:
29 
30  /// The constructor.
32 
33  /// The copy constructor.
34  /// @param Comp The component to create a copy of.
36 
37  /// This function is used for posting an event of the given type.
38  /// It's the twin of `PostEvent(lua_State* LuaState)` below, but allows also C++ code to post events.
39  /// It is assumed that in the script (e.g. "HumanPlayer.lua"), script method InitEventTypes() has been called.
40  ///
41  /// The event is automatically sent from the entity instance on the server to the entity instances
42  /// on the clients, and causes a matching call to the ProcessEvent() callback there.
43  /// The meaning of the event type is up to the script code that implements ProcessEvent().
44  /// Note that events are fully predictable: they work well even in the presence of client prediction.
45  void PostEvent(unsigned int EventType);
46 
47 
48  // Base class overrides.
49  ComponentScriptT* Clone() const;
50  const char* GetName() const { return "Script"; }
51  unsigned int GetEditorColor() const { return 0x4482FC; }
52  void OnPostLoad(bool OnlyStatic);
53 
54 
55  // The TypeSys related declarations for this class.
56  const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
57  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
58  static const cf::TypeSys::TypeInfoT TypeInfo;
59 
60 
61  protected:
62 
63  // The Lua API methods of this class.
64  static int InitEventTypes(lua_State* LuaState);
65  static int PostEvent(lua_State* LuaState);
66  static int DamageAll(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::MethsDocT DocCallbacks[];
73  static const cf::TypeSys::VarsDocT DocVars[];
74 
75 
76  private:
77 
78  void DoSerialize(cf::Network::OutStreamT& Stream) const /*override*/;
79  void DoDeserialize(cf::Network::InStreamT& Stream, bool IsIniting) /*override*/;
80  void DoServerFrame(float t) /*override*/;
81 
82  TypeSys::VarT<std::string> m_FileName;
83  TypeSys::VarT<std::string> m_ScriptCode;
84 
85  ArrayT<unsigned int> m_EventsCount; ///< A counter for each event type for the number of its occurrences. Serialized (and deserialized) normally along with the entity state.
86  ArrayT<unsigned int> m_EventsRef; ///< A reference counter for each event type for the number of processed occurrences. Never serialized (or deserialized), never reset, strictly growing.
87  };
88  }
89 }
90 
91 #endif
unsigned int GetEditorColor() const
Returns a color that the Map Editor can use to render the representation of this component's entity...
Definition: CompScript.hpp:51
void OnPostLoad(bool OnlyStatic)
This method is called after all entities and their components have been loaded.
Definition: CompScript.cpp:91
This class is used for reading data from a StateT instance (deserialization).
Definition: State.hpp:207
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompScript.hpp:69
const char * GetName() const
Returns the name of this component.
Definition: CompScript.hpp:50
This class is used for writing data into a StateT instance (serialization).
Definition: State.hpp:81
ComponentScriptT()
The constructor.
Definition: CompScript.cpp:52
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
This component runs custom Lua script code, implementing the behaviour of the entity in the game worl...
Definition: CompScript.hpp:26
ComponentScriptT * Clone() const
The virtual copy constructor.
Definition: CompScript.cpp:85
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54
void PostEvent(unsigned int EventType)
This function is used for posting an event of the given type.
Definition: CompScript.cpp:76