-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrosewood.js
More file actions
1054 lines (1035 loc) · 40.7 KB
/
rosewood.js
File metadata and controls
1054 lines (1035 loc) · 40.7 KB
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview The Rosewood js gaming engine, because games should be fun
* @author Caz vonKow caz@vonkow.com
* @version 0.75.3
*/
(function(){
/****** "Global" vars ******/
var Run,
runGame = false,
curT = 0,
globT = 0,
speed = 0,
currentLag = 0,
X = 0,
Y = 0,
tiles = false,
tileX = 0,
tileY = 0,
keyChange = false,
keys = [['bsp',8],['tab',9],['ent',13],['shf',16],['ctr',17],['alt',18],['pau',19],['cap',20],['esc',27],['sp',32],['pgu',33],['pgd',34],['end',35],['hom',36],['la',37],['ua',38],['ra',39],['da',40],['ins',45],['del',46],['d0',48],['d1',49],['d2',50],['d3',51],['d4',52],['d5',53],['d6',54],['d7',55],['d8',56],['d9',57],['sem',59],['eql',61],['a',65],['b',66],['c',67],['d',68],['e',69],['f',70],['g',71],['h',72],['i',73],['j',74],['k',75],['l',76],['m',77],['n',78],['o',79],['p',80],['q',81],['r',82],['s',83],['t',84],['u',85],['v',86],['w',87],['x',88],['y',89],['z',90],['lwn',91],['rwn',92],['sel',93],['n0',96],['n1',97],['n2',98],['n3',99],['n4',100],['n5',101],['n6',102],['n7',103],['n8',104],['n9',105],['mul',106],['add',107],['sub',109],['dec',110],['div',111],['f1',112],['f2',113],['f3',114],['f4',115],['f5',116],['f6',117],['f7',118],['f8',119],['f9',120],['f10',121],['f11',122],['f12',123],['num',144],['scr',145],['com',188],['per',190],['fsl',191],['acc',192],['obr',219],['bsl',220],['cbr',221],['qot',222]],
mouseX = 0,
mouseY = 0,
mouseD = false,
moveAllX = 0,
moveAllY = 0,
moveAllZ = 0,
states = {},
stopCallback = null,
AJResps = [],
rw = {
sprites: {},
soundBank: {},
sounds: [],
ents: [],
rules: {},
ruleList: []
}
/****** Loaders ******/
rw.loadSprites = function(sprites, callback) {
function loadNext(arr) {
if (arr.length) {
var i = arr.pop(),
img = new Image()
img.onload = function() {
// Add logic for multi-sprite images, check for object vs array
if (i.length==2) {
rw.sprites[i[0]] = {}
for (var x in i[1]) {
if (x!='src') {
rw.sprites[i[0]][x] = [this, i[1][x][0], i[1][x][1], i[1][x][2]||0, i[1][x][3]||0]
}
}
} else {
rw.sprites[i[0]] = [this, i[2], i[3], i[4]||0, i[5]||0]
}
loadNext(arr)
}
img.onerror = function() { loadNext(arr) }
//Add check here too
img.src = (i.length==2) ? i[1].src : i[1]
} else {
callback()
}
}
var x,
arr = []
for (x in sprites) {
// This may also need checking for multi-sprite images
var i = sprites[x]
i.length ? arr.push([x, i[0], i[1], i[2], i[3], i[4]]) : arr.push([x, i])
}
loadNext(arr)
}
rw.loadSounds = function(sounds, callback) {
function loadNext(arr) {
if (arr.length) {
var s = arr.pop(),
snd = new Audio()
rw.soundBank[s[0]] = snd
function nxt() { loadNext(arr) }
snd.addEventListener("canplay", nxt, false)
snd.src = s[1]
} else {
callback()
}
}
loadNext(sounds)
}
/****** Main Methods ******/
rw.playSound = function(sound) {
var len = rw.sounds.length
rw.sounds[len] = document.createElement('audio')
rw.sounds[len].src = rw.soundBank[sound].src
rw.sounds[len].play()
return rw
}
rw.getTime = function(type) {
return (type=='g') ? curT+globT : curT
}
rw.Xdim = function() {
return X
}
rw.Ydim = function() {
return Y
}
rw.setFPS = function(fps) {
speed = 1000/parseInt(fps)
return rw
}
rw.getFPS = function() {
return 1000/speed
}
rw.getLag = function() {
return currentLag
}
rw.tilesOn = function(xDim, yDim) {
tiles = true
tileX = xDim
tileY = yDim
return rw
}
rw.tilesOff = function() {
tiles = false
tileX = 0
tileY = 0
return rw
}
// AJAX Functions, not quite finished
rw.get = function(targ, func) {
var xhr = new XMLHttpRequest()
xhr.open("GET",targ,true)
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
var resp = xhr.responseText
AJResps.push([func,resp])
}
}
xhr.send(null)
return rw
}
rw.post = function(targ, params, func) {
var xhr = new XMLHttpRequest()
xhr.open("POST",targ,true)
xhr.setRequestHeader("Content-type", "application/json")
xhr.setRequestHeader("Content-length", params.length)
xhr.setRequestHeader("Connection", "close")
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
var resp = xhr.responseText||'error'
AJResps.push([func,resp])
}
}
xhr.send(params)
return rw
}
rw.func = function() {
return rw
}
// Not needed in canvas, keep for if we bring back divs?
rw.wipeBoard = function() {
var board = document.getElementById('board'),
total = board.childNodes.length
for (var x=0; x<total; x++) {
board.removeChild(board.childNodes[0])
}
return rw
}
rw.wipeEnts = function() {
rw.ents = []
return rw
}
rw.wipeRules = function() {
rw.rules = {}
rw.ruleList = [[],[],[],[]]
return rw
}
rw.wipeAll = function() {
rw.wipeBoard().wipeEnts().wipeRules()
return rw
}
rw.start = function() {
if (runGame==false) {
runGame = true
curT = setTimeout(function() { Run([]) }, speed)
}
return rw
}
rw.stop = function(callback) {
stopCallback = callback
runGame = false
return rw
}
/****** Keyboard Input ******/
var keySwitch = function(code, bit) {
var len = keys.length
for (var x=0;x<len;x++) {
if (keys[x][1]==code) {
keys[x][2] = bit
}
}
}
var keyDown = function(e) {
var ev = e ? e : window.event
keySwitch(ev.keyCode, true)
keyChange = true
}
var keyUp = function(e) {
var ev = e ? e : window.event
keySwitch(ev.keyCode, false)
keyChange = true
}
rw.key = function(key) {
var len=keys.length
for (var x=0; x<len;x++) {
if (keys[x][0]==key) {
return keys[x][2]
}
}
}
/****** Mouse Input ******/
var mousePos = function(e) {
if (!e) var e=window.event
if (e.offsetX) {
mouseX=e.offsetX
mouseY=e.offsetY
} else {
mouseX=e.layerX
mouseY=e.layerY
}
}
var mouseDown = function(e) {
if (!e) var e = window.event
mouseD = true
}
var mouseUp = function(e) {
if (!e) var e = window.event
mouseD = false
}
rw.mouse = {
x : function() {
return mouseX
},
y : function() {
return mouseY
},
down : function() {
return mouseD
}
}
rw.changeCursor = function(cursor) {
document.getElementById('board').style.cursor="url('"+cursor+"')"
return rw
}
/****** Ents ******/
rw.Ent = function(nameIn, spriteIn, widthIn, heightIn) {
this.ent = ''
this.name = nameIn
this.sprite = spriteIn
this.width = widthIn
this.height = heightIn
this.posX = 0
this.posY = 0
this.posZ = 0
this.velX = 0
this.velY = 0
this.velZ = 0
this.r = 0
this.active = false
this.visible = false
}
rw.Ent.prototype.back = function() {
return this.ent
}
rw.Ent.prototype.end = function() {
return rw
}
rw.Ent.prototype.posX1 = function() {
return this.posX
}
rw.Ent.prototype.posY1 = function() {
return this.posY
}
rw.Ent.prototype.posX2 = function() {
return this.posX+this.width
}
rw.Ent.prototype.posY2 = function() {
return this.posY+this.height
}
rw.Ent.prototype.display = function (posXIn, posYIn, posZIn) {
this.posX = posXIn
this.posY = posYIn
this.posZ = (posZIn||posZIn===0) ? posZIn : posYIn
this.active = true
this.visible = (this.sprite!==' ') ? true : false
return this
}
rw.Ent.prototype.hide = function() {
this.active=false
this.visible=false
return this
}
rw.Ent.prototype.changeSprite = function(sprite) {
this.sprite = sprite
this.visible = (this.sprite!='')
return this
}
rw.Ent.prototype.rot = function(deg) {
this.r = deg * 0.0174532925
return this
}
rw.Ent.prototype.curRot = function() {
return this.r * 57.2957795
}
rw.Ent.prototype.curMove = function() {
return [this.velX, this.velY, this.velZ]
}
rw.Ent.prototype.wipeMove = function(axis) {
if (axis) {
(axis=='x') ? this.velX = 0 :
(axis=='y') ? this.velY = 0 :
(axis=='z') ? this.velZ = 0 : true
} else {
this.velX = 0
this.velY = 0
this.velZ = 0
}
return this
}
rw.Ent.prototype.moveTo = function(x, y, z) {
this.posX = x
this.posY = y
this.posZ = (z||z===0) ? z : y
return this
}
rw.Ent.prototype.getTileX=function() {
return (tiles) ? Math.floor(this.posY/tileY) : false
}
rw.Ent.prototype.getTileY=function() {
return (tiles) ? Math.floor(this.posY/tileY) : false
}
// Depreciated
rw.Ent.prototype.clicked = function() {
if (rw.mouse.down()) {
if ((rw.mouse.x()>this.posX1())&&(rw.mouse.x()<this.posX2())) {
if ((rw.mouse.y()>this.posY1())&&(rw.mouse.y()<this.posY2())) {
return true
}
}
}
return false
}
rw.Ent.prototype.move = function(x,y,z) {
this.velX += x
this.velY += y
this.velZ += (z||z===0) ? z : y
return this
}
rw.newEnt = function(ent) {
ent.base['ent'] = ent
ent.update = ent.update || function() {}
rw.ents.push(ent)
if (ent.base.sprite!=='') ent.base.visible = true
if (ent.init) ent.init()
return ent
}
rw.removeEnt = function(entNum) {
rw.ents.splice(entNum, 1)
return rw
}
rw.moveAll = function(x,y,z) {
moveAllX = x
moveAllY = y
moveAllZ = z || 0
}
/****** Rules ******/
rw.Rule = function(pos) {
this.pos = pos
}
rw.newRule = function(name, rule) {
rw.rules[name] = rule
rw.ruleList[rule.base.pos].push(name)
return rw
}
rw.removeRule = function(rule) {
if (rw.rules[rule]) {
var pos = rw.rules[rule].base.pos,
list = rw.ruleList[pos]
for (var x=0, len=list.length; x<len; x++) {
if (list[x]==rule) {
list.splice(x,1)
break
}
}
delete rw.rules[rule]
}
return rw
}
/****** States ******/
var copy = function(obj,par) {
var newCopy = (obj instanceof Array) ? [] : {}
// Maybe add hasOwnProperty here
for (prop in obj) {
// Don't add ent.base.ent, as this gets all loopy (in the infinite sense)
// rw.loadState() handles re-adding ent.base.ent on state load.
if (prop=='ent') {
newCopy[prop]=''
} else {
if (obj[prop] && typeof obj[prop] == 'object') {
newCopy[prop] = copy(obj[prop],newCopy)
} else {
newCopy[prop] = obj[prop]
}
}
}
return newCopy
}
rw.saveState = function(name) {
states[name] = {}
states[name]['ents']=copy(rw.ents,rw)
states[name]['rules']=copy(rw.rules,rw)
states[name]['ruleList']=copy(rw.ruleList,rw)
var len = states[name]['ents'].length
for (var x=0; x<len; x++) {
states[name]['ents'][x].base['ent'] = states[name]['ents'][x]
}
return rw
}
rw.isState=function(name) {
return (states[name]) ? true : false
}
rw.loadState = function(name) {
if (states[name]) {
rw.ents = copy(states[name]['ents'],name)
rw.rules = copy(states[name]['rules'],name)
rw.ruleList = copy(states[name]['ruleList'],name)
var len = rw.ents.length
for (var x=0;x<len;x++) {
rw.ents[x].base.ent = rw.ents[x]
if (rw.ents[x].base.active==true) {
rw.ents[x].base.display(
rw.ents[x].base.posX,
rw.ents[x].base.posY,
rw.ents[x].base.posZ
)
}
}
// Force keychange
keyChange = true
}
return rw
}
rw.rmState = function(name) {
if (states[name]) delete states[name]
return rw
}
/****** Init and Run ******/
rw.browser = {
check: function() {
var trans = function() {
var body = document.getElementsByTagName('body')[0],
properties = ['transform', 'WebkitTransform', 'MozTransform'],
p
while (p = properties.shift()){
if (typeof body.style[p]!='undefined') return p
}
return false
}
this.trans_name = (trans()||'none')
},
trans_name: 'none'
}
rw.init = function(target, uSet, callback) {
var settings = {
x: uSet.x||0,
y: uSet.y||0,
FPS: uSet.FPS||30,
keys: (((uSet.keys)||(uSet.keys===false)) ? uSet.keys : true),
mouse: (((uSet.mouse)||(uSet.mouse===false)) ? uSet.mouse : true),
sound: (((uSet.sound)||(uSet.sound===false)) ? uSet.sound : true),
// if you set canvas=false, there's no dom fallback, so you're basically screwed, for now.
canvas: (((uSet.canvas)||(uSet.canvas===false)) ? uSet.canvas : true),
sequence: uSet.sequence || [
'ajax',
'rule',
'ents',
'rule',
'cols',
'kill',
'rule',
'blit',
'rule'
]
}
rw.browser.check()
rw.setFPS(settings.FPS)
X = settings.x
Y = settings.y
if (settings.canvas) {
var board = document.createElement('canvas')
board.id = 'board'
board.width = settings.x
board.height = settings.y
board.style.width = settings.x
board.style.height = settings.y
// Keep for when/if we re-add dom support?
//board.style.overflow = 'hidden' // Not needed?
//board.style.position = "relative" // Not needed?
document.getElementById(target).appendChild(board)
}
if (settings.keys) {
if (settings.keys instanceof Array) {
for (var x = 0; x<keys.length; x++) {
var needed = false
for (var y = 0; y<settings.keys.length; y++) {
if (keys[x][0]==settings.keys[y]) needed = true
}
if (!needed) keys.splice(x--, 1)
}
}
if (window.document.addEventListener) {
window.document.addEventListener("keydown", keyDown, false)
window.document.addEventListener("keyup", keyUp, false)
} else {
window.document.attachEvent("onkeydown", keyDown)
window.document.attachEvent("onkeyup", keyUp)
}
}
// Change this to do it the "right" way
if (settings.mouse) {
board.onmousemove = mousePos
board.onmousedown = mouseDown
board.onmouseup = mouseUp
}
var runFunc = composeLoop(settings.sequence, 0, 0, function(tbk){return tbk})
Run = function(tbk) {
var startTime = new Date(),
tbk = tbk||[]
moveAllX = 0
moveAllY = 0
moveAllZ = 0
killFinishedSounds()
tbk = runFunc(tbk)
var endTime = new Date()
var timeTotal = endTime-startTime
if (runGame==true) {
if (timeTotal<speed) {
curT = setTimeout(function() { Run(tbk) }, speed-timeTotal)
} else {
// Gotta set a timeout here so we don't grow the call stack
curT = setTimeout(function() { Run(tbk) }, 0)
}
currentLag = timeTotal-speed
} else {
clearTimeout(rw.curT)
globT += curT
curT = 0
if (typeof(stopCallback)=='function') stopCallback()
stopCallback = null
}
}
return rw
}
// Here lies witchcraft and hints of erlang...
function composeLoop(seq, count, ruleCount, func) {
var f = function(tbk) {func(tbk)}
if (count<seq.length) {
var cur = seq[seq.length-count-1]
if (cur=='rule') {
var rl = ruleLoop(ruleCount)
f = function(tbk) {func(rl(tbk))}
rw.ruleList.push([])
ruleCount++
} else if (cur=='ents') {
f = function(tbk) {func(updateEnts(tbk))}
} else if (cur=='cols') {
f = function(tbk) {func(collisionLoop(tbk))}
} else if (cur=='ajax') {
f = function(tbk) {func(ajLoop(tbk))}
} else if (cur=='kill') {
f = function(tbk) {func(killLoop(tbk))}
} else if (cur=='blit') {
f = function(tbk) {func(redrawLoop(tbk))}
}
return composeLoop(seq, count+1, ruleCount, f)
} else {
return f
}
}
function killFinishedSounds() {
for (var x=0; x<rw.sounds.length; x++) {
if (rw.sounds[x].ended) {
rw.sounds.splice(x,1)
x--
}
}
}
function ajLoop(tbk) {
while (AJResps.length) {
AJResps[0][0](AJResps[0][1])
AJResps.splice(0,1)
}
return tbk
}
function ruleLoop(listId) {
return function(tbk) {
for (var x=0, l=rw.ruleList[listId].length; x<l; x++) {
rw.rules[rw.ruleList[listId][x]].rule()
}
return tbk
}
}
function updateEnts(toBeRemoved) {
// Start Update Loop
var len = rw.ents.length
// Update Loop
/*
if (keyChange==true) {
for (var x=0; x<len; x++) {
var curEnt = rw.ents[x]
if (curEnt.base.active==true) {
if (curEnt.keyChange) {
curEnt.keyChange()
}
var currentSprite = curEnt.update(curEnt.base.posX, curEnt.base.posY, curEnt.base.posX+curEnt.base.width, curEnt.base.posY+curEnt.base.height)
if (currentSprite==false) {
toBeRemoved.push(x)
} else {
//Nothing for now
}
} else if (curEnt.inactive) {
var currentSprite = curEnt.inactive()
if (currentSprite==false) {
toBeRemoved.push(x)
}
}
}
keyChange = false
} else {
*/
for(var x=0; x<len; x++) {
// Could change curEnt to ent.base and use .ent to access update(), make things cleaner
var curEnt = rw.ents[x]
if (curEnt.base.active==true) {
var currentSprite = curEnt.update(curEnt.base.posX, curEnt.base.posY, curEnt.base.posX+curEnt.base.width, curEnt.base.posY+curEnt.base.height)
if (currentSprite==false) toBeRemoved.push(x)
} else if (curEnt.inactive) {
if (curEnt.inactive()==false) toBeRemoved.push(x)
}
}
//}
return toBeRemoved
}
var rotatePoint=function(p,o,a) {
var ang = a*0.0174532925,
trans=[p[0]-o[0],p[1]-o[1]],
newP=[(trans[0]*Math.cos(ang))-(trans[1]*Math.sin(ang)),(trans[0]*Math.sin(ang))+(trans[1]*Math.cos(ang))]
newP[0]+=o[0]
newP[1]+=o[1]
return newP
}
// Point in Triangle Test
var pointInTri=function(p,a,b,c) {
var cp = ((b[0]-a[0])*(p[1]-a[1]))-((b[1]-a[1])*(p[0]-a[0])),
cp_ref = ((b[0]-a[0])*(c[1]-a[1]))-((b[1]-a[1])*(c[0]-a[0]))
return (cp_ref>=0) ? cp>=0 : cp<=0
}
// Check Point in tri
var checkTriCol=function(p,a,b,c) {
return (pointInTri(p,a,b,c) && pointInTri(p,b,c,a) && pointInTri(p,c,a,b))
}
// Check Point in rec
var checkRecCol=function(p,a,b) {
return !((p[0]<a[0]) || (p[0]>b[0]) || (p[1]<a[1]) || (p[1]>b[1]))
}
// Check Point in circle
var checkPtCirc=function(px,py,cx,cy,cr) {
var dist = ((cx-px)*(cx-px))+((cy-py)*(cy-py))
return dist<(cr*cr)
}
// Check Circle in circle
var checkCircCirc=function(c1x,c1y,c1r,c2x,c2y,c2r) {
var dist = ((c1x-c2x)*(c1x-c2x))+((c1y-c2y)*(c1y-c2y))
return dist<((c1r+c2r)*(c1r+c2r))
}
// Check circle cross line
var checkCircLine=function(a,b,c) {
if (((c[0]-c[2]>a[0])&&(c[0]-c[2]>b[0])) ||
((c[0]+c[2]<a[0])&&(c[0]+c[2]<b[0])) ||
((c[1]-c[2]>a[1])&&(c[1]-c[2]>b[1])) ||
((c[1]+c[2]<a[1])&&(c[1]+c[2]<b[1]))) {
return false
} else {
var doubleArea=(a[0]*b[1])+(b[0]*c[1])+(c[0]*a[1])-(b[0]*a[1])-(c[0]*b[1])-(a[0]*c[1])
if (doubleArea<0) doubleArea=-doubleArea
var base=Math.sqrt(((b[0]-a[0])*(b[0]-a[0]))+((b[1]-a[1])*(b[1]-a[1]))),
dist=doubleArea/base
return c[2]>dist
}
}
// Check Circle in rec
// Currently Fucked
var checkCircRec=function(cx,cy,cr,rx1,ry1,rx2,ry2) {
//console.log(cx+' '+cy+' '+cr+' '+rx1+' '+ry1+' '+rx2+' '+ry2)
var rW = (rx2-rx1)/2,
circDistX = cx-(rx1+rW)
if (circDistX<0) circDistX=-circDistX
if (circDistX<rW+cr) {
var rH = (ry2-ry1)/2,
circDistY = cy-(ry1+rH)
if (circDistY<0) circDistY=-circDistY
if (circDistY<rH+cr) {
if ((circDistX<=rW) ||
(circDistY<=rH)) {
return true
} else {
var cornerDistSq = ((circDistX+rW)*(circDistX+rW))+((circDistY+rH)*(circDistY+rH))
return cornerDistSq<=cr*cr
}
}
}
return false
}
function collisionLoop(toBeRemoved) {
var len = rw.ents.length,
cols = []
// For each ent
for (var x=0;x<len;x++) {
var eX=rw.ents[x]
if (eX.base.active&&eX.hitMap) {
for (var y=x+1;y<len;y++) {
var eY=rw.ents[y]
if (eY.base.active&&eY.hitMap) {
for (var z=0;z<eX.hitMap.length;z++) {
var eXm=eX.hitMap[z]
for (var w=0;w<eY.hitMap.length;w++) {
var eYm=eY.hitMap[w]
var canHit=false,
canHitMap=eXm[1]
if (canHitMap) {
var hitLen=canHitMap.length
for (var v=0;v<hitLen;v++) {
if (canHitMap[v]==eYm[0]) canHit=true
}
}
if (canHit) {
var eXx=eX.base.posX+eX.base.velX,
eXy=eX.base.posY+eX.base.velY,
eYx=eY.base.posX+eY.base.velX,
eYy=eY.base.posY+eY.base.velY,
hit = true
// If ent 1 hitMap is triangle
if (eXm.length==8) {
// If ent 2 hitMap is triangle
if (eYm.length==8) {
// Test tri tri
var eXp1 = [eXm[2]+eXx,eXm[3]+eXy],
eXp2 = [eXm[4]+eXx,eXm[5]+eXy],
eXp3 = [eXm[6]+eXx,eXm[7]+eXy],
eYp1 = [eYm[2]+eYx,eYm[3]+eYy],
eYp2 = [eYm[4]+eYx,eYm[5]+eYy],
eYp3 = [eYm[6]+eYx,eYm[7]+eYy]
hit = (checkTriCol(eXp1,eYp1,eYp2,eYp3) ? true :
checkTriCol(eXp2,eYp1,eYp2,eYp3) ? true :
checkTriCol(eXp3,eYp1,eYp2,eYp3) ? true :
checkTriCol(eYp1,eXp1,eXp2,eXp3) ? true :
checkTriCol(eYp2,eXp1,eXp2,eXp3) ? true :
checkTriCol(eYp3,eXp1,eXp2,eXp3) ? true : false)
} else if (eYm.length==6) {
// Test tri rec
var eXp1 = [eXm[2]+eXx,eXm[3]+eXy],
eXp2 = [eXm[4]+eXx,eXm[5]+eXy],
eXp3 = [eXm[6]+eXx,eXm[7]+eXy],
eYp1 = [eYm[2]+eYx,eYm[3]+eYy],
eYp2 = [eYm[2]+eYx,eYm[5]+eYy],
eYp3 = [eYm[4]+eYx,eYm[5]+eYy],
eYp4 = [eYm[4]+eYx,eYm[3]+eYy]
hit = (checkRecCol(eXp1,eYp1,eYp3) ? true :
checkRecCol(eXp2,eYp1,eYp3) ? true :
checkRecCol(eXp3,eYp1,eYp3) ? true :
checkTriCol(eYp1,eXp1,eXp2,eXp3) ? true :
checkTriCol(eYp2,eXp1,eXp2,eXp3) ? true :
checkTriCol(eYp3,eXp1,eXp2,eXp3) ? true :
checkTriCol(eYp4,eXp1,eXp2,eXp3) ? true : false)
} else if (eYm.length==5) {
// Test tri circ
var c = [eYm[2]+eYx,eYm[3]+eYy,eYm[4]],
tp1 = [eXm[2]+eXx,eXm[3]+eXy],
tp2 = [eXm[4]+eXx,eXm[5]+eXy],
tp3 = [eXm[6]+eXx,eXm[7]+eXy]
hit = (checkTriCol([c[0],c[1]],tp1,tp2,tp3) ? true :
checkCircLine(tp1,tp2,c) ? true :
checkCircLine(tp2,tp3,c) ? true :
checkCircLine(tp1,tp3,c) ? true : false)
} else {
// Test tri point
var eXp1 = [eXm[2]+eXx,eXm[3]+eXy],
eXp2 = [eXm[4]+eXx,eXm[5]+eXy],
eXp3 = [eXm[6]+eXx,eXm[7]+eXy],
eYp1 = [eYm[2]+eYx,eYm[3]+eYy]
hit = checkTriCol(eYp1,eXp1,eXp2,eXp3)
}
} else if (eXm.length==6) {
// Ent 1 is rec
if (eYm.length==8) {
// Test rec tri
var eXp1 = [eXm[2]+eXx,eXm[3]+eXy],
eXp2 = [eXm[2]+eXx,eXm[5]+eXy],
eXp3 = [eXm[4]+eXx,eXm[5]+eXy],
eXp4 = [eXm[4]+eXx,eXm[3]+eXy],
eYp1 = [eYm[2]+eYx,eYm[3]+eYy],
eYp2 = [eYm[4]+eYx,eYm[5]+eYy],
eYp3 = [eYm[6]+eYx,eYm[7]+eYy]
hit = (checkRecCol(eYp1,eXp1,eXp3) ? true :
checkRecCol(eYp2,eXp1,eXp3) ? true :
checkRecCol(eYp3,eXp1,eXp3) ? true :
checkTriCol(eXp1,eYp1,eYp2,eYp3) ? true :
checkTriCol(eXp2,eYp1,eYp2,eYp3) ? true :
checkTriCol(eXp3,eYp1,eYp2,eYp3) ? true :
checkTriCol(eXp4,eYp1,eYp2,eYp3) ? true : false)
} else if (eYm.length==6) {
// Test rec rec
if (eXx+eXm[4]<=eYx+eYm[2]) {
hit = false
} else if (eXx+eXm[2]>=eYx+eYm[4]) {
hit = false
} else if (eXy+eXm[5]<=eYy+eYm[3]) {
hit = false
} else if (eXy+eXm[3]>=eYy+eYm[5]) {
hit = false
}
} else if (eYm.length==5) {
// Test rec circ
var cx=eYm[2]+eYx,
cy=eYm[3]+eYy,
cr=eYm[4],
rx1 = eXm[2]+eXx,
rx2 = eXm[4]+eXx,
ry1 = eXm[3]+eXy,
ry2 = eXm[5]+eXy
hit = checkCircRec(cx,cy,cr,rx1,ry1,rx2,ry2)
} else {
// Test rec pt
var rp1 = [eXm[2]+eXx,eXm[3]+eXy],
rp2 = [eXm[4]+eXx,eXm[5]+eXy],
p = [eYm[2]+eYx,eYm[5]+eYy]
hit = checkRecCol(p,rp1,rp2)
}
} else if (eXm.length==5) {
// Ent 1 is circ
if (eYm.length==8) {
// Test circ tri
var c = [eXm[2]+eXx,eXm[3]+eXy,eXm[4]],
tp1 = [eYm[2]+eYx,eYm[3]+eYy],
tp2 = [eYm[4]+eYx,eYm[5]+eYy],
tp3 = [eYm[6]+eYx,eYm[7]+eYy]
hit = (checkTriCol([c[0],c[1]],tp1,tp2,tp3) ? true :
checkCircLine(tp1,tp2,c) ? true :
checkCircLine(tp2,tp3,c) ? true :
checkCircLine(tp1,tp3,c) ? true : false)
} else if (eYm.length==6) {
// Test circ rec
var cx=eXm[2]+eXx,
cy=eXm[3]+eXy,
cr=eXm[4],
rx1 = eYm[2]+eYx,
rx2 = eYm[4]+eYx,
ry1 = eYm[3]+eYy,
ry2 = eYm[5]+eYy
hit = checkCircRec(cx,cy,cr,rx1,ry1,rx2,ry2)
} else if (eYm.length==5) {
// Test circ circ
var c1x = eXm[2]+eXx,
c1y = eXm[3]+eXy,
c1r = eXm[4],
c2x = eYm[2]+eYx,
c2y = eYm[3]+eYy,
c2r = eYm[4]
hit = checkCircCirc(c1x,c1y,c1r,c2x,c2y,c2r)
} else {
// Test circ pt
}
} else {
// Ent 1 is point
}
// If collision, add to list.
// Maybe change this to call a collision resolution function for each ent?
if (hit==true) cols[cols.length]=[[x,eXm[0]],[y,eYm[0]]]
}
}
}
}
}
}
// does this still work?
if (eX.postCol) eX.postCol()
}
for (var x=0; x<cols.length; x++) {
if (rw.ents[cols[x][0][0]].gotHit) {
if (rw.ents[cols[x][0][0]].gotHit(cols[x][1][1],cols[x][0][1],cols[x][1][0])==false) {
toBeRemoved.push(cols[x][0][0])
}
}
if (rw.ents[cols[x][1][0]].gotHit) {
if (rw.ents[cols[x][1][0]].gotHit(cols[x][0][1],cols[x][1][1],cols[x][0][0])==false) {
toBeRemoved.push(cols[x][1][0])
}
}
}
return toBeRemoved
}
function killLoop(toBeRemoved) {
if (toBeRemoved.length>0) {
toBeRemoved.sort(function(a,b){return a - b})
toBeRemoved.reverse()
var killThese = []
o:for(var x=0; x<toBeRemoved.length; x++) {
for(var y=0; y<killThese.length; y++) {
if (killThese[y]==toBeRemoved[x]) {
continue o
}
}
killThese[killThese.length] = toBeRemoved[x]
}
for (var x=0;x<killThese.length;x++) {
rw.removeEnt(killThese[x])
}
}
return []
}
function redrawLoop(toBeRemoved) {
var zOrder = {},
zKey = [],
board = document.getElementById('board').getContext('2d')
for (var x=0, l=rw.ents.length; x<l; x++) {
var z = rw.ents[x].base.posZ
if (!zOrder[z]) {
zOrder[z]= []
zKey.push(z)
}
zOrder[z].push(x)
}
zKey = zKey.sort(function(a,b){return a-b})
// Clear old board
board.clearRect(0,0,X,Y)
// Redraw all ents, starting with the lowest z-Index
for (var x=0, xl=zKey.length; x<xl;x++) {
var curOrder = zOrder[zKey[x]]
for (var y=0, yl=curOrder.length; y<yl; y++) {
var curEnt = rw.ents[curOrder[y]].base
// !!! Move ent movement to previous loop, so zIndex is properly calc'd
curEnt.posX += curEnt.velX+moveAllX
curEnt.posY += curEnt.velY+moveAllY
curEnt.posZ += curEnt.velZ+moveAllZ
curEnt.wipeMove()
if (curEnt.visible) {
// THIS SHOULD BE CHANGED TO SOME FLAGS INSTED OF STRING COMPARISON
if (curEnt.sprite=='text') {
var txt = curEnt.ent.text
board.font = txt.style.font || board.font || '10px sans-serif'
board.textAlign = txt.style.align || board.textAlign || 'start'
board.textBaseline = txt.style.baseline || board.textBaseline || 'alphabetic'
if (txt.form=='fill') {
board.fillStyle = txt.style.color || board.fillStyle || '#000'
board.fillText(txt.text, curEnt.posX, curEnt.posY)
} else if (txt.form=='stroke') {
board.strokeStyle = txt.style.color || board.strokeStyle || '#000'
board.strokeText(txt.text, curEnt.posX, curEnt.posY)
}
} else if (curEnt.sprite=='draw') {
// Add drawing stuff
//board.beginPath()
//board.rect(
//curEnt.posX,curEnt.posY,
//curEnt.posX+curEnt.width,
//curEnt.posY+curEnt.height
//)
//board.clip()
curEnt.ent.draw(board)
//board.beginPath()
//board.rect(0,0,X,Y)
//board.clip()