Leviathan  0.8.0.0
Leviathan game engine
Event.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "Event.h"
3 
4 #include "Exceptions.h"
5 #include <boost/assign/list_of.hpp>
6 using namespace Leviathan;
7 using namespace std;
8 // ------------------------------------ //
10  Type(type), Data(data)
11 {
12  // Check that types that require values have values //
13  if(!Data) {
14  // Check that the event has data //
16  Type == EVENT_TYPE_TICK) {
17 
18  throw InvalidArgument("Event that requires data, didn't get it");
19  }
20  }
21 }
22 
24 {
25  // Delete our data //
27 }
28 // ------------------------------------ //
30 {
31  return Type;
32 }
33 // ------------------------------------ //
34 DLLEXPORT void Leviathan::Event::AddDataToPacket(sf::Packet& packet) const
35 {
36  // Add the type first //
37  packet << (int)Type;
38 
39  // Add the data object //
40  if(Data)
41  Data->AddDataToPacket(packet);
42 }
43 
45 {
46  // Get the type from the packet //
47  int tmptype;
48 
49  if(!(packet >> tmptype)) {
50 
51  throw InvalidArgument("packet has invalid format");
52  }
53 
54  // Set our type //
55  Type = static_cast<EVENT_TYPE>(tmptype);
56 
57  // Load based on type //
58  switch(Type) {
61  case EVENT_TYPE_TICK: {
62  Data = new IntegerEventData(packet);
63  break;
64  }
66  Data = new ClientInterpolationEventData(packet);
67  break;
68  }
69  default:
70  // No data required //
71  Data = NULL;
72  }
73 }
74 // ------------------------------------ //
76 {
78  return static_cast<ClientInterpolationEventData*>(Data);
79  return NULL;
80 }
81 
83 {
84  if(Type == EVENT_TYPE_TICK || Type == EVENT_TYPE_FRAME_BEGIN ||
85  Type == EVENT_TYPE_FRAME_END)
86  return static_cast<IntegerEventData*>(Data);
87  return NULL;
88 }
89 // ------------------ GenericEvent ------------------ //
91  const std::string& type, const NamedVars& copyvals) :
92  TypeStr(new std::string(type)),
93  Variables(new NamedVars(copyvals))
94 {}
95 
97  std::string* takeownershipstr, NamedVars* takeownershipvars) :
98  TypeStr(takeownershipstr),
99  Variables(takeownershipvars)
100 {}
101 
103  TypeStr(new std::string(type)), Variables(new NamedVars())
104 {}
105 
107 {
108  // release memory //
109  SAFE_DELETE(TypeStr);
110  SAFE_RELEASE(Variables);
111 }
112 // ------------------------------------ //
114 {
115  // Load data from the packet //
116  unique_ptr<std::string> tmpstr(new std::string());
117  if(!(packet >> *tmpstr)) {
118 
119  throw InvalidArgument("packet has invalid format");
120  }
121 
122  // Try to get the named variables //
123  unique_ptr<NamedVars> tmpvars(new NamedVars(packet));
124 
125  // Take the string away from the smart pointer //
126  TypeStr = tmpstr.release();
127 
128  // Take the variables away //
129  Variables = tmpvars.release();
130 }
131 
133 {
134  // Add data to the packet //
135  packet << *TypeStr;
136 
137  Variables->AddDataToPacket(packet);
138 }
139 // ------------------------------------ //
141 {
142  return TypeStr;
143 }
144 
146 {
147  return *TypeStr;
148 }
149 
151 {
152  return *Variables;
153 }
154 
156 {
157  return Variables;
158 }
159 // ------------------------------------ //
161 {
162  Variables->AddRef();
163  return Variables;
164 }
165 // ------------------ ClientInterpolationEventData ------------------ //
166 void ClientInterpolationEventData::CalculatePercentage()
167 {
168 
169  Percentage = TimeInTick / (float)TICKSPEED;
170 
171  // Clamp the value to avoid breaking animations //
172  if(Percentage < 0) {
173 
174  Percentage = 0;
175 
176  } else if(Percentage > 1.f) {
177 
178  Percentage = 1.f;
179  }
180 }
181 
183  TickNumber(tick), TimeInTick(mspassed)
184 {
185  CalculatePercentage();
186 }
187 
189 {
190 
191  packet >> TickNumber >> TimeInTick;
192 
193  if(!packet)
194  throw InvalidArgument("packet for ClientInterpolationEventData is invalid");
195 
196  CalculatePercentage();
197 }
198 
200 {
201 
202  packet << TickNumber << TimeInTick;
203 }
204 // ------------------ IntegerEventData ------------------ //
206 {
207 
208  packet >> IntegerDataValue;
209 
210  if(!packet)
211  throw InvalidArgument("packet has invalid format");
212 }
213 
215  IntegerDataValue(ticknumber)
216 {}
217 
219 {
220  packet << IntegerDataValue;
221 }
222 // ------------------ BaseEventData ------------------ //
DLLEXPORT GenericEvent(sf::Packet &packet)
Constructs this object from a packet.
Definition: Event.cpp:113
DLLEXPORT std::string * GetTypePtr()
Returns the TypeStr ptr.
Definition: Event.cpp:140
EVENT_TYPE Type
Events type.
Definition: Event.h:134
DLLEXPORT NamedVars * GetVariables()
Returns a direct pointer to this objects variables.
Definition: Event.cpp:155
EVENT_TYPE
Engine events that are triggered at certain times.
Definition: Event.h:13
STL namespace.
BaseEventData * Data
Direct pointer to the data.
Definition: Event.h:137
DLLEXPORT std::string GetType() const
Returns the name of the event.
Definition: Event.cpp:145
Data for EVENT_TYPE_ENGINE_TICK and all others that have only int data.
Definition: Event.h:95
constexpr auto TICKSPEED
Number of milliseconds between engine and world ticks.
Definition: Define.h:21
DLLEXPORT IntegerEventData * GetIntegerDataForEvent() const
Gets the data if this is an event that has only one integer data member.
Definition: Event.cpp:82
DLLEXPORT ClientInterpolationEventData(int tick, int mspassed)
Definition: Event.cpp:182
#define SAFE_RELEASE(x)
Definition: Define.h:114
DLLEXPORT const NamedVars GetVariablesConst() const
Gets this event&#39;s variables.
Definition: Event.cpp:150
DLLEXPORT EVENT_TYPE GetType() const
Gets the Type of the event.
Definition: Event.cpp:29
DLLEXPORT IntegerEventData(sf::Packet &packet)
Loads from a packet.
Definition: Event.cpp:205
FORCE_INLINE void AddRef()
virtual void AddDataToPacket(sf::Packet &packet)
Adds this to a packet for retrieving it later.
Definition: Event.cpp:218
DLLEXPORT NamedVars * GetNamedVarsRefCounted()
Proxy for script to get Variables.
Definition: Event.cpp:160
DLLEXPORT Event(sf::Packet &packet)
Loads this event from a packet.
Definition: Event.cpp:44
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
Saves this event to a packet.
Definition: Event.cpp:34
DLLEXPORT ~Event()
Definition: Event.cpp:23
virtual ~BaseEventData()
Definition: Event.cpp:223
DLLEXPORT ClientInterpolationEventData * GetDataForClientInterpolationEvent() const
Definition: Event.cpp:75
DLLEXPORT ~GenericEvent()
Definition: Event.cpp:106
Data for EVENT_TYPE_CLIENT_INTERPOLATION.
Definition: Event.h:68
#define DLLEXPORT
Definition: Include.h:115
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
#define SAFE_DELETE(x)
Definition: Define.h:116
Base type for all event data types.
Definition: Event.h:59
int TickNumber
The current tick to use for interpolation.
Definition: Event.h:81
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
Serializes this event to a packet.
Definition: Event.cpp:132
DLLEXPORT void AddDataToPacket(sf::Packet &packet) override
Adds this to a packet for retrieving it later.
Definition: Event.cpp:199