-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfishes.js
144 lines (130 loc) · 3.85 KB
/
fishes.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
import { Collidable } from "./collidable.js";
var fishes = new Set();
var baleineTexture;
var fishTexture;
var fishTexture2;
var fishTexture3;
var fishTexture4;
var fishTexture5;
var fishTexture6;
var fishTexture7;
var fishTexture8;
function spawnFish(screen) {
let randomTexture;
switch (Math.floor(Math.random() * 8)) {
case 0:
randomTexture = fishTexture;
break;
case 1:
randomTexture = fishTexture2;
break;
case 2:
randomTexture = fishTexture3;
break;
case 3:
randomTexture = fishTexture4;
break;
case 4:
randomTexture = fishTexture5;
break;
case 5:
randomTexture = fishTexture6;
break;
case 6:
randomTexture = fishTexture7;
break;
case 7:
randomTexture = fishTexture8;
break;
}
const miniFish = new PIXI.Sprite(randomTexture);
miniFish.width *= 0.3;
miniFish.height *= 0.3;
let [x, y] = findRandomPositionInWater();
miniFish.x = x;
miniFish.y = y;
miniFish.anchor.set(0.5);
new Fish(miniFish, screen, 2);
}
export async function initFishes(app, screen) {
let lastSpawn = 0;
app.ticker.add((delta) => {
if (lastSpawn + 5000 < delta.lastTime && fishes.size < 100) {
spawnFish(screen);
lastSpawn = delta.lastTime;
}
for (let fish of fishes) {
fish.move(delta.deltaTime);
}
});
baleineTexture = await PIXI.Assets.load('./assets/baleine.png');
fishTexture = await PIXI.Assets.load('./assets/fish.png');
fishTexture2 = await PIXI.Assets.load('./assets/fish2.png');
fishTexture3 = await PIXI.Assets.load('./assets/fish3.png');
fishTexture4 = await PIXI.Assets.load('./assets/fish4.png');
fishTexture5 = await PIXI.Assets.load('./assets/fish5.png');
fishTexture6 = await PIXI.Assets.load('./assets/fish6.png');
fishTexture7 = await PIXI.Assets.load('./assets/fish7.png');
fishTexture8 = await PIXI.Assets.load('./assets/fish8.png');
const baleine = new PIXI.Sprite(baleineTexture);
baleine.width *= 0.3;
baleine.height *= 0.3;
let [x, y] = findRandomPositionInWater();
baleine.x = x;
baleine.y = y;
baleine.anchor.set(0.5);
new Fish(baleine, screen, 3);
for (let i = 0; i < 100; i++) {
spawnFish(screen);
}
}
export class Fish extends Collidable {
constructor(sprite, screen, speed) {
super(sprite, "fish", ["trash"]);
this.sprite = sprite;
this.speed = speed;
this.screen = screen;
this.changeSteer();
fishes.add(this);
screen.addChild(this.sprite);
}
changeSteer() {
this.vx = Math.random() - 0.5;
this.vy = Math.random() - 0.5;
let norm = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
let multiplier = this.speed / norm;
this.vx *= multiplier;
this.vy *= multiplier;
if (this.vx < 0) {
if (this.sprite.scale.x > 0) {
this.sprite.scale.x = -this.sprite.scale.x;
}
} else {
if (this.sprite.scale.x < 0) {
this.sprite.scale.x = -this.sprite.scale.x;
}
}
}
move(delta) {
let newX = this.sprite.x + this.vx * delta;
let newY = this.sprite.y + this.vy * delta;
if (!isInWater(newX, newY)) {
this.changeSteer();
} else {
this.sprite.x = newX;
this.sprite.y = newY;
}
}
onCollision(object) {
for (let enemy of this.enenemies) {
if (object.me == enemy) {
this.destroy();
break;
}
}
}
destroy() {
this.screen.removeChild(this.sprite);
fishes.delete(this);
}
}