-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
2519 lines (2356 loc) · 131 KB
/
game.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
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
/*
------------------------------------------------------Changelog------------------------------------------------------
Rarities:
normal --> uncommon --> rare --> super rare --> epic --> legendary --> mythical --> godly --> EX
grey green blue purple silver gold red diamond black
0 1 2 3 4 5 6 7 8
x1 x1.3 x1.7 x2.2 x3 x4 x6 x9
for (let i = 0; i < 250; i++) {
game.gamestate.player.characters[0].exp += 75; focusCharacter(0); await sleep(10);
}
---------------------------------------------------------------------------------------------------------------------
*/
// Constants (catches spelling mistakes, the code will error if one of these is spelt wrong)
const N = 0;
const UC = 1;
const R = 2;
const SR = 3;
const E = 4;
const L = 5;
const M = 6;
const G = 7;
const EX = 8;
const str = 'str';
const int = 'int';
const hp = 'hp';
const mp = 'mp';
const none = false;
const physical = 'physical';
const magic = 'magic';
const piercing = 'piercing';
const normal = 'normal';
const heal = 'heal';
const effect = 'effect';
const male = 'male';
const female = 'female';
const single = 'single target';
const multi = 'multi target';
const aoe = 'area of effect';
const selfOnly = 'affects self only';
const summon = 'summon';
const ranged = 'ranged';
const melee = 'melee';
const fullScreen = 'fullScreen';
const self = 'self';
const grey = 'grey';
const red = 'red';
const bronze = 'bronze';
const silver = 'silver';
const black = 'black';
const gold = 'gold'; // both a colour and a currency
const exp = 'exp';
const item = 'item';
const hand = 'hand';
const body = 'body';
function deepFreeze(obj) {
let propNames = Object.getOwnPropertyNames(obj);
for (let name of propNames) {
let value = obj[name];
if (typeof value === 'object' && value !== null) {
deepFreeze(value);
}
}
return Object.freeze(obj);
};
// load data
import {gachaGameData} from "./data.js";
const data = JSON.parse(JSON.stringify(gachaGameData));
data.items = sortInventory(data.items);
deepFreeze(data);
window.data = data;
const game = {
gamestate: undefined,
keypresses: [], // obsolete
mousepos: {x: 0, y: 0}, // obsolete
forceMobile: false,
forceDesktop: false,
altMobile: false,
particles: {},
display: {x: window.innerWidth, y: window.innerHeight},
tempStorage: {},
debug: false,
}; window.game = game;
// The support functions that might not be necessary
function generateId() {
const timestamp = Date.now().toString(36);
const randomNum = Math.random().toString(36).slice(2, 11);
return `${timestamp}-${randomNum}`;
}; window.generateId = generateId;
function randchoice(list, remove = false) { // chose 1 from a list and update list
let length = list.length;
let choice = randint(0, length-1);
if (remove) {
let chosen = list.splice(choice, 1);
return [chosen, list];
}
return list[choice];
}; window.randchoice = randchoice;
function randint(min, max, notequalto=false) {
if (max - min < 1) {
return min;
}
var gen;
var i = 0;
do {
gen = Math.floor(Math.random() * (max - min + 1)) + min;
i += 1;
if (i >= 100) {
console.log('ERROR: could not generate suitable number');
return gen;
}
} while (notequalto && (gen === min || gen === max));
return gen;
}; window.randint = randint;
function randProperty(obj) { // stolen from stack overflow
var keys = Object.keys(obj);
return obj[keys[keys.length * Math.random() << 0]];
}; window.randProperty = randProperty;
function replacehtml(element, text) {
document.getElementById(element).innerHTML = text;
}; window.replacehtml = replacehtml;
function addhtml(element, text) {
document.getElementById(element).innerHTML = document.getElementById(element).innerHTML + text;
}; window.addhtml = addhtml;
function correctAngle(a) {
a = a%(Math.PI*2);
return a;
}; window.correctAngle = correctAngle;
function aim(initial, final) {
if (initial == final) {
return 0;
}
let diff = {x: final.x - initial.x, y: initial.y - final.y};
if (diff.x == 0) {
if (diff.y > 0) {
return 0;
} else {
return Math.PI;
}
} else if (diff.y == 0) {
if (diff.x > 0) {
return Math.PI/2;
} else {
return 3*Math.PI/2;
}
}
let angle = Math.atan(Math.abs(diff.y / diff.x));
if (diff.x > 0 && diff.y > 0) {
return Math.PI/2 - angle;
} else if (diff.x > 0 && diff.y < 0) {
return Math.PI/2 + angle;
} else if (diff.x < 0 && diff.y < 0) {
return 3*Math.PI/2 - angle;
} else {
return 3*Math.PI/2 + angle;
}
}; window.aim = aim;
function roman(number) {
if (number <= 0 || number >= 4000) {
var symbols = ['0','1','2','3','4','5','6','7','8','9','¡','£','¢','∞','§','¶','œ','ß','∂','∫','∆','√','µ','†','¥','ø'];
return `${randchoice(symbols)}${randchoice(symbols)}${randchoice(symbols)}`;
}
const romanNumerals = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};
let romanNumeral = '';
for (let key in romanNumerals) {
while (number >= romanNumerals[key]) {
romanNumeral += key;
number -= romanNumerals[key];
}
}
return romanNumeral;
}; window.roman = roman;
function bigNumber(number) {
const bacs = [`K`, `M`, `B`, `T`, `q`, `Q`, 's', 'S', 'o', 'n', 'd', 'U', 'D']; // caps out at duodectillion (10^39)
let bac = ``;
let i = 0;
while (number >= 1000) {
number /= 1000;
bac = bacs[i]? bacs[i] : '∞';
i++;
}
let a = number >= 10? number >= 100? 1 : 10 : 100;
return bac == '∞'? bac : `${Math.floor(number*a)/a}${bac}`;
}; window.bigNumber = bigNumber;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}; window.sleep = sleep;
function vMath(v1, v2, mode) {
switch (mode) {
case '||':
case 'magnitude':
return Math.sqrt(v1.x**2+v1.y**2);
case '+':
case 'addition':
case 'add':
return {x: v1.x+v2.x, y: v1.y+v2.y};
case '-':
case 'subtraction':
case 'subtract':
return {x: v1.x-v2.x, y: v1.y-v2.y};
case '*':
case 'x':
case 'scalar multiplication':
case 'multiplication':
case 'multiply': // v2 is now a scalar
return {x: v1.x*v2, y: v1.y*v2};
case '/':
case 'division':
case 'divide': // v2 is now a scalar
return {x: v1.x/v2, y: v1.y/v2};
case '•':
case '.':
case 'dot product':
return v1.x * v2.x + v1.y * v2.y;
case 'projection':
case 'vector resolute':
return vMath(v2, vMath(v1, v2, '.')/vMath(v2, null, '||')**2, 'x');
default:
console.error('what are you trying to do to to that poor vector?');
}
}; window.vMath = vMath;
function toComponent(m, r) {
return {x: m * Math.sin(r), y: -m * Math.cos(r), i: m * Math.sin(r), j: -m * Math.cos(r)};
}; window.toComponent = toComponent;
function toPol(i, j) {
if (i instanceof Object) {
if (typeof i.i === 'number') return {m: Math.sqrt(i.i**2+i.j**2), r: aim({x: 0, y: 0}, {x: i.i, y: i.j})};
return {m: Math.sqrt(i.x**2+i.y**2), r: aim({x: 0, y: 0}, {x: i.x, y: i.y})};
}
return {m: Math.sqrt(i**2+j**2), r: aim({x: 0, y: 0}, {x: i, y: j})};
}; window.toPol = toPol;
// a graveyard of failed getCoords functions
/*
function getCoords(id) {
const element = document.getElementById(id);
let rect = element.getBoundingClientRect();
let left = window.pageXOffset || document.documentElement.scrollLeft;
let top = window.pageYOffset || document.documentElement.scrollTop;
let offsetParent = element;
while (offsetParent) {
if (offsetParent.style.position === 'absolute') break;
left += offsetParent.scrollLeft || 0;
top += offsetParent.scrollTop || 0;
left += unPixel(offsetParent.style.left) || 0;
top += unPixel(offsetParent.style.top) || 0;
left -= unPixel(offsetParent.style.right) || 0;
top -= unPixel(offsetParent.style.bottom) || 0;
offsetParent = offsetParent.offsetParent;
}
return {
x: rect.left + rect.width / 2 + left,
y: rect.top + rect.height / 2 + top
};
};*/
/*
function getCoords(id) {
let el = document.getElementById(id);
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { y: _y, x: _x };
}*/
/*
function getCoords(id) {
const relativeRect = document.getElementById(id).getBoundingClientRect();
// Calculate the centers relative to the viewport
const relativeCenterX = relativeRect.left + relativeRect.width / 2;
const relativeCenterY = relativeRect.top + relativeRect.height / 2;
// Calculate the scroll offsets
const scrollX = window.pageXOffset;
const scrollY = window.pageYOffset;
// Calculate the offsets needed to align the centers
const offsetX = (relativeCenterX + scrollX);
const offsetY = (relativeCenterY + scrollY);
return { x: offsetX, y: offsetY };
}*/
// old working version
/*
function getCardCoords(card) { // calculate coordinates manually
let pos = readID(card.id);
let coords = {x: 175, y: 0}; // idk why
//console.log(pos);
switch (pos.row) {
case 'eb':
coords.y += 10;
break;
case 'ef':
coords.y += 220;
break;
case 'pf':
coords.y += document.getElementById('battleScreen').getBoundingClientRect().height - 430;
break;
case 'pb':
coords.y += document.getElementById('battleScreen').getBoundingClientRect().height - 220;
break;
}
coords.x += document.getElementById('battleScreen').getBoundingClientRect().width / 2; // centre
coords.x -= (150 * game.gamestate.battleState[pos.row].length + 20 * (game.gamestate.battleState[pos.row].length - 1)) / 2; // find position 0
coords.x += 150 * pos.pos + 20 * pos.pos; // move to correct pos
//console.log(coords);
return coords;
};*/
function getCardCoords(card) { // calculate coordinates manually
//console.log(document.getElementById('battleScreen').getBoundingClientRect().width, document.getElementById('battleScreen').getBoundingClientRect().height);
let pos = undefined;
if (card.row && typeof card.pos === 'number') pos = card; // allow directly giving position
else pos = readID(card.id);
let coords = {x: -10, y: 0}; // idk why
//console.log(pos);
switch (pos.row) {
case 'eb':
coords.y += 10;
break;
case 'ef':
coords.y += 220;
break;
case 'pf':
coords.y += document.getElementById('battleScreen').getBoundingClientRect().height - 430;
break;
case 'pb':
coords.y += document.getElementById('battleScreen').getBoundingClientRect().height - 220;
break;
}
coords.x += document.getElementById('battleScreen').getBoundingClientRect().width / 2; // centre
coords.x -= (150 * game.gamestate.battleState[pos.row].length + 20 * (game.gamestate.battleState[pos.row].length - 1)) / 2; // find position 0
coords.x += 150 * pos.pos + 20 * pos.pos; // move to correct pos
//console.log(coords);
return coords;
}; window.getCardCoords = getCardCoords;
function unPixel(px) {
return parseFloat(px.slice(0, -2));
}; window.unPixel = unPixel;
// Most of game logic and stuff
window.onkeyup = function(e) {
game.keypresses[e.key.toLowerCase()] = false;
};
window.onkeydown = function(e) {
game.keypresses[e.key.toLowerCase()] = true;
};
document.addEventListener('mousedown', function(event) {
if (event.button === 0) { // Check if left mouse button was clicked
game.keypresses.click = true;
}
});
document.addEventListener('mouseup', function(event) {
if (event.button === 0) { // Check if left mouse button was released
game.keypresses.click = false;
}
});
window.addEventListener("resize", function () {
game.display = {x: window.innerWidth, y: window.innerHeight};
resize();
});
function tellPos(p){
game.mousepos = {x: p.pageX, y:p.pageY};
};
window.addEventListener('mousemove', tellPos, false);
function isMobileDevice() {
let isMobile = /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
return isMobile;
}; window.isMobileDevice = isMobileDevice;
function forceMobile() {
game.forceMobile = true;
game.forceDesktop = false;
resize();
console.warn(`Device type set to mobile`);
return true;
}; window.forceMobile = forceMobile;
function forceDesktop() {
game.forceDesktop = true;
game.forceMobile = false;
resize();
console.warn(`Device type set to desktop`);
return true;
}; window.forceDesktop = forceDesktop;
function altMobile() {
game.forceMobile = true;
game.forceDesktop = false;
game.altMobile = game.altMobile? false : true;
resize();
console.warn(`Toggled alternate mobile view to: ${game.altMobile}`);
return true;
}; window.altMobile = altMobile;
function sortInventory(list, property='name') {
let nList = [];
for (let i = 0; i <= EX; i++) {
let sList = [];
for (let j = 0; j < list.length; j++) {
if (list[j].rarity == i) sList.push(list[j]);
}
sList.sort((a,b) => a[property].localeCompare(b[property]));
for (let j = 0; j < sList.length; j++) {
nList.push(sList[j]);
}
}
return nList;
}; window.sortInventory = sortInventory;
function calcDamage(character, skill) {
console.log(character);
if (skill.dmg == 0) return 0;
let weaponType = skill.weaponType? skill.weaponType : skill.type;
let dmg = skill.dmg;
let isNegative = false;
if (dmg < 0) {
dmg *= -1;
isNegative = true;
}
console.log(dmg);
if (weaponType == physical || weaponType == magic) {
dmg += weaponType == physical? character.physDmgIncrease[0] : character.magiDmgIncrease[0];
console.log(dmg);
dmg *= weaponType == physical? character.physDmgIncrease[1] : character.magiDmgIncrease[1];
console.log(dmg);
dmg = Math.min(dmg, weaponType == physical? character.physDmgIncrease[2] : character.magiDmgIncrease[2]);
}
console.log(dmg);
switch (skill.multiplier) {
case str:
dmg *= character.str;
break;
case int:
dmg *= character.int/100;
break;
}
console.log(dmg);
if (isNegative) dmg *= -1;
return Math.floor(dmg);
}; window.calcDamage = calcDamage;
function getCompactStats(item) {
if (item) return `<strong>${item.displayName}</strong><br>Stats:<br><img src="assets/redSword.png" class="smallIcon"> ATK: ${item.effects.atk.physical[0] >= 0? '+': '-'}${bigNumber(item.effects.atk.physical[0])} ${item.effects.atk.physical[1] >= 0? '+': '-'}${item.effects.atk.physical[1]}%${item.effects.atk.physical[2] > 0? ` <${bigNumber(item.effects.atk.physical[2])}` : ``}<br><img src="assets/blueStar.png" class="smallIcon"> ATK: ${item.effects.atk.magic[0] >= 0? '+': '-'}${bigNumber(item.effects.atk.magic[0])} ${item.effects.atk.magic[1] >= 0? '+': '-'}${item.effects.atk.magic[1]}%${item.effects.atk.magic[2] > 0? ` <${bigNumber(item.effects.atk.magic[2])}` : ``}<br><img src="assets/shield.png" class="smallIcon"> DEF: ${item.effects.def.physical[0] >= 0? '+': '-'}${bigNumber(item.effects.def.physical[0])} +${item.effects.def.physical[1]}%<br><img src="assets/blueShield.png" class="smallIcon"> DEF: ${item.effects.def.magic[0] >= 0? '+': '-'}${bigNumber(item.effects.def.magic[0])} +${item.effects.def.magic[1]}%<br>`;
}; window.getCompactStats = getCompactStats;
function adjustedStats(character) {
return {
hp: Math.floor((character.hp + (character.inventory.hand? character.inventory.hand.effects.stat.hp[0] : 0) + (character.inventory.body? character.inventory.body.effects.stat.hp[0] : 0)) * (1 + (character.inventory.hand? character.inventory.hand.effects.stat.hp[1]/100 : 0) + (character.inventory.body? character.inventory.body.effects.stat.hp[1]/100 : 0))),
mp: Math.floor((character.mp + (character.inventory.hand? character.inventory.hand.effects.stat.mp[0] : 0) + (character.inventory.body? character.inventory.body.effects.stat.mp[0] : 0)) * (1 + (character.inventory.hand? character.inventory.hand.effects.stat.mp[1]/100 : 0) + (character.inventory.body? character.inventory.body.effects.stat.mp[1]/100 : 0))),
str: Math.floor((character.str * (character.inventory.hand? 1+character.inventory.hand.effects.stat.str : 1) * (character.inventory.body? 1+character.inventory.body.effects.stat.str : 1))*10000)/10000,
int: Math.floor((character.int * (character.inventory.hand? 1+character.inventory.hand.effects.stat.int/100 : 1) * (character.inventory.body? 1+character.inventory.body.effects.stat.int/100 : 1))),
armour: {
physical: [Math.max(0, character.armour.physical[0] + (character.inventory.hand? character.inventory.hand.effects.def.physical[0] : 0) + (character.inventory.body? character.inventory.body.effects.def.physical[0] : 0)), Math.round((character.armour.physical[1] + (100-character.armour.physical[1])*(character.inventory.hand? character.inventory.hand.effects.def.physical[1]/100 : 0) + (100-character.armour.physical[1] + (100-character.armour.physical[1])*(character.inventory.hand? character.inventory.hand.effects.def.physical[1]/100 : 0))*(character.inventory.body? character.inventory.body.effects.def.physical[1]/100 : 0))*100)/100],
magic: [Math.max(0, character.armour.magic[0] + (character.inventory.hand? character.inventory.hand.effects.def.magic[0] : 0) + (character.inventory.body? character.inventory.body.effects.def.magic[0] : 0)), Math.round((character.armour.magic[1] + (100-character.armour.magic[1])*(character.inventory.hand? character.inventory.hand.effects.def.magic[1]/100 : 0) + (100-character.armour.magic[1] + (100-character.armour.magic[1])*(character.inventory.hand? character.inventory.hand.effects.def.magic[1]/100 : 0))*(character.inventory.body? character.inventory.body.effects.def.magic[1]/100 : 0))*100)/100],
},
hpRegen: (character.inventory.hand? character.inventory.hand.effects.stat.hpReg : 0) + (character.inventory.body? character.inventory.body.effects.stat.hpReg : 0),
mpRegen: character.mpRegen + (character.inventory.hand? character.inventory.hand.effects.stat.mpReg : 0) + (character.inventory.body? character.inventory.body.effects.stat.mpReg : 0),
physDmgIncrease: [(character.inventory.hand? character.inventory.hand.effects.atk.physical[0] : 0) + (character.inventory.body? character.inventory.body.effects.atk.physical[0] : 0), 1 + (character.inventory.hand? character.inventory.hand.effects.atk.physical[1]/100 : 0) + (character.inventory.body? character.inventory.body.effects.atk.physical[1]/100 : 0), Math.min((character.inventory.hand && character.inventory.hand.effects.atk.physical[2] != -1? character.inventory.hand.effects.atk.physical[2] : Infinity), (character.inventory.body && character.inventory.body.effects.atk.physical[2] != -1? character.inventory.body.effects.atk.physical[2] : Infinity))],
magiDmgIncrease: [(character.inventory.hand? character.inventory.hand.effects.atk.magic[0] : 0) + (character.inventory.body? character.inventory.body.effects.atk.magic[0] : 0), 1 + (character.inventory.hand? character.inventory.hand.effects.atk.magic[1]/100 : 0) + (character.inventory.body? character.inventory.body.effects.atk.magic[1]/100 : 0), Math.min((character.inventory.hand && character.inventory.hand.effects.atk.magic[2] != -1? character.inventory.hand.effects.atk.magic[2] : Infinity), (character.inventory.body && character.inventory.body.effects.atk.magic[2] != -1? character.inventory.body.effects.atk.magic[2] : Infinity))],
};
}; window.adjustedStats = adjustedStats;
function blankCard(rarity, id=undefined, onClick=undefined) {
return `<button ${id? `id="${id}"` : ``}${onClick ? `onclick="${onClick}" ` : ``}class="smallCharacterButton${rarity != -1? ` rank${rarity}Button` : ``}"><p class="noPadding characterTitle"> </p><img src="assets/empty.png" class="characterIcon"><span id="placeholder"><p class="noPadding medium"> </p></span></button>`;
}; window.blankCard = blankCard;
function itemCard(item, itemID=undefined, hideQuantity=false, isShop=false) {
let numItems = isShop? getItemByName(item.name)? getItemByName(item.name).quantity : 0 : item.quantity;
let title = `<strong>${item.displayName ? item.displayName : item.name}</strong>`;
let buttonData = `${ isShop==true? `onclick="focusItem(${itemID}, true)" ` : typeof itemID == 'number'? game.gamestate.inBattle ? `onclick="useItem(${itemID})" ` : `onclick="focusItem(${itemID})" ` : ``}class="smallCharacterButton rank${item.rarity}Button" id="${typeof itemID == 'number'? `item${itemID}` : itemID}"`;
return `<button ${buttonData}><p class="noPadding ${(item.displayName ? item.displayName : item.name).length > 11? `smallC` : `c`}haracterTitle">${title}</p><img src="${item.pfp}" class="characterIcon"><span id="placeholder"><p class="noPadding medium"> </p></span>${numItems > (isShop? 0 : 1) && hideQuantity == false? `<div id="cornerIcon"><span id="down">${numItems > 99 ? `99+`: numItems}</span></div>` : ``}</button>`;
}; window.itemCard = itemCard;
function isItem(item) {
for (let i = 0; i < data.items.length; i++) {
if (item.name == data.items[i].name) return true;
}
return false;
}; window.isItem = isItem;
function createCharacterCard(character, id=undefined, onClick=undefined) {
if (character.hidden) return blankCard(character.rarity, id, onClick);
if (isItem(character)) return itemCard(character, id, true); // in case the character is an item (for pulls)
let title = `<strong>${character.name}</strong>`;
let buttonData = `${onClick ? `onclick="${onClick}" ` : ``}class="smallCharacterButton rank${character.rarity}Button" id="${id}"`;
let desc = `<span id="left"><div id='hpBar'><div id="${id}hp" class="hpBarInner"></div></div><img src="assets/redCross.png" class="smallIcon"><span id="${id}hpDisplay">${bigNumber(Math.floor(character.hp))}</span></span><span id="right"><div id='mpBar'><div id="${id}mp" class="mpBarInner"></div></div><span id="${id}mpDisplay">${bigNumber(Math.floor(character.mp))}</span><img src="assets/blueStar.png" class="smallIcon"></span>`;
return `<button ${buttonData}>${character.ap > 0? `<div id="cornerIcon"><span id="down">${character.ap > 99? `∞` : (character.ap > 1? character.ap : `!`)}</span></div>` : ``}<p class="noPadding ${character.name.length > 11? `smallC` : `c`}haracterTitle">${title}</p><img src="${character.pfp}" class="characterIcon"><span id="placeholder"><p class="noPadding medium"> </p></span>${desc}</button>`;
}; window.createCharacterCard = createCharacterCard;
function cardLine(cards, pos, onClick) {
let html = ``;
for (let i = 0; i < cards.length; i++) {
cards[i].id = `${pos}${i}ID`;
html += createCharacterCard(cards[i], `${pos}${i}ID`, onClick? `${onClick}('${pos}${i}ID')`: ``);
}
return html;
}; window.cardLine = cardLine;
function massUpdateBars(cards) {
cards.forEach(obj => {
updateBar(obj.id+'hp', obj.hp/obj.hpMax, obj.hp);
updateBar(obj.id+'mp', obj.mp/obj.mpMax, obj.mp);
});
}; window.massUpdateBars = massUpdateBars;
function renderCards(pOnClick=undefined, eOnClick=undefined, enemyBack=game.gamestate.battleState.eb, enemyFront=game.gamestate.battleState.ef, playerFront=game.gamestate.battleState.pf, playerBack=game.gamestate.battleState.pb) {
//console.log(pOnClick, eOnClick);
if (!game.gamestate.inBattle) return;
replacehtml(`enemyBackline`, cardLine(enemyBack, 'EB', eOnClick));
replacehtml(`enemyFrontline`, cardLine(enemyFront, 'EF', eOnClick));
replacehtml(`playerFrontline`, cardLine(playerFront, 'PF', pOnClick));
replacehtml(`playerBackline`, cardLine(playerBack, 'PB', pOnClick));
massUpdateBars(game.gamestate.battleState.eb);
massUpdateBars(game.gamestate.battleState.ef);
massUpdateBars(game.gamestate.battleState.pf);
massUpdateBars(game.gamestate.battleState.pb);
// updateBar(id, percent)
/*
let toUpdate = ['eb', 'ef', 'pb', 'pf'];
for (let i = 0; i < toUpdate.length; i++) {
for (let j = 0; j < game.gamestate.battleState[toUpdate[i]].length; j++) {
game.gamestate.battleState[toUpdate[i]][j]
}
}*/
}; window.renderCards = renderCards;
function clearParticles() {
game.particles = {};
if (document.getElementById('effects')) replacehtml('effects', '');
}; window.clearParticles = clearParticles;
const countValidProperties = (obj) => {
return Object.keys(obj).filter(key => {
const value = obj[key];
return value !== undefined && value !== null && value !== false && value !== 0 && value !== "" && !Number.isNaN(value);
}).length;
}; window.countValidProperties = countValidProperties;
const handleParticles = (obj) => {
Object.keys(obj).forEach(key => {
const value = obj[key];
if ((!value || Number.isNaN(value)) && value != -1) {
delete obj[key];
} else {
if (value.life != -1) value.life--;
if (value.type.includes('spin')) {
document.getElementById(value.id).style.transform = document.getElementById(value.id).style.transform.replace(`${value.rot}.69deg`, `${value.rot+value.speed}.69deg`);
value.rot += value.speed;
}
if (value.type.includes('float')) {
document.getElementById(value.id).style.top = `${unPixel(document.getElementById(value.id).style.top) - 2}px`;
}
if (value.type.includes('fall')) {
document.getElementById(value.id).style.top = `${unPixel(document.getElementById(value.id).style.top) + 2}px`;
}
if (value.type.includes('fade')) {
if (value.life < 25) document.getElementById(value.id).style.opacity *= 0.9;
}
if (value.life == 0) {
document.getElementById(value.id).remove();
delete obj[key];
}
}
});
return obj;
}; window.handleParticles = handleParticles;
async function handleEffects() {
if (countValidProperties(game.particles) == 0) return
for (let i = 0; i < 9; i++) {
handleParticles(game.particles);
await sleep(25);
}
}; window.handleEffects = handleEffects;
async function aoeEffect(effect, team) {
let id = `aoeEffect${effect}`;
console.log('drawing effect');
if (document.getElementById(id)) return;
let html = `<img src="assets/${effect}.png" id="${id}"></img>`;
addhtml('effects', html);
document.getElementById(id).style.opacity = 1;
document.getElementById(id).style.position = `absolute`;
document.getElementById(id).style.top = `${team == 'E'? 0 : document.getElementById('battleScreen').getBoundingClientRect().height-450}px`;
document.getElementById(id).style.left = `${document.getElementById('battleScreen').getBoundingClientRect().width/2 - 510}px`;
console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
await sleep(1000);
for (let i = 0; i < 300; i++) {
document.getElementById(id).style.opacity = document.getElementById(id).style.opacity * 0.99;
await sleep(5);
}
document.getElementById(id).remove();
}; window.aoeEffect = aoeEffect;
async function hitEffect(effect, pos, offset, noRotate=false, duration=250, fadeDuration=50, fadeAmount=0.95) {
let id = generateId();
if (effect == 'integral' || effect == 'derivitive') noRotate = true;
let r = noRotate ? 0 : randint(0,360);
let html = ``;
// special animated hit effect
let icon = ``;
let bg = ``;
switch(effect) {
case `hpUp`:
icon = `greenCross.png`;
bg = `greenGlow`;
break;
case `defDown`:
icon = `brokenShield.png`;
bg = `greyGlow`;
break;
case `defUp`:
icon = `shield.png`;
bg = `greyGlow`;
break;
case `attackUp`:
icon = `redSword.png`;
bg = `redGlow`;
break;
case `mpDown`:
case `mpUp`:
icon = `blueStar.png`;
bg = `blueGlow`;
break;
case `statUp`:
icon = `yellowArrow.png`;
bg = `yellowGlow`;
break;
case `attackDown`:
icon = `brokenRedSword.png`;
bg = `redGlow`;
break;
case `poisonUp`:
case `gravityDown`:
icon = `purpleParticle.png`;
bg = `purpleGlow`;
break;
default:
if (effect.includes('Up') || effect.includes('Down')) console.warn(`WARNING: this stat change effect is not supported: ${effect}`);
}
if (icon != ``) {
hitEffect(bg, pos, {x: 87.5-75, y: 100-95}, true, 750, 300, 0.99);
for (let i = 0; i < 4; i++) {
let particle = {
id: generateId(),
life: 50,
type: effect.includes('Up')? 'float fade' : 'fall fade',
};
let html = `<img src="assets/${icon}" id="${particle.id}" class="mediumIcon">`;
addhtml('effects', html);
document.getElementById(particle.id).style.opacity = 1;
document.getElementById(particle.id).style.position = `absolute`;
document.getElementById(particle.id).style.top = `${pos.y + 95 + randint(0, 180) - 90 - document.getElementById(particle.id).offsetHeight/2}px`;
document.getElementById(particle.id).style.left = `${pos.x + 75 + randint(-75, 75) - document.getElementById(particle.id).offsetWidth/2}px`;
//console.log(document.getElementById(particle.id).style.top, document.getElementById(particle.id).style.left);
game.particles[particle.id] = particle;
}
for (let i = 0; i < 8; i++) {
let particle = {
id: generateId(),
life: 50,
type: effect.includes('Up')? 'float fade' : 'fall fade',
};
let html = `<img src="assets/${icon}" id="${particle.id}" class="smallIcon">`;
addhtml('effects', html);
document.getElementById(particle.id).style.opacity = 1;
document.getElementById(particle.id).style.position = `absolute`;
document.getElementById(particle.id).style.top = `${pos.y + 95 + randint(0, 180) - 90 - document.getElementById(particle.id).offsetHeight/2}px`;
document.getElementById(particle.id).style.left = `${pos.x + 75 + randint(-75, 75) - document.getElementById(particle.id).offsetWidth/2}px`;
//console.log(document.getElementById(particle.id).style.top, document.getElementById(particle.id).style.left);
game.particles[particle.id] = particle;
}
} else {
// normal hit effect
html = `<img src="assets/${effect}.png" style="transform: rotate(${r}deg);" id="${id}"></img>`;
addhtml('effects', html);
//console.log(pos.y+95-document.getElementById(id).offsetHeight/2+randint(-50, 50));
//console.log(pos);
//document.getElementById(id).style.display = 'none';
document.getElementById(id).style.opacity = 0;
document.getElementById(id).style.position = `absolute`;
await sleep(50); // let stuff load
//console.log(document.getElementById(id).offsetHeight, document.getElementById(id).offsetWidth);
if (document.getElementById(id).offsetHeight) {
document.getElementById(id).style.top = `${pos.y+95-document.getElementById(id).offsetHeight/2}px`;
document.getElementById(id).style.left = `${pos.x+75-document.getElementById(id).offsetWidth/2}px`;
} else { // If dimensions don't load, assume it is a glow effect
console.warn('HIT EFFECTS: Dimensions not loaded, assume glow effect');
document.getElementById(id).style.top = `${pos.y+95-200/2}px`;
document.getElementById(id).style.left = `${pos.x+75-175/2}px`;
}
document.getElementById(id).style.opacity = 1;
//document.getElementById(id).style.display = 'block';
//console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
if (offset) {
document.getElementById(id).style.top = `${unPixel(document.getElementById(id).style.top) + offset.y}px`;
document.getElementById(id).style.left = `${unPixel(document.getElementById(id).style.left) + offset.x}px`;
} else {
document.getElementById(id).style.top = `${unPixel(document.getElementById(id).style.top) + randint(-50, 50)}px`;
document.getElementById(id).style.left = `${unPixel(document.getElementById(id).style.left) + randint(-50, 50)}px`;
}
//console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
//console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
await sleep(duration);
for (let i = 0; i < fadeDuration; i++) {
document.getElementById(id).style.opacity = document.getElementById(id).style.opacity * fadeAmount;
await sleep(5);
}
document.getElementById(id).remove();
}
}; window.hitEffect = hitEffect;
async function simulateSmoothProjectileAttack(animation, start, target, dmg, number=true, miss=false) {
let projectile = animation.projectile;
let steps = animation.projectileSpeed;
let fade = animation.projectileFade;
let id = generateId();
let end = getCardCoords(target);
let randOffset = {x: randint(-50, 50), y: randint(-50, 50)};
end = vMath(end, randOffset, '+');
let toMove = vMath(end, start, '-');
//console.log(toPol(toMove));
let r = toPol(toMove).r * 180 / Math.PI;
//console.log(r);
let html = `<img src="assets/${projectile}.png" style="transform: rotate(${r}deg);" id="${id}"></img>`;
addhtml('effects', html);
let velocity = vMath(toMove, steps, '/');
// unfortunately I can't define a variable to be the element otherwise async stuff breaks (am I doing it wrong?)
document.getElementById(id).style.opacity = 1;
document.getElementById(id).style.position = `absolute`;
document.getElementById(id).style.top = `${start.y+95-document.getElementById(id).offsetHeight/2}px`;
document.getElementById(id).style.left = `${start.x+75-document.getElementById(id).offsetWidth/2}px`;
for (let i = 0; i < steps; i++) {
//console.log(i);
document.getElementById(id).style.top = `${unPixel(document.getElementById(id).style.top)+velocity.y}px`;
document.getElementById(id).style.left = `${unPixel(document.getElementById(id).style.left)+velocity.x}px`;
if (steps - i < 30 && fade) document.getElementById(id).style.opacity = document.getElementById(id).style.opacity * 0.95;
//console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
await sleep(10);
}
document.getElementById(id).remove();
changeStat(target, {stat: 'hp', change: -dmg});
if (number) dmgNumber(target, dmg, miss);
if (animation.hitEffect != 'none') {
hitEffect(animation.hitEffect, getCardCoords(target), randOffset);
}
}; window.simulateSmoothProjectileAttack = simulateSmoothProjectileAttack;
async function simulateProjectileAttack(projectile, start, end, steps, fade) {
let id = generateId();
let randOffset = {x: randint(-50, 50), y: randint(-50, 50)};
end = vMath(end, randOffset, '+');
let toMove = vMath(end, start, '-');
//console.log(toPol(toMove));
let r = toPol(toMove).r * 180 / Math.PI;
//console.log(r);
let html = `<img src="assets/${projectile}.png" style="transform: rotate(${r}deg);" id="${id}"></img>`;
addhtml('effects', html);
let velocity = vMath(toMove, steps, '/');
// unfortunately I can't define a variable to be the element otherwise async stuff breaks
//document.getElementById(id).style.display = 'none';
document.getElementById(id).style.opacity = 0;
document.getElementById(id).style.position = `absolute`;
await sleep(50); // let stuff load
document.getElementById(id).style.top = `${start.y+95-document.getElementById(id).offsetHeight/2}px`;
document.getElementById(id).style.left = `${start.x+75-document.getElementById(id).offsetWidth/2}px`;
document.getElementById(id).style.opacity = 1;
//document.getElementById(id).style.display = 'block';
for (let i = 0; i < steps; i++) {
//console.log(i);
document.getElementById(id).style.top = `${unPixel(document.getElementById(id).style.top)+velocity.y}px`;
document.getElementById(id).style.left = `${unPixel(document.getElementById(id).style.left)+velocity.x}px`;
if (steps - i < 30 && fade) document.getElementById(id).style.opacity = document.getElementById(id).style.opacity * 0.95;
//console.log(document.getElementById(id).style.top, document.getElementById(id).style.left);
await sleep(10);
}
document.getElementById(id).remove();
return randOffset;
}; window.simulateProjectileAttack = simulateProjectileAttack;
async function fakeMoveCard(card, targetCard, steps, reset=false, offset={x: 0, y: 0}) {
let startingPos = getCardCoords(card);
let pos = targetCard.x? targetCard : getCardCoords(targetCard);
if (!reset) pos = vMath(pos, {x: 0, y: targetCard.id[0] == 'E' ? 210 : -210}, '+');
pos = vMath(pos, offset, '+');
//console.log(getCoordsScuffed(id));
let original = document.getElementById(card.id);
let element = undefined;
//startingPos = getCoords(id);
if (!document.getElementById(card.id+'animation')) {
//console.log('ceating clone');
element = original.cloneNode(true);
original.style.opacity = 0;
element.id = element.id+'animation';
document.getElementById('effects').appendChild(element); // bruh, you can do this?!?! would have been nice to know a few months ago
} else {
element = document.getElementById(card.id+'animation');
startingPos = {x: unPixel(element.style.left), y: unPixel(element.style.top)}; // redundent ik
}
//console.log(element.style);
//startingPos = vMath(getCoords(id+'animation'), {x: parseFloat(element.style.left.slice(0, -2)), y: parseFloat(element.style.top.slice(0, -2))},'+');
//console.log(pos, startingPos);
let toMove = vMath(pos, startingPos, '-');
//console.log(toMove);
let velocity = vMath(toMove, steps, '/');
//console.log('v', velocity);
element.style.position = 'relative';
element.style.top = `${startingPos.y}px`;
element.style.left = `${startingPos.x}px`;
for (let i = 0; i < steps; i++) {
//console.log('moving');
element.style.top = `${unPixel(element.style.top)+velocity.y}px`;
element.style.left = `${unPixel(element.style.left)+velocity.x}px`;
//console.log(element.style.top, element.style.left);
await sleep(10);
}
element.style.top = `${pos.y}px`;
element.style.left = `${pos.x}px`;
if (reset) {
document.getElementById(card.id+'animation').remove();
original.style.opacity = 1;
}
//console.log('final', element.style.top, element.style.left);
//console.log(getCoordsScuffed(id+'animation'));
}; window.fakeMoveCard = fakeMoveCard;
async function changeStat(target, effect={stat: '', change: 0}, time=750) {
if (effect.change == 0) return;
let steps = 20;
for (let i = 0; i < steps; i++) {
target[effect.stat] = Math.max(0, Math.min(target[effect.stat] + effect.change/steps, target[effect.stat+'Max']));
if (target[effect.stat] < 0.25) target[effect.stat] = 0;
updateBar(target.id+effect.stat, target[effect.stat]/target[effect.stat+'Max'], Math.floor(target[effect.stat]));
await new Promise(resolve => setTimeout(resolve, time/steps));
if (target[effect.stat] == 0) break;
}
}; window.changeStat = changeStat;
function readID(id) {
return {
row: id.slice(0, 2).toLowerCase(),
pos: parseInt(id.slice(2, -2))
};
}; window.readID = readID;
function getCardById(id) {
let pos = readID(id);
if (pos.row == 'rr') return game.tempStorage.rewardCards[pos.pos];
return game.gamestate.battleState[pos.row][pos.pos];
}; window.getCardById = getCardById;
function getCardByName(name) {
for (let i = 0; i < game.gamestate.player.team.length; i++) {
if (name == game.gamestate.player.team[i].name) return game.gamestate.player.team[i];
}
console.warn(`GET CARD BY NAME: Card with name '${name}' not found in team!`);
return undefined;
}; window.getCardByName = getCardByName;
function getItemByName(name) {
for (let i = 0; i < game.gamestate.player.inventory.length; i++) {
if (name == game.gamestate.player.inventory[i].name) return game.gamestate.player.inventory[i];
}
return undefined;
} window.getItemByName = getItemByName;
function calcResistance(dmgType, dmg, target) {
switch (dmgType) {
case magic:
return Math.max(0, (dmg-target.armour.magic[0])*(100-target.armour.magic[1])/100);
case physical:
return Math.max(0, (dmg-target.armour.physical[0])*(100-target.armour.physical[1])/100);
case piercing:
// if defender has 100% resistance to damage, piercing damage can be blocked by damage negation.
if (target.armour.physical[1] >= 100 && target.armour.magic[1] >= 100) return Math.max(0, Math.max(dmg-target.armour.physical[0], dmg-target.armour.magic[0]));
return dmg;
case normal:
return Math.max(0, Math.max((dmg-target.armour.physical[0])*(100-target.armour.physical[1])/100, (dmg-target.armour.magic[0])*(100-target.armour.magic[1])/100));
}
}; window.calcResistance = calcResistance;
function calculateEffect(card, effect) {
//console.log('calculateEffect');
if (!effect) return false;
if (!effect.initialised && !effect.duration == 0) {
// wtf is this
if (effect.defChange.physical[0] < 0) effect.defChange.physical[0] = -Math.min(card.armour.physical[0], -effect.defChange.physical[0]);
card.armour.physical[0] += effect.defChange.physical[0];
if (effect.defChange.magic[0] < 0) effect.defChange.magic[0] = -Math.min(card.armour.magic[0], -effect.defChange.magic[0]);
card.armour.magic[0] += effect.defChange.magic[0];
if (effect.defChange.physical[1] > 0) effect.defChange.physical[1] = Math.floor((100 - card.armour.physical[1]) * effect.defChange.physical[1]/100);
if (effect.defChange.physical[1] < 0) effect.defChange.physical[1] = -Math.min(card.armour.physical[1], -effect.defChange.physical[1]);
card.armour.physical[1] += effect.defChange.physical[1];
if (effect.defChange.magic[1] > 0) effect.defChange.magic[1] = Math.floor((100 - card.armour.magic[1]) * effect.defChange.magic[1]/100);
if (effect.defChange.magic[1] < 0) effect.defChange.magic[1] = -Math.min(card.armour.magic[1], -effect.defChange.magic[1]);
card.armour.magic[1] += effect.defChange.magic[1];
card.str += effect.statChange.str;
card.str = Math.round(card.str*100)/100;
card.int += effect.statChange.int;
card.mpRegen += effect.statChange.reg;
if (effect.specialEffects) {
for (let i = 0; i < effect.specialEffects.length; i++) {
card.specialConditions[effect.specialEffects[i].effect] = effect.specialEffects[i].effect.value;
}
}
effect.initialised = true;
return effect;
}
//console.log('effects updated');
if (effect.dmg > 0) {
let miss = randint(0, 100) > effect.accuracy;
let dmg = calcResistance(effect.type, effect.dmg, card);
if (!miss) changeStat(card, {stat: 'hp', change: -dmg}, 500);
dmgNumber(card, miss? 0 : dmg, miss);
}
if (effect.change.hp) {
changeStat(card, {stat: 'hp', change: effect.change.hp}, 500);
dmgNumber(card, -effect.change.hp);
}
if (effect.change.mp) {
changeStat(card, {stat: 'mp', change: effect.change.mp}, 500);
}
effect.duration--;
if (effect.duration <= 0) {
//console.log('removing');
card.armour.physical[0] -= effect.defChange.physical[0];
card.armour.physical[1] -= effect.defChange.physical[1];
card.armour.magic[0] -= effect.defChange.magic[0];
card.armour.magic[1] -= effect.defChange.magic[1];
card.str -= effect.statChange.str;
card.str = Math.round(card.str*100)/100;
card.int -= effect.statChange.int;
card.mpRegen -= effect.statChange.reg;
if (effect.specialEffects) {
for (let i = 0; i < effect.specialEffects.length; i++) {