diff --git a/modules/particle.otpa b/modules/particle.otpa index 9b5f316c..f23e82ec 100644 --- a/modules/particle.otpa +++ b/modules/particle.otpa @@ -1,92 +1,22 @@ ParticleSystem - // continuous fire, yellow - Emitter - position: 100 100 - duration: 8 - delay: 0.1 - burstRate: 0.03 - burstCount: 5 + GoToPointAffector + destination: 150 150 - particle-min-duration: 0.6 - particle-max-duration: 0.8 - particle-position-radius: 0 - - particle-velocity: 96 - particle-min-velocity-angle: 310 - particle-max-velocity-angle: 340 - - particle-acceleration: 0 - - particle-size: 24 24 - particle-color: #ffff0044 - particle-texture: circle2.png - - // fire ball, up side, orange Emitter position: 100 100 - duration: 0.5 - delay: 0.1 - burstRate: 0.01 - burstCount: 6 - - particle-min-duration: 0.1 - particle-max-duration: 0.3 - particle-position-radius: 0 - - particle-velocity: 128 - particle-min-velocity-angle: 360 - particle-max-velocity-angle: 380 - - particle-acceleration: 512 - particle-acceleration-angle: 270 - - particle-size: 24 24 - particle-color: #ffa50044 - particle-texture: circle2.png - - // fire ball, down side, orange - Emitter - position: 100 100 - duration: 0.5 - delay: 0.1 - burstRate: 0.01 - burstCount: 6 - - particle-min-duration: 0.1 - particle-max-duration: 0.3 - particle-position-radius: 0 - - particle-velocity: 128 - particle-min-velocity-angle: 270 - particle-max-velocity-angle: 290 - - particle-acceleration: 512 - particle-acceleration-angle: 45 - - particle-size: 24 24 - particle-color: #ffa50044 - particle-texture: circle2.png - - // smoke - Emitter - position: 100 100 - duration: 9 - burstRate: 0.03 + burstRate: 2 burstCount: 1 - particle-min-duration: 0.6 - particle-max-duration: 1 + particle-duration: 1 particle-position-radius: 0 - particle-velocity: 64 - particle-min-velocity-angle: 290 - particle-max-velocity-angle: 360 + particle-velocity: 32 + particle-velocity-angle: 0 particle-acceleration: 0 particle-size: 24 24 - particle-color: #cccccc22 + particle-color: #ffff0044 particle-texture: circle2.png - diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 3ee09aec..67610682 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -146,6 +146,7 @@ SET(framework_SOURCES ${framework_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/graphics/particlemanager.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particlesystem.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particleemitter.cpp + ${CMAKE_CURRENT_LIST_DIR}/graphics/particleaffector.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particle.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/shader.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/shaderprogram.cpp diff --git a/src/framework/graphics/declarations.h b/src/framework/graphics/declarations.h index 9b0c3391..6c94fdab 100644 --- a/src/framework/graphics/declarations.h +++ b/src/framework/graphics/declarations.h @@ -37,6 +37,7 @@ class ShaderProgram; class PainterShaderProgram; class Particle; class ParticleEmitter; +class ParticleAffector; class ParticleSystem; typedef std::weak_ptr TextureWeakPtr; @@ -53,6 +54,7 @@ typedef std::shared_ptr ShaderProgramPtr; typedef std::shared_ptr PainterShaderProgramPtr; typedef std::shared_ptr ParticlePtr; typedef std::shared_ptr ParticleEmitterPtr; +typedef std::shared_ptr ParticleAffectorPtr; typedef std::shared_ptr ParticleSystemPtr; typedef std::vector ShaderList; diff --git a/src/framework/graphics/particle.cpp b/src/framework/graphics/particle.cpp index 841d02ff..7dce5bdb 100644 --- a/src/framework/graphics/particle.cpp +++ b/src/framework/graphics/particle.cpp @@ -27,7 +27,7 @@ Particle::Particle(const Point& pos, const Size& size, const PointF& velocity, const PointF& acceleration, float duration, const Color& color, TexturePtr texture) { m_rect = Rect(pos, size); - m_pos = PointF(pos.x, pos.y); + m_position = PointF(pos.x, pos.y); m_size = size; m_velocity = velocity; m_acceleration = acceleration; @@ -66,10 +66,10 @@ void Particle::update() // update position PointF delta = m_velocity * elapsedTime; delta.y *= -1; // painter orientate Y axis in the inverse direction - m_pos += delta; + m_position += delta; // update acceleration m_velocity += m_acceleration * elapsedTime; - m_rect.moveTo((int)m_pos.x, (int)m_pos.y); + m_rect.moveTo((int)m_position.x, (int)m_position.y); } diff --git a/src/framework/graphics/particle.h b/src/framework/graphics/particle.h index cf6b600e..71f49399 100644 --- a/src/framework/graphics/particle.h +++ b/src/framework/graphics/particle.h @@ -35,10 +35,16 @@ public: bool hasFinished() { return m_finished; } + PointF getPosition() { return m_position; } + PointF getVelocity() { return m_velocity; } + + void setPos(const PointF& position) { m_position = position; } + void setVelocity(const PointF& velocity) { m_velocity = velocity; } + private: Color m_color; TexturePtr m_texture; - PointF m_pos; + PointF m_position; PointF m_velocity; PointF m_acceleration; Size m_size; diff --git a/src/framework/graphics/particleaffector.cpp b/src/framework/graphics/particleaffector.cpp new file mode 100644 index 00000000..bb25bc58 --- /dev/null +++ b/src/framework/graphics/particleaffector.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2010-2011 OTClient + * + * 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 + +void Gravity270Affector::update(const ParticlePtr& particle, double elapsedTime) +{ + // earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant + const float gravity = 9.8 * 32; + + PointF velocity = particle->getVelocity(); + velocity += PointF(0, -gravity * elapsedTime); + + particle->setVelocity(velocity); +} + +void Gravity315Affector::update(const ParticlePtr& particle, double elapsedTime) +{ + // earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant + const float gravity = 9.8 * 32; + + PointF velocity = particle->getVelocity(); + velocity += PointF(gravity * elapsedTime * cos(7 * Fw::pi / 4), gravity * elapsedTime * sin(7 * Fw::pi / 4)); + + particle->setVelocity(velocity); +} + +bool GoToPointAffector::load(const OTMLNodePtr& node) +{ + for(const OTMLNodePtr& childNode : node->children()) { + if(childNode->tag() == "destination") + m_destination = childNode->value(); + else if(childNode->tag() == "rotate-speed-percent") + m_rotateSpeedPercent = childNode->value(); + } + return true; +} + +void GoToPointAffector::update(const ParticlePtr& particle, double elapsedTime) +{ + // must change velocity angle, keeping modulus. + + + +} diff --git a/src/framework/graphics/particleaffector.h b/src/framework/graphics/particleaffector.h new file mode 100644 index 00000000..2ab5afc1 --- /dev/null +++ b/src/framework/graphics/particleaffector.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2010-2011 OTClient + * + * 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. + */ + +#ifndef PARTICLEAFFECTOR_H +#define PARTICLEAFFECTOR_H + +#include "declarations.h" +#include + +class ParticleAffector { +public: + virtual bool load(const OTMLNodePtr&) { return true; } + virtual void update(const ParticlePtr&, double) {} +}; + +class Gravity270Affector : public ParticleAffector { +public: + void update(const ParticlePtr& particle, double elapsedTime); +}; + +class Gravity315Affector : public ParticleAffector { +public: + void update(const ParticlePtr& particle, double elapsedTime); +}; + +class GoToPointAffector : public ParticleAffector { +public: + bool load(const OTMLNodePtr& node); + void update(const ParticlePtr& particle, double elapsedTime); + +private: + Point m_destination; + float m_rotateSpeedPercent; +}; + +#endif diff --git a/src/framework/graphics/particlemanager.cpp b/src/framework/graphics/particlemanager.cpp index 86680ab5..c71ec4ff 100644 --- a/src/framework/graphics/particlemanager.cpp +++ b/src/framework/graphics/particlemanager.cpp @@ -35,7 +35,7 @@ bool ParticleManager::load(const std::string& filename) if(node->tag() == "ParticleSystem") { ParticleSystemPtr particleSystem = ParticleSystemPtr(new ParticleSystem); particleSystem->load(node); - m_particleSystems.push_back(particleSystem); + m_systems.push_back(particleSystem); } } return true; @@ -47,17 +47,17 @@ bool ParticleManager::load(const std::string& filename) void ParticleManager::render() { - for(auto it = m_particleSystems.begin(), end = m_particleSystems.end(); it != end; ++it) + for(auto it = m_systems.begin(), end = m_systems.end(); it != end; ++it) (*it)->render(); } void ParticleManager::update() { - for(auto it = m_particleSystems.begin(), end = m_particleSystems.end(); it != end;) { + for(auto it = m_systems.begin(), end = m_systems.end(); it != end;) { const ParticleSystemPtr& particleSystem = *it; if(particleSystem->hasFinished()) { - it = m_particleSystems.erase(it); + it = m_systems.erase(it); continue; } diff --git a/src/framework/graphics/particlemanager.h b/src/framework/graphics/particlemanager.h index d904f164..ac9d73fe 100644 --- a/src/framework/graphics/particlemanager.h +++ b/src/framework/graphics/particlemanager.h @@ -25,6 +25,7 @@ #include "declarations.h" #include "particlesystem.h" +#include "particleaffector.h" class ParticleManager { public: @@ -34,7 +35,7 @@ public: void update(); private: - std::list m_particleSystems; + std::list m_systems; }; extern ParticleManager g_particleManager; diff --git a/src/framework/graphics/particlesystem.cpp b/src/framework/graphics/particlesystem.cpp index 6355d482..cea32138 100644 --- a/src/framework/graphics/particlesystem.cpp +++ b/src/framework/graphics/particlesystem.cpp @@ -22,10 +22,12 @@ #include "particle.h" #include "particlesystem.h" +#include ParticleSystem::ParticleSystem() { m_finished = false; + m_lastUpdateTime = g_clock.time(); } bool ParticleSystem::load(const OTMLNodePtr& node) @@ -36,6 +38,21 @@ bool ParticleSystem::load(const OTMLNodePtr& node) emitter->load(childNode); m_emitters.push_back(emitter); } + else if(childNode->tag().find("Affector") != std::string::npos) { + ParticleAffectorPtr affector; + + if(childNode->tag() == "Gravity270Affector") + affector = ParticleAffectorPtr(new Gravity270Affector); + else if(childNode->tag() == "Gravity315Affector") + affector = ParticleAffectorPtr(new Gravity315Affector); + else if(childNode->tag() == "GoToPointAffector") + affector = ParticleAffectorPtr(new GoToPointAffector); + + if(affector) { + affector->load(childNode); + m_affectors.push_back(affector); + } + } } return true; } @@ -53,6 +70,9 @@ void ParticleSystem::render() void ParticleSystem::update() { + float elapsedTime = g_clock.timeElapsed(m_lastUpdateTime); + m_lastUpdateTime = g_clock.time(); + // check if finished if(m_particles.empty() && m_emitters.empty()) { m_finished = true; @@ -77,6 +97,11 @@ void ParticleSystem::update() it = m_particles.erase(it); continue; } + + // pass particles through affectors + for(const ParticleAffectorPtr& particleAffector : m_affectors) + particleAffector->update(particle, elapsedTime); + particle->update(); ++it; } diff --git a/src/framework/graphics/particlesystem.h b/src/framework/graphics/particlesystem.h index 221f9e92..e577112e 100644 --- a/src/framework/graphics/particlesystem.h +++ b/src/framework/graphics/particlesystem.h @@ -25,18 +25,7 @@ #include "declarations.h" #include "particleemitter.h" - -class Affector { -public: - virtual void update() {} -}; - -class Gravity270Affector : public Affector { -public: - void update() { - // earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant - } -}; +#include "particleaffector.h" class ParticleSystem : public std::enable_shared_from_this { public: @@ -54,9 +43,10 @@ public: private: bool m_finished; + double m_lastUpdateTime; std::list m_particles; std::list m_emitters; - std::list m_affectors; + std::list m_affectors; }; #endif