Skip to content

Instantly share code, notes, and snippets.

@dan-cooke
Created May 23, 2017 10:26
Show Gist options
  • Save dan-cooke/1164309011aa2acde39a445312a17a15 to your computer and use it in GitHub Desktop.
Save dan-cooke/1164309011aa2acde39a445312a17a15 to your computer and use it in GitHub Desktop.
abstract class AnimationState {
abstract name : string;
sprite: Sprite;
constructor(context? : Sprite, state? : AnimationState){
if(context)
this.sprite = context;
else if(state){
this.sprite = state.sprite;
}
}
isPlaying: boolean;
stop() : void {
this.isPlaying = false;
}
play() : void {
this.isPlaying = true;
}
abstract setDying() : void;
abstract setRunning() : void;
abstract setIdle() : void;
}
class IdleAnimation extends AnimationState{
name: string = "IDLE";
setDying(): void {
this.sprite.animation = new DyingAnimation(this.sprite);
}
setRunning(): void {
this.sprite.animation = new RunningAnimation(this.sprite);
}
setIdle(): void {
//can't set IDLE when already IDLE
return;
}
}
class DyingAnimation extends AnimationState{
name: string = "DYING";
animationTime: number = 5000;
//can't change ANIMATION when dying
constructor(context? : Sprite, state? : AnimationState){
super();
setTimeout(() => {this.kill()} , this.animationTime);
}
kill(){
this.sprite.kill();
}
setDying(): void {
}
setRunning(): void {
}
setIdle(): void {
}
}
class RunningAnimation extends AnimationState{
name: string = "RUNNING";
setDying(): void {
this.sprite.animation = new DyingAnimation(this.sprite);
}
setRunning(): void {
}
setIdle(): void {
this.sprite.animation = new IdleAnimation(this.sprite);
}
}
class Sprite {
private _animationState : AnimationState;
constructor(){
this._animationState = new IdleAnimation(this);
}
get animation(){
return this._animationState;
}
set animation(s){
this._animationState = s;
}
kill(){
//remove game object
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment