Repository navigation
Expand file tree
/
Copy pathanimator.js
More file actions
191 lines (154 loc) · 5.26 KB
/
Copy pathanimator.js
File metadata and controls
191 lines (154 loc) · 5.26 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/******
*
*
* @constructor
*/
clippy.Animator = function (el, path, data, sounds) {
this._el = el;
this._data = data;
this._path = path;
this._currentFrameIndex = 0;
this._currentFrame = undefined;
this._exiting = false;
this._currentAnimation = undefined;
this._endCallback = undefined;
this._started = false;
this._sounds = {};
this.currentAnimationName = undefined;
this.preloadSounds(sounds);
this._overlays = [this._el];
var curr = this._el;
this._setupElement(this._el);
for (var i = 1; i < this._data.overlayCount; i++) {
var inner = this._setupElement($('<div></div>'));
curr.append(inner);
this._overlays.push(inner);
curr = inner;
}
};
clippy.Animator.prototype = {
_setupElement:function (el) {
var frameSize = this._data.framesize;
el.css('display', "none");
el.css({width:frameSize[0], height:frameSize[1]});
el.css('background', "url('" + this._path + "/map.png') no-repeat");
return el;
},
animations:function () {
var r = [];
var d = this._data.animations;
for (var n in d) {
r.push(n);
}
return r;
},
preloadSounds:function (sounds) {
for (var i = 0; i < this._data.sounds.length; i++) {
var snd = this._data.sounds[i];
var uri = sounds[snd];
if (!uri) continue;
this._sounds[snd] = new Audio(uri);
}
},
hasAnimation:function (name) {
return !!this._data.animations[name];
},
exitAnimation:function () {
this._exiting = true;
},
showAnimation:function (animationName, stateChangeCallback) {
this._exiting = false;
if (!this.hasAnimation(animationName)) {
return false;
}
this._currentAnimation = this._data.animations[animationName];
this.currentAnimationName = animationName;
if (!this._started) {
this._step();
this._started = true;
}
this._currentFrameIndex = 0;
this._currentFrame = undefined;
this._endCallback = stateChangeCallback;
return true;
},
_draw:function () {
var images = [];
if (this._currentFrame) images = this._currentFrame.images || [];
for (var i = 0; i < this._overlays.length; i++) {
if (i < images.length) {
var xy = images[i];
var bg = -xy[0] + 'px ' + -xy[1] + 'px';
this._overlays[i].css({'background-position':bg, 'display':'block'});
}
else {
this._overlays[i].css('display', 'none');
}
}
},
_getNextAnimationFrame:function () {
if (!this._currentAnimation) return undefined;
// No current frame. start animation.
if (!this._currentFrame) return 0;
var currentFrame = this._currentFrame;
var branching = this._currentFrame.branching;
if (this._exiting && currentFrame.exitBranch !== undefined) {
return currentFrame.exitBranch;
}
else if (branching) {
var rnd = Math.random() * 100;
for (var i = 0; i < branching.branches.length; i++) {
var branch = branching.branches[i];
if (rnd <= branch.weight) {
return branch.frameIndex;
}
rnd -= branch.weight;
}
}
return this._currentFrameIndex + 1;
},
_playSound:function () {
var s = this._currentFrame.sound;
if (!s) return;
var audio = this._sounds[s];
if (audio) audio.play();
},
_atLastFrame:function () {
return this._currentFrameIndex >= this._currentAnimation.frames.length - 1;
},
_step:function () {
if (!this._currentAnimation) return;
var newFrameIndex = Math.min(this._getNextAnimationFrame(), this._currentAnimation.frames.length - 1);
var frameChanged = !this._currentFrame || this._currentFrameIndex !== newFrameIndex;
this._currentFrameIndex = newFrameIndex;
// always switch frame data, unless we're at the last frame of an animation with a useExitBranching flag.
if (!(this._atLastFrame() && this._currentAnimation.useExitBranching)) {
this._currentFrame = this._currentAnimation.frames[this._currentFrameIndex];
}
this._draw();
this._playSound();
this._loop = window.setTimeout($.proxy(this._step, this), this._currentFrame.duration);
// fire events if the frames changed and we reached an end
if (this._endCallback && frameChanged && this._atLastFrame()) {
if (this._currentAnimation.useExitBranching && !this._exiting) {
this._endCallback(this.currentAnimationName, clippy.Animator.States.WAITING);
}
else {
this._endCallback(this.currentAnimationName, clippy.Animator.States.EXITED);
}
}
},
/***
* Pause animation execution
*/
pause:function () {
window.clearTimeout(this._loop);
},
/***
* Resume animation
*/
resume:function () {
this._step();
}
};
clippy.Animator.States = { WAITING:1, EXITED:0 };