-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
735 lines (629 loc) · 18 KB
/
script.js
File metadata and controls
735 lines (629 loc) · 18 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
// //繪製滑鼠座標 滑鼠點擊會有消失Bug xy 變null
// mousePos.set(evt.X,evt.Y) 打錯 xy 是小寫
//-------環境變數----------
//更新速率
var updateFPS = 30;
//滑鼠顯示十字座標
var showMouse = true;
// 全局時間
var time = 0;
//預設背景顏色
var bgColor ="black";
//控制項
var controls = {
value: 0,
showId: true,
showMap: true,
}
var gui = new dat.GUI()
//控制項撰寫範例:控制項名稱 範圍,每次變動0.01,變動時呼叫function
gui.add(controls,"showId")
gui.add(controls,"showMap")
//-------向量class----------
// Vec2
class Vec2{
constructor(x,y){
this.x = x
this.y = y
}
set(x,y){
this.x =x
this.y =y
}
add(v){
return new Vec2(this.x+v.x,this.y+v.y)
}
sub(v){
return new Vec2(this.x-v.x,this.y-v.y)
}
mul(s){
return new Vec2(this.x*s,this.y*s)
}
//改變自己本身的(x,y)
move(x,y){
this.x+=x
this.y+=y
}
//複製回傳一個向量
clone(){
return new Vec2(this.x,this.y)
}
toString(){
return `(${this.x}, ${this.y})`
}
//判斷兩個向量是否相同
equal(v){
return this.x==v.x && this.y ==v.y
}
//使用計算屬性
//向量長度
get length(){
return Math.sqrt(this.x*this.x+this.y*this.y)
}
//變更向量長度 nv:new vector
// a.length =10
set length(nv){
let temp = this.unit.mul(nv)
this.set(temp.x,temp.y)
}
//向量角度
get angle(){
//使用三角函數計算角度
return Math.atan2(this.y,this.x)
}
//單位向量
get unit(){
//a.unit.length = 1
return this.mul(1/this.length)
}
}
//-----------------------------
//-------1.初始化Canvas設定---------
var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext("2d");
function initCanvas(){
ww = canvas.width = window.innerWidth;
wh = canvas.height = window.innerHeight;
}
initCanvas();
//預設全圓
ctx.circle= function(v,r){
this.arc(v.x,v.y,r,0,Math.PI*2)
}
ctx.line= function(v1,v2){
//移動
this.moveTo(v1.x,v1.y)
//連線
this.lineTo(v2.x,v2.y)
}
//-------2.初始化邏輯:遊戲物件與遊戲數值---------
//global class
var global = {
//整個視野比例
scale: 1,
//實際遊戲可以玩的範圍是2000~-2000
//遊戲寬度
width: 4000,
//遊戲高度
height: 4000,
//最多食物量
foodmax: 500,
//最多玩家量
playermax: 50,
//決定能不能合併,1完全隔開,0代表合在一起
collideFactor: 0
}
//隨機轉移速度
//舊方法
//Math.random()*10-5 (-5~5之間)
//新方法 用新舊比例
// 舊
// ---|------
// 新
// ----|---------------
//map(Math.random(),0,1,-5,5) (-5~5之間)
//原始值、原始最小值、原始最大值、新最小值、新最大值
function map(value,min,max,nmin,nmax){
let l1 = max-min
let l2 = nmax - nmin
//換算新舊相差比例
let ratio = l2/l1
//原始值減掉最小值,再乘上比例+新的最小值
return (value-min)*ratio+nmin
}
//玩家
class Player{
constructor(args){
let def = {
//*100000 避免重複
id: parseInt(Math.random()*100000),
p: new Vec2(0,0),
//初始速度在5~-5之間
v: new Vec2(map(Math.random(),0,1,-5,5),map(Math.random(),0,1,-5,5)),
a: new Vec2(0,0),
//重量
mass: 100,
//預設活著
living: true,
//預設七彩顏色 60%彩度 50%明度
color: `hsl(${Math.random()*360},60%,50%)`
}
Object.assign(def,args)
Object.assign(this,def)
}
draw(){
ctx.fillStyle=this.color
ctx.beginPath()
ctx.arc(this.p.x,this.p.y,this.r,0,Math.PI*2)
ctx.fill()
//寫出自己的id
if(this.type!="food" && controls.showId ){
ctx.font="10px Arial"
ctx.fillStyle="white"
ctx.textAlign="center"
//中心點
ctx.fillText(parseInt(this.id),this.p.x,this.p.y)
}
//在下方寫出AI正在追的目標id
if (this.lastTarget && controls.showId){
ctx.font="10px Arial"
ctx.fillStyle="azure"
ctx.textAlign="center"
ctx.fillText(this.lastTarget.id,this.p.x,this.p.y+20)
}
}
update(){
//移動位置
this.p.move(this.v.x, this.v.y)
//21:30先去除加速度
this.v.move(this.a.x, this.a.y)
//如果是食物,會越來越慢
if (this.type=="food"){
//用物件比較慢
//this.v = this.v.mul(0.95)
//比較快
this.v.x*=0.95
this.v.y*=0.95
}
this.a= this.a.mul(0.98)
//如果重量<0 設定玩家死亡
if(this.mass<0){
this.living=false
}
//檢查邊界
this.checkBoundary()
}
//檢查邊界
checkBoundary(){
if (this.p.x-this.r<-global.width/2){
this.p.x = -global.width/2+this.r
}
if (this.p.x +this.r> global.width/2){
this.p.x = global.width/2-this.r
}
if (this.p.y-this.r<-global.height/2){
this.p.y = -global.height/2+this.r
}
if (this.p.y+this.r>global.height/2){
this.p.y = global.height/2-this.r
}
}
//eat 41.49
eat(target){
//把目標的重量搶過來
TweenMax.to(this,0.1,{mass: this.mass+target.mass})
//設定目標已經死了
target.living=false
}
get r(){
return Math.sqrt(this.mass)
}
//滑鼠控球
get maxSpeed(){
// 本身半徑
// return 1/this.r
// return 30/this.r
//次方會小很多,+1避免/0時出錯
return 30/ (1+Math.log(this.r))
}
//選擇敵人目標
isTarget(p){
//目標的R要是我的0.9倍
let result = p.r<this.r*0.9 && p.p.sub(this.p).length<500
return result
}
}
//遊戲邏輯初始化
//所有的玩家球
players= []
//自己的玩家
myplayers = []
function init(){
//300個玩家
for(var i =0; i<300; i++ ){
players.push(new Player({
//重量在20~1020之間
mass: Math.random()*1000+20,
//遊戲畫面負到正的寬度/2
p: new Vec2(
map(Math.random(),0,1,-global.width/2,global.width/2),
map(Math.random(),0,1,-global.height/2,global.height/2),
)
}))
}
//把第一個當作自己的玩家
// players[0].mass=500
myplayers.push(players[0])
setInterval(function(){
// let scale = 1/myplayers[0].r
//縮到合理範圍 ,log不能是0,所以+2
let scale = 1/Math.log(Math.sqrt(myplayers[0].r)/4+2)
TweenMax.to(global,2,{scale: scale})
},2000)
//每隔10毫秒執行 定時加入補新玩家跟食物
setInterval(function(){
//增加食物有上限
if(players.filter(p=>p.type=="food").length<global.foodmax){
players.push(new Player({
mass:10,
p: new Vec2(
map(Math.random(),0,1,-global.height/2,global.height/2),
map(Math.random(),0,1,-global.height/2,global.height/2),
),
v: new Vec2(0,0),
type: "food"
}))
}
//增加玩家
if (players.filter(p=>p.type!="food").length<global.playermax){
players.push(new Player({
mass: Math.random()*1000+20,
p: new Vec2(
map(Math.random(),0,1,-global.height/2,global.height/2),
map(Math.random(),0,1,-global.height/2,global.height/2),
),
}))
}
},10)
}
//-------3.初始化遊戲邏輯更新----------
//更新遊戲邏輯
function update(){
//可以當作遊戲時間,每秒往上+30
time++;
let myplayer = myplayers[0]
// console.log(global.collideFactor);
players.forEach( (player, pid)=>{
//如果現在這顆是活著
if(player.living){
player.update()
//非自己玩家-敵人AI模式
//0.2把目標清掉 20%機率清除目標亂走
//每0.02秒 判斷不是自己的ID 且不是食物
//time+pid*5,讓每個敵人的變化更新時間錯開
if( (time+pid*5)%20==0 && player.id!=myplayer.id && player.type!="food" ){
//自動去執行
if (Math.random()<0.2){
//隨機移動時,清掉選定的敵人
player.lastTarget=null
//隨機指定一個角度的最大速度,去追人
let angle = Math.PI*2*Math.random()
let len = player.maxSpeed
// player.speed = new Vec2(Math.cos(angle)*len,Math.sin(angle)*len)
let newV = new Vec2(Math.cos(angle)*len,Math.sin(angle)*len)
TweenMax.to(player.v,0.1, newV)
}
//30%機率選定敵人//0.3選新目標
if (Math.random()<0.3){
let targets = players.filter(t=>player.isTarget(t))
// .sort((p1,p2)=>p2.mass-p1.mass).slice(0,5)
//其他自動吃小圓
// if ( player.type!='food' && player!=myplayer){
if (targets[0]){
// let delta = targets[0].p.sub(player.p)
// let mm = delta.unit.mul(targets[0].maxSpeed/2)
// TweenMax.to(player.v,0.4,{x: mm.x,y: mm.y,ease: Cubic.easeOut})
player.lastTarget = targets[0]
}
// }
//0.5 追
}else{
//去追自己紀錄的目標
if (player.lastTarget && player.lastTarget.living){
//目標的球會相對於自己的向量是什麼
let delta = player.lastTarget.p.sub(player.p)
let newV = delta.unit.mul(player.maxSpeed)
TweenMax.to(player.v,0.2,{x: newV.x,y: newV.y})
}
}
}
//抓第二層
//球兩兩比對,判斷有沒有被吃
players.forEach( (player2 , pid2 ) =>{
if(pid!=pid2 && player.id != player2.id && player2.living){
// player.p.sub(player2.p).length -10 在邊界時能吃掉旁邊的,避免太嚴格
if(player.r*0.9>player2.r && player.p.sub(player2.p).length -10 <= (player.r-player2.r) ){
if(player.id==myplayer.id){
// 太常印log容易lag
// console.log("collide")
player.eat(player2)
}
}
}
})
}
})
//把分裂的排開
myplayers.forEach( (c1,c1id)=>{
myplayers.forEach( (c2,c2id)=>{
// //只對不同配對做檢查,且都活著
if (c1id!= c2id && c1.living && c2.living){
//讓球彼此聚集
let delta = c2.p.sub(c1.p)
if (delta.length<c1.r+c2.r){
let pan = delta.unit.mul( (c1.r+c2.r )*global.collideFactor)
//還原為原本位置
c2.p=c1.p.add(pan)
c2.v=c1.v.clone()
}
//互吃合併(記得不要吃掉自己的0)
delta = c2.p.sub(c1.p)
if (global.collideFactor<0.7 && delta.length< (c1.r+c2.r)*0.6 && c2id!=0 ){
//重量相加
c1.mass+=c2.mass
//讓合併時不要太突兀,更新成兩球中心
c1.p = c1.p.add(c2.p).mul(0.5 )
c2.living=false
// console.log(`${c1.id}(${c1.mass}) eat ${c2.id}(${c2.mass})`)
}
}
})
})
//分裂跟著球跑
myplayers.forEach( (c1,c1id)=>{
if(c1id!=0){
let mdelta = myplayer.p.sub(c1.p)
//換算 0~100 到0~20
c1.p=c1.p.add(mdelta.unit.mul(map(mdelta.length,0,100,0,20)/Math.sqrt(c1.r)))
let delta = mousePos.sub(new Vec2(ww/2,wh/2)).mul(0.1)
//移動速度大於最大速度
if (delta.length>c1.maxSpeed){
delta=delta.unit.mul(c1.maxSpeed)
}
c1.v =delta
c1.v =c1.v.add(c1.a)
}
})
//球兩兩比對,判斷有沒有被吃
let delta = mousePos.sub(new Vec2(ww/2,wh/2)).mul(0.1)
let deltaLen = delta.length
//如果deltaLen>本身速度
if(deltaLen>myplayer.maxSpeed){
delta = delta.unit.mul(myplayer.maxSpeed)
}
myplayer.v=delta
//把死掉的篩掉
players = players.filter(p=>p.living)
myplayers = myplayers.filter(p=>p.living)
//如果自己被吃掉
if(myplayers.length==0){
//推一個players進去
myplayers.push(players.filter(p=>p.type!="food")[0] )
}
}
//-------4.初始化遊戲畫面繪製----------
//繪製畫面,也可以稱為render
function draw(){
//清空背景
ctx.fillStyle=bgColor
// 填滿背景
ctx.fillRect(0,0,ww,wh)
//-------------------------
// 在這裡繪製
//玩家是中心點
let cen = myplayers[0].p;
ctx.save()
ctx.translate(ww/2,wh/2)
//球的大小倍數
ctx.scale(global.scale, global.scale)
// ctx.scale(0.5,0.5)
//讓玩家自己在畫面正中央
ctx.translate(-cen.x,-cen.y)
//重新繪製 ctx.beginPath(20210422-0443
ctx.beginPath()
//繪製網格
//改成4000能整除的
let gridWidth = 250;
let gcount = global.width/gridWidth
for(var i=-gcount/2;i<=gcount/2;i++){
//直向
ctx.moveTo(i*gridWidth,-global.height/2)
ctx.lineTo(i*gridWidth,global.height/2)
//橫向
ctx.moveTo(-global.width/2,i*gridWidth)
ctx.lineTo(global.width/2,i*gridWidth)
}
ctx.strokeStyle="rgba(255,255,255,0.4)"
ctx.stroke()
//排序 slice().sort((p1,p2)=>p1.r-p2.r) 吃掉時,看起來會在下方
players.slice().sort((p1,p2)=>p1.r-p2.r).forEach(player=>{
//如果活著才畫
if(player.living){
player.draw()
}
})
ctx.restore()
//繪製分數
ctx.font="20px Arial"
ctx.fillStyle="white"
//總成績 ruduce 初始值為0
let score = myplayers.map(p=>p.mass).reduce((total,mass)=>{return total+mass},0 )
ctx.fillText("Score: "+parseInt(score),30,30)
//global
ctx.fillText("collide: "+global.collideFactor,30,60)
ctx.fillText("Shooting: W ; Divides: Space ",30,90)
//未教學,小地圖
if (controls.showMap){
//小地圖
ctx.save()
ctx.translate(0,wh)
ctx.scale(1/30,1/30)
ctx.translate(0,-global.height)
ctx.fillStyle="rgba(255,255,255,0.2)"
ctx.fillRect(0,0,global.width,global.height)
ctx.translate(global.width/2,global.height/2)
// console.log(players.length)
players.forEach(player=>{
if (player.type!="food"){
ctx.beginPath()
ctx.fillStyle="white"
let r = 20
if (myplayers.map(mp=>mp.id).indexOf(player.id)!=-1){
ctx.fillStyle="red"
r=100
}
ctx.arc(player.p.x,player.p.y,r,0,Math.PI*2)
ctx.fill()
}
})
ctx.restore()
}
//-----------------------
//繪製滑鼠座標
ctx.fillStyle="red"
ctx.beginPath()
//滑鼠點
ctx.circle(mousePos,2)
ctx.fill()
ctx.save()
ctx.beginPath()
ctx.translate(mousePos.x,mousePos.y)
ctx.strokeStyle="red"
let len = 20
ctx.line(new Vec2(-len,0),new Vec2(len,0))
//標示座標
ctx.fillText(mousePos,10,-10)
//寫法一使用旋轉
ctx.rotate(Math.PI/2)
ctx.line(new Vec2(-len,0),new Vec2(len,0))
//寫法二
// ctx.line(new Vec2(0,-len),new Vec2(0,len))
ctx.stroke()
ctx.restore()
//schedule next render 預定下一次執行
requestAnimationFrame(draw);
}
//-------頁面載入----------
//頁面載入完成後依序載入
function loaded(){
initCanvas()
init()
//快速執行
requestAnimationFrame(draw)
//每隔1秒執行30次更新
setInterval(update,1000/updateFPS)
}
//載入,頁面載入完成後才執行
window.addEventListener("load",loaded)
//縮放,瀏覽器視窗大小改變時resize
window.addEventListener("resize",initCanvas)
//-------滑鼠事件跟紀錄----------
//滑鼠紀錄位置
var mousePos = new Vec2(0,0)
var mousePosDown = new Vec2(0,0)
var mousePosUp = new Vec2(0,0)
//滑鼠事件
window.addEventListener("mousemove",mousemove)
window.addEventListener("mouseup",mouseup)
window.addEventListener("mousedown",mousedown)
function mousemove(evt){
mousePos.set(evt.x,evt.y)
//顯示目前滑鼠位置
// console.log(mousePos)
}
function mouseup(evt){
// mousePos.set(evt.offsetX,evt.offsetY)
mousePos.set(evt.x,evt.y)
mousePosUp = mousePos.clone()
}
function mousedown(evt){
// mousePos.set(evt.offsetX,evt.offsetY)
mousePos.set(evt.x,evt.y)
mousePosDown = mousePos.clone()
}
//監聽鍵盤按鍵
window.addEventListener('keydown',function(evt){
//監聽鍵盤按鍵
// console.log(evt.key)
//空白鍵 分裂事件
if (evt.key==" "){
let newballs = []
//不能合併 0才能合併
global.collideFactor=1
//如果有的話清除
if (global.splitTimer){
clearTimeout(global.splitTimer)
}
//刪掉TweenMax
TweenMax.killTweensOf(global)
//分裂計時器 8秒後歸零
global.splitTimer = setTimeout(()=>{
TweenMax.to(global,10,{collideFactor: 0})
},8000)
//抓出所有玩家
myplayers.forEach(mp=>{
if (mp.mass>400){
TweenMax.to(mp,0.2,{mass: mp.mass/2})
//分裂
let splitSelf = new Player({
//id要相同才能避免誤時
id: mp.id,
mass: mp.mass/2,
p: mp.p.clone(),
v: mousePos.sub(new Vec2(ww/2,wh/2)).unit.mul(mp.maxSpeed*3),
a: mousePos.sub(new Vec2(ww/2,wh/2)).unit.mul(mp.maxSpeed*3),
color: mp.color,
})
// // TweenMax.to(splitSelf.p,0.3,{ㄊ
// // x: mp.x,
// // y: mp.y
// // })
newballs.push(splitSelf)
}
})
players = players.concat(newballs)
myplayers = myplayers.concat(newballs)
}
//w射出食物
if (evt.key=="w"){
//把所有的players抓出來
myplayers.forEach(mp=>{
//至少要200才射
if (mp.mass>200){
//射出80 消耗100
TweenMax.to(mp,0.2,{mass:mp.mass-100})
//滑鼠和自己的差
let mouseDelta = mousePos.sub(new Vec2(ww/2,wh/2))
let mouseAngle = mouseDelta.angle
//往哪個方向射
//初始半徑
let initR = mp.r+10
//現在位置
let initPosition = mp.p.add( new Vec2(initR*Math.cos(mouseAngle),initR*Math.sin(mouseAngle)))
//食物參數
let args = {
//改成初始位置才能發射
// p: mp.p,
p: initPosition,
//自己速度的1.5倍+滑鼠方向
v: mp.v.mul(1.5).add(mouseDelta.unit.mul(Math.random()*5+10)),
mass: 80,
color: mp.color,
type: "food"
}
// console.log(args)
players.push(new Player(args))
}
})
}
})