Hello Guest

Author Topic: How to use Otter.Particle?  (Read 1736 times)

0 Members and 1 Guest are viewing this topic.

badman

  • Member
  • Posts: 3
    • View Profile
How to use Otter.Particle?
« on: April 30, 2015, 02:06:46 AM »
Could someone provide some example usage of Particle class?

I managed to display a moving particle, but I don't know how to control the movement.

Code: [Select]
using Otter;

namespace ParticlesTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var game = new Game();
            game.Start(new ParticleScene(1000, 1000));
        }

class ParticleScene : Scene
{
public Particle particle { get; set; }

public ParticleScene(int width, int height)
            : base(width, height)
{
particle = new Particle(Global.WIDTH * 0.3f, Global.HEIGHT * 0.3f, Assets.PARTICLE_SPRITE, 32, 32);
particle.FinalX = Global.WIDTH*0.7f;
particle.FinalY = Global.HEIGHT*0.7f;
particle.Start();
Add(particle);
}

public override void UpdateLast()
{
base.UpdateLast();
particle.Update();
}

public override void Render()
{
base.Render();
Draw.Entity(particle);
}
}
    }
}
« Last Edit: April 30, 2015, 06:09:39 AM by badman »

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: How to use Otter.Particle?
« Reply #1 on: April 30, 2015, 07:22:08 PM »
Particles are more meant for a "set and forget" style.  If you want something you can control the movement then I recommend making your own entity.  Here's an example of how I use the particles in my games:

Code: [Select]
public static void ExplodeTiny(float x, float y) {
    Scene.Add(new Particle(x, y, Assets.Image.Effects.Explode, 100, 100) {
        FrameCount = 9,
        LifeSpan = Rand.Int(15, 30),
        Angle = Rand.Angle,
        FlipX = Rand.Flip,
        FlipY = Rand.Flip,
        Ease = Ease.ExpoOut,
        ScaleX = Rand.Float(0.5f, 0.7f),
        LockScaleRatio = true
    });
}

Code: [Select]
public static void ExplodeSmall(float x, float y) {
    var bigExplodeCount = 1;
    for (int i = 0; i < bigExplodeCount; i++) {
        Scene.Add(new Particle(x, y, Assets.Image.Effects.Explode, 100, 100) {
            FrameCount = 9,
            OffsetX = Rand.Float(-10, 10),
            OffsetY = Rand.Float(-10, 10),
            SpeedDir = Rand.Angle,
            SpeedLen = Rand.Float(0, 1),
            FinalSpeedLen = 0,
            Layer = O.Layer.Effects,
            LifeSpan = Rand.Int(10, 30),
            ScaleX = Rand.Float(0.95f, 1),
            LockScaleRatio = true,
            Angle = Rand.Angle,
            FlipX = Rand.Flip,
            FlipY = Rand.Flip,
            Ease = Ease.ExpoOut
        });
    }

    var smallExplodeCount = 5;
    var angleOffset = Rand.Angle;
    for (int i = 0; i < smallExplodeCount; i++) {
        Scene.Add(new Particle(x, y, Assets.Image.Effects.ExplodeRing, 100, 100) {
            FrameCount = 6,
            SpeedDir = 360f / smallExplodeCount * i + angleOffset,
            SpeedLen = 3,
            FinalSpeedLen = 0,
            AdvanceSteps = 6,
            Angle = Rand.Angle,
            Layer = O.Layer.Effects + 1,
            LifeSpan = 20,
            LockScaleRatio = true,
            ScaleX = 0.75f,
            FlipX = Rand.Flip,
            FlipY = Rand.Flip
        });

        var angle = Rand.Angle;
        Scene.Add(new Particle(x, y, Assets.Image.Effects.ExplodeRay, 200, 50) {
            FrameCount = 4,
            OriginX = 0,
            OriginY = 25,
            Angle = angle,
            ScaleX = Rand.Float(0.25f, 2),
            ScaleY = Rand.Float(0.5f, 1),
            LifeSpan = Rand.Int(4, 30),
            Delay = Rand.Int(3),
            OffsetX = Util.PolarX(angle, 50),
            OffsetY = Util.PolarY(angle, 50)
        });
    }
}

I just quickly ripped this code from one of my projects and modified it to make it look more general purpose, so it probably won't actually compile, but it should still show how I make use of the Particle class.

You can set the initial and final properties like you've done with the X and Y positions, but beyond that there's not really an easy way to control the movement.  Also, a particle is an Entity, so if you call Update() on it in your UpdateLast() function, you're going to end up updating it twice per frame which might lead to some wacky results.

badman

  • Member
  • Posts: 3
    • View Profile
Re: How to use Otter.Particle?
« Reply #2 on: May 01, 2015, 02:15:54 AM »
Awesome. Thank you very much for explanations. I will play more with this class and maybe I will achieve something interesting...