-
Notifications
You must be signed in to change notification settings - Fork 21
/
Fighter.js
315 lines (272 loc) · 7.28 KB
/
Fighter.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
var Sburb = (function(Sburb){
////////////////////////////////////////
//Fighter Class (inherits Sprite)
////////////////////////////////////////
//Fighter
Sburb.Fighter = function(name,x,y,width,height){
Sburb.Sprite.call(this,name,x,y,width,height,null,null,Sburb.Sprite.prototype.MG_DEPTHING,true);
this.accel = 1.5;
this.decel = 1;
this.friction = 0.87;
this.vx = 0;
this.vy = 0;
this.facing = "Right";
}
Sburb.Fighter.prototype = new Sburb.Sprite();
//update the Fighter one frame
Sburb.Fighter.prototype.update = function(curRoom){
this.tryToMove(curRoom);
Sburb.Sprite.prototype.update.call(this,curRoom);
this.animation.flipX = (this.facing=="Left");
}
//parse keyboard input into movements
Sburb.Fighter.prototype.handleInputs = function(pressed){
var moved = false;
if(pressed[Sburb.Keys.down] || pressed[Sburb.Keys.s]){
this.moveDown(); moved = true;
}else if(pressed[Sburb.Keys.up] || pressed[Sburb.Keys.w]){
this.moveUp(); moved = true;
}
if(pressed[Sburb.Keys.left] || pressed[Sburb.Keys.a]){
this.moveLeft(); moved = true;
}else if(pressed[Sburb.Keys.right] || pressed[Sburb.Keys.d]){
this.moveRight(); moved = true;
}
if(pressed[Sburb.Keys.space] || pressed[Sburb.Keys.enter] || pressed[Sburb.Keys.ctrl]){
this.attack();
}
if(!moved){
this.idle();
}
}
//stand still
Sburb.Fighter.prototype.idle = function(){
if(this.state=="walk"){
this.startAnimation("idle");
}
}
//walk
Sburb.Fighter.prototype.walk = function(){
if(this.state=="idle"){
this.startAnimation("walk");
}
}
//attack
Sburb.Fighter.prototype.attack = function(){
this.startAnimation("attack");
}
//impulse fighter to move up
Sburb.Fighter.prototype.moveUp = function(){
this.walk();
this.vy-=this.accel;
}
//impulse fighter to move down
Sburb.Fighter.prototype.moveDown = function(){
this.walk();
this.vy+=this.accel;
}
//impulse fighter to move left
Sburb.Fighter.prototype.moveLeft = function(){
this.walk();
this.vx-=this.accel;
this.facing = "Left";
}
//impulse fighter to move right
Sburb.Fighter.prototype.moveRight = function(){
this.walk();
this.vx+=this.accel;
this.facing = "Right";
}
Sburb.Fighter.prototype.moveNone = function(){
}
//behave as a PC
Sburb.Fighter.prototype.becomePlayer = function(){
}
//behave as an NPC
Sburb.Fighter.prototype.becomeNPC = function(){
}
//get all the locations the Fighter would wich to query for actions
Sburb.Fighter.prototype.getActionQueries = function(){
var queries = [];
return queries;
}
//determine if the Fighter collides with the given sprite, if it were offset by dx,dy
Sburb.Fighter.prototype.collides = function(sprite,dx,dy){
if(!this.width || !sprite.width){
return false;
}
var x1 = this.x+(dx?dx:0);
var y1 = this.y+(dy?dy:0);
var w1 = this.width/2;
var h1 = this.height/2;
var x2 = sprite.x;
var y2 = sprite.y;
var w2 = sprite.width/2;
var h2 = sprite.height/2;
var xDiff = x2-x1;
var yDiff = y2-y1;
return Math.sqrt(xDiff*xDiff/w2/w1+yDiff*yDiff/h2/h1)<2;
}
//get the points where the Fighter might collide with something
Sburb.Fighter.prototype.getBoundaryQueries = function(dx,dy){
var x = this.x+(dx?dx:0);
var y = this.y+(dy?dy:0);
var queries = {};
var queryCount = 8;
var angleDiff = 2*Math.PI/queryCount;
for(var i=0,theta=0;i<queryCount;i++,theta+=angleDiff){
queries[i] = {x:x+Math.cos(theta)*this.width/2 ,y:y+Math.sin(theta)*this.height/2};
}
return queries;
}
//try to move through the room
Sburb.Fighter.prototype.tryToMove = function(room){
this.vx*=this.friction;
this.vy*=this.friction;
if(Math.abs(this.vx)<this.decel){
this.vx = 0;
}
if(Math.abs(this.vy)<this.decel){
this.vy = 0;
}
var vx = this.vx;
var vy = this.vy;
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 dx = vx;
var dy = vy;
this.x+=vx;
this.y+=vy;
var collides = room.collides(this);
if(collides){
var tx = 0;
var ty = 0;
var theta = Math.atan2(this.y-collides.y,this.x-collides.x);
var xOff = Math.cos(theta);
var yOff = Math.sin(theta);
while(this.collides(collides,tx,ty)){
tx-=(dx-xOff)*0.1;
ty-=(dy-yOff)*0.1;
}
if(room.collides(this,tx,ty)){
this.x-=dx;
this.y-=dy;
return false;
}
this.x+=tx;
this.y+=ty;
dx+=tx;
dy+=ty;
var theta = Math.atan2(this.y-collides.y,this.x-collides.x);
this.vx += tx;
this.vy += ty;
this.vx*=0.9;
this.vy*=0.9;
}
var queries = room.isInBoundsBatch(this.getBoundaryQueries());
var queryCount = 8;
var collided = false;
var hitX = 0;
var hitY = 0;
var angleDiff = 2*Math.PI/queryCount;
for(var i=0,theta=0;i<queryCount;i++,theta+=angleDiff){
var query = queries[i];
if(!query){
hitX+=Math.cos(theta);
hitY+=Math.sin(theta);
collided = true;
}
}
if(collided){
var tx = 0;
var ty = 0;
var theta = Math.atan2(hitY,hitX);
var xOff = Math.cos(theta);
var yOff = Math.sin(theta);
var timeout = 0;
while(!room.isInBounds(this,tx,ty) && timeout<20){
tx-=xOff*2;
ty-=yOff*2;
timeout++;
}
if(timeout>=20 || room.collides(this,tx,ty)){
console.log(tx,ty);
this.x-=dx;
this.y-=dy;
return false;
}
this.x+=tx;
this.y+=ty;
dx+=tx;
dy+=ty;
this.vx += tx;
this.vy += ty;
this.vx*=0.9;
this.vy*=0.9;
}
return true;
}
//serialize this Fighter to XML
Sburb.Fighter.prototype.serialize = function(output){
var animationCount = 0;
for(var anim in this.animations){
if(!this.animations.hasOwnProperty(anim)) continue;
animationCount++;
}
output = output.concat("<fighter "+
Sburb.serializeAttributes(this,"name","x","y","width","height","facing")+
(animationCount>1?"state='"+this.state+"' ":"")+
">");
for(var animation in this.animations){
if(!this.animations.hasOwnProperty(animation)) continue;
output = this.animations[animation].serialize(output);
}
for(var i=0; i < this.actions.length; i++){
output = this.actions[i].serialize(output);
}
output = output.concat("</fighter>");
return output;
}
//////////////////////////////////////////
//Related Utility Functions
//////////////////////////////////////////
Sburb.parseFighter = function(spriteNode, assetFolder) {
var attributes = spriteNode.attributes;
var newName = null;
var newX = 0;
var newY = 0;
var newWidth = 0;
var newHeight = 0;
var newState = null;
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;
newState = (temp=attributes.getNamedItem("state"))?temp.value:newState;
var newFacing = (temp=attributes.getNamedItem("facing"))?temp.value:"Right";
var newSprite = new Sburb.Fighter(newName,newX,newY,newWidth,newHeight);
newSprite.facing = newFacing;
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;
}
return Sburb;
})(Sburb || {});