TDME2  1.9.200
Logic.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <tdme/tdme.h>
8 #include <tdme/engine/Engine.h>
12 #include <tdme/utilities/Time.h>
13 
14 using std::vector;
15 
23 
24 /**
25  * Logic
26  * @author Andreas Drewke
27  */
29 public:
30  struct QueuedSound {
32  const string& id,
33  bool attachedToLogic,
34  const Vector3& position,
35  int64_t timeIssuedAt,
36  int timeDelay,
37  float gain,
38  float pitch,
39  bool ignoreIfPlaying
40  ):
41  id(id),
46  gain(gain),
47  pitch(pitch),
49  {}
50  string id;
53  int64_t timeIssuedAt;
54  int timeDelay;
55  float gain;
56  float pitch;
58  };
59 
60  struct SignalStruct {
62  const string& signal,
63  const vector<EngineMiniScript::ScriptVariable>& arguments
65  string signal;
66  vector<EngineMiniScript::ScriptVariable> arguments;
67  };
68  vector<SignalStruct> signals;
69 
70  // forbid class copy
72 
73  /**
74  * Public constructor
75  * @param context context
76  * @param id id
77  * @param handlingHIDInput is handling hid input
78  */
79  Logic(Context* context, const string& id, bool handlingHIDInput);
80 
81  /**
82  * Destructor
83  */
84  virtual ~Logic();
85 
86  /**
87  * @return context
88  */
89  inline Context* getContext() {
90  return context;
91  }
92 
93  /**
94  * @return id
95  */
96  inline const string& getId() {
97  return id;
98  }
99 
100  /**
101  * Update engine
102  */
103  virtual void updateEngine() = 0;
104 
105  /**
106  * Update logic
107  */
108  virtual void updateLogic() = 0;
109 
110  /**
111  * @return if handling human interface devices input
112  */
113  inline bool isHandlingHIDInput() {
114  return handlingHIDInput;
115  }
116 
117  /**
118  * Handle HID events
119  * @param mouseEvents mouse events
120  * @param keyEvents keyboard events
121  */
122  virtual void handleHIDEvents(vector<GUIMouseEvent>& mouseEvents, vector<GUIKeyboardEvent>& keyEvents);
123 
124  /**
125  * On logic added
126  */
127  virtual void onLogicAdded();
128 
129  /**
130  * On logics processed
131  */
132  virtual void onLogicsProcessed();
133 
134  /**
135  * TODO: updated sounds that are already playing
136  * Play sound
137  * @param id sound id
138  * @param delay delay
139  * @param gain gain
140  * @param pitch pitch
141  * @param ignoreIfPlaying ignore if already playing
142  */
143  inline void playSound(const string& id, int delay = 0, float gain = 1.0, float pitch = 1.0, bool ignoreIfPlaying = false) {
144  queuedSounds.emplace_back(
145  id,
146  true,
147  Vector3(),
148  Time::getCurrentMillis(),
149  delay,
150  gain,
151  pitch,
152  ignoreIfPlaying
153  );
154  }
155 
156  /**
157  * TODO: updated sounds that are already playing
158  * Play sound with position
159  * @param id sound id
160  * @param position position
161  * @param delay delay
162  * @param gain gain
163  * @param pitch pitch
164  * @param ignoreIfPlaying ignore if already playing
165  */
166  inline void playSound(const string& id, const Vector3& position, int delay = 0, float gain = 1.0, float pitch = 1.0, bool ignoreIfPlaying = false) {
167  queuedSounds.emplace_back(
168  id,
169  false,
170  position,
171  Time::getCurrentMillis(),
172  delay,
173  gain,
174  pitch,
175  ignoreIfPlaying
176  );
177  }
178 
179  /**
180  * @return queued sounds associated with this game logic
181  */
182  inline const vector<QueuedSound>& getQueuedSounds() {
183  return queuedSounds;
184  }
185 
186  /**
187  * Clear queued sounds
188  */
189  inline void clearQueuedSounds() {
190  queuedSounds.clear();
191  }
192 
193  /**
194  * Set queued sounds
195  * @param queuedSounds queued sounds
196  */
197  inline void setQueuedSounds(const vector<QueuedSound>& queuedSounds) {
198  this->queuedSounds = queuedSounds;
199  }
200 
201  /**
202  * Returns if a signal is in signal queue
203  * @return has signal
204  */
205  inline bool hasSignal() {
206  return signals.empty() == false;
207  }
208 
209  /**
210  * Add signal
211  * @param signal signal
212  * @param arguments arguments
213  */
214  inline void addSignal(const string& signal, const vector<EngineMiniScript::ScriptVariable>& arguments) {
215  signals.emplace_back(
216  signal,
217  arguments
218  );
219  }
220 
221  /**
222  * Get signal name from first signal in signal queue
223  * @return signal name
224  */
225  inline const string getSignalName() {
226  if (signals.empty() == true) return string();
227  return signals[0].signal;
228  }
229 
230  /**
231  * Get signal argument count
232  * @return signal argument count
233  */
234  inline int getSignalArgumentCount() {
235  if (signals.empty() == true) return 0;
236  return signals[0].arguments.size();
237  }
238 
239  /**
240  * Get signal argument
241  * @return signal argument at given index
242  */
243  inline EngineMiniScript::ScriptVariable getSignalArgument(int idx) {
244  if (signals.empty() == true) return EngineMiniScript::ScriptVariable();
245  if (idx >= signals[0].arguments.size()) return EngineMiniScript::ScriptVariable();
246  return signals[0].arguments[idx];
247  }
248 
249  /**
250  * Remove first signal from signal queue
251  */
252  inline void removeSignal() {
253  if (signals.empty() == true) return;
254  signals.erase(signals.begin());
255  }
256 
257  /**
258  * Log state
259  */
260  virtual void logState(int indent = 0);
261 
262 protected:
264  string id;
266  vector<QueuedSound> queuedSounds;
267 
268 };
Engine main class.
Definition: Engine.h:131
void playSound(const string &id, int delay=0, float gain=1.0, float pitch=1.0, bool ignoreIfPlaying=false)
TODO: updated sounds that are already playing Play sound.
Definition: Logic.h:143
const vector< QueuedSound > & getQueuedSounds()
Definition: Logic.h:182
const string getSignalName()
Get signal name from first signal in signal queue.
Definition: Logic.h:225
void setQueuedSounds(const vector< QueuedSound > &queuedSounds)
Set queued sounds.
Definition: Logic.h:197
virtual void onLogicAdded()
On logic added.
Definition: Logic.cpp:23
virtual ~Logic()
Destructor.
Definition: Logic.cpp:16
bool hasSignal()
Returns if a signal is in signal queue.
Definition: Logic.h:205
Context * getContext()
Definition: Logic.h:89
int getSignalArgumentCount()
Get signal argument count.
Definition: Logic.h:234
void playSound(const string &id, const Vector3 &position, int delay=0, float gain=1.0, float pitch=1.0, bool ignoreIfPlaying=false)
TODO: updated sounds that are already playing Play sound with position.
Definition: Logic.h:166
virtual void updateEngine()=0
Update engine.
void addSignal(const string &signal, const vector< EngineMiniScript::ScriptVariable > &arguments)
Add signal.
Definition: Logic.h:214
Logic(Context *context, const string &id, bool handlingHIDInput)
Public constructor.
Definition: Logic.cpp:10
virtual void updateLogic()=0
Update logic.
void removeSignal()
Remove first signal from signal queue.
Definition: Logic.h:252
void clearQueuedSounds()
Clear queued sounds.
Definition: Logic.h:189
vector< QueuedSound > queuedSounds
Definition: Logic.h:266
EngineMiniScript::ScriptVariable getSignalArgument(int idx)
Get signal argument.
Definition: Logic.h:243
virtual void onLogicsProcessed()
On logics processed.
Definition: Logic.cpp:27
virtual void logState(int indent=0)
Log state.
Definition: Logic.cpp:31
virtual void handleHIDEvents(vector< GUIMouseEvent > &mouseEvents, vector< GUIKeyboardEvent > &keyEvents)
Handle HID events.
Definition: Logic.cpp:19
const string & getId()
Definition: Logic.h:96
vector< SignalStruct > signals
Definition: Logic.h:68
Dynamic physics world class.
Definition: World.h:38
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Time utility class.
Definition: Time.h:20
QueuedSound(const string &id, bool attachedToLogic, const Vector3 &position, int64_t timeIssuedAt, int timeDelay, float gain, float pitch, bool ignoreIfPlaying)
Definition: Logic.h:31
vector< EngineMiniScript::ScriptVariable > arguments
Definition: Logic.h:66
SignalStruct(const string &signal, const vector< EngineMiniScript::ScriptVariable > &arguments)
Definition: Logic.h:61
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6