VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
MagneticAttractorLogic.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 MAGNETICATTRACTORLOGIC_H
18 #define MAGNETICATTRACTORLOGIC_H
19 
20 #include <Box2D/Box2D.h>
21 #include <Engine/Logic.h>
22 #include <Engine/audio/SoundEffectInstance.h>
23 
24 class Body;
25 
41 class MagneticAttractorLogic : public Logic {
42  Q_OBJECT
43 
49  Q_PROPERTY(bool hasTargetInRange READ hasTargetInRange WRITE setHasTargetInRange
57  Q_PROPERTY(bool on READ isOn WRITE setOn NOTIFY onChanged)
61  Q_PROPERTY(float strength READ getStrength WRITE setStrength NOTIFY strengthChanged)
69  Q_PROPERTY(float softness READ getSoftness WRITE setSoftness NOTIFY softnessChanged)
80  Q_PROPERTY(QString activeSound READ getActiveSound WRITE setActiveSound
82 public:
87 
91  bool hasTargetInRange() const { return mHasTargetInRange; }
96  void setHasTargetInRange(bool value);
100  bool isOn() const { return mOn; }
105  void setOn(bool value);
109  float getStrength() const { return mStrength; }
114  void setStrength(float value);
118  float getSoftness() const { return mSoftness; }
123  void setSoftness(float value);
127  Body* getSensorBody() const { return mSensorBody; }
132  void setSensorBody(Body* value);
136  const QString& getActiveSound() const { return mActiveSound; }
141  void setActiveSound(const QString& value);
142 
146  Q_INVOKABLE QPointF getCurrentMagneticForce() const;
147 
151  virtual void init() override;
155  virtual void update() override;
156 
157 signals:
165  void onChanged();
169  void strengthChanged();
173  void softnessChanged();
177  void sensorBodyChanged();
181  void activeSoundChanged();
182 
183 private slots:
184  void onActiveChanged();
185 
186 private:
187  Body* getEffectiveSensorBody() const;
188 
189  bool mHasTargetInRange = false;
190  bool mOn = false;
191  float mStrength = 20.0f;
192  float mSoftness = 1.0f;
193  Body* mSensorBody = nullptr;
194  QString mActiveSound;
195 
196  bool mWasOn = false;
197  float mCachedGravityScale = 1.0f;
198  bool mCachedMagnetism = false;
199  b2Vec2 mCurrentMagneticForce = b2Vec2(0.0f, 0.0f);
200  SoundEffectInstance mActiveSoundInstance;
201 
202  static const float SOFTEN_STRENGTH;
203 };
204 Q_DECLARE_METATYPE(MagneticAttractorLogic*)
205 
206 #endif // MAGNETICATTRACTORLOGIC_H
float strength
Overall strength of the magnet.
Definition: MagneticAttractorLogic.h:61
An instance of a sound effect.
Definition: SoundEffectInstance.h:36
A QObject container for a Box2D b2Body.
Definition: Body.h:57
void activeSoundChanged()
Emitted when activeSound changed.
bool on
Whether or not this magnet is on, and should attract magnetic Bodys.
Definition: MagneticAttractorLogic.h:57
Q_INVOKABLE QPointF getCurrentMagneticForce() const
Returns the sum of magnetic forces currently being exerted by this magnet.
void setActiveSound(const QString &value)
Sets activeSound.
virtual void update() override
Computes and exerts forces on magnetic objects, if on is true.
void softnessChanged()
Emitted when softness changed.
const QString & getActiveSound() const
Returns activeSound.
Definition: MagneticAttractorLogic.h:136
QString activeSound
Path to sound to play looped while the magnet is on.
Definition: MagneticAttractorLogic.h:81
void onChanged()
Emitted when on changed.
void strengthChanged()
Emitted when strength changed.
void setHasTargetInRange(bool value)
Sets hasTargetInRange.
bool isOn() const
Returns on.
Definition: MagneticAttractorLogic.h:100
float getStrength() const
Returns strength.
Definition: MagneticAttractorLogic.h:109
Non-visual entities in the QML item tree that define behavior for their parent Actor.
Definition: Logic.h:31
void setSensorBody(Body *value)
Sets sensorBody.
float getSoftness() const
Returns softness.
Definition: MagneticAttractorLogic.h:118
void setSoftness(float value)
Sets softness.
void setOn(bool value)
Sets on.
float softness
Amount of softness (linearity) of the magnet's attraction.
Definition: MagneticAttractorLogic.h:69
bool hasTargetInRange() const
Returns hasTargetInRange.
Definition: MagneticAttractorLogic.h:91
Body sensorBody
Body to use as a sensor for this magnet.
Definition: MagneticAttractorLogic.h:76
void hasTargetInRangeChanged()
Emitted when hasTargetInRange changed.
void sensorBodyChanged()
Emitted when sensorBody changed.
Logic which pulls its parent actor in the direction of magnetic Bodys and also attracting them toward...
Definition: MagneticAttractorLogic.h:41
Body * getSensorBody() const
Returns sensorBody.
Definition: MagneticAttractorLogic.h:127
virtual void init() override
Implements Logic::init() to perform additional initialization.
void setStrength(float value)
Sets strength.