forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sprite.js
244 lines (205 loc) · 6.43 KB
/
Sprite.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
var Sburb = (function(Sburb){
//////////////////////////////////////////
//Sprite Class
//////////////////////////////////////////
function Sprite(name,x,y,width,height,dx,dy,depthing,collidable){
this.x = x;
this.y = y;
this.dx = typeof dx == "number" ? dx : 0;
this.dy = typeof dy == "number" ? dy : 0;
this.width = width;
this.height = height;
this.depthing = typeof depthing == "number" ? depthing : this.BG_DEPTHING; //bg, fg, or mg
this.collidable = typeof collidable == "boolean" ? collidable : false;
this.animations = {};
this.animation = null;
this.state = null;
this.lastTime = 0;
this.actions = [];
this.name = name;
this.queries = null;
}
Sprite.prototype.BG_DEPTHING = 0;
Sprite.prototype.MG_DEPTHING = 1;
Sprite.prototype.FG_DEPTHING = 2;
Sprite.prototype.addAnimation = function(anim){
this.animations[anim.name] = anim;
}
Sprite.prototype.startAnimation = function(name){
if(this.state!=name && this.animations[name]){
this.animation = this.animations[name];
this.animation.reset();
this.state = name;
}
}
Sprite.prototype.update = function(curRoom){
if(this.animation){
if(this.animation.hasPlayed() && this.animation.followUp){
this.startAnimation(this.animation.followUp);
}else{
this.animation.update();
}
}
}
Sprite.prototype.draw = function(){
if(this.animation){
this.animation.draw(this.x,this.y);
}
}
Sprite.prototype.isBehind = function(other){
if(this.depthing == other.depthing){
return this.y+this.dy<other.y+other.dy;
}else{
return this.depthing<other.depthing;
}
}
Sprite.prototype.collides = function(other,dx,dy){
var x = this.x+(dx?dx:0);
var y = this.y+(dy?dy:0);
if(other.collidable){
if( (x-this.width/2<other.x+other.width/2) &&
(x+this.width/2>other.x-other.width/2) &&
(y-this.height/2<other.y+other.height/2) &&
(y+this.height/2>other.y-other.height/2) ) {
return true;
}
}
return false;
}
Sprite.prototype.hitsPoint = function(x,y){
if( (this.x-this.width/2 <=x) &&
(this.x+this.width/2 >=x) &&
(this.y-this.height/2 <=y) &&
(this.y+this.height/2 >=y) ) {
return true;
}
return false;
}
Sprite.prototype.isVisuallyUnder = function(x,y){
return this.animation && this.animation.isVisuallyUnder(x-this.x,y-this.y);
}
Sprite.prototype.addAction = function(action){
this.actions.push(action);
}
Sprite.prototype.removeAction = function(name){
for(var i=0;i<this.actions.length;i++){
if(this.actions[i].name==name){
this.actions.splice(i,1);
return;
}
}
}
Sprite.prototype.getActions = function(sprite){
var validActions = [];
for(var i=0;i<this.actions.length;i++){
var action = this.actions[i];
var desired = action.sprite;
if(!desired || desired==sprite.name
|| (desired.charAt(0)=="!" && desired.substring(1)!=sprite.name)){
validActions.push(action);
}
}
return validActions;
}
Sprite.prototype.getBoundaryQueries = function(dx,dy){
var spriteX = this.x+(dx?dx:0);
var spriteY = this.y+(dy?dy:0);
var w = this.width/2;
var h = this.height/2;
if(!this.queries){
this.queries = {upRight:{},upLeft:{},downLeft:{},downRight:{},downMid:{},upMid:{}};
}
this.queries.upRight.x=spriteX+w;
this.queries.upRight.y=spriteY-h;
this.queries.upLeft.x=spriteX-w;
this.queries.upLeft.y=spriteY-h;
this.queries.downLeft.x=spriteX-w;
this.queries.downLeft.y=spriteY+h;
this.queries.downRight.x=spriteX+w;
this.queries.downRight.y=spriteY+h;
this.queries.downMid.x=spriteX;
this.queries.downMid.y=spriteY+h;
this.queries.upMid.x=spriteX;
this.queries.upMid.y=spriteY-h;
return this.queries;
}
Sprite.prototype.serialize = function(output){
var animationCount = 0;
for(var anim in this.animations){
if(!this.animations.hasOwnProperty(anim)) continue;
animationCount++;
}
output = output.concat("\n<sprite "+
Sburb.serializeAttributes(this,"name","x","y","dx","dy","width","height","depthing","collidable")+
(animationCount>1?"state='"+this.state+"' ":"")+
">");
for(var anim in this.animations){
if(!this.animations.hasOwnProperty(anim)) continue;
output = this.animations[anim].serialize(output);
}
for(var i=0; i < this.actions.length; i++){
output = this.actions[i].serialize(output);
}
output = output.concat("\n</sprite>");
return output;
}
Sprite.prototype.clone = function(newName) {
var newSprite = new Sburb.Sprite(newName,this.x,this.y,this.width,this.height,this.dx,this.dy,this.depthing,this.collidable);
for(var anim in this.animations) {
if(this.animations.hasOwnProperty(anim)) {
newSprite.addAnimation(this.animations[anim].clone());
}
}
for(var action in this.actions) {
if(this.actions.hasOwnProperty(action)) {
newSprite.addAction(this.actions[action].clone());
}
}
if(this.animation) {
newSprite.startAnimation(this.animation.name);
}
Sburb.sprites[newName]=newSprite;
return newSprite;
}
///////////////////////////////////////////
//Related Utility Functions
///////////////////////////////////////////
Sburb.parseSprite = function(spriteNode, assetFolder) {
var attributes = spriteNode.attributes;
var newName = null;
var newX = 0;
var newY = 0;
var newWidth = 0;
var newHeight = 0;
var newDx = 0;
var newDy = 0;
var newDepthing = 0;
var newCollidable = false;
var newState = null;
var newAnimations = {};
var temp;
newName = (temp=attributes.getNamedItem("name"))?temp.value:newName;
newX = (temp=attributes.getNamedItem("x"))?parseInt(temp.value):newX;
newY = (temp=attributes.getNamedItem("y"))?parseInt(temp.value):newY;
newWidth = (temp=attributes.getNamedItem("width"))?parseInt(temp.value):newWidth;
newHeight = (temp=attributes.getNamedItem("height"))?parseInt(temp.value):newHeight;
newDx = (temp=attributes.getNamedItem("dx"))?parseInt(temp.value):newDx;
newDy = (temp=attributes.getNamedItem("dy"))?parseInt(temp.value):newDy;
newDepthing = (temp=attributes.getNamedItem("depthing"))?parseInt(temp.value):newDepthing;
newCollidable = (temp=attributes.getNamedItem("collidable"))?temp.value!="false":newCollidable;
newState = (temp=attributes.getNamedItem("state"))?temp.value:newState;
var newSprite = new Sprite(newName,newX,newY,newWidth,newHeight,newDx,newDy,newDepthing,newCollidable);
var anims = spriteNode.getElementsByTagName("animation");
for(var j=0;j<anims.length;j++){
var newAnim = Sburb.parseAnimation(anims[j],assetFolder);
newSprite.addAnimation(newAnim);
if(newState==null){
newState = newAnim.name;
}
}
newSprite.startAnimation(newState);
return newSprite;
}
Sburb.Sprite = Sprite;
return Sburb;
})(Sburb || {});