Specify pause function in constructors of Animation if required, and remove the unused pause function from AnimationWithOffset. Cleanup Animation.cs and reduce code duplication.
214 lines
5.2 KiB
C#
214 lines
5.2 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation. For more information,
|
|
* see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenRA.Support;
|
|
|
|
namespace OpenRA.Graphics
|
|
{
|
|
public class Animation
|
|
{
|
|
public ISpriteSequence CurrentSequence { get; private set; }
|
|
public string Name { get; private set; }
|
|
public bool IsDecoration { get; set; }
|
|
|
|
readonly SequenceProvider sequenceProvider;
|
|
readonly Func<int> facingFunc;
|
|
readonly Func<bool> paused;
|
|
|
|
int frame;
|
|
bool backwards;
|
|
bool tickAlways;
|
|
int timeUntilNextFrame;
|
|
Action tickFunc = () => { };
|
|
|
|
public Animation(World world, string name)
|
|
: this(world, name, () => 0) { }
|
|
|
|
public Animation(World world, string name, Func<int> facingFunc)
|
|
: this(world, name, facingFunc, null) { }
|
|
|
|
public Animation(World world, string name, Func<bool> paused)
|
|
: this(world, name, () => 0, paused) { }
|
|
|
|
public Animation(World world, string name, Func<int> facingFunc, Func<bool> paused)
|
|
{
|
|
sequenceProvider = world.Map.SequenceProvider;
|
|
Name = name.ToLowerInvariant();
|
|
this.facingFunc = facingFunc;
|
|
this.paused = paused;
|
|
}
|
|
|
|
public int CurrentFrame { get { return backwards ? CurrentSequence.Start + CurrentSequence.Length - frame - 1 : frame; } }
|
|
public Sprite Image { get { return CurrentSequence.GetSprite(CurrentFrame, facingFunc()); } }
|
|
|
|
public IEnumerable<IRenderable> Render(WPos pos, WVec offset, int zOffset, PaletteReference palette, float scale)
|
|
{
|
|
var imageRenderable = new SpriteRenderable(Image, pos, offset, CurrentSequence.ZOffset + zOffset, palette, scale, IsDecoration);
|
|
|
|
if (CurrentSequence.ShadowStart >= 0)
|
|
{
|
|
var shadow = CurrentSequence.GetShadow(CurrentFrame, facingFunc());
|
|
var shadowRenderable = new SpriteRenderable(shadow, pos, offset, CurrentSequence.ShadowZOffset + zOffset, palette, scale, true);
|
|
return new IRenderable[] { shadowRenderable, imageRenderable };
|
|
}
|
|
|
|
return new IRenderable[] { imageRenderable };
|
|
}
|
|
|
|
public IEnumerable<IRenderable> Render(WPos pos, PaletteReference palette)
|
|
{
|
|
return Render(pos, WVec.Zero, 0, palette, 1f);
|
|
}
|
|
|
|
public void Play(string sequenceName)
|
|
{
|
|
PlayThen(sequenceName, null);
|
|
}
|
|
|
|
int CurrentSequenceTickOrDefault()
|
|
{
|
|
const int DefaultTick = 40; // 25 fps == 40 ms
|
|
return CurrentSequence != null ? CurrentSequence.Tick : DefaultTick;
|
|
}
|
|
|
|
void PlaySequence(string sequenceName)
|
|
{
|
|
CurrentSequence = GetSequence(sequenceName);
|
|
timeUntilNextFrame = CurrentSequenceTickOrDefault();
|
|
}
|
|
|
|
public void PlayRepeating(string sequenceName)
|
|
{
|
|
backwards = false;
|
|
tickAlways = false;
|
|
PlaySequence(sequenceName);
|
|
|
|
frame = 0;
|
|
tickFunc = () =>
|
|
{
|
|
++frame;
|
|
if (frame >= CurrentSequence.Length)
|
|
frame = 0;
|
|
};
|
|
}
|
|
|
|
public bool ReplaceAnim(string sequenceName)
|
|
{
|
|
if (!HasSequence(sequenceName))
|
|
return false;
|
|
|
|
CurrentSequence = GetSequence(sequenceName);
|
|
timeUntilNextFrame = Math.Min(CurrentSequenceTickOrDefault(), timeUntilNextFrame);
|
|
frame %= CurrentSequence.Length;
|
|
return true;
|
|
}
|
|
|
|
public void PlayThen(string sequenceName, Action after)
|
|
{
|
|
backwards = false;
|
|
tickAlways = false;
|
|
PlaySequence(sequenceName);
|
|
|
|
frame = 0;
|
|
tickFunc = () =>
|
|
{
|
|
++frame;
|
|
if (frame >= CurrentSequence.Length)
|
|
{
|
|
frame = CurrentSequence.Length - 1;
|
|
tickFunc = () => { };
|
|
if (after != null) after();
|
|
}
|
|
};
|
|
}
|
|
|
|
public void PlayBackwardsThen(string sequenceName, Action after)
|
|
{
|
|
PlayThen(sequenceName, after);
|
|
backwards = true;
|
|
}
|
|
|
|
public void PlayFetchIndex(string sequenceName, Func<int> func)
|
|
{
|
|
backwards = false;
|
|
tickAlways = true;
|
|
PlaySequence(sequenceName);
|
|
|
|
frame = func();
|
|
tickFunc = () => frame = func();
|
|
}
|
|
|
|
public void PlayFetchDirection(string sequenceName, Func<int> direction)
|
|
{
|
|
tickAlways = false;
|
|
PlaySequence(sequenceName);
|
|
|
|
frame = 0;
|
|
tickFunc = () =>
|
|
{
|
|
var d = direction();
|
|
if (d > 0 && ++frame >= CurrentSequence.Length)
|
|
frame = 0;
|
|
|
|
if (d < 0 && --frame < 0)
|
|
frame = CurrentSequence.Length - 1;
|
|
};
|
|
}
|
|
|
|
public void Tick()
|
|
{
|
|
if (paused == null || !paused())
|
|
Tick(40); // tick one frame
|
|
}
|
|
|
|
public void Tick(int t)
|
|
{
|
|
if (tickAlways)
|
|
tickFunc();
|
|
else
|
|
{
|
|
timeUntilNextFrame -= t;
|
|
while (timeUntilNextFrame <= 0)
|
|
{
|
|
tickFunc();
|
|
timeUntilNextFrame += CurrentSequenceTickOrDefault();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChangeImage(string newImage, string newAnimIfMissing)
|
|
{
|
|
newImage = newImage.ToLowerInvariant();
|
|
|
|
if (Name != newImage)
|
|
{
|
|
Name = newImage;
|
|
if (!ReplaceAnim(CurrentSequence.Name))
|
|
ReplaceAnim(newAnimIfMissing);
|
|
}
|
|
}
|
|
|
|
public bool HasSequence(string seq) { return sequenceProvider.HasSequence(Name, seq); }
|
|
|
|
public ISpriteSequence GetSequence(string sequenceName)
|
|
{
|
|
return sequenceProvider.GetSequence(Name, sequenceName);
|
|
}
|
|
|
|
public string GetRandomExistingSequence(string[] sequences, MersenneTwister random)
|
|
{
|
|
return sequences.Where(s => HasSequence(s)).RandomOrDefault(random);
|
|
}
|
|
}
|
|
}
|