2011-12-16 02:59:29 +01:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-12-16 02:59:29 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "particle.h"
|
|
|
|
#include "particleaffector.h"
|
|
|
|
#include <framework/core/clock.h>
|
|
|
|
|
2011-12-17 03:28:51 +01:00
|
|
|
ParticleAffector::ParticleAffector()
|
|
|
|
{
|
|
|
|
m_active = false;
|
|
|
|
m_finished = false;
|
|
|
|
m_delay = 0;
|
|
|
|
m_duration = 0;
|
|
|
|
m_elapsedTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleAffector::update(double elapsedTime)
|
|
|
|
{
|
2011-12-18 03:21:12 +01:00
|
|
|
if(m_duration >= 0 && m_elapsedTime >= m_duration + m_delay) {
|
2011-12-17 03:28:51 +01:00
|
|
|
m_finished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_active && m_elapsedTime > m_delay)
|
|
|
|
m_active = true;
|
|
|
|
|
|
|
|
m_elapsedTime += elapsedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticleAffector::load(const OTMLNodePtr& node)
|
|
|
|
{
|
|
|
|
float minDelay = 0, maxDelay = 0;
|
|
|
|
float minDuration = -1, maxDuration = -1;
|
|
|
|
|
|
|
|
for(const OTMLNodePtr& childNode : node->children()) {
|
|
|
|
if(childNode->tag() == "delay") {
|
|
|
|
minDelay = childNode->value<float>();
|
|
|
|
maxDelay = childNode->value<float>();
|
|
|
|
}
|
|
|
|
else if(childNode->tag() == "min-delay")
|
|
|
|
minDelay = childNode->value<float>();
|
|
|
|
else if(childNode->tag() == "max-delay")
|
|
|
|
maxDelay = childNode->value<float>();
|
|
|
|
|
|
|
|
if(childNode->tag() == "duration") {
|
|
|
|
minDuration = childNode->value<float>();
|
|
|
|
maxDuration = childNode->value<float>();
|
|
|
|
}
|
|
|
|
else if(childNode->tag() == "min-duration")
|
|
|
|
minDuration = childNode->value<float>();
|
|
|
|
else if(childNode->tag() == "max-duration")
|
|
|
|
maxDuration = childNode->value<float>();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_delay = Fw::randomRange(minDelay, maxDelay);
|
|
|
|
m_duration = Fw::randomRange(minDuration, maxDuration);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-16 03:48:10 +01:00
|
|
|
bool GravityAffector::load(const OTMLNodePtr& node)
|
|
|
|
{
|
2011-12-17 03:28:51 +01:00
|
|
|
if(!ParticleAffector::load(node))
|
|
|
|
return false;
|
|
|
|
|
2011-12-16 03:48:10 +01:00
|
|
|
m_angle = 270 * DEG_TO_RAD;
|
|
|
|
m_gravity = 9.8;
|
2011-12-16 02:59:29 +01:00
|
|
|
|
2011-12-16 03:48:10 +01:00
|
|
|
for(const OTMLNodePtr& childNode : node->children()) {
|
|
|
|
if(childNode->tag() == "angle")
|
|
|
|
m_angle = childNode->value<float>() * DEG_TO_RAD;
|
|
|
|
else if(childNode->tag() == "gravity")
|
|
|
|
m_gravity = childNode->value<float>();
|
|
|
|
}
|
|
|
|
return true;
|
2011-12-16 02:59:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 03:28:51 +01:00
|
|
|
void GravityAffector::updateParticle(const ParticlePtr& particle, double elapsedTime)
|
2011-12-16 02:59:29 +01:00
|
|
|
{
|
2011-12-17 03:28:51 +01:00
|
|
|
if(!m_active)
|
|
|
|
return;
|
|
|
|
|
2011-12-16 02:59:29 +01:00
|
|
|
PointF velocity = particle->getVelocity();
|
2011-12-16 03:48:10 +01:00
|
|
|
velocity += PointF(m_gravity * elapsedTime * cos(m_angle), m_gravity * elapsedTime * sin(m_angle));
|
2011-12-16 02:59:29 +01:00
|
|
|
particle->setVelocity(velocity);
|
|
|
|
}
|
|
|
|
|
2011-12-16 03:48:10 +01:00
|
|
|
bool AttractionAffector::load(const OTMLNodePtr& node)
|
2011-12-16 02:59:29 +01:00
|
|
|
{
|
2011-12-17 03:28:51 +01:00
|
|
|
if(!ParticleAffector::load(node))
|
|
|
|
return false;
|
|
|
|
|
2011-12-16 03:48:10 +01:00
|
|
|
m_acceleration = 32;
|
2011-12-19 04:24:35 +01:00
|
|
|
m_reduction = 0;
|
|
|
|
m_repelish = false;
|
2011-12-16 03:48:10 +01:00
|
|
|
|
2011-12-16 02:59:29 +01:00
|
|
|
for(const OTMLNodePtr& childNode : node->children()) {
|
2011-12-18 03:21:12 +01:00
|
|
|
if(childNode->tag() == "position")
|
2012-01-30 01:00:12 +01:00
|
|
|
m_position = childNode->value<Point>();
|
2011-12-16 03:48:10 +01:00
|
|
|
else if(childNode->tag() == "acceleration")
|
|
|
|
m_acceleration = childNode->value<float>();
|
2011-12-16 07:24:04 +01:00
|
|
|
else if(childNode->tag() == "velocity-reduction-percent")
|
|
|
|
m_reduction = childNode->value<float>();
|
2011-12-19 04:24:35 +01:00
|
|
|
else if(childNode->tag() == "repelish")
|
|
|
|
m_repelish = childNode->value<bool>();
|
2011-12-16 02:59:29 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-17 03:28:51 +01:00
|
|
|
void AttractionAffector::updateParticle(const ParticlePtr& particle, double elapsedTime)
|
2011-12-16 02:59:29 +01:00
|
|
|
{
|
2011-12-17 03:28:51 +01:00
|
|
|
if(!m_active)
|
|
|
|
return;
|
2011-12-16 02:59:29 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
PointF pPosition = particle->getPosition();
|
|
|
|
PointF d = PointF(m_position.x - pPosition.x, pPosition.y - m_position.y);
|
2011-12-16 03:48:10 +01:00
|
|
|
if(d.length() == 0)
|
|
|
|
return;
|
2011-12-16 02:59:29 +01:00
|
|
|
|
2011-12-19 04:24:35 +01:00
|
|
|
PointF direction = PointF(1, 1);
|
|
|
|
if(m_repelish)
|
|
|
|
direction = PointF(-1, -1);
|
|
|
|
|
|
|
|
PointF pVelocity = particle->getVelocity() + (d / d.length() * m_acceleration * elapsedTime) * direction;
|
2011-12-16 07:24:04 +01:00
|
|
|
particle->setVelocity(pVelocity - pVelocity * m_reduction/100.0 * elapsedTime);
|
2011-12-16 02:59:29 +01:00
|
|
|
}
|