-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bird.chai
111 lines (91 loc) · 2.1 KB
/
Bird.chai
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
class Bird {
def serialize() {
return [
"x": this.x,
"y": this.y,
"yspeed": this.yspeed,
"speed": this.speed
]
}
def deserialize(obj) {
this.x = obj["x"]
this.y = obj["y"]
this.yspeed = obj["yspeed"]
this.speed = obj["speed"]
}
def Bird() {
this.collisionPadding = 5
this.x = 80.0f
this.speed = 150.0f
this.y = 100.0f
this.yspeed = 0.0f
this.ygravity = 1000.0f
this.animationslide = 2
this.animtimer = 0.0f
this.img = []
for (var i = 0; i <= 3; ++i) {
var imageId = i == 3 ? 1 : i
this.img.push_back_ref(love.graphics.newImage("assets/bird" + to_string(imageId) + ".png"))
}
this.width = this.img[0].getWidth()
this.height = this.img[0].getHeight() / 4
this.ox = this.img[0].getWidth() / 2.0f
this.oy = this.img[0].getWidth() / 2.0f
}
def left() {
return this.x - this.img[0].getWidth() / 2.0f
}
def right() {
return this.x + this.img[0].getWidth() / 2.0f
}
def top() {
return this.y - this.img[0].getHeight() / 2.0f
}
def bottom() {
return this.y + this.img[0].getHeight() / 2.0f
}
def draw() {
if (love.config.options["highquality"]) {
var angle = atan2(this.yspeed, this.speed) * -20
love.graphics.draw(this.img[this.animationslide], this.x, this.y, love.math.rad(angle), 1.0f, 1.0f, this.ox, this.oy)
}
else {
love.graphics.draw(this.img[this.animationslide], this.left(), this.top())
}
}
def update(delta) {
this.animtimer += delta
if (this.animtimer > 0.12f) {
this.animtimer = 0
this.animationslide += 1
if (this.animationslide > 3) {
this.animationslide = 3
if (cheatFly) {
this.flap(false)
}
}
}
this.yspeed += this.ygravity * delta
this.y += this.yspeed * delta
if (this.bottom() - this.collisionPadding > landTop) {
assets["die"].play()
return false
}
if (this.yspeed < 0 && this.top() < 0) {
this.yspeed = 0
this.y = this.collisionPadding
}
return true
}
def flap(mute) {
this.yspeed = -280.0f
this.animationslide = 0
if (mute == false) {
assets["wing"].play()
}
}
def reset() {
this.y = love.graphics.getHeight() / 3
this.flap(true)
}
}