-
Notifications
You must be signed in to change notification settings - Fork 11
/
livingobject.js
623 lines (567 loc) · 12.7 KB
/
livingobject.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*\
* livingobject
*
* a base class for all living objects
\*/
define(['LF/global','LF/sprite','LF/mechanics','LF/util','F.core/sprite'],
function ( Global, Sprite, Mech, util, Fsprite)
{
var GC=Global.gameplay;
/*\
* livingobject
[ class ]
| config=
| {
| match,
| controller, (characters only)
| team
| }
\*/
function livingobject(config,data,thisID)
{
if( !config)
return;
var $=this;
//identity
$.name=data.bmp.name;
$.uid=-1; //unique id, set by scene
$.id=thisID; //character id, specify tactical behavior. accept values from 0~99
$.data=data;
$.team=config.team;
$.statemem = {}; //state memory, will be cleared on every state transition
//handles
$.match=config.match;
$.spec=$.match.spec;
$.scene=$.match.scene;
$.visualeffect=$.match.visualeffect;
$.bg=$.match.background;
//states
$.sp = new Sprite(data.bmp, $.match.stage);
if( !$.proper('no_shadow'))
{
var sp_sha=
{
canvas: $.match.stage,
wh: 'fit',
img: $.bg.shadow.img
}
$.shadow = new Fsprite(sp_sha);
}
$.health=
{
hp: 100,
mp: 100
};
$.frame=
{
PN: 0, //previous frame number
N: 0, //current frame number
D: data.frame[0], //current frame's data object
ani: //animation sequence
{
i:0, up:true
}
};
$.mech = new Mech($);
$.ps = $.mech.create_metric(); //position, velocity, and other physical properties
$.trans = new frame_transistor($);
$.itr=
{
arest: 0, //attack rest - time until the attacker can do a single hit again
vrest:[], //victim rest - time until a character can be hit again
};
$.effect=
{
num: -99, //effect number
dvx: 0, dvy: 0,
stuck: false, //when an object is said to be 'stuck', there is not state and frame update
oscillate: 0, //if oscillate is non-zero, will oscillate for amplitude equals value of oscillate
blink: false, //blink: hide 2 TU, show 2 TU ,,, until effect vanishs
super: false, //when an object is in state 'super', it does not return body volume, such that it cannot be hit
timein: 0, //time to take effect
timeout: 0 //time to lose effect
};
$.catching= 0; //state 9: the object being caught by me now
//OR state 10: the object catching me now
$.allow_switch_dir=true; //direction switcher
}
livingobject.prototype.type='livingobject';
//livingobject.prototype.states = null; //the collection of states forming a state machine
//livingobject.prototype.states_switch_dir = null; //whether to allow switch dir in each state
livingobject.prototype.destroy = function()
{
this.sp.destroy();
this.shadow.remove();
}
livingobject.prototype.log = function(mes)
{
this.match.log(mes);
}
//setup for a match
livingobject.prototype.setup = function()
{
var $=this;
$.state_update('setup');
}
//update done at every frame
livingobject.prototype.frame_update = function()
{
var $=this;
//show frame
$.sp.show_pic($.frame.D.pic);
$.ps.fric=1; //reset friction
if( !$.state_update('frame_force'))
$.frame_force();
//wait for next frame
$.trans.set_wait($.frame.D.wait,99);
$.trans.set_next($.frame.D.next,99);
//state generic then specific update
$.state_update('frame');
}
livingobject.prototype.frame_force = function()
{
var $=this;
if( $.frame.D.dvx)
{
var avx = $.ps.vx>0?$.ps.vx:-$.ps.vx;
if( $.ps.y<0 || avx < $.frame.D.dvx) //accelerate..
$.ps.vx = $.dirh() * $.frame.D.dvx; //..is okay
//decelerate must be gradual
if( $.frame.D.dvx<0)
$.ps.vx = $.ps.vx - $.dirh();
}
if( $.frame.D.dvz) $.ps.vz = $.dirv() * $.frame.D.dvz;
if( $.frame.D.dvy) $.ps.vy += $.frame.D.dvy;
}
//update done at every TU (30fps)
livingobject.prototype.TU_update = function()
{
var $=this;
if( !$.state_update('TU_force'))
$.frame_force();
//effect
if( $.effect.timein<0)
{
if( $.effect.oscillate)
{
if( $.effect.oi===1)
$.effect.oi=-1;
else
$.effect.oi=1;
$.sp.set_x_y($.ps.sx + $.effect.oscillate*$.effect.oi, $.ps.sy+$.ps.sz);
}
else if( $.effect.blink)
{
if( $.effect.bi===undefined)
$.effect.bi = 0;
switch ($.effect.bi%4)
{
case 0: case 1:
$.sp.hide();
break;
case 2: case 3:
$.sp.show();
break;
}
$.effect.bi++;
}
if( $.effect.timeout===0)
{
$.effect.num = -99;
if( $.effect.stuck)
{
$.effect.stuck = false;
}
if( $.effect.oscillate)
{
$.effect.oscillate = 0;
$.sp.set_x_y($.ps.sx, $.ps.sy+$.ps.sz);
}
if( $.effect.blink)
{
$.effect.blink = false;
$.effect.bi = undefined;
$.sp.show();
}
if( $.effect.super)
{
$.effect.super = false;
}
}
else if( $.effect.timeout===-1)
{
if( $.effect.dvx) $.ps.vx = $.effect.dvx;
if( $.effect.dvy) $.ps.vy = $.effect.dvy;
$.effect.dvx=0;
$.effect.dvy=0;
}
$.effect.timeout--;
}
if( $.effect.timein<0 && $.effect.stuck)
; //stuck
else
$.state_update('TU');
//attack rest
for( var I in $.itr.vrest)
{ //watch out that itr.vrest might grow very big
if( $.itr.vrest[I] > 0)
$.itr.vrest[I]--;
}
if( $.itr.arest > 0)
$.itr.arest--;
}
livingobject.prototype.state_update=function(event)
{
var $=this;
var tar1=$.states['generic'];
if( tar1) var res1=tar1.apply($,arguments);
//
var tar2=$.states[$.frame.D.state];
if( tar2) var res2=tar2.apply($,arguments);
//
return res1 || res2;
}
livingobject.prototype.TU=function()
{
var $=this;
//state
$.TU_update();
//combo detector
if( $.con)
$.combodec.frame();
}
livingobject.prototype.transit=function()
{
var $=this;
//fetch inputs
if( $.con)
{
$.con.fetch();
$.combo_update();
}
//frame transition
if( $.effect.timein<0 && $.effect.stuck)
; //stuck!
else
$.trans.trans();
$.effect.timein--;
if( $.effect.timein<0 && $.effect.stuck)
; //stuck!
else
$.state_update('transit');
}
livingobject.prototype.set_pos=function(x,y,z)
{
this.mech.set_pos(x,y,z);
}
//return the body volume for collision detection
// all other volumes e.g. itr should start with prefix vol_
livingobject.prototype.vol_body=function()
{
return this.mech.body();
}
livingobject.prototype.cur_state=function()
{
return this.frame.D.state;
}
livingobject.prototype.effect_id=function(num)
{
return num+GC.effect.num_to_id;
}
livingobject.prototype.effect_create=function(num,duration,dvx,dvy)
{
var $=this;
if( num >= $.effect.num)
{
var efid= num+GC.effect.num_to_id;
if( $.proper(efid,'oscillate'))
$.effect.oscillate=$.proper(efid,'oscillate');
if( $.proper(efid,'cant_move'))
$.effect.stuck=true;
if( dvx!==undefined)
$.effect.dvx = dvx;
if( dvy!==undefined)
$.effect.dvy = dvy;
if( $.effect.num>=0)
{ //only allow extension of effect
if( 0 < $.effect.timein)
$.effect.timein=0;
if( duration > $.effect.timeout)
$.effect.timeout=duration;
}
else
{
$.effect.timein=0;
$.effect.timeout=duration;
}
$.effect.num = num;
}
}
livingobject.prototype.effect_stuck=function(timein,timeout)
{
var $=this;
if( !$.effect.stuck || $.effect.num<=-1)
{
$.effect.num=-1; //magic number
$.effect.stuck=true;
$.effect.timein=timein;
$.effect.timeout=timeout;
}
}
livingobject.prototype.visualeffect_create=function(num, rect, righttip, variant)
{
var $=this;
var efid= num+GC.effect.num_to_id;
var pos=
{
x: rect.x+ rect.vx+ (righttip?rect.w:0),
y: rect.y+ rect.vy+ rect.h/2,
z: rect.z>$.ps.z ? rect.z:$.ps.z
}
$.visualeffect.create(pos,efid,variant);
}
//animate back and forth between frame a and b
livingobject.prototype.frame_ani_oscillate=function(a,b)
{
var $=this;
var $f=$.frame;
if( $f.ani.i<a || $f.ani.i>b)
{
$f.ani.up=true;
$f.ani.i=a+1;
}
if( $f.ani.i<b && $f.ani.up)
$.trans.set_next($f.ani.i++);
else if( $f.ani.i>a && !$f.ani.up)
$.trans.set_next($f.ani.i--);
if( $f.ani.i==b) $f.ani.up=false;
if( $f.ani.i==a) $f.ani.up=true;
}
livingobject.prototype.frame_ani_sequence=function(a,b)
{
var $=this;
var $f=$.frame;
if( $f.ani.i<a || $f.ani.i>b)
{
$f.ani.i=a+1;
}
trans.set_next($f.ani.i++);
if( $f.ani.i > b)
$f.ani.i=a;
}
livingobject.prototype.itr_rest_update=function(uid,ITR)
{
var $=this;
//rest: cannot interact again for some time
if( ITR && (ITR.arest || ITR.vrest))
{
if( ITR.arest)
$.itr.arest = ITR.arest;
else if( ITR.vrest)
$.itr.vrest[uid] = ITR.vrest;
}
else
$.itr.arest = GC.default.character.arest;
}
livingobject.prototype.itr_rest_test=function(uid,ITR)
{
var $=this;
return !$.itr.arest && !$.itr.vrest[uid];
}
livingobject.prototype.switch_dir = function(e)
{
var $=this;
if( $.ps.dir==='left' && e==='right')
{
$.ps.dir='right';
$.sp.switch_lr('right');
}
else if( $.ps.dir==='right' && e==='left')
{
$.ps.dir='left';
$.sp.switch_lr('left');
}
}
livingobject.prototype.dirh = function()
{
var $=this;
return ($.ps.dir==='left'?-1:1);
}
livingobject.prototype.dirv = function()
{
var $=this;
var d=0;
if( $.con)
{
if( $.con.state.up) d-=1;
if( $.con.state.down) d+=1;
}
return d;
}
livingobject.prototype.proper = function(id,prop)
{
var $=this;
if( arguments.length===1)
{
prop=id;
id=$.id;
}
if( $.spec[id])
return $.spec[id][prop];
return undefined;
}
function frame_transistor($)
{
/*DC*/
var wait=1; //when wait decreases to zero, a frame transition happens
var next=999; //next frame
var lock=0;
var lockout=1; //when lockout equals 0, the lock will be reset automatically
//frame transitions are caused differently: going to the next frame, a combo is pressed, being hit, or being burnt
// and they can all happen *at the same TU*, to determine which frame to go to,
// each cause is given an authority which is used to resolve frame transition conflicts.
// lock=0 means unlocked
// common authority values:
//0-9: natural
// 0: natural
// 10: move,defend,jump,punch,catching,caught
// 11: special moves
// 15: environmental interactions
// 2x: interactions
// 20: being punch
// 25: hit by special attack
// 3x: strong interactions
// 30: in effect type 0
// 35: fire, ice, blast
this.frame=function(F,au)
{
this.set_next(F,au);
this.set_wait(0,au);
}
this.set_wait=function(value,au,out)
{
if(!au) au=0; //authority
if( au===99) au=lock; //au=99 means always has just enough authority
if(!out) out=1; //lock timeout
if( au >= lock)
{
lock=au;
lockout=out;
if( out===99) //out=99 means lock until frame transition
lockout=wait;
wait=value;
if( wait<0) wait=0;
}
}
this.inc_wait=function(inc,au,out) //increase wait by inc amount
{
if(!au) au=0;
if( au===99) au=lock;
if(!out) out=1;
if( au >= lock)
{
lock=au;
lockout=out;
if( out===99)
lockout=wait;
wait+=inc;
if( wait<0) wait=0;
}
}
this.next=function()
{
return next;
}
this.wait=function()
{
return wait;
}
this.set_next=function(value,au,out)
{
if(!au) au=0;
if( au===99) au=lock;
if(!out) out=1;
if( au >= lock)
{
lock=au;
lockout=out;
if( out===99)
lockout=wait;
next=value;
}
}
this.reset_lock=function(au)
{
if(!au) au=0;
if( au===99) au=lock;
if( au >= lock)
{
lock=0;
}
}
this.next_frame_D=function()
{
var anext = next;
if( anext===999)
anext=0;
return $.data.frame[anext];
}
this.trans=function()
{
var oldlock=lock;
lockout--;
if( lockout===0)
lock=0; //reset transition lock
if( wait===0)
{
if( next===0)
{
//do nothing
}
else
{
if( next===1000)
{
$.match.destroy_object($);
return;
}
if( next===999)
next=0;
$.frame.PN=$.frame.N;
$.frame.N=next;
$.state_update('frame_exit');
//state transition
var is_trans = $.frame.D.state !== $.data.frame[next].state;
if( is_trans)
$.state_update('state_exit');
$.frame.D=$.data.frame[next];
if( is_trans)
{
$.statemem = {};
var old_switch_dir=$.allow_switch_dir;
if( $.states_switch_dir && $.states_switch_dir[$.frame.D.state] !== undefined)
$.allow_switch_dir=$.states_switch_dir[$.frame.D.state];
else
$.allow_switch_dir=false;
$.state_update('state_entry');
if( $.allow_switch_dir && !old_switch_dir)
{
if( $.con)
{
if($.con.state.left) $.switch_dir('left');
if($.con.state.right) $.switch_dir('right');
}
}
}
$.frame_update();
if( oldlock===10 || oldlock===11) //combo triggered action
if( wait>0)
wait-=1;
}
}
else
wait--;
}
} // frame_transistor
return livingobject;
});