forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.js
430 lines (383 loc) · 11.9 KB
/
Character.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
var Sburb = (function(Sburb){
///////////////////////////////////////
//Chracter Class (inherits Sprite)
///////////////////////////////////////
//constructor
Sburb.Character = function(name,x,y,width,height,sx,sy,sWidth,sHeight,sheet,bootstrap){
Sburb.Sprite.call(this,name,x,y,width,height,null,null,Sburb.Sprite.prototype.MG_DEPTHING,true);
this.speed = 12;
this.vx = 0;
this.vy = 0;
this.facing = "Front";
this.npc = true;
this.spriteType = "character";
this.following = null;
this.followBuffer = null;
this.follower = null;
this.lastLeaderPos = null;
this.handledInput = -1;
if(!bootstrap){ //automagically generate standard animations
sWidth = typeof sWidth == "number" ? sWidth : width;
sHeight = typeof sHeight == "number" ? sHeight : height;
this.addAnimation(new Sburb.Animation("idleFront",sheet,sx,sy,sWidth,sHeight,0,1,2));
this.addAnimation(new Sburb.Animation("idleRight",sheet,sx,sy,sWidth,sHeight,1,1,2));
this.addAnimation(new Sburb.Animation("idleBack",sheet,sx,sy,sWidth,sHeight,2,1,2));
this.addAnimation(new Sburb.Animation("idleLeft",sheet,sx,sy,sWidth,sHeight,3,1,2));
this.addAnimation(new Sburb.Animation("walkFront",sheet,sx,sy,sWidth,sHeight,4,2,4));
this.addAnimation(new Sburb.Animation("walkRight",sheet,sx,sy,sWidth,sHeight,6,2,4));
this.addAnimation(new Sburb.Animation("walkBack",sheet,sx,sy,sWidth,sHeight,8,2,4));
this.addAnimation(new Sburb.Animation("walkLeft",sheet,sx,sy,sWidth,sHeight,10,2,4));
this.startAnimation("walkFront");
}else{
this.bootstrap = true;
}
this.becomeNPC();
}
Sburb.Character.prototype = new Sburb.Sprite();
Sburb.Character.prototype.followBufferLength = 9;
//update as if one frame has passed
Sburb.Character.prototype.update = function(curRoom){
if(this.following){
if(this.following.isNPC() && !this.isNPC()){
this.becomeNPC();
this.collidable = true;
this.walk();
}else if(!this.following.isNPC() && this.isNPC()){
this.becomePlayer();
this.collidable = false;
}
if(this.following.x!=this.lastLeaderPos.x || this.following.y!=this.lastLeaderPos.y){
this.followBuffer.push({x:this.following.x,y:this.following.y});
this.lastLeaderPos.x = this.following.x;
this.lastLeaderPos.y = this.following.y;
}
while(this.followBuffer.length>this.followBufferLength){
var destPos = this.followBuffer[0];
if(Math.abs(destPos.x-this.x)>=this.speed/1.9){
if(destPos.x>this.x){
this.moveRight();
}else{
this.moveLeft();
}
}else if(Math.abs(destPos.y-this.y)>=this.speed/1.9){
if(destPos.y>this.y){
this.moveDown();
}else{
this.moveUp();
}
}else {
this.followBuffer.splice(0,1);
continue;
}
break;
}
if(this.followBuffer.length<=this.followBufferLength && !this.following.isNPC()){
this.moveNone();
}
}
if(this.handleInput>0){
--this.handleInput;
if(this.handleInput==0){
moveNone();
}
}
this.tryToMove(this.vx,this.vy,curRoom);
Sburb.Sprite.prototype.update.call(this,curRoom);
}
//impulse character to move up
Sburb.Character.prototype.moveUp = function(){
this.facing = "Back";
this.walk();
this.vx = 0; this.vy = -this.speed;
}
//impulse character to move down
Sburb.Character.prototype.moveDown = function(){
this.facing = "Front";
this.walk();
this.vx = 0; this.vy = this.speed;
}
//impulse character to move left
Sburb.Character.prototype.moveLeft = function(){
this.facing = "Left";
this.walk();
this.vx = -this.speed; this.vy = 0;
}
//impulse character to move right
Sburb.Character.prototype.moveRight = function(){
this.facing = "Right";
this.walk();
this.vx = this.speed; this.vy = 0;
}
//impulse character to stand still
Sburb.Character.prototype.moveNone = function(){
if(this.animations.walkFront.frameInterval == 4){
this.idle();
this.vx = 0; this.vy = 0;
}
}
//make character walk
Sburb.Character.prototype.walk = function(){
this.startAnimation("walk"+this.facing);
}
//make character idle
Sburb.Character.prototype.idle = function(){
this.startAnimation("idle"+this.facing);
}
//behave as an NPC
Sburb.Character.prototype.becomeNPC = function(){
this.animations.walkFront.frameInterval = 12;
this.animations.walkBack.frameInterval = 12;
this.animations.walkLeft.frameInterval = 12;
this.animations.walkRight.frameInterval = 12;
}
//behave as a PC
Sburb.Character.prototype.becomePlayer = function(){
this.animations.walkFront.frameInterval = 4;
this.animations.walkBack.frameInterval = 4;
this.animations.walkLeft.frameInterval = 4;
this.animations.walkRight.frameInterval = 4;
}
//parse key inputs into actions
Sburb.Character.prototype.handleInputs = function(pressed, order){
var down = -1, up = -1, left = -1, right = -1, most = 0;
down = Math.max(order.indexOf(Sburb.Keys.down), order.indexOf(Sburb.Keys.s));
up = Math.max(order.indexOf(Sburb.Keys.up), order.indexOf(Sburb.Keys.w));
left = Math.max(order.indexOf(Sburb.Keys.left), order.indexOf(Sburb.Keys.a));
right = Math.max(order.indexOf(Sburb.Keys.right),order.indexOf(Sburb.Keys.d));
most = Math.max(down, up, left, right, most);
if(down == most) {
this.moveDown();
} else if(up == most) {
this.moveUp();
} else if(left == most) {
this.moveLeft();
} else if(right == most) {
this.moveRight();
} else {
this.moveNone();
}
this.handledInput = 2;
}
//have character try to move through room
Sburb.Character.prototype.tryToMove = function(vx,vy,room){
var i;
var moveMap = room.getMoveFunction(this);
var wasShifted = false;
if(moveMap) { //our motion could be modified somehow
l = moveMap(vx, vy);
if(vx!=l.x || vy!=l.y){
wasShifted = true;
}
vx = l.x;
vy = l.y;
}
var minX = Sburb.Stage.scaleX;
var minY = Sburb.Stage.scaleY;
while(Math.abs(vx)>=minX || Math.abs(vy)>=minY){
var dx = 0;
var dy = 0;
if(Math.abs(vx)>=minX){
dx=Math.round((minX)*vx/Math.abs(vx));
this.x+=dx;
vx-=dx;
}
if(Math.abs(vy)>=minY){
dy=Math.round((minY)*vy/Math.abs(vy));
this.y+=dy;
vy-=dy;
}
if(!this.following){
var collision;
if(collision = room.collides(this)){
var fixed = false;
if(dx!=0){
if(!this.collides(collision,0,minY)){
dy+=minY;
this.y+=minY;
fixed = true;
}else if(!this.collides(collision,0,-minY)){
dy-=minY;
this.y-=minY;
fixed = true;
}
}
if(!fixed && dy!=0){
if(!this.collides(collision,minX,0)){
dx+=minX;
this.x+=minX;
fixed = true;
}else if(!this.collides(collision,-minX,0)){
dx-=minX;
this.x-=minX;
fixed = true;
}
}
if(!fixed || room.collides(this)){
this.x-=dx;
this.y-=dy;
return false;
}
}
if(!room.isInBounds(this)){
var fixed = false;
if(dx!=0){
if(room.isInBounds(this,0,minY)){
dy+=minY;
this.y+=minY;
fixed = true;
}else if(room.isInBounds(this,0,-minY)){
dy-=minY;
this.y-=minY;
fixed = true;
}
}
if(!fixed && dy!=0){
if(room.isInBounds(this,minX,0)){
dx+=minX;
this.x+=minX;
fixed = true;
}else if(room.isInBounds(this,-minX,0)){
dx-=minX;
this.x-=minX;
fixed = true;
}
}
if(!fixed || room.collides(this)){
this.x-=dx;
this.y-=dy;
return false;
}
}
}
}
return true;
}
Sburb.Character.prototype.follow = function(sprite){
while(sprite.follower!=null){
sprite = sprite.follower;
}
this.following = sprite;
sprite.follower = this;
this.followBuffer = [];
this.lastLeaderPos = {};
this.collidable = false;
}
Sburb.Character.prototype.unfollow = function(){
if(this.following){
this.following.follower = this.follower;
if(this.follower){
this.follower.following = this.following;
this.follower.followBuffer = [];
}
this.following = null;
this.follower = null;
this.lastLeaderPos = null;
this.collidable = true;
this.becomeNPC();
}
}
//get locations character wishes to query for actions
Sburb.Character.prototype.getActionQueries = function(){
var queries = [];
queries.push({x:this.x,y:this.y});
if(this.facing=="Front"){
queries.push({x:this.x,y:this.y+(this.height/2+15)});
queries.push({x:this.x-this.width/2,y:this.y+(this.height/2+15)});
queries.push({x:this.x+this.width/2,y:this.y+(this.height/2+15)});
}else if(this.facing=="Back"){
queries.push({x:this.x,y:this.y-(this.height/2+15)});
queries.push({x:this.x-this.width/2,y:this.y-(this.height/2+15)});
queries.push({x:this.x+this.width/2,y:this.y-(this.height/2+15)});
}else if(this.facing=="Right"){
queries.push({x:this.x+(this.width/2+15),y:this.y});
queries.push({x:this.x+(this.width/2+15),y:this.y+this.height/2});
queries.push({x:this.x+(this.width/2+15),y:this.y-this.height/2});
}else if(this.facing=="Left"){
queries.push({x:this.x-(this.width/2+15),y:this.y});
queries.push({x:this.x-(this.width/2+15),y:this.y+this.height/2});
queries.push({x:this.x-(this.width/2+15),y:this.y-this.height/2});
}
return queries;
}
//serialize character to XML
Sburb.Character.prototype.serialize = function(output){
output = output.concat("\n<character name='"+this.name+
"' x='"+this.x+
"' y='"+this.y+
"' width='"+this.width+
"' height='"+this.height+
"' state='"+this.state+
"' facing='"+this.facing);
if(!this.bootstrap){
output = output.concat("' sx='"+this.animations.walkFront.x+
"' sy='"+this.animations.walkFront.y+
"' sWidth='"+this.animations.walkFront.colSize+
"' sHeight='"+this.animations.walkFront.rowSize+
"' sheet='"+this.animations.walkFront.sheet.name);
}else{
output = output.concat("' bootstrap='true");
}
if(this.following){
output = output.concat("' following='"+this.following.name+"");
}
if(this.follower){
output = output.concat("' follower='"+this.follower.name+"");
}
output = output.concat("'>");
for(var animation in this.animations){
if(!this.animations.hasOwnProperty(animation)) continue;
var anim = this.animations[animation];
if(this.bootstrap || (anim.name.indexOf("idle")==-1 && anim.name.indexOf("walk")==-1)){
output = anim.serialize(output);
}
}
for(var i=0; i < this.actions.length; i++){
output = this.actions[i].serialize(output);
}
output = output.concat("\n</character>");
return output;
}
Sburb.Character.prototype.isNPC = function(){
return this.animations.walkFront.frameInterval == 12;
}
////////////////////////////////////////////
//Related Utiltity functions
////////////////////////////////////////////
//parse character from XML DOM Node
Sburb.parseCharacter = function(charNode, assetFolder) {
var attributes = charNode.attributes;
var newChar = new Sburb.Character(attributes.getNamedItem("name").value,
attributes.getNamedItem("x")?parseInt(attributes.getNamedItem("x").value):0,
attributes.getNamedItem("y")?parseInt(attributes.getNamedItem("y").value):0,
parseInt(attributes.getNamedItem("width").value),
parseInt(attributes.getNamedItem("height").value),
attributes.getNamedItem("sx")?parseInt(attributes.getNamedItem("sx").value):0,
attributes.getNamedItem("sy")?parseInt(attributes.getNamedItem("sy").value):0,
parseInt(attributes.getNamedItem("sWidth").value),
parseInt(attributes.getNamedItem("sHeight").value),
assetFolder[attributes.getNamedItem("sheet").value]);
var temp = attributes.getNamedItem("following");
if(temp){
var following = Sburb.sprites[temp.value];
if(following){
newChar.follow(following);
}
}
var temp = attributes.getNamedItem("follower");
if(temp){
var follower = Sburb.sprites[temp.value];
if(follower){
follower.follow(newChar);
}
}
var anims = charNode.getElementsByTagName("animation");
for(var j=0;j<anims.length;j++){
var newAnim = Sburb.parseAnimation(anims[j],assetFolder);
newChar.addAnimation(newAnim);
}
newChar.startAnimation(attributes.getNamedItem("state").value);
newChar.facing = attributes.getNamedItem("facing").value;
return newChar;
}
return Sburb;
})(Sburb || {});