-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb8.js
77 lines (66 loc) · 1.71 KB
/
bb8.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
class bb8 {
constructor() {
this.side = 100;
this.x = 100;
this.y = height - 50;
this.velocityY = 0.7;
this.gravity = 1.5;
this.headY = height - this.side;
this.body = loadImage("./images/body.png");
this.head = loadImage("./images/head.png");
this.flying = false;
this.width = this.side;
}
jump() {
if (this.y == height - this.side) {
this.velocityY = -25;
this.headY = 250;
this.flying = true;
}
}
move() {
if (keyIsDown(40) && this.headY + this.height === height) {
// down arrow key and bb8 on the ground
this.headY = this.headY + (height - this.side - this.headY) * 0.1;
} else {
// key is released (or in air)
if (scoreCounter <= 0) {
this.velocityY = 3;
}
this.y += this.velocityY;
this.headY += this.velocityY - 0.1;
this.velocityY += this.gravity;
if (
this.headY + 0.5 * this.side >= height - this.side &&
scoreCounter >= 0
) {
this.headY = height - this.side * 1.5;
}
}
if (this.y >= height - this.side && scoreCounter >= 0) {
this.y = height - this.side;
this.flying = false;
}
}
show() {
this.height = this.side + (this.y - this.headY);
// push();
// fill("red");
// rect(this.x, this.headY, this.width, this.height);
// pop();
push();
translate(
this.x + (random(1) < 0.9) + (1 / 2) * this.width,
this.y + (random(1) < 0.9) + (1 / 2) * this.side
);
rotate((PI / 20) * (frameCount * levelSpeed));
imageMode(CENTER);
image(this.body, 0, 0);
pop();
image(
this.head,
this.x + (random(1) < 0.9),
this.headY + (random(1) < 0.9)
);
}
}