forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.js
299 lines (245 loc) · 7.46 KB
/
Animation.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
var Sburb = (function(Sburb){
////////////////////////////////////
//Animation Class
////////////////////////////////////
//Constructor
Sburb.Animation = function(name,sheet,x,y,colSize,rowSize,startPos,length,frameInterval,loopNum,followUp,flipX,flipY){
this.sheet = sheet;
this.x = x;
this.y = y;
this.rowSize = rowSize;
this.colSize = colSize;
this.startPos = startPos;
this.length = length;
this.curInterval = 0;
this.curFrame = 0;
this.numRows = sheet.height/rowSize;
this.numCols = sheet.width/colSize;
this.name = name;
this.loopNum = typeof loopNum == "number"?loopNum:-1;
this.curLoop = 0;
this.followUp = followUp;
this.flipX = flipX?true:false;
this.flipY = flipY?true:false;
if(typeof frameInterval == "string"){
if(frameInterval.indexOf(":")==-1){
this.frameInterval = parseInt(frameInterval);
}else{
var intervals = frameInterval.split(",");
this.frameIntervals = {};
for(var i=0; i<intervals.length; i++){
var pair = intervals[i].split(":");
this.frameIntervals[parseInt(pair[0])] = parseInt(pair[1]);
}
if(!this.frameIntervals[0]){
this.frameIntervals[0] = 1;
}
this.frameInterval = this.frameIntervals[this.curFrame];
}
}else{
this.frameInterval = frameInterval;
}
}
//go to the next frame of the animation
Sburb.Animation.prototype.nextFrame = function() {
this.curFrame++;
if(this.curFrame>=this.length){
if(this.curLoop==this.loopNum){
this.curFrame = this.length-1;
}else{
this.curFrame=0;
if(this.loopNum>=0){
this.curLoop++;
}
}
}
if(this.frameIntervals && this.frameIntervals[this.curFrame]){
this.frameInterval = this.frameIntervals[this.curFrame];
}
}
//update the animation as if a frame of time has elapsed
Sburb.Animation.prototype.update = function(){
this.curInterval++;
while(this.curInterval>this.frameInterval){
this.curInterval-=this.frameInterval;
this.nextFrame();
}
}
//draw the animation
Sburb.Animation.prototype.draw = function(x,y){
var Stage = Sburb.Stage;
var stage = Sburb.stage;
var stageX = Stage.offset?Stage.x:0;
var stageY = Stage.offset?Stage.y:0;
var stageWidth = Stage.width;
var stageHeight = Stage.height;
if(this.flipX){
stageX = -stageX-stageWidth;
x = -x;
}
if(this.flipY){
stageY = -stageY-stageHeight;
y = -y;
}
x= Math.round((this.x+x)/Stage.scaleX)*Stage.scaleX;
y= Math.round((this.y+y)/Stage.scaleY)*Stage.scaleY;
var colNum = ((this.startPos+this.curFrame)%this.numCols);
var rowNum = (Math.floor((this.startPos+this.curFrame-colNum)/this.numRows));
var frameX = colNum*this.colSize;
var frameY = rowNum*this.rowSize;
var drawWidth = this.colSize;
var drawHeight = this.rowSize;
var delta = x-stageX;
if(delta<0){
frameX-=delta;
drawWidth+=delta;
x=stageX;
if(frameX>=this.sheet.width){
return;
}
}
delta = y-stageY;
if(delta<0){
frameY-=delta;
drawHeight+=delta;
y=stageY;
if(frameY>=this.sheet.height){
return;
}
}
delta = drawWidth+x-stageX-stageWidth;
if(delta>0){
drawWidth-=delta;
}
if(drawWidth<=0){
return;
}
delta = drawHeight+y-stageY-stageHeight;
if(delta>0){
drawHeight-=delta;
}
if(drawHeight<=0){
return;
}
var scaleX = 1;
var scaleY = 1;
if(this.flipX){
scaleX = -1;
}
if(this.flipY){
scaleY = -1;
}
if(scaleX!=1 || scaleY!=1){
stage.scale(scaleX,scaleY);
}
stage.drawImage(this.sheet,frameX,frameY,drawWidth,drawHeight,x,y,drawWidth,drawHeight);
if(scaleX!=1 || scaleY!=1){
stage.scale(scaleX,scaleY);
}
}
//reinitialize the animation to its first frame and loop
Sburb.Animation.prototype.reset = function(){
this.curFrame = 0;
this.curInterval = 0;
this.curLoop = 0;
}
//has the animation stopped playing
Sburb.Animation.prototype.hasPlayed = function(){
return this.curLoop == this.loopNum && this.curFrame==this.length-1;
}
//set the column size (width)
Sburb.Animation.prototype.setColSize = function(newSize){
this.colSize = newSize;
this.numCols = this.sheet.width/this.colSize;
this.reset();
}
//set the row size (height)
Sburb.Animation.prototype.setRowSize = function(newSize){
this.rowSize = newSize;
this.numRows = this.sheet.height/this.rowSize;
this.reset();
}
//set the sheet
Sburb.Animation.prototype.setSheet = function(newSheet){
this.sheet = newSheet;
this.numRows = this.sheet.height/this.rowSize;
this.numCols = this.sheet.width/this.colSize;
this.reset();
}
//does the image render in the given pixel
Sburb.Animation.prototype.isVisuallyUnder = function(x,y){
if(x>=this.x && x<=this.x+this.colSize){
if(y>=this.y && y<=this.y+this.rowSize){
return true;
}
}
return false;
}
//make an exact copy of this animation
Sburb.Animation.prototype.clone = function(x,y){
return new Sburb.Animation(this.name, this.sheet, x+this.x, y+this.y, this.colSize,this.rowSize, this.startPos, this.length, this.frameInterval, this.loopNum);
}
//serialize this Animation to XML
Sburb.Animation.prototype.serialize = function(output){
var frameInterval = "";
var firstInterval = true;
if(this.frameIntervals)
{
for(var interval in this.frameIntervals)
{
frameInterval = frameInterval + (firstInterval?"":",") + interval + ":" + this.frameIntervals[interval];
firstInterval = false;
}
}
else if(this.frameInterval !== 1)
{
frameInterval = this.frameInterval;
}
output = output.concat("\n<animation "+
("sheet='"+this.sheet.name+"' ")+
((this.name!="image")?"name='"+this.name+"' ":"")+
Sburb.serializeAttributes(this,"x","y")+
((this.rowSize!=this.sheet.height)?"rowSize='"+this.rowSize+"' ":"")+
((this.colSize!=this.sheet.width)?"colSize='"+this.colSize+"' ":"")+
Sburb.serializeAttribute(this,"startPos")+
((this.length!=1)?"length='"+this.length+"' ":"")+
((frameInterval!=="")?"frameInterval='"+frameInterval+"' ":"")+
((this.loopNum!=-1)?"loopNum='"+this.loopNum+"' ":"")+
Sburb.serializeAttributes(this,"folowUp","flipX","flipY")+
" />");
return output;
}
///////////////////////////////////////
//Related Utility functions
///////////////////////////////////////
Sburb.parseAnimation = function(animationNode, assetFolder){
var attributes = animationNode.attributes;
var name = "image";
var sheet = null;
var x = 0;
var y = 0;
var colSize = null;
var rowSize = null;
var startPos = 0;
var length = 1;
var frameInterval = 1;
var loopNum = -1;
var followUp = null;
var temp;
name = (temp = attributes.getNamedItem("name"))?temp.value:name;
sheet = (temp = attributes.getNamedItem("sheet"))?assetFolder[temp.value]:sheet;
x = (temp = attributes.getNamedItem("x"))?parseInt(temp.value):x;
y = (temp = attributes.getNamedItem("y"))?parseInt(temp.value):y;
length = (temp = attributes.getNamedItem("length"))?parseInt(temp.value):length;
colSize = (temp = attributes.getNamedItem("colSize"))?parseInt(temp.value):Math.round(sheet.width/length);
rowSize = (temp = attributes.getNamedItem("rowSize"))?parseInt(temp.value):sheet.height;
startPos = (temp = attributes.getNamedItem("startPos"))?parseInt(temp.value):startPos;
frameInterval = (temp = attributes.getNamedItem("frameInterval"))?temp.value:frameInterval;
loopNum = (temp = attributes.getNamedItem("loopNum"))?parseInt(temp.value):loopNum;
followUp = (temp = attributes.getNamedItem("followUp"))?temp.value:followUp;
var flipX = (temp = attributes.getNamedItem("flipX"))?temp.value!="false":false;
var flipY = (temp = attributes.getNamedItem("flipY"))?temp.value!="false":false;
return new Sburb.Animation(name,sheet,x,y,colSize,rowSize,startPos,length,frameInterval,loopNum,followUp,flipX,flipY);
}
return Sburb;
})(Sburb || {});