tibia-client/src/framework/graphics/particleaffector.cpp

137 lines
4.6 KiB
C++
Raw Normal View History

2011-12-16 02:59:29 +01:00
/*
2017-01-13 11:47:07 +01:00
* Copyright (c) 2010-2017 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>
ParticleAffector::ParticleAffector()
{
m_active = false;
m_finished = false;
m_delay = 0;
m_duration = 0;
m_elapsedTime = 0;
}
2012-03-20 16:17:10 +01:00
void ParticleAffector::update(float elapsedTime)
{
2011-12-18 03:21:12 +01:00
if(m_duration >= 0 && m_elapsedTime >= m_duration + m_delay) {
m_finished = true;
return;
}
if(!m_active && m_elapsedTime > m_delay)
m_active = true;
m_elapsedTime += elapsedTime;
}
void 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>();
else 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 = stdext::random_range(minDelay, maxDelay);
m_duration = stdext::random_range(minDuration, maxDuration);
}
void GravityAffector::load(const OTMLNodePtr& node)
2011-12-16 03:48:10 +01:00
{
ParticleAffector::load(node);
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>();
}
2011-12-16 02:59:29 +01:00
}
2012-03-20 16:17:10 +01:00
void GravityAffector::updateParticle(const ParticlePtr& particle, float elapsedTime)
2011-12-16 02:59:29 +01:00
{
if(!m_active)
return;
2011-12-16 02:59:29 +01:00
PointF velocity = particle->getVelocity();
velocity += PointF(m_gravity * elapsedTime * std::cos(m_angle), m_gravity * elapsedTime * std::sin(m_angle));
2011-12-16 02:59:29 +01:00
particle->setVelocity(velocity);
}
void AttractionAffector::load(const OTMLNodePtr& node)
2011-12-16 02:59:29 +01:00
{
ParticleAffector::load(node);
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
}
}
2012-03-20 16:17:10 +01:00
void AttractionAffector::updateParticle(const ParticlePtr& particle, float elapsedTime)
2011-12-16 02:59:29 +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
}