Logo Search packages:      
Sourcecode: mana version File versions  Download package

particle.h

/*
 *  The Mana Client
 *  Copyright (C) 2006-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PARTICLE_H
#define PARTICLE_H

#include "guichanfwd.h"
#include "sprite.h"
#include "vector.h"

#include <list>
#include <string>

class Map;
class Particle;
class ParticleEmitter;

typedef std::list<Particle *> Particles;
typedef Particles::iterator ParticleIterator;
typedef std::list<ParticleEmitter *> Emitters;
typedef Emitters::iterator EmitterIterator;

/**
 * A particle spawned by a ParticleEmitter.
 */
00044 class Particle : public Sprite
{
    public:
00047         static const float PARTICLE_SKY; /**< Maximum Z position of particles */
00048         static int fastPhysics;          /**< Mode of squareroot calculation */
00049         static int particleCount;        /**< Current number of particles */
00050         static int maxCount;             /**< Maximum number of particles */
00051         static int emitterSkip;          /**< Duration of pause between two emitter updates in ticks */
00052         static bool enabled;   /**< true when non-crucial particle effects are disabled */

        /**
         * Constructor.
         *
         * @param map the map this particle will add itself to, may be NULL
         */
        Particle(Map *map);

        /**
         * Destructor.
         */
        ~Particle();

        /**
         * Deletes all child particles and emitters.
         */
        void clear();

        /**
         * Gives a particle the properties of an engine root particle and loads
         * the particle-related config settings.
         */
        void setupEngine();

        /**
         * Updates particle position, returns false when the particle should
         * be deleted.
         */
        virtual bool update();

        /**
         * Draws the particle image.
         */
        virtual void draw(Graphics *graphics, int offsetX, int offsetY) const;

        /**
         * Necessary for sorting with the other sprites.
         */
00091         virtual int getPixelY() const
        { return (int) (mPos.y + mPos.z) - 64; }

        /**
         * Sets the map the particle is on.
         */
        void setMap(Map *map);


        /**
         * Creates a blank particle as a child of the current particle
         * Useful for creating target particles
         */
        Particle *createChild();

        /**
         * Creates a child particle that hosts some emitters described in the
         * particleEffectFile.
         */
        Particle *addEffect(const std::string &particleEffectFile,
                            int pixelX, int pixelY, int rotation = 0);

        /**
         * Creates a standalone text particle.
         */
        Particle *addTextSplashEffect(const std::string &text, int x, int y,
                const gcn::Color *color, gcn::Font *font,
                bool outline = false);

        /**
         * Creates a standalone text particle.
         */
        Particle *addTextRiseFadeOutEffect(const std::string &text,
                int x, int y, const gcn::Color *color, gcn::Font *font,
                bool outline = false);

        /**
         * Adds an emitter to the particle.
         */
00130         void addEmitter (ParticleEmitter* emitter)
        { mChildEmitters.push_back(emitter); }

        /**
         * Sets the position in 3 dimensional space in pixels relative to map.
         */
00136         void moveTo(const Vector &pos)
        { moveBy (pos - mPos);}

        /**
         * Sets the position in 2 dimensional space in pixels relative to map.
         */
        void moveTo(float x, float y);

        /**
         * Returns the particle position.
         */
00147         const Vector& getPosition() const
        { return mPos; }

        /**
         * Changes the particle position relative
         */
        void moveBy (const Vector &change);

        /**
         * Sets the time in game ticks until the particle is destroyed.
         */
00158         void setLifetime(int lifetime)
        { mLifetimeLeft = lifetime; mLifetimePast = 0; }

        /**
         * Sets the age of the pixel in game ticks where the particle has
         * faded in completely.
         */
00165         void setFadeOut(int fadeOut)
        { mFadeOut = fadeOut; }

        /**
         * Sets the remaining particle lifetime where the particle starts to
         * fade out.
         */
00172         void setFadeIn(int fadeIn)
        { mFadeIn = fadeIn; }

        /**
         * Sets the alpha value of the particle
         */
00178         void setAlpha(float alpha)
        { mAlpha = alpha; }

        /**
         * Returns the current alpha opacity of the particle.
         */
00184         virtual float getAlpha() const
        { return mAlpha; }

        /**
         * Sets the sprite iterator of the particle on the current map to make
         * it easier to remove the particle from the map when it is destroyed.
         */
00191         void setSpriteIterator(std::list<Sprite*>::iterator spriteIterator)
        { mSpriteIterator = spriteIterator; }

        /**
         * Gets the sprite iterator of the particle on the current map.
         */
        std::list<Sprite*>::iterator
00198         getSpriteIterator() const
        { return mSpriteIterator; }

        /**
         * Sets the current velocity in 3 dimensional space.
         */
00204         void setVelocity(float x, float y, float z)
        { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }

        /**
         * Sets the downward acceleration.
         */
00210         void setGravity(float gravity)
        { mGravity = gravity; }

        /**
         * Sets the ammount of random vector changes
         */
00216         void setRandomness(int r)
        { mRandomness = r; }

        /**
         * Sets the ammount of velocity particles retain after
         * hitting the ground.
         */
00223         void setBounce(float bouncieness)
        { mBounce = bouncieness; }

        /**
         * Sets the flag if the particle is supposed to be moved by its parent
         */
00229         void setFollow(bool follow)
        { mFollow = follow; }

        /**
         * Gets the flag if the particle is supposed to be moved by its parent
         */
00235         bool doesFollow()
        { return mFollow; }

        /**
         * Makes the particle move toward another particle with a
         * given acceleration and momentum
         */
00242         void setDestination(Particle *target, float accel, float moment)
        { mTarget = target; mAcceleration = accel; mMomentum = moment; }

        /**
         * Sets the distance in pixel the particle can come near the target
         * particle before it is destroyed. Does only make sense after a target
         * particle has been set using setDestination.
         */
00250         void setDieDistance(float dist)
        { mInvDieDistance = 1.0f / dist; }

        /**
         * Changes the size of the emitters so that the effect fills a
         * rectangle of this size
         */
        void adjustEmitterSize(int w, int h);

        void setAllowSizeAdjust(bool adjust)
        { mAllowSizeAdjust = adjust; }

        bool isAlive()
        { return mAlive; }

        /**
         * Determines whether the particle and its children are all dead
         */
00268         bool isExtinct()
        { return !isAlive() && mChildParticles.empty(); }

        /**
         * Manually marks the particle for deletion.
         */
00274         void kill()
        { mAlive = false; mAutoDelete = true; }

        /**
         * After calling this function the particle will only request
         * deletion when kill() is called
         */
00281         void disableAutoDelete()
        { mAutoDelete = false; }

        /** We consider particles (at least for now) to be one layer-sprites */
00285         virtual int getNumberOfLayers() const
        { return 1; }

    protected:
        /** Calculates the current alpha transparency taking current fade status into account*/
        float getCurrentAlpha() const;

00292         bool mAlive;                /**< Is the particle supposed to be drawn and updated?*/
00293         Vector mPos;                /**< Position in pixels relative to map. */
00294         int mLifetimeLeft;          /**< Lifetime left in game ticks*/
00295         int mLifetimePast;          /**< Age of the particle in game ticks*/
00296         int mFadeOut;               /**< Lifetime in game ticks left where fading out begins*/
00297         int mFadeIn;                /**< Age in game ticks where fading in is finished*/
00298         float mAlpha;               /**< Opacity of the graphical representation of the particle */

        // generic properties
00301         bool mAutoDelete;           /**< May the particle request its deletion by the parent particle? */
00302         Map *mMap;                  /**< Map the particle is on. */
00303         std::list<Sprite*>::iterator mSpriteIterator;   /**< iterator of the particle on the current map */
00304         Emitters mChildEmitters;    /**< List of child emitters. */
00305         Particles mChildParticles;  /**< List of particles controlled by this particle */
00306         bool mAllowSizeAdjust;      /**< Can the effect size be adjusted by the object props in the map file? */

        // dynamic particle
00309         Vector mVelocity;           /**< Speed in pixels per game-tick. */
00310         float mGravity;             /**< Downward acceleration in pixels per game-tick. */
00311         int mRandomness;            /**< Ammount of random vector change */
00312         float mBounce;              /**< How much the particle bounces off when hitting the ground */
00313         bool mFollow;               /**< is this particle moved when its parent particle moves? */

        // follow-point particles
00316         Particle *mTarget;          /**< The particle that attracts this particle*/
00317         float mAcceleration;        /**< Acceleration towards the target particle in pixels per game-tick*/
00318         float mInvDieDistance;      /**< Distance in pixels from the target particle that causes the destruction of the particle*/
00319         float mMomentum;            /**< How much speed the particle retains after each game tick*/
};

extern Particle *particleEngine;

#endif

Generated by  Doxygen 1.6.0   Back to index