-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
1376 lines (1164 loc) · 40.2 KB
/
main.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
let gamemap, camera
let player, players = []
let abilities = []
let images = {
characters: {},
others: {}
}
let mobileControl
let isMobile = false
function preload() {
images.characters['yasuo'] = loadImage('./images/character/yasuo.png')
images.characters['jinx'] = loadImage('./images/character/jinx.png')
images.characters['blitzcrank'] = loadImage('./images/character/blitzcrank.png')
images.others['rocket'] = loadImage('./images/rocket2.png')
images.others['locxoay'] = loadImage('./images/locXoay.png')
images.others['cautuyet'] = loadImage('./images/cautuyet.png')
}
function setup() {
createCanvas(windowWidth, windowHeight)
imageMode(CENTER)
rectMode(CENTER)
gamemap = new GameMap({
width: 1000,
height: 1000
})
camera = new Camera()
// Khởi tạo người chơi
player = new Character({
champion: CHARACTERS.Yasuo,
picture: images.characters['jinx'],
onBorn: function () {
},
onAlive: function () {
this.lookAt(camera.screenToWorld(mouseX, mouseY))
this.move()
this.collideBound({
bound: gamemap.getBound()
})
this.showTargetPosition()
this.show()
}
})
players.push(player)
camera.setTarget(player)
// thêm máy
for (let i = 0; i < 2; i++) {
players.push(new AICharacter({
champion: CHARACTERS.Yasuo,
position: createVector(random(width), random(height)),
onAlive: function () {
this.move()
this.collideBound({
bound: gamemap.getBound()
})
this.show()
}
}))
}
// điều khiển cho đt
// isMobile = mobilecheck()
mobileControl = new MobileCircleControl({
radius: 60,
onChange: function () {
this.display()
let velocity = this.getVector().setMag(5)
player.targetPosition.add(velocity)
}
})
}
function draw() {
background(25)
isMobile && mouseIsPressed && mobileControl.setHandPosition(mouseX, mouseY)
camera.beginState()
camera.follow()
gamemap.showGrid()
gamemap.showEdges()
for (let p of players) {
p.run()
}
for (let i = abilities.length - 1; i >= 0; i--) {
abilities[i].run()
}
camera.endState()
// if (random() > 0.99) {
// let speed = 10
// let direction = getVectorDirection({
// fromPosition: players[1].position.copy(),
// toPosition: camera.screenToWorld(mouseX, mouseY),
// mag: speed
// })
// // bắn ra TenLuaDanDaoSieuKhungKhiep
// new Ability({
// owner: players[1],
// info: OBJECTS.TenLuaDanDaoSieuKhungKhiep,
// customInfo: {
// direction: direction,
// position: players[1].position.copy()
// }
// })
// }
showFrameRate()
}
function mousePressed() {
if (isMobile) {
mobileControl.mousePressed()
} else {
player.setTargetPosition(camera.screenToWorld(mouseX, mouseY))
}
}
function mouseReleased() {
mobileControl.mouseReleased()
}
function keyReleased() {
if (key == 't' || key == 'T') {
camera.followTarget = !camera.followTarget
} else {
let upperKey = key.toUpperCase()
player.action(upperKey)
}
}
function mouseWheel(e) {
let currentScale = camera.getScale()
let newScale = currentScale - e.delta / 700
if (newScale > 0)
camera.setScale(newScale)
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight, true)
}
const randomProperty = function (obj) {
var keys = Object.keys(obj)
return obj[keys[keys.length * Math.random() << 0]];
}
const showFrameRate = function () {
fill(255)
noStroke()
text(~~frameRate(), 10, 10)
}
const getVectorDirection = function (config = {}) {
const {
toPosition = createVector(0, 0),
fromPosition = createVector(0, 0),
limit = null,
mag = null
} = config
let direction = p5.Vector.sub(toPosition, fromPosition)
let addValue = createVector(0, 0)
if (limit) addValue = direction.limit(limit)
if (mag) addValue = direction.setMag(mag)
return addValue
}
const getVectorPosition = function (config = {}) {
const {
fromPosition
} = config
return fromPosition.copy().add(getVectorDirection(config))
}
const vaChamTronTron = function (tron1, tron2) {
let distance = p5.Vector.dist(tron1.position, tron2.position)
if (distance < tron1.radius + tron2.radius) {
return true
}
return false
}
const vaChamObjectPlayer = function (obj, playerArr, getAll) {
let result = []
for (let p of playerArr) {
if (!p.disable && p != obj.owner) {
if (vaChamTronTron(p, obj)) {
if (!getAll) return p
result.push(p)
}
}
}
if (getAll) return result
else return null
}
// ================== DATABASE ABILITIES ==================
const ABILITIES = {
Q_Rammus: {
onBorn: function () {
this.lifeTime = 5000
this.bornTime = millis()
this.maxSpeed = this.owner.getSpeed() * 3
abilities.push(this)
},
onAlive: function () {
// this.owner.speedUp(0.04)
let period = millis() - this.bornTime
let speed = map(period, 0, this.lifeTime, this.owner.defaultSpeed, this.maxSpeed)
this.owner.setSpeed(speed)
if (this.owner.getSpeed() > this.owner.defaultSpeed * 2) {
if (random() > 0.8)
new Ability({
owner: this.owner,
info: EFFECTS.Smoke,
customInfo: {
lifeTime: random(100, 600)
}
})
}
},
checkFinish: function () {
return millis() - this.bornTime > this.lifeTime
},
onFinish: function () {
this.owner.setSpeed(this.owner.defaultSpeed)
abilities.splice(abilities.indexOf(this), 1)
}
},
R_Patheon: {
onBorn: function () {
this.range = 500
this.jumpAt = 1000
this.downAt = 2000
this.finishAt = 3100
this.bornTime = millis()
this.effectRange = 0
this.effectRangeMax = 200
this.targetR = getVectorPosition({
fromPosition: this.owner.position.copy(),
toPosition: camera.screenToWorld(mouseX, mouseY),
limit: this.range
})
abilities.push(this)
},
onAlive: function () {
let period = millis() - this.bornTime
// Khi đang dùng chiêu này thì ko thể di chuyển
this.owner.setSpeed(0)
// Khi chuẩn bị nhảy
if (period < this.jumpAt) {
camera.shake(2)
this.effectRange = map(period, 0, this.jumpAt, 0, this.effectRangeMax)
// Khi đang trên không trung
} else if (period < this.downAt) {
this.owner.disable = true
let newScale = map(period, this.jumpAt, this.downAt, 1, .4)
camera.setScale(newScale)
// Khi đang rơi xuống
} else if (period < this.finishAt) {
let newScale = map(period, this.downAt, this.finishAt, .4, 1)
camera.setScale(newScale)
this.owner.position = this.targetR.copy()
this.owner.targetPosition = this.targetR.copy()
camera.shake(5)
}
// vẽ vùng ảnh hưởng
noFill()
stroke(255, 100)
strokeWeight(1)
circle(this.targetR.x, this.targetR.y, this.effectRange * 2)
},
checkFinish: function () {
return millis() - this.bornTime > this.finishAt
},
onFinish: function () {
this.owner.setSpeed(this.owner.defaultSpeed)
this.owner.disable = false
camera.shake(50)
// làm chậm những player trong vùng ảnh hưởng
let R_info = {
position: this.targetR,
radius: this.effectRangeMax
}
let playersInRange = vaChamObjectPlayer(R_info, players, true)
for (let p of playersInRange) {
if (p != this.owner)
new Ability({
owner: p,
info: EFFECTS.SlowDown
})
}
// tạo khói trong vùng ảnh hưởng
let r = this.effectRangeMax
for (let i = 0; i < 15; i++) {
// lấy 1 vị trí ngẫu nhiên trong vùng ảnh hưởng
let randomVecInRange = createVector(random(-r, r), random(-r, r))
let randomPosInRange = this.owner.position.copy().add(randomVecInRange)
// khói sống lâu hay ko phụ thuộc khoảng cách từ vị trí tìm được tới tâm ảnh hưởng
let distance = p5.Vector.dist(this.targetR, randomPosInRange)
let life_time = map(distance, 0, this.effectRangeMax, 100, 1500)
// tạo khói với những customInfo vừa tính
new Ability({
owner: this.owner,
info: EFFECTS.Smoke,
customInfo: {
position: randomPosInRange,
radius: random(10, 20),
lifeTime: life_time
}
})
}
camera.setScale(1)
abilities.splice(abilities.indexOf(this), 1)
}
},
R_Jinx: {
onBorn: function () {
// tính hướng bắn
let speed = 10
let direction = getVectorDirection({
fromPosition: this.owner.position.copy(),
toPosition: camera.screenToWorld(mouseX, mouseY),
mag: speed
})
// bắn ra TenLuaDanDaoSieuKhungKhiep
new Ability({
owner: this.owner,
info: OBJECTS.TenLuaDanDaoSieuKhungKhiep,
customInfo: {
direction: direction,
position: this.owner.position.copy()
}
})
}
},
Q_Shaco: {
onBorn: function () {
this.bornTime = millis()
this.lifeTime = 4000
this.range = 200
let targetQ = getVectorPosition({
fromPosition: this.owner.position.copy(),
toPosition: camera.screenToWorld(mouseX, mouseY),
limit: this.range
})
this.owner.position = targetQ
this.owner.targetPosition = targetQ
this.owner.invisible = true
abilities.push(this)
},
onAlive: function () {
},
checkFinish: function () {
return millis() - this.bornTime > this.lifeTime
},
onFinish: function () {
this.owner.invisible = false
abilities.splice(abilities.indexOf(this), 1)
}
},
W_Lucian: {
onBorn: function () {
// tính hướng bắn
let speed = 10
let direction = getVectorDirection({
fromPosition: this.owner.position.copy(),
toPosition: camera.screenToWorld(mouseX, mouseY),
mag: speed
})
// bắn ra TenLuaDanDaoSieuKhungKhiep
new Ability({
owner: this.owner,
info: OBJECTS.W_Lucian,
customInfo: {
direction: direction,
position: this.owner.position.copy()
}
})
}
},
Q_Anivia: {
onBorn: function () {
// tính hướng bắn
let speed = 3
let direction = getVectorDirection({
fromPosition: this.owner.position.copy(),
toPosition: camera.screenToWorld(mouseX, mouseY),
mag: speed
})
// bắn ra QuaCauBang
new Ability({
owner: this.owner,
info: OBJECTS.QuaCauBang,
customInfo: {
direction: direction,
position: this.owner.position.copy()
}
})
}
},
Flash: {
onBorn: function () {
this.range = 300
let direction = p5.Vector.sub(camera.screenToWorld(mouseX, mouseY), this.owner.position)
direction.limit(this.range)
// nếu khởi tạo hàm ở trong onBorn này, thì chỉ gọi được trong onBorn này
function addSmoke(owner, pos, r) {
new Ability({
owner: owner,
info: EFFECTS.Smoke,
customInfo: {
lifeTime: 400,
position: pos.copy(),
radius: r
}
})
}
addSmoke(this.owner, this.owner.position, this.owner.radius)
this.owner.position.add(direction)
this.owner.targetPosition = this.owner.position.copy()
addSmoke(this.owner, this.owner.position, this.owner.radius)
}
},
Teleport: {
onBorn: function () {
this.lifeTime = 4000
this.bornTime = millis()
this.teleportTarget = camera.screenToWorld(mouseX, mouseY)
this.targetRadiusMax = 40
this.angle = 0
this.showEffectTeleport = function (pos, radius) {
push()
translate(pos.x, pos.y)
rotate(this.angle)
noFill()
strokeWeight(5)
stroke('#1d2c5e')
ellipse(0, 0, radius * 1.5, radius * 3)
ellipse(0, 0, radius * 3, radius * 1.5)
pop()
}
this.owner.setSpeed(0)
abilities.push(this)
},
onAlive: function () {
let period = millis() - this.bornTime
let radius = map(period, 0, this.lifeTime, this.targetRadiusMax, 10)
let rotateSpeed = map(period, 0, this.lifeTime, 0, PI / 5)
this.angle += rotateSpeed
// vị trí tới
this.showEffectTeleport(this.teleportTarget, radius)
// vị trí người chơi
this.showEffectTeleport(this.owner.position, radius)
},
checkFinish: function () {
return millis() - this.bornTime > this.lifeTime
},
onFinish: function () {
this.owner.position = this.teleportTarget.copy()
this.owner.targetPosition = this.teleportTarget.copy()
this.owner.setSpeed(this.owner.defaultSpeed)
abilities.splice(abilities.indexOf(this), 1)
}
}
}
const EFFECTS = {
SlowDown: {
onBorn: function () {
this.lifeTime = 3000
this.bornTime = millis()
this.owner.setSpeed(this.owner.getSpeed() / 10)
abilities.push(this)
},
onAlive: function () {
},
checkFinish: function () {
return millis() - this.bornTime > this.lifeTime
},
onFinish: function () {
this.owner.setSpeed(this.owner.defaultSpeed)
abilities.splice(abilities.indexOf(this), 1)
}
},
Smoke: {
onBorn: function () {
this.lifeTime = random(500, 2000)
this.bornTime = millis()
let r = this.owner.radius
let randomAdd = createVector(random(-r, r), random(-r, r))
this.position = this.owner.position.copy().add(randomAdd)
this.radius = random(10, 30)
abilities.push(this)
},
onAlive: function () {
let period = millis() - this.bornTime
let alpha = map(period, 0, this.lifeTime, 255, 0)
noStroke()
fill(255, alpha)
circle(this.position.x, this.position.y, this.radius * 2)
let r = 3
this.position.add(random(-r, r), random(-r, r))
this.radius += 1
},
checkFinish: function () {
return millis() - this.bornTime > this.lifeTime
},
onFinish: function () {
abilities.splice(abilities.indexOf(this), 1)
}
},
Force: {
onBorn: function () {
this.position = createVector(0, 0)
this.range = 100
}
},
IceBreak: {
onBorn: function () {
this.lifeTime = random(500, 2000)
this.timeBorn = millis()
let r = this.owner.radius
let randomAdd = createVector(random(-r / 2, r / 2), random(-r / 2, r / 2))
this.position = this.owner.position.copy().add(randomAdd)
this.radius = random(2, 5)
abilities.push(this)
},
onAlive: function () {
let period = millis() - this.timeBorn
let alpha = map(period, 0, this.lifeTime, 255, 0)
noStroke()
fill(255, alpha)
circle(this.position.x, this.position.y, this.radius)
let r = 3
this.position.add(random(-r, r), random(-r, r))
// this.radius += 1
},
checkFinish: function () {
return millis() - this.timeBorn > this.lifeTime
},
onFinish: function () {
abilities.splice(abilities.indexOf(this), 1)
}
}
}
const OBJECTS = {
TenLuaDanDaoSieuKhungKhiep: {
onBorn: function () {
this.lifeTime = 10000
this.bornTime = millis()
// khởi tạo hướng
this.direction = createVector(0, 0)
this.position = createVector(0, 0)
this.speed = 0
this.radius = 25
abilities.push(this)
},
onAlive: function () {
// di chuyển theo hướng
this.position.add(this.direction)
// hien thi ten lua
push()
translate(this.position.x, this.position.y)
rotate(this.direction.heading())
image(images.others['rocket'], 0, 0, this.radius * 2, this.radius * 2)
pop()
// tạo khói ở đuôi
if (random() > 0.5) {
let dir = this.direction.copy().setMag(this.radius)
let pos = this.position.copy().sub(dir)
new Ability({
owner: this.owner,
info: EFFECTS.Smoke,
customInfo: {
position: pos,
radius: random(this.radius / 2, this.radius),
lifeTime: random(200, 700)
}
})
}
},
checkFinish: function () {
if (millis() - this.bornTime > this.lifeTime)
return true
this.playerTrung = vaChamObjectPlayer(this, players)
return this.playerTrung
},
onFinish: function () {
// hiệu ứng nổ
for (let i = 0; i < 5; i++) {
let r = this.radius * 2
let newPosition = this.position.copy().add(random(-r, r), random(-r, r))
new Ability({
owner: this.owner,
info: EFFECTS.Smoke,
customInfo: {
position: newPosition,
radius: random(10, 30),
lifeTime: random(500, 1300)
}
})
}
if (this.playerTrung)
new Ability({
owner: this.playerTrung,
info: EFFECTS.SlowDown
})
// xoá
abilities.splice(abilities.indexOf(this), 1)
}
},
TroiAnhSanh: {
onBorn: function () {
// khởi tạo hướng
},
onAlive: function () {
// di chuyển theo hướng
// hien thi trói ánh sáng
},
checkFinish: function () {
// check trung ai chua
},
onFinish: function () {
// hiệu ứng trói
}
},
W_Lucian: {
onBorn: function () {
this.lifeRange = 500
this.bornPosition = this.owner.position.copy()
// khởi tạo hướng
this.direction = createVector(0, 0)
this.position = createVector(0, 0)
this.speed = 0
this.radius = 15
abilities.push(this)
},
onAlive: function () {
// di chuyển theo hướng
this.position.add(this.direction)
// hien thi ten lua
// push()
// translate(this.position.x, this.position.y)
// rotate(this.direction.heading())
// image(images.others['locxoay'], 0, 0, this.radius * 2, this.radius * 2)
// pop()
fill(0, 0, 200)
strokeWeight(2)
stroke(150)
circle(this.position.x, this.position.y, this.radius * 2)
},
checkFinish: function () {
let dist = p5.Vector.dist(this.position, this.bornPosition)
if (dist > this.lifeRange)
return true
this.playerTrung = vaChamObjectPlayer(this, players)
return this.playerTrung
},
onFinish: function () {
// hiệu ứng nổ 4 hướng
push()
translate(this.position.x, this.position.y)
rotate(this.direction.heading())
stroke(255)
strokeWeight(4)
line(-100, 0, 100, 0)
line(0, -100, 0, 100)
pop()
if (this.playerTrung)
new Ability({
owner: this.playerTrung,
info: EFFECTS.SlowDown
})
// xoá
abilities.splice(abilities.indexOf(this), 1)
}
},
No4Huong: {
onBorn: function () {
// this.position
// this.range
// this.radius = 0
},
onAlive: function () {
},
checkFinish: function () {
},
onFinish: function () {
}
},
QuaCauBang: {
onBorn: function () {
this.lifeTime = 3000
this.timeBorn = millis()
this.startPosition = this.owner.position.copy()
// this.range = 300
// khởi tạo hướng
this.direction = createVector(0, 0)
this.position = createVector(0, 0)
this.speed = 0
this.radius = 50
abilities.push(this)
},
onAlive: function () {
// di chuyển theo hướng
this.position.add(this.direction)
// hien thi cau bang
push()
let period = millis() - this.timeBorn
translate(this.position.x, this.position.y)
// xoay cau bang
let r = map(period, 0, this.lifeTime, 0, 720)
rotate(radians(r * 2))
image(images.others['cautuyet'], 0, 0, this.radius * 2, this.radius * 2)
pop()
},
checkFinish: function () {
// return millis() - this.timeBorn > this.lifeTime
return p5.Vector.dist(this.startPosition, this.position) > 300
},
onFinish: function () {
// hiệu ứng nổ
stroke(255)
noFill()
circle(this.position.x, this.position.y, this.radius * 3)
for (let i = 0; i < 20; i++) {
let r = this.radius * 1.5
let newPosition = this.position.copy().add(random(-r, r), random(-r, r))
new Ability({
owner: this.owner,
info: EFFECTS.IceBreak,
customInfo: {
position: newPosition,
radius: random(3, 5),
lifeTime: random(500, 1300)
}
})
}
// xoá
abilities.splice(abilities.indexOf(this), 1)
}
}
}
const CHARACTERS = {
Yasuo: {
pictureName: 'yasuo',
Q: ABILITIES.W_Lucian,
W: ABILITIES.Q_Anivia,
E: ABILITIES.R_Patheon,
R: ABILITIES.R_Jinx,
F: ABILITIES.Flash,
D: ABILITIES.Teleport
}
}
// =========================================
// ============= CLASSES ===================
// =========================================
class EventableClass {
constructor(config = {}) {
const {
onBorn = function () { },
onAlive = function () { },
onFinish = function () { },
checkFinish = function () { }
} = config
this.onBorn = onBorn
this.onAlive = onAlive
this.onFinish = onFinish
this.checkFinish = checkFinish
}
run() {
this.onAlive()
this.checkFinish() && this.onFinish()
}
}
class Character extends EventableClass {
constructor(config = {}) {
super(config)
let {
radius = 35,
position = createVector(500, 100),
name = 'Character',
movementSpeed = 4,
champion = {}
} = config
this.champion = champion
this.position = position
this.radius = radius
this.name = name
this.movementSpeed = movementSpeed
this.defaultSpeed = movementSpeed
this.targetPosition = position.copy()
this.lookAtPosition = position.copy()
this.invisible = false
this.disable = false
this.onBorn()
}
action(chieuThuc) {
if (this.champion[chieuThuc]) {
new Ability({
owner: this,
info: this.champion[chieuThuc]
})
}
}
getSpeed() {
return this.movementSpeed
}
setSpeed(speed) {
this.movementSpeed = speed
}
speedUp(v) {
this.movementSpeed += v
}
lookAt(x, y) {
if (x instanceof p5.Vector) {
this.lookAtPosition = x
} else {
this.lookAtPosition = createVector(x, y)
}
}
getLookAtAngle() {
let direction = p5.Vector.sub(this.lookAtPosition, this.position)
return direction.heading()
}
showLookAtDirection() {
// vẽ hướng nhìn
push()
translate(this.position.x, this.position.y)
rotate(this.getLookAtAngle())
stroke(255)
strokeWeight(2)
line(0, 0, this.radius, 0)
pop()
}
setTargetPosition(x, y) {
if (x instanceof p5.Vector) {
this.targetPosition = x
} else {
this.targetPosition = createVector(x, y)
}
}
showTargetPosition() {
fill(0, 255, 0)
circle(this.targetPosition.x, this.targetPosition.y, 15)
}
move() {
let distance = p5.Vector.dist(this.targetPosition, this.position)
if (distance > this.movementSpeed) {
let direction = p5.Vector.sub(this.targetPosition, this.position)
let speed = direction.setMag(this.movementSpeed)
this.position.add(speed)
return true
}
return false
}
show() {
if (this.disable) return
push()
translate(this.position.x, this.position.y)
// vẽ hướng cần tới
let vectorHuong = p5.Vector.sub(this.targetPosition, this.position)
stroke(255, 50)
strokeWeight(1)
line(0, 0, vectorHuong.x, vectorHuong.y)
// vẽ hình ảnh
if (this.invisible) {
noFill()
stroke(200, 100)
strokeWeight(1)
circle(0, 0, this.radius * 2)
} else if (this.champion.pictureName) {