-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird.js
85 lines (76 loc) · 1.92 KB
/
bird.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
export class Bird {
#app;
#container;
#img;
#state;
#target = {
"x": 0,
"y": 0
}
#bird_state = {
"out": 1,
"down": 2,
"adjust": 3,
"stay": 4,
"leave": 5
};
// out, down, adjust, stay
constructor(app) {
this.#state = this.#bird_state.out;
this.#app = app;
this.#container = new PIXI.Container();
this.#container.visible = false;
this.#app.stage.addChild(this.#container);
const texure = PIXI.Texture.from('smallbird_1.png');
this.#img = new PIXI.Sprite(texure);
this.#img.anchor.set(0.5)
this.#img.scale.set(0.5);
this.#container.addChild(this.#img);
this.#container.x = Math.floor(Math.random() * 150) - 150;
this.#container.y = Math.floor(Math.random() * 300) - 149;
this.#searchPlace();
}
appear(delta) {
if (this.#state === this.#bird_state.out) {
this.#state = this.#bird_state.down;
}
switch (this.#state) {
case this.#bird_state.down:
this.#moveDown(delta);
break;
case this.#bird_state.adjust:
this.#moveAdjust(delta);
break;
default:
//do nothing;
}
}
#searchPlace() {
this.#target.x = Math.floor(Math.random() * this.#app.screen.width) + 10;
this.#target.y = this.#app.screen.height / 2;
}
#moveDown(delta) {
if (this.#target.y + 30 === this.#container.y) {
this.#state = this.#bird_state.adjust;
}
if (((this.#target.y - 30) < this.#container.y + 30) && (this.#container.y < this.#target.y + 30)) {
this.#container.x += 0.5;
this.#container.y += 0.5;
}
else if (this.#container.y < this.#target.y) {
this.#container.x += 1;
this.#container.y += 1;
}
}
#moveAdjust(delta) {
if (this.#target.y === this.#container.y) {
this.#state = this.#bird_state.stay;
}
this.#container.y -= 0.5;
}
visible(visible) {
this.#container.visible = visible;
}
leave() {
}
}