/
AnimationController.js
153 lines (128 loc) · 3.88 KB
/
AnimationController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Component } from "../core/Component";
import { Debug } from "../core/Debug";
import { AnimationInfo } from "./AnimationInfo";
import { Sprite } from "../display/Sprite";
/**
* A Component which allows to play sprite animations.
*
* @cat animation
* @extends Component
*/
export class AnimationController extends Component {
/**
* Creates an instance of AnimationController
*/
constructor() {
super();
/**
* @private
* @type {Object<string, AnimationInfo>}
*/
this.mAnimations = {};
/**
* @private
* @type {AnimationInfo|null}
*/
this.mCurrentAnim = null;
}
/**
* Returns the AnimationInfo object that exists with the specified name.
*
* @param {string} name The name of the child to return.
* @returns {AnimationInfo} Animation object that exists with the specified name.
*/
getByName(name) {
Debug.assert(name !== null, 'Animation must be set first.');
Debug.assert(this.mAnimations.hasOwnProperty(name), 'Animation must be set first.');
return this.mAnimations[name];
}
/**
* Removes Animation object that exists with the specified name. If animation is playing right now it will be stopped.
*
* @param {string} name The name of the animation to remove.
* @returns {void}
*/
remove(name) {
Debug.assert(name !== null, 'Animation name shall not be null.');
Debug.assert(this.mAnimations.hasOwnProperty(name) === true, 'Unable to find animation.');
let anim = this.mAnimations[name];
if (this.mCurrentAnim !== null && this.mCurrentAnim === anim) {
this.stop();
delete this.mAnimations[name];
}
this.mCurrentAnim = null;
}
/**
* Add the Animation object into the list of animations. If animation with given name already exists exception will be thrown.
*
* @param {string} name The name of animation to update
* @param {Array<Texture>} textures Array of Textures
* @param {number} [fps=14] Frames Per Second
* @param {boolean} [loop=true] Indicated if animation should be started over at the end.
* @return {AnimationInfo} The newly created Animation Object.
*/
add(name, textures, fps = 14, loop = true) {
Debug.assert(textures.length > 0, 'Animation cannot be empty.');
Debug.assert(fps > 0, 'FPS must be greater than 0.');
Debug.assert(this.mAnimations.hasOwnProperty(name) == false, 'Animation with same name already exists');
let anim = new AnimationInfo(this, name, textures, fps, loop);
this.mAnimations[name] = anim;
return anim;
}
/**
* Plays animation that exists with the specified name.
*
* @param {string} name The name of animation to play.
* @return {void}
*/
play(name) {
Debug.assert(this.mAnimations.hasOwnProperty(name), 'Animation must be set first.');
this.mCurrentAnim = this.mAnimations[name];
let texture = this.mCurrentAnim.__play();
let sprite = /** @type {Sprite} */ (this.gameObject);
if (sprite === null)
return;
if (texture !== null)
sprite.texture = texture;
}
/**
* Stops active animation. If no animations are playing at the moment nothing will happen.
*
* @return {void}
*/
stop() {
if (this.mCurrentAnim === null)
return;
this.mCurrentAnim.__stop();
}
/**
* Pauses active animation.
*
* @return {void}
*/
pause() {
if (this.mCurrentAnim === null)
return;
this.mCurrentAnim.__pause();
}
/**
* @inheritDoc
*/
onRender() {
if (this.mCurrentAnim === null)
return;
let newTexture = this.mCurrentAnim.__update();
if (newTexture === null)
return;
let sprite = /** @type {Sprite} */ (this.gameObject);
sprite.texture = newTexture;
}
/**
* Returns current active animation.
*
* @returns {AnimationInfo|null}
*/
get currentAnimation() {
return this.mCurrentAnim;
}
}