VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
EmitterLogic.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EMITTERLOGIC_H
18 #define EMITTERLOGIC_H
19 
20 #include <Engine/Logic.h>
21 
35 class EmitterLogic : public Logic {
36  Q_OBJECT
37 
62  Q_PROPERTY(int maxEntityCount READ getMaxEntityCount)
67  Q_PROPERTY(int entityCount READ getEntityCount)
71  Q_PROPERTY(float spawnRate READ getSpawnRate WRITE setSpawnRate NOTIFY spawnRateChanged)
77  Q_PROPERTY(float spawnVelocity READ getSpawnVelocity WRITE setSpawnVelocity
85  Q_PROPERTY(float spawnSpread READ getSpawnSpread WRITE setSpawnSpread NOTIFY spawnSpreadChanged)
93  Q_PROPERTY(float spawnSpray READ getSpawnSpray WRITE setSpawnSpray NOTIFY spawnSprayChanged)
97  Q_PROPERTY(bool spawnRandom READ isSpawnRandom WRITE setSpawnRandom NOTIFY spawnRandomChanged)
101  Q_PROPERTY(float timeOn READ getTimeOn WRITE setTimeOn NOTIFY timeOnChanged)
105  Q_PROPERTY(float timeOff READ getTimeOff WRITE setTimeOff NOTIFY timeOffChanged)
112  Q_PROPERTY(float transitionOnTime READ getTransitionOnTime WRITE setTransitionOnTime
129  Q_PROPERTY(float decayTime READ getDecayTime WRITE setDecayTime NOTIFY decayTimeChanged)
138  Q_PROPERTY(float decayTimeRandomLength READ getDecayTimeRandomLength
146  Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
147 
148 public:
164  float decayTime;
165  };
166 
170  int getRequestedMaxEntityCount() const { return mRequestedMaxEntityCount; }
175  void setRequestedMaxEntityCount(int value);
179  virtual int getSupportedMaxEntityCount() const { return -1; }
183  int getMaxEntityCount() const;
187  virtual int getEntityCount() const { return 0; }
191  float getSpawnRate() const { return mSpawnRate; }
196  void setSpawnRate(float value);
200  float getSpawnVelocity() const { return mSpawnVelocity; }
205  void setSpawnVelocity(float value);
209  float getSpawnSpread() const { return mSpawnSpread; }
214  void setSpawnSpread(float value);
218  float getSpawnSpray() const { return mSpawnSpray; }
223  void setSpawnSpray(float value);
227  bool isSpawnRandom() const { return mSpawnRandom; }
232  void setSpawnRandom(bool value);
236  float getTimeOn() const { return mTimeOn; }
241  void setTimeOn(float value);
245  float getTimeOff() const { return mTimeOff; }
250  void setTimeOff(float value);
254  float getTransitionOnTime() const { return mTransitionOnTime; }
259  void setTransitionOnTime(float value);
263  float getTransitionOffTime() const { return mTransitionOffTime; }
268  void setTransitionOffTime(float value);
272  float getDecayTime() const { return mDecayTime; }
277  void setDecayTime(float value);
281  float getDecayTimeRandomLength() const { return mDecayTimeRandomLength; }
286  void setDecayTimeRandomLength(float value);
290  bool isPaused() const { return mPaused; }
295  void setPaused(bool value);
296 
300  virtual void init() override;
305  virtual void update() override;
306 
307 signals:
315  void spawnRateChanged();
319  void spawnVelocityChanged();
323  void spawnSpreadChanged();
327  void spawnSprayChanged();
331  void spawnRandomChanged();
335  void timeOffChanged();
339  void timeOnChanged();
351  void decayTimeChanged();
359  void pausedChanged();
360 
361 protected:
365  virtual void onPausedChanged() { }
369  SpawnParameters getNextRandomSpawnParameters() const;
375  virtual void emitEntity(const SpawnParameters& parameters);
376 
377 private:
378  void updateHasTimer();
379 
380  int mRequestedMaxEntityCount = 500;
381 
382  float mSpawnRate = 1.0f;
383  float mSpawnVelocity = 5.0f;
384  float mSpawnSpread = 0.0f;
385  float mSpawnSpray = 0.0f;
386  bool mSpawnRandom = true;
387  bool mHasTimer = false;
388  float mTimeOn = 1.0f;
389  float mTimeOff = 0.0f;
390  float mTransitionOnTime = 0.3f;
391  bool mHasTransitionOn = true;
392  float mTransitionOffTime = 0.3f;
393  bool mHasTransitionOff = true;
394  float mDecayTime = 3.33f;
395  float mDecayTimeRandomLength = 1.667f;
396 
397  int mSpawnIndex = 0;
398  float mSpawnRateCounter = 0.0f;
399  float mCycleTimer = 0.0f;
400  bool mPaused = false;
401 
402  static const float SPAWN_OFFSETS[];
403  static const int SPAWN_OFFSET_COUNT;
404  static const float SPRAY_STRENGTH;
405 };
406 Q_DECLARE_METATYPE(EmitterLogic*)
407 
408 #endif // EMITTERLOGIC_H
void transitionOnTimeChanged()
Emitted when transitionOnTime changes.
void setSpawnRandom(bool value)
Sets spawnRandom.
void setSpawnSpread(float value)
Sets spawnSpread.
void timeOnChanged()
Emitted when timeOn changes.
int getRequestedMaxEntityCount() const
Returns requestedMaxEntityCount.
Definition: EmitterLogic.h:170
float spawnRate
Number of entities spawned per frame (i.e. each non-paused call to update()).
Definition: EmitterLogic.h:71
float timeOn
Number of seconds, in game time, the emitter will stay on in one cycle.
Definition: EmitterLogic.h:101
void setSpawnVelocity(float value)
Sets spawnVelocity.
int supportedMaxEntityCount
Class-defined maximum number of entities to spawn.
Definition: EmitterLogic.h:56
void setDecayTime(float value)
Sets decayTime.
int getMaxEntityCount() const
Returns maxEntityCount.
bool paused
Whether or not this emitter should spawn entities.
Definition: EmitterLogic.h:146
float getTransitionOnTime() const
Returns transitionOnTime.
Definition: EmitterLogic.h:254
void decayTimeChanged()
Emitted when decayTime changes.
float getSpawnSpray() const
Returns spawnSpray.
Definition: EmitterLogic.h:218
void setSpawnRate(float value)
Sets spawnRate.
void timeOffChanged()
Emitted when timeOff changes.
bool spawnRandom
true if SpawnParameters are pseudo randomized, or patterned if false.
Definition: EmitterLogic.h:97
float decayTimeRandomLength
Maximum time delta an entity can randomly stay alive if spawnRandom is true.
Definition: EmitterLogic.h:139
void setTransitionOnTime(float value)
Sets transitionOnTime.
Parameters to use for creating and initializing spawned entities.
Definition: EmitterLogic.h:152
float getTimeOff() const
Returns timeOff.
Definition: EmitterLogic.h:245
void spawnVelocityChanged()
Emitted when spawnVelocity changes.
float timeOff
Number of seconds, in game time, the emitter will stay off in one cycle.
Definition: EmitterLogic.h:105
void setTransitionOffTime(float value)
Sets transitionOffTime.
QPointF position
Position, in world space, at which to spawn the entity.
Definition: EmitterLogic.h:156
float getSpawnSpread() const
Returns spawnSpread.
Definition: EmitterLogic.h:209
void setTimeOn(float value)
Sets timeOn.
void decayTimeRandomLengthChanged()
Emitted when decayTimeRandomLength changes.
float spawnSpray
Divergence factor affecting the amount entities will stray from the direction of the emitter...
Definition: EmitterLogic.h:93
int requestedMaxEntityCount
User-defined maximum number of entities to spawn.
Definition: EmitterLogic.h:48
SpawnParameters getNextRandomSpawnParameters() const
Returns a randomized set of SpawnParameters according to EmitterLogic's parameters.
virtual void init() override
Checks that the EmitterLogic has an actor.
float getSpawnRate() const
Returns spawnRate.
Definition: EmitterLogic.h:191
float getSpawnVelocity() const
Returns spawnVelocity.
Definition: EmitterLogic.h:200
float getTransitionOffTime() const
Returns transitionOffTime.
Definition: EmitterLogic.h:263
float transitionOffTime
Number of seconds, in game time, the emitter takes to turn off.
Definition: EmitterLogic.h:121
void setRequestedMaxEntityCount(int value)
Sets requestedMaxEntityCount.
int maxEntityCount
Actual maximum number of entities to spawn, bounded by both requestedMaxEntityCount and supportedMaxE...
Definition: EmitterLogic.h:62
virtual int getSupportedMaxEntityCount() const
Returns supportedMaxEntityCount.
Definition: EmitterLogic.h:179
Non-visual entities in the QML item tree that define behavior for their parent Actor.
Definition: Logic.h:31
float getDecayTime() const
Returns decayTime.
Definition: EmitterLogic.h:272
float decayTime
Number of seconds, in game time, after which the entity should be destroyed.
Definition: EmitterLogic.h:164
void requestedMaxEntityCountChanged()
Emitted when requestedMaxEntityCount changes.
virtual void onPausedChanged()
Called when the emitter is moving to or from a paused state.
Definition: EmitterLogic.h:365
void setTimeOff(float value)
Sets timeOff.
void spawnRandomChanged()
Emitted when spawnRandom changes.
virtual void update() override
Updates the EmitterLogic cycle, calling emitEntity() for any entities that need to be spawned...
float getTimeOn() const
Returns timeOn.
Definition: EmitterLogic.h:236
void pausedChanged()
Emitted when paused changes.
float spawnSpread
Horizontal axis spread to apply to spawned entities.
Definition: EmitterLogic.h:85
Abstract base class to spawn generic entities into the Game based on configurable parameters...
Definition: EmitterLogic.h:35
float transitionOnTime
Number of seconds, in game time, the emitter takes to turn on.
Definition: EmitterLogic.h:113
void spawnSpreadChanged()
Emitted when spawnSpread changes.
void setPaused(bool value)
Sets paused.
bool isSpawnRandom() const
Returns spawnRandom.
Definition: EmitterLogic.h:227
void spawnRateChanged()
Emitted when spawnRate changes.
void transitionOffTimeChanged()
Emitted when transitionOffTime changes.
float spawnVelocity
Forward velocity for spawned entities.
Definition: EmitterLogic.h:78
virtual int getEntityCount() const
Returns entityCount.
Definition: EmitterLogic.h:187
bool isPaused() const
Returns paused.
Definition: EmitterLogic.h:290
virtual void emitEntity(const SpawnParameters &parameters)
Performs the creation and initialization of a spawned entity with parameters.
void setSpawnSpray(float value)
Sets spawnSpray.
float decayTime
Number of seconds, in game time, before the entity is destroyed.
Definition: EmitterLogic.h:129
QPointF velocity
Initial velocity to give the spawned entity.
Definition: EmitterLogic.h:160
void spawnSprayChanged()
Emitted when spawnSpray changes.
int entityCount
Number of spawned entities still in existence.
Definition: EmitterLogic.h:67
float getDecayTimeRandomLength() const
Returns decayTimeRandomLength.
Definition: EmitterLogic.h:281
void setDecayTimeRandomLength(float value)
Sets decayTimeRandomLength.