-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentities.js
More file actions
1793 lines (1521 loc) · 56.5 KB
/
Copy pathentities.js
File metadata and controls
1793 lines (1521 loc) · 56.5 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
// entities.js - 完整实体模块代码,与game.js完全兼容
// 基础游戏实体类
class GameEntity {
constructor() {
if (new.target === GameEntity) {
throw new Error("GameEntity is an abstract class and cannot be instantiated directly.");
}
}
update() {
throw new Error("Method 'update()' must be implemented.");
}
dispose() {
throw new Error("Method 'dispose()' must be implemented.");
}
}
// 食物类
class Food extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.mesh = null;
this.create();
}
create() {
const geometry = new THREE.SphereGeometry(8, 16, 16);
const material = new THREE.MeshPhongMaterial({
color: 0xff5555,
shininess: 10,
emissive: 0xaa0000,
emissiveIntensity: 0.9
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
// 随机位置
this.randomizePosition();
// 标记为可见
this.mesh.userData = { visible: true };
this.scene.add(this.mesh);
}
randomizePosition() {
this.mesh.position.set(
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
-(Math.floor(Math.random() * this.worldSize) - this.worldSize/2)
);
}
update() {
// 食物旋转动画
this.mesh.rotation.x += 0.008;
this.mesh.rotation.y += 0.012;
}
dispose() {
this.scene.remove(this.mesh);
this.mesh.geometry.dispose();
this.mesh.material.dispose();
}
}
// 液态食物类 - 增强版
class LiquidFood extends Food {
constructor(scene, worldSize) {
super(scene, worldSize);
}
create() {
// 创建更复杂的液态食物几何体
const geometry = this.createLiquidGeometry();
// 创建液态食物的材质
const material = new THREE.MeshPhongMaterial({
color: new THREE.Color().setHSL(Math.random(), 0.8, 0.6),
transparent: true,
opacity: 0.8,
shininess: 90,
emissive: new THREE.Color().setHSL(Math.random(), 0.5, 0.4),
emissiveIntensity: 0.4,
specular: new THREE.Color(0xffffff),
metalness: 0.1
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
// 随机位置
this.randomizePosition();
// 标记为可见
this.mesh.userData = {
visible: true,
flowSpeed: Math.random() * 0.02 + 0.01,
flowDirection: new THREE.Vector3(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
).normalize(),
timeOffset: Math.random() * 1000,
pulseSpeed: Math.random() * 0.005 + 0.003,
rotationSpeed: new THREE.Vector3(
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01
)
};
this.scene.add(this.mesh);
}
// 使用分形噪声创建更复杂的液态几何体
createLiquidGeometry() {
const segments = 16;
const geometry = new THREE.SphereGeometry(10, segments, segments);
// 应用分形噪声使表面不规则
const positionAttribute = geometry.attributes.position;
const originalPositions = positionAttribute.array.slice();
// 应用多层级分形噪声
for (let i = 0; i < positionAttribute.count; i++) {
const x = originalPositions[i * 3];
const y = originalPositions[i * 3 + 1];
const z = originalPositions[i * 3 + 2];
// 计算原始球体法线
const normal = new THREE.Vector3(x, y, z).normalize();
// 应用分形噪声
let noise = 0;
let frequency = 1.0;
let amplitude = 1.0;
for (let j = 0; j < 4; j++) {
noise += this.fbm(
x * frequency,
y * frequency,
z * frequency
) * amplitude;
frequency *= 2.0;
amplitude *= 0.5;
}
// 应用噪声位移
const displacement = noise * 2.0;
positionAttribute.setXYZ(
i,
x + normal.x * displacement,
y + normal.y * displacement,
z + normal.z * displacement
);
}
positionAttribute.needsUpdate = true;
geometry.computeVertexNormals();
return geometry;
}
// 分形布朗运动噪声函数
fbm(x, y, z) {
// 简化版分形噪声
return Math.sin(x * 0.5) * Math.cos(y * 0.7) * Math.sin(z * 0.3) +
Math.sin(x * 1.0) * Math.cos(y * 1.4) * Math.sin(z * 0.6) * 0.5 +
Math.sin(x * 2.0) * Math.cos(y * 2.8) * Math.sin(z * 1.2) * 0.25;
}
update(timestamp) {
// 液态流动效果
const time = timestamp * 0.001;
const flow = this.mesh.userData;
// 脉动效果
const pulse = Math.sin(time * flow.pulseSpeed * 2 + flow.timeOffset) * 0.15 + 1;
this.mesh.scale.set(pulse, pulse, pulse);
// 颜色变化效果
const hue = (time * 0.001 + flow.timeOffset * 0.0001) % 1;
this.mesh.material.color.setHSL(hue, 0.8, 0.6);
this.mesh.material.emissive.setHSL((hue + 0.5) % 1, 0.5, 0.4);
// 轻微移动效果
const move = Math.sin(time * flow.flowSpeed + flow.timeOffset) * 0.8;
this.mesh.position.add(flow.flowDirection.clone().multiplyScalar(move * 0.1));
// 旋转效果
this.mesh.rotation.x += flow.rotationSpeed.x;
this.mesh.rotation.y += flow.rotationSpeed.y;
this.mesh.rotation.z += flow.rotationSpeed.z;
// 边界检查
if (Math.abs(this.mesh.position.x) > this.worldSize/2 - 20) {
flow.flowDirection.x *= -1;
this.mesh.position.x = Math.sign(this.mesh.position.x) * (this.worldSize/2 - 20);
}
if (Math.abs(this.mesh.position.y) > this.worldSize/2 - 20) {
flow.flowDirection.y *= -1;
this.mesh.position.y = Math.sign(this.mesh.position.y) * (this.worldSize/2 - 20);
}
if (Math.abs(this.mesh.position.z) > this.worldSize/2 - 20) {
flow.flowDirection.z *= -1;
this.mesh.position.z = Math.sign(this.mesh.position.z) * (this.worldSize/2 - 20);
}
}
}
// 食物管理器
class FoodManager {
constructor(scene, worldSize, count = 900) {
this.scene = scene;
this.worldSize = worldSize;
this.count = count;
this.foods = [];
this.createFoods();
}
createFoods() {
// 创建普通食物和液态食物的混合
for (let i = 0; i < this.count; i++) {
// 30%的概率创建液态食物(从10%提高)
if (Math.random() < 0.3) {
this.foods.push(new LiquidFood(this.scene, this.worldSize));
} else {
this.foods.push(new Food(this.scene, this.worldSize));
}
}
}
// 在指定位置添加食物
addFoodAtPosition(position) {
// 80%的概率创建普通食物,20%的概率创建液态食物
let food;
if (Math.random() < 0.2) {
food = new LiquidFood(this.scene, this.worldSize);
} else {
food = new Food(this.scene, this.worldSize);
}
food.mesh.position.copy(position);
this.foods.push(food);
return food;
}
update(timestamp) {
for (let food of this.foods) {
food.update(timestamp);
}
}
checkCollision(headPosition, onCollision) {
for (let i = this.foods.length - 1; i >= 0; i--) {
const food = this.foods[i];
const distance = headPosition.distanceTo(food.mesh.position);
if (distance < 50) {
onCollision(food, i);
break;
}
}
}
dispose() {
for (let food of this.foods) {
food.dispose();
}
this.foods = [];
}
}
// 障碍物类
class Obstacle extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.mesh = null;
this.create();
}
create() {
const size = Math.random() * 30 + 20;
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshPhongMaterial({
color: 0xaa55ff,
shininess: 60,
emissive: 0x5511aa,
emissiveIntensity: 0.3
});
this.mesh = new THREE.Mesh(geometry, material);
// 随机位置
this.mesh.position.set(
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
-(Math.floor(Math.random() * this.worldSize) - this.worldSize/2)
);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
// 标记为可见
this.mesh.userData = { visible: true };
this.scene.add(this.mesh);
}
update() {
// 障碍物不需要特殊更新
}
checkCollision(position) {
const distance = position.distanceTo(this.mesh.position);
const obstacleSize = Math.max(
this.mesh.geometry.parameters.width,
this.mesh.geometry.parameters.height,
this.mesh.geometry.parameters.depth
);
const collisionThreshold = 6 + (obstacleSize / 2);
return distance < collisionThreshold;
}
dispose() {
this.scene.remove(this.mesh);
this.mesh.geometry.dispose();
this.mesh.material.dispose();
}
}
// 障碍物管理器
class ObstacleManager {
constructor(scene, worldSize, count = 15) {
this.scene = scene;
this.worldSize = worldSize;
this.count = count;
this.obstacles = [];
this.createObstacles();
}
createObstacles() {
for (let i = 0; i < this.count; i++) {
this.obstacles.push(new Obstacle(this.scene, this.worldSize));
}
}
update() {
for (let obstacle of this.obstacles) {
obstacle.update();
}
}
checkCollisions(position) {
for (let obstacle of this.obstacles) {
if (obstacle.checkCollision(position)) {
return true;
}
}
return false;
}
dispose() {
for (let obstacle of this.obstacles) {
obstacle.dispose();
}
this.obstacles = [];
}
}
// 小球藻类
class Algae extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.mesh = null;
this.create();
}
create() {
this.mesh = new THREE.Group();
// 创建小球藻主体
const mainGeometry = new THREE.SphereGeometry(12, 8, 8);
mainGeometry.scale(
0.8 + Math.random() * 0.4,
0.8 + Math.random() * 0.4,
0.8 + Math.random() * 0.4
);
// 粗糙材质
const algaeMaterial = new THREE.MeshPhongMaterial({
color: 0x55ff55,
transparent: true,
opacity: 0.7,
shininess: 20,
emissive: 0x22aa22,
emissiveIntensity: 0.3
});
const algaeMesh = new THREE.Mesh(mainGeometry, algaeMaterial);
algaeMesh.castShadow = true;
algaeMesh.receiveShadow = true;
this.mesh.add(algaeMesh);
// 添加絮状效果
const fluffCount = 2 + Math.floor(Math.random() * 8);
for (let j = 0; j < fluffCount; j++) {
const fluffGeometry = new THREE.SphereGeometry(
2 + Math.random() * 4,
6,
6
);
// 随机位置偏移
const offset = new THREE.Vector3(
(Math.random() - 0.5) * 25,
(Math.random() - 0.5) * 25,
(Math.random() - 0.5) * 25
);
const fluffMesh = new THREE.Mesh(fluffGeometry, algaeMaterial);
fluffMesh.position.copy(offset);
fluffMesh.castShadow = true;
fluffMesh.receiveShadow = true;
this.mesh.add(fluffMesh);
}
// 随机位置
this.mesh.position.set(
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
-(Math.floor(Math.random() * this.worldSize) - this.worldSize/2)
);
// 随机漂浮参数
this.mesh.userData = {
speed: Math.random() * 0.5 + 0.2,
direction: new THREE.Vector3(
Math.random() * 0.1 - 0.05,
Math.random() * 0.1 - 0.05,
Math.random() * 0.1 - 0.05
),
rotationSpeed: new THREE.Vector3(
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01
),
visible: true
};
this.scene.add(this.mesh);
}
update() {
// 更新位置
this.mesh.position.add(this.mesh.userData.direction);
// 旋转
this.mesh.rotation.x += this.mesh.userData.rotationSpeed.x;
this.mesh.rotation.y += this.mesh.userData.rotationSpeed.y;
this.mesh.rotation.z += this.mesh.userData.rotationSpeed.z;
// 边界检查 - 反弹
if (Math.abs(this.mesh.position.x) > this.worldSize/2 - 20) {
this.mesh.userData.direction.x *= -1;
this.mesh.position.x = Math.sign(this.mesh.position.x) * (this.worldSize/2 - 20);
}
if (Math.abs(this.mesh.position.y) > this.worldSize/2 - 20) {
this.mesh.userData.direction.y *= -1;
this.mesh.position.y = Math.sign(this.mesh.position.y) * (this.worldSize/2 - 20);
}
if (Math.abs(this.mesh.position.z) > this.worldSize/2 - 20) {
this.mesh.userData.direction.z *= -1;
this.mesh.position.z = Math.sign(this.mesh.position.z) * (this.worldSize/2 - 20);
}
}
checkCollision(headPosition, onCollision) {
const distance = headPosition.distanceTo(this.mesh.position);
if (distance < 30) {
onCollision(this);
return true;
}
return false;
}
dispose() {
this.scene.remove(this.mesh);
// 需要遍历所有子网格并处置
this.mesh.traverse(child => {
if (child.isMesh) {
child.geometry.dispose();
child.material.dispose();
}
});
}
}
// 小球藻管理器
class AlgaeManager {
constructor(scene, worldSize, count = 200) {
this.scene = scene;
this.worldSize = worldSize;
this.count = count;
this.algae = [];
this.createAlgae();
}
createAlgae() {
for (let i = 0; i < this.count; i++) {
this.algae.push(new Algae(this.scene, this.worldSize));
}
}
update() {
for (let alga of this.algae) {
alga.update();
}
}
checkCollisions(headPosition, onCollision) {
for (let i = this.algae.length - 1; i >= 0; i--) {
const alga = this.algae[i];
if (alga.checkCollision(headPosition, onCollision)) {
// 从场景中移除小球藻
this.scene.remove(alga.mesh);
this.algae.splice(i, 1);
break;
}
}
}
dispose() {
for (let alga of this.algae) {
alga.dispose();
}
this.algae = [];
}
}
// 海带类
class Kelp extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.mesh = null;
this.create();
}
create() {
this.mesh = new THREE.Group();
// 创建海带茎
const stemGeometry = new THREE.CylinderGeometry(1.8, 2.0, 100, 12, 32);
const stemMaterial = new THREE.MeshPhongMaterial({
color: 0x5533cc,
shininess: 60,
emissive: 0x331188,
emissiveIntensity: 0.3
});
// 扭曲茎干
const positionAttribute = stemGeometry.attributes.position;
for (let j = 0; j < positionAttribute.count; j++) {
const y = positionAttribute.getY(j);
const twistAmount = Math.sin(y * 0.1) * 7;
positionAttribute.setX(j, positionAttribute.getX(j) + twistAmount);
}
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.position.y = 50;
this.mesh.add(stem);
// 创建叶片材质
const leafMaterial = new THREE.MeshPhongMaterial({
color: 0x8855ff,
shininess: 40,
emissive: 0x5533cc,
emissiveIntensity: 0.2,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.85
});
// 创建多层叶片
for (let j = 0; j < 6; j++) {
const layerHeight = j * 15;
// 创建主叶片
const leafGeometry = new THREE.PlaneGeometry(35, 16, 16, 4);
// 弯曲叶片
const positions = leafGeometry.attributes.position.array;
for (let k = 0; k < positions.length; k += 3) {
const y = positions[k + 1];
const bend = Math.sin((y + 8) * 0.3) * 8;
positions[k] += bend;
}
leafGeometry.attributes.position.needsUpdate = true;
const leaf = new THREE.Mesh(leafGeometry, leafMaterial);
leaf.position.y = layerHeight;
leaf.rotation.x = Math.PI / 2;
// 随机旋转角度
const rotationAngle = (j % 2 === 0 ? 1 : -1) * (Math.PI / 4 + Math.random() * 0.2);
leaf.rotation.z = rotationAngle;
// 存储原始值用于动画
leaf.userData = {
originalX: leaf.position.x,
originalY: leaf.position.y,
originalRotationZ: leaf.rotation.z
};
this.mesh.add(leaf);
// 添加对称叶片
const leaf2 = leaf.clone();
leaf2.rotation.z = -rotationAngle;
// 存储原始值
leaf2.userData = {
originalX: leaf2.position.x,
originalY: leaf2.position.y,
originalRotationZ: leaf2.rotation.z
};
this.mesh.add(leaf2);
// 添加小型侧叶
for (let s = 0; s < 2; s++) {
const sideLeafGeometry = new THREE.PlaneGeometry(15, 8, 8, 2);
// 弯曲侧叶
const sidePositions = sideLeafGeometry.attributes.position.array;
for (let k = 0; k < sidePositions.length; k += 3) {
const y = sidePositions[k + 1];
const bend = Math.sin((y + 4) * 0.4) * 4;
sidePositions[k] += bend;
}
sideLeafGeometry.attributes.position.needsUpdate = true;
const sideLeaf = new THREE.Mesh(sideLeafGeometry, leafMaterial);
sideLeaf.position.y = layerHeight + 5;
sideLeaf.position.x = (s === 0 ? -1 : 1) * 15;
sideLeaf.rotation.x = Math.PI / 2;
sideLeaf.rotation.z = (s === 0 ? 1 : -1) * (Math.PI / 3 + Math.random() * 0.2);
// 存储原始值
sideLeaf.userData = {
originalX: sideLeaf.position.x,
originalY: sideLeaf.position.y,
originalRotationZ: sideLeaf.rotation.z
};
this.mesh.add(sideLeaf);
}
}
// 添加顶部装饰
const topGeometry = new THREE.ConeGeometry(4, 12, 8);
const topMaterial = new THREE.MeshPhongMaterial({
color: 0xaa77ff,
emissive: 0x7744dd,
emissiveIntensity: 0.3,
shininess: 50
});
const top = new THREE.Mesh(topGeometry, topMaterial);
top.position.y = 100;
this.mesh.add(top);
// 随机位置
this.mesh.position.set(
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
Math.floor(Math.random() * 100) - 50,
-(Math.floor(Math.random() * this.worldSize) - this.worldSize/2)
);
// 随机缩放
const scale = 0.7 + Math.random() * 0.6;
this.mesh.scale.set(scale, scale, scale);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
// 摆动参数
this.mesh.userData = {
baseRotation: Math.random() * Math.PI * 2,
swingSpeed: Math.random() * 0.01 + 0.02,
swingAmplitude: Math.random() * 0.3 + 0.2,
leafWaveOffset: Math.random() * Math.PI * 2,
timeOffset: Math.random() * 1000,
visible: true
};
this.scene.add(this.mesh);
// 添加气泡效果
this.createBubbles();
}
createBubbles() {
const bubbleGroup = new THREE.Group();
const bubbleMaterial = new THREE.MeshPhongMaterial({
color: 0x88ccff,
transparent: true,
opacity: 0.6,
shininess: 100,
emissive: 0x4488ff,
emissiveIntensity: 0.3
});
// 创建多个气泡
for (let i = 0; i < 8; i++) {
const bubbleGeometry = new THREE.SphereGeometry(1.5 + Math.random() * 1, 8, 8);
const bubble = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
// 随机位置
const angle = Math.random() * Math.PI * 2;
const radius = 15 + Math.random() * 10;
const height = Math.random() * 80;
bubble.position.set(
Math.cos(angle) * radius,
height,
Math.sin(angle) * radius
);
bubble.castShadow = true;
bubble.receiveShadow = true;
bubble.userData = {
speed: 0.5 + Math.random() * 0.1,
startHeight: height,
amplitude: 0.5 + Math.random(),
offset: Math.random() * Math.PI * 2
};
bubbleGroup.add(bubble);
}
this.mesh.add(bubbleGroup);
}
update(timestamp) {
const time = timestamp * 0.001;
const t = time + this.mesh.userData.timeOffset * 0.001;
// 整体摆动效果
this.mesh.rotation.y = this.mesh.userData.baseRotation +
Math.sin(t * this.mesh.userData.swingSpeed) * this.mesh.userData.swingAmplitude;
// 叶片波浪效果
const leaves = [];
this.mesh.traverse(child => {
if (child.isMesh && child.geometry.type === "PlaneGeometry") {
leaves.push(child);
}
});
for (let j = 0; j < leaves.length; j++) {
const leaf = leaves[j];
const waveOffset = this.mesh.userData.leafWaveOffset + j * 0.3;
// 波浪效果 - 上下移动
const waveY = Math.sin(t * 1.5 + waveOffset) * 3;
// 波浪效果 - 左右摆动
const waveX = Math.sin(t * 1.8 + waveOffset) * 2;
// 应用波浪效果
leaf.position.y = leaf.userData.originalY + waveY;
leaf.position.x = leaf.userData.originalX + waveX;
// 轻微旋转变化
leaf.rotation.z = leaf.userData.originalRotationZ +
Math.sin(t * 2 + waveOffset) * 0.1;
}
// 更新气泡效果
const bubbleGroup = this.mesh.children.find(child => child.isGroup);
if (bubbleGroup) {
bubbleGroup.children.forEach(bubble => {
// 气泡上下浮动
bubble.position.y = bubble.userData.startHeight +
Math.sin(t * bubble.userData.speed + bubble.userData.offset) *
bubble.userData.amplitude * 10;
// 气泡旋转
bubble.rotation.x += 0.01;
bubble.rotation.y += 0.015;
});
}
}
checkCollision(headPosition, onCollision) {
const distance = headPosition.distanceTo(this.mesh.position);
if (distance < 50) {
onCollision(this);
return true;
}
return false;
}
dispose() {
this.scene.remove(this.mesh);
// 需要遍历所有子网格并处置
this.mesh.traverse(child => {
if (child.isMesh) {
child.geometry.dispose();
child.material.dispose();
}
});
}
}
// 海带管理器
class KelpManager {
constructor(scene, worldSize, count = 30) {
this.scene = scene;
this.worldSize = worldSize;
this.count = count;
this.kelps = [];
this.createKelps();
}
createKelps() {
for (let i = 0; i < this.count; i++) {
this.kelps.push(new Kelp(this.scene, this.worldSize));
}
}
update(timestamp) {
for (let kelp of this.kelps) {
kelp.update(timestamp);
}
}
checkCollisions(headPosition, onCollision) {
for (let i = this.kelps.length - 1; i >= 0; i--) {
const kelp = this.kelps[i];
if (kelp.checkCollision(headPosition, onCollision)) {
// 从场景中移除海带
this.scene.remove(kelp.mesh);
this.kelps.splice(i, 1);
break;
}
}
}
dispose() {
for (let kelp of this.kelps) {
kelp.dispose();
}
this.kelps = [];
}
}
// 变形虫类
class Amoeba extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.mesh = null;
this.create();
}
create() {
// 创建简单的球体作为变形虫
const geometry = new THREE.SphereGeometry(15, 16, 16);
const material = new THREE.MeshPhongMaterial({
color: 0xff88ff,
emissive: 0xaa44aa,
emissiveIntensity: 0.3
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.position.set(
Math.floor(Math.random() * this.worldSize) - this.worldSize/2,
-200,
-(Math.floor(Math.random() * this.worldSize) - this.worldSize/2)
);
const scale = 0.8 + Math.random() * 0.4;
this.mesh.scale.set(scale, scale, scale);
this.mesh.userData = {
pulseSpeed: 0.5 + Math.random() * 0.5,
rotationSpeed: new THREE.Vector3(
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01,
(Math.random() - 0.5) * 0.01
),
visible: true
};
this.scene.add(this.mesh);
}
update(timestamp) {
// 脉动效果
const pulse = Math.sin(timestamp * 0.001 * this.mesh.userData.pulseSpeed) * 0.1 + 1;
this.mesh.scale.set(pulse, pulse, pulse);
// 旋转
this.mesh.rotation.x += this.mesh.userData.rotationSpeed.x;
this.mesh.rotation.y += this.mesh.userData.rotationSpeed.y;
this.mesh.rotation.z += this.mesh.userData.rotationSpeed.z;
}
checkCollision(headPosition, onCollision) {
const distance = headPosition.distanceTo(this.mesh.position);
if (distance < 50) {
onCollision(this);
return true;
}
return false;
}
dispose() {
this.scene.remove(this.mesh);
this.mesh.geometry.dispose();
this.mesh.material.dispose();
}
}
// 变形虫管理器
class AmoebaManager {
constructor(scene, worldSize, count = 5) {
this.scene = scene;
this.worldSize = worldSize;
this.count = count;
this.amoebas = [];
this.createAmoebas();
}
createAmoebas() {
for (let i = 0; i < this.count; i++) {
this.amoebas.push(new Amoeba(this.scene, this.worldSize));
}
}
update(timestamp) {
for (let amoeba of this.amoebas) {
amoeba.update(timestamp);
}
}
checkCollisions(headPosition, onCollision) {
for (let i = this.amoebas.length - 1; i >= 0; i--) {
const amoeba = this.amoebas[i];
if (amoeba.checkCollision(headPosition, onCollision)) {
// 从场景中移除变形虫
this.scene.remove(amoeba.mesh);
this.amoebas.splice(i, 1);
break;
}
}
}
dispose() {
for (let amoeba of this.amoebas) {
amoeba.dispose();
}
this.amoebas = [];
}
}
// AI蛇类
class AISnake extends GameEntity {
constructor(scene, worldSize) {
super();
this.scene = scene;
this.worldSize = worldSize;
this.body = [];
this.positionHistory = [];
this.length = 1 + Math.floor(Math.random() * 40);
this.color = this.getRandomColor();
this.direction = new THREE.Vector3(