-
-
Save rumanwork/4690139 to your computer and use it in GitHub Desktop.
Animation class for Futile framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class AnimatableSprite : FSprite { | |
private string _file; | |
private int _currentIndex; | |
private int _maxFrames; | |
private Animation _currentAnimation; | |
private float _passedTime = 0.0f; | |
private FAtlasElement _defaultFrame; | |
private IDictionary<string, Animation> animations = new Dictionary<string, Animation>(); | |
public AnimatableSprite(string defaultFrame, string animationsFile): | |
base(defaultFrame) | |
{ | |
_defaultFrame = Futile.atlasManager.GetElementWithName(defaultFrame); | |
_file = animationsFile; | |
LoadAnimations(); | |
} | |
private void LoadAnimations() | |
{ | |
TextAsset animationsFile = (TextAsset)Resources.Load(_file, typeof(TextAsset)); | |
Dictionary<string, object> anims = animationsFile.text.dictionaryFromJson(); | |
foreach(KeyValuePair<string, object> anim in anims) | |
{ | |
float duration = 1.0f; | |
string name = anim.Key; | |
Animation newAnim = new Animation(name); | |
Dictionary<string, object> animInfo = (Dictionary<string, object>)anim.Value; | |
if(animInfo.ContainsKey("duration")) | |
{ | |
duration = float.Parse(animInfo["duration"].ToString()); | |
} | |
List<object> frames = (List<object>)animInfo["frames"]; | |
if(frames[0].GetType() == typeof(string)) | |
{ | |
foreach(string elementName in frames) | |
{ | |
var animationFrame = new AnimationFrame(){ | |
Duration = duration, | |
Element = Futile.atlasManager.GetElementWithName(elementName) | |
}; | |
newAnim.Frames.Add(animationFrame); | |
} | |
} | |
else | |
{ | |
foreach(IDictionary<string, object> frame in frames) | |
{ | |
var animationFrame = new AnimationFrame(){ | |
Duration = float.Parse(frame["duration"].ToString()), | |
Element = Futile.atlasManager.GetElementWithName(frame["element"].ToString()) | |
}; | |
newAnim.Frames.Add(animationFrame); | |
} | |
} | |
animations.Add(name, newAnim); | |
} | |
} | |
public void RunAnimation(string animation) | |
{ | |
_currentAnimation = animations[animation]; | |
_maxFrames = _currentAnimation.Frames.Count; | |
_currentIndex = 0; | |
} | |
public void StopAnimation() | |
{ | |
_currentAnimation = null; | |
element = _defaultFrame; | |
} | |
override public void HandleAddedToStage() | |
{ | |
Futile.instance.SignalUpdate += HandleUpdate; | |
base.HandleAddedToStage(); | |
} | |
override public void HandleRemovedFromStage() | |
{ | |
Futile.instance.SignalUpdate -= HandleUpdate; | |
base.HandleRemovedFromStage(); | |
} | |
public void HandleUpdate() | |
{ | |
if(_currentAnimation == null) | |
return; | |
_passedTime += Time.deltaTime; | |
if(_passedTime > _currentAnimation.Frames[_currentIndex].Duration) | |
{ | |
element = _currentAnimation.Frames[_currentIndex].Element; | |
_currentIndex++; | |
if(_currentIndex >= _maxFrames) | |
_currentIndex = 0; | |
_passedTime = 0.0f; | |
} | |
} | |
public int AnimationCount { | |
get { return animations.Count; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Animation | |
{ | |
public IList<AnimationFrame> Frames { get;set;} | |
public string Name { get; set; } | |
public Animation(string name) | |
{ | |
Name = name; | |
Frames = new List<AnimationFrame>(); | |
} | |
} | |
public class AnimationFrame | |
{ | |
public float Duration { get; set; } | |
public FAtlasElement Element { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"flying" : { | |
"duration": 0.05, | |
"frames": [ | |
"laserbeam_1.png", | |
"laserbeam_2.png", | |
"laserbeam_3.png", | |
"laserbeam_4.png", | |
"laserbeam_5.png", | |
"laserbeam_6.png" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AnimatableSprite laser = new AnimatableSprite("legs8.png", "Json/laser"); | |
laser.RunAnimation("laserbeam_1.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment