From 56410d9ff28448a73be7a7d115d54c1ee578972c Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sun, 26 Oct 2008 01:46:40 +0000 Subject: Implemented particle emitters with properties which change over time according to wave functions. --- src/CMakeLists.txt | 2 +- src/Makefile.am | 2 +- src/particle.cpp | 2 +- src/particleemitter.cpp | 100 ++++++++++++++++++++++++----------------- src/particleemitter.h | 37 +++++++-------- src/particleemitterprop.h | 112 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/minmax.h | 50 --------------------- 7 files changed, 193 insertions(+), 112 deletions(-) create mode 100644 src/particleemitterprop.h delete mode 100644 src/utils/minmax.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 20f2f761..94ee854a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -265,7 +265,6 @@ SET(SRCS utils/base64.h utils/dtor.h utils/fastsqrt.h - utils/minmax.h utils/strprintf.cpp utils/strprintf.h utils/tostring.h @@ -328,6 +327,7 @@ SET(SRCS particle.h particleemitter.cpp particleemitter.h + particleemitterprop.h player.cpp player.h player_relations.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 340f1947..867953ab 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -215,7 +215,6 @@ tmw_SOURCES = gui/widgets/resizegrip.cpp \ utils/base64.h \ utils/dtor.h \ utils/fastsqrt.h \ - utils/minmax.h \ utils/strprintf.cpp \ utils/strprintf.h \ utils/tostring.h \ @@ -278,6 +277,7 @@ tmw_SOURCES = gui/widgets/resizegrip.cpp \ particle.h \ particleemitter.cpp \ particleemitter.h \ + particleemitterprop.h \ player.cpp \ player.h \ player_relations.cpp \ diff --git a/src/particle.cpp b/src/particle.cpp index 8591838f..1521e75b 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -181,7 +181,7 @@ Particle::update() e++ ) { - Particles newParticles = (*e)->createParticles(); + Particles newParticles = (*e)->createParticles(mLifetimePast); for ( ParticleIterator p = newParticles.begin(); p != newParticles.end(); p++ diff --git a/src/particleemitter.cpp b/src/particleemitter.cpp index 816a5d28..bb4a7c6e 100644 --- a/src/particleemitter.cpp +++ b/src/particleemitter.cpp @@ -74,20 +74,22 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map * if (name == "position-x") { - mParticlePosX = readMinMax(propertyNode, 0.0f); + mParticlePosX = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "position-y") { - mParticlePosY = readMinMax(propertyNode, 0.0f); + mParticlePosY = readParticleEmitterProp(propertyNode, 0.0f); mParticlePosY.minVal *= SIN45; mParticlePosY.maxVal *= SIN45; + mParticlePosY.changeAmplitude *= SIN45; } else if (name == "position-z") { - mParticlePosZ = readMinMax(propertyNode, 0.0f); + mParticlePosZ = readParticleEmitterProp(propertyNode, 0.0f); mParticlePosZ.minVal *= SIN45; mParticlePosZ.maxVal *= SIN45; + mParticlePosZ.changeAmplitude *= SIN45; } else if (name == "image") { @@ -101,70 +103,72 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map * } else if (name == "horizontal-angle") { - mParticleAngleHorizontal = readMinMax(propertyNode, 0.0f); + mParticleAngleHorizontal = readParticleEmitterProp(propertyNode, 0.0f); mParticleAngleHorizontal.minVal *= DEG_RAD_FACTOR; mParticleAngleHorizontal.maxVal *= DEG_RAD_FACTOR; + mParticleAngleHorizontal.changeAmplitude *= DEG_RAD_FACTOR; } else if (name == "vertical-angle") { - mParticleAngleVertical = readMinMax(propertyNode, 0.0f); + mParticleAngleVertical = readParticleEmitterProp(propertyNode, 0.0f); mParticleAngleVertical.minVal *= DEG_RAD_FACTOR; mParticleAngleVertical.maxVal *= DEG_RAD_FACTOR; + mParticleAngleVertical.changeAmplitude *= DEG_RAD_FACTOR; } else if (name == "power") { - mParticlePower = readMinMax(propertyNode, 0.0f); + mParticlePower = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "gravity") { - mParticleGravity = readMinMax(propertyNode, 0.0f); + mParticleGravity = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "randomnes" || name == "randomness") // legacy bug { - mParticleRandomness = readMinMax(propertyNode, 0); + mParticleRandomness = readParticleEmitterProp(propertyNode, 0); } else if (name == "bounce") { - mParticleBounce = readMinMax(propertyNode, 0.0f); + mParticleBounce = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "lifetime") { - mParticleLifetime = readMinMax(propertyNode, 0); + mParticleLifetime = readParticleEmitterProp(propertyNode, 0); mParticleLifetime.minVal += 1; } else if (name == "output") { - mOutput = readMinMax(propertyNode, 0); + mOutput = readParticleEmitterProp(propertyNode, 0); mOutput.maxVal +=1; } else if (name == "output-pause") { - mOutputPause = readMinMax(propertyNode, 0); - mOutputPauseLeft = mOutputPause.value(); + mOutputPause = readParticleEmitterProp(propertyNode, 0); + mOutputPauseLeft = mOutputPause.value(0); } else if (name == "acceleration") { - mParticleAcceleration = readMinMax(propertyNode, 0.0f); + mParticleAcceleration = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "die-distance") { - mParticleDieDistance = readMinMax(propertyNode, 0.0f); + mParticleDieDistance = readParticleEmitterProp(propertyNode, 0.0f); } else if (name == "momentum") { - mParticleMomentum = readMinMax(propertyNode, 1.0f); + mParticleMomentum = readParticleEmitterProp(propertyNode, 1.0f); } else if (name == "fade-out") { - mParticleFadeOut = readMinMax(propertyNode, 0); + mParticleFadeOut = readParticleEmitterProp(propertyNode, 0); } else if (name == "fade-in") { - mParticleFadeIn = readMinMax(propertyNode, 0); + mParticleFadeIn = readParticleEmitterProp(propertyNode, 0); } else if (name == "alpha") { - mParticleAlpha = readMinMax(propertyNode, 1.0f); + mParticleAlpha = readParticleEmitterProp(propertyNode, 1.0f); } else if (name == "follow-parent") { @@ -299,21 +303,35 @@ ParticleEmitter::~ParticleEmitter() } -template MinMax -ParticleEmitter::readMinMax(xmlNodePtr propertyNode, T def) +template ParticleEmitterProp +ParticleEmitter::readParticleEmitterProp(xmlNodePtr propertyNode, T def) { - MinMax retval; + ParticleEmitterProp retval; def = (T) XML::getFloatProperty(propertyNode, "value", (double) def); retval.set((T) XML::getFloatProperty(propertyNode, "min", (double) def), (T) XML::getFloatProperty(propertyNode, "max", (double) def)); + std::string change = XML::getProperty(propertyNode, "change-func", "none"); + T amplitude = (T) XML::getFloatProperty(propertyNode, "change-amplitude", 0.0); + int period = XML::getProperty(propertyNode, "change-period", 0); + int phase = XML::getProperty(propertyNode, "change-phase", 0); + if (change == "saw" || change == "sawtooth") { + retval.setFunction(FUNC_SAW, amplitude, period, phase); + } else if (change == "sine" || change == "sinewave") { + retval.setFunction(FUNC_SINE, amplitude, period, phase); + } else if (change == "triangle") { + retval.setFunction(FUNC_TRIANGLE, amplitude, period, phase); + } else if (change == "square"){ + retval.setFunction(FUNC_SQUARE, amplitude, period, phase); + } + return retval; } std::list -ParticleEmitter::createParticles() +ParticleEmitter::createParticles(int tick) { std::list newParticles; @@ -322,9 +340,9 @@ ParticleEmitter::createParticles() mOutputPauseLeft--; return newParticles; } - mOutputPauseLeft = mOutputPause.value(); + mOutputPauseLeft = mOutputPause.value(tick); - for (int i = mOutput.value(); i > 0; i--) + for (int i = mOutput.value(tick); i > 0; i--) { // Limit maximum particles if (Particle::particleCount > Particle::maxCount) break; @@ -346,33 +364,33 @@ ParticleEmitter::createParticles() newParticle->setPosition( - mParticlePosX.value(), - mParticlePosY.value(), - mParticlePosZ.value()); + mParticlePosX.value(tick), + mParticlePosY.value(tick), + mParticlePosZ.value(tick)); - float angleH = mParticleAngleHorizontal.value(); - float angleV = mParticleAngleVertical.value(); - float power = mParticlePower.value(); + float angleH = mParticleAngleHorizontal.value(tick); + float angleV = mParticleAngleVertical.value(tick); + float power = mParticlePower.value(tick); newParticle->setVelocity( cos(angleH) * cos(angleV) * power, sin(angleH) * cos(angleV) * power, sin(angleV) * power); - newParticle->setRandomness(mParticleRandomness.value()); - newParticle->setGravity(mParticleGravity.value()); - newParticle->setBounce(mParticleBounce.value()); + newParticle->setRandomness(mParticleRandomness.value(tick)); + newParticle->setGravity(mParticleGravity.value(tick)); + newParticle->setBounce(mParticleBounce.value(tick)); newParticle->setFollow(mParticleFollow); newParticle->setDestination(mParticleTarget, - mParticleAcceleration.value(), - mParticleMomentum.value() + mParticleAcceleration.value(tick), + mParticleMomentum.value(tick) ); - newParticle->setDieDistance(mParticleDieDistance.value()); + newParticle->setDieDistance(mParticleDieDistance.value(tick)); - newParticle->setLifetime(mParticleLifetime.value()); - newParticle->setFadeOut(mParticleFadeOut.value()); - newParticle->setFadeIn(mParticleFadeIn.value()); - newParticle->setAlpha(mParticleAlpha.value()); + newParticle->setLifetime(mParticleLifetime.value(tick)); + newParticle->setFadeOut(mParticleFadeOut.value(tick)); + newParticle->setFadeIn(mParticleFadeIn.value(tick)); + newParticle->setAlpha(mParticleAlpha.value(tick)); for (std::list::iterator i = mParticleChildEmitters.begin(); i != mParticleChildEmitters.end(); diff --git a/src/particleemitter.h b/src/particleemitter.h index 5cf3fd46..4ef088f6 100644 --- a/src/particleemitter.h +++ b/src/particleemitter.h @@ -27,10 +27,11 @@ #include #include "utils/xml.h" -#include "utils/minmax.h" #include "resources/animation.h" +#include "particleemitterprop.h" + class Image; class Map; class Particle; @@ -66,7 +67,7 @@ class ParticleEmitter * Spawns new particles * @return: a list of created particles */ - std::list createParticles(); + std::list createParticles(int tick); /** * Sets the target of the particles that are created @@ -76,50 +77,50 @@ class ParticleEmitter { mParticleTarget = target; }; private: - template MinMax readMinMax(xmlNodePtr propertyNode, T def); + template ParticleEmitterProp readParticleEmitterProp(xmlNodePtr propertyNode, T def); /** * initial position of particles: */ - MinMax mParticlePosX, mParticlePosY, mParticlePosZ; + ParticleEmitterProp mParticlePosX, mParticlePosY, mParticlePosZ; /** * initial vector of particles: */ - MinMax mParticleAngleHorizontal, mParticleAngleVertical; + ParticleEmitterProp mParticleAngleHorizontal, mParticleAngleVertical; /** * Initial velocity of particles */ - MinMax mParticlePower; + ParticleEmitterProp mParticlePower; /* * Vector changing of particles: */ - MinMax mParticleGravity; - MinMax mParticleRandomness; - MinMax mParticleBounce; + ParticleEmitterProp mParticleGravity; + ParticleEmitterProp mParticleRandomness; + ParticleEmitterProp mParticleBounce; bool mParticleFollow; /* * Properties of targeting particles: */ Particle *mParticleTarget; - MinMax mParticleAcceleration; - MinMax mParticleDieDistance; - MinMax mParticleMomentum; + ParticleEmitterProp mParticleAcceleration; + ParticleEmitterProp mParticleDieDistance; + ParticleEmitterProp mParticleMomentum; /* * Behavior over time of the particles: */ - MinMax mParticleLifetime; - MinMax mParticleFadeOut; - MinMax mParticleFadeIn; + ParticleEmitterProp mParticleLifetime; + ParticleEmitterProp mParticleFadeOut; + ParticleEmitterProp mParticleFadeIn; Map *mMap; /**< Map the particles are spawned on */ - MinMax mOutput; /**< Number of particles spawned per update */ - MinMax mOutputPause; /**< Pause in frames between two spawns */ + ParticleEmitterProp mOutput; /**< Number of particles spawned per update */ + ParticleEmitterProp mOutputPause; /**< Pause in frames between two spawns */ int mOutputPauseLeft; /* @@ -127,7 +128,7 @@ class ParticleEmitter */ Image *mParticleImage; /**< Particle image, if used */ Animation mParticleAnimation; /**< Filename of particle animation file */ - MinMax mParticleAlpha; /**< Opacity of the graphical representation of the particles */ + ParticleEmitterProp mParticleAlpha; /**< Opacity of the graphical representation of the particles */ /** List of emitters the spawned particles are equipped with */ std::list mParticleChildEmitters; diff --git a/src/particleemitterprop.h b/src/particleemitterprop.h new file mode 100644 index 00000000..e645c169 --- /dev/null +++ b/src/particleemitterprop.h @@ -0,0 +1,112 @@ +/* + * The Mana World + * Copyright 2006 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include +#include + +/** + * Returns a random numeric value that is larger than or equal min and smaller + * than max + */ + +enum ChangeFunc +{ + FUNC_NONE, + FUNC_SINE, + FUNC_SAW, + FUNC_TRIANGLE, + FUNC_SQUARE +}; + +template struct ParticleEmitterProp +{ + ParticleEmitterProp(): + changeFunc(FUNC_NONE) + { + } + + void set(T min, T max) + { + minVal=min; maxVal=max; + } + + void set(T val) + { + set(val, val); + } + + void setFunction(ChangeFunc func, T amplitude, int period, int phase) + { + changeFunc = func; + changeAmplitude = amplitude; + changePeriod = period; + changePhase = phase; + } + + T value(int tick) + { + tick += changePhase; + T val = (T) (minVal + (maxVal - minVal) * (rand() / ((double) RAND_MAX + 1))); + + switch (changeFunc) + { + case FUNC_SINE: + val += (T) std::sin(M_PI * 2 * ((double)(tick%changePeriod) / (double)changePeriod)) * changeAmplitude; + break; + case FUNC_SAW: + val += (T) (changeAmplitude * ((double)(tick%changePeriod) / (double)changePeriod)) * 2 - changeAmplitude; + break; + case FUNC_TRIANGLE: + if ((tick%changePeriod) * 2 < changePeriod) + { + val += changeAmplitude - (T)((tick%changePeriod) / (double)changePeriod) * changeAmplitude * 4; + } else { + val += changeAmplitude * -3 + (T)((tick%changePeriod) / (double)changePeriod) * changeAmplitude * 4; + // I have no idea why this works but it does + } + break; + case FUNC_SQUARE: + if ((tick%changePeriod) * 2 < changePeriod) + { + val += changeAmplitude; + } else { + val -= changeAmplitude; + } + break; + case FUNC_NONE: + default: + //nothing + break; + } + + return val; + } + + T minVal; + T maxVal; + + ChangeFunc changeFunc; + T changeAmplitude; + int changePeriod; + int changePhase; +}; diff --git a/src/utils/minmax.h b/src/utils/minmax.h deleted file mode 100644 index 427c5da7..00000000 --- a/src/utils/minmax.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * The Mana World - * Copyright 2006 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#include - -/** - * Returns a random numeric value that is larger than or equal min and smaller - * than max - */ - -template struct MinMax -{ - void set(T min, T max) - { - minVal=min; maxVal=max; - } - - void set(T val) - { - set(val, val); - } - - T value() - { - return (T)(minVal + (maxVal - minVal) * (rand() / ((double) RAND_MAX + 1))); - } - - T minVal; - T maxVal; -}; -- cgit v1.2.3-60-g2f50