-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
278 lines (230 loc) · 8.4 KB
/
player.lua
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
local Anim8 = require('modules/anim8/anim8')
local Class = require('modules/middleclass/middleclass')
local HC = require('modules/HC')
local Stateful = require('modules/stateful/stateful')
local Vector = require('modules/hump/vector')
local animations = {}
local sprites = {
ball = love.graphics.newImage('res/images/baseball.png'),
ballShadow = love.graphics.newImage('res/images/baseball_shadow.png'),
bird = love.graphics.newImage('res/images/bird.png'),
birdToBall = love.graphics.newImage('res/images/bird_transform.png'),
fireTrail = love.graphics.newImage('res/images/fire_trail.png')
}
--============================================================================== PLAYER
local Player = Class('Player')
Player:include(Stateful)
Player.Ball = Player:addState('Ball')
Player.Bird = Player:addState('Bird')
Player.BirdToBall = Player:addState('BirdToBall')
Player.BallToBird = Player:addState('BallToBird')
Player.SIZE = 16
Player.ballAngularSpeed = 0.03
Player.ballMinimumSpeed = 4
Player.birdFallSpeedX = 3
Player.birdFallSpeedY = 3
Player.birdFlappySpeedX = 3
Player.birdFlappySpeedY = -4
Player.birdFlappyDecayRateX = 0.1
Player.birdFlappyDecayRateY = 0.15
Player.Ball.animationTime = 0.05
Player.Bird.animationTime = 0.1
Player.BirdToBall.animationTime = 0.1
Player.BallToBird.animationTime = Player.BirdToBall.animationTime
Player.STATE = { BIRD = 0, BALL = 1, DEAD = 2 }
function Player:initialize(x, y)
self.pos = Vector(0, 0)
self.vel = Vector(x, y)
self.body = HC.circle(self.pos.x, self.pos.y, 8)
self.intro = true
self.userCanTurn = false
self.userCanTransform = true
self.fallSpeed = 1
local grid = nil
grid = Anim8.newGrid(Player.SIZE, Player.SIZE, Player.SIZE * 6, Player.SIZE)
animations.ball = Anim8.newAnimation(grid:getFrames('1-6', 1), Player.Ball.animationTime)
grid = Anim8.newGrid(24, 24, 24 * 4, 24)
animations.bird = Anim8.newAnimation(grid:getFrames('1-4', 1), Player.Bird.animationTime, 'pauseAtEnd')
grid = Anim8.newGrid(24, 24, 24 * 6, 24)
animations.birdToBall = Anim8.newAnimation(grid:getFrames('1-6', 1), Player.BirdToBall.animationTime, function()
self.state = Player.STATE.BALL
self:gotoState('Ball')
end)
animations.ballToBird = Anim8.newAnimation(grid:getFrames('6-1', 1), Player.BallToBird.animationTime, function()
self.state = Player.STATE.BIRD
self:gotoState('Bird')
end)
grid = Anim8.newGrid(80, 24, 80 * 3, 24)
animations.fireTrail = Anim8.newAnimation(grid:getFrames('1-3', 1), 0.05)
self.state = Player.STATE.BALL
self:gotoState('Ball')
end
function Player:update(dt)
self.pos.x = self.pos.x + self.vel.x
self.pos.y = self.pos.y + self.vel.y
self.body:moveTo(self.pos:unpack())
end
function Player:draw()
if not self.intro then
Particles.update('fire', 1 / 60)
love.graphics.push()
love.graphics.translate(self.pos:unpack())
Particles.draw('fire')
love.graphics.pop()
if DEBUG then
r, g, b, a = love.graphics.getColor()
love.graphics.setColor(255, 0, 0, 255)
love.graphics.line(self.pos.x, self.pos.y, self.pos.x + self.vel.x * 5, self.pos.y + self.vel.y * 5)
love.graphics.setColor(r, g, b, a)
end
end
end
function Player:boost()
self.vel = Vector(20,-20)
SFX.sweep:play()
if self.state == Player.STATE.BIRD then
self:gotoState('BirdToBall')
else
self:gotoState('Ball')
end
end
function Player:gotoSpeed()
if self.state == Player.STATE.BALL then
local tolerance = 3
local rate = 0.0075
if math.abs(self.vel:len() - Player.ballMinimumSpeed) <= tolerance then
self:gotoState('BallToBird')
end
if self.vel:len() < Player.ballMinimumSpeed then
self.vel = self.vel * (1 + rate)
elseif self.vel:len() > Player.ballMinimumSpeed then
self.vel = self.vel * (1 - rate)
end
elseif self.state == Player.STATE.BIRD then
if self.vel.y < Player.birdFallSpeedY then
self.vel.y = self.vel.y + Player.birdFlappyDecayRateY
elseif self.vel.y > Player.birdFallSpeedY then
self.vel.y = self.vel.y - Player.birdFlappyDecayRateY
end
if self.vel.x < Player.birdFallSpeedX then
self.vel.x = self.vel.x + Player.birdFlappyDecayRateX
elseif self.vel.x > Player.birdFallSpeedX then
self.vel.x = self.vel.x - Player.birdFlappyDecayRateX
end
end
end
function Player:getHeight()
return -self.pos.y
end
function Player:prepareLanding()
self.userCanTurn = false
self.userCanTransform = false
self.vel = Vector(20, -20)
self:gotoState('Ball')
end
function Player:halt()
self.state = Player.STATE.DEAD
self.vel = Vector(0, 0)
self:gotoState('Ball')
end
--============================================================================== PLAYER.BALL
function Player.Ball:enteredState()
Particles.get('fire'):reset()
end
function Player.Ball:update(dt)
if Input.isDown('up') and self.userCanTurn then
if self.vel:angleTo() > -math.pi / 2 then
self.vel:rotateInplace(-Player.ballAngularSpeed)
end
elseif Input.isDown('down') and self.userCanTurn then
if self.vel:angleTo() < math.pi / 2 then
self.vel:rotateInplace(Player.ballAngularSpeed)
end
end
Player.update(self, dt)
Player.gotoSpeed(self)
if self.userCanTurn then
local fire = Particles.get('fire')
fire:setDirection(self.vel:angleTo(Vector(-1, 0)))
fire:setSpeed(self.vel:len() * 8, self.vel:len() * 40)
Particles.emit('fire', 0, 0, self.vel:len() / 3)
end
if DEBUG and Input.pressed('t') then
self:gotoState('BallToBird')
end
end
function Player.Ball:draw()
if not self.intro then
Player.draw(self)
animations.ball:update(self.vel:len() / 1000)
animations.ball:draw(sprites.ball, self.pos.x, self.pos.y, self.vel:angleTo(), 1, 1, Player.SIZE / 2, Player.SIZE / 2)
love.graphics.setBlendMode('multiply')
love.graphics.draw(sprites.ballShadow, self.pos.x, self.pos.y, 0, 1, 1, Player.SIZE / 2, Player.SIZE / 2)
love.graphics.setBlendMode('alpha')
end
end
--============================================================================== PLAYER.BIRD
function Player.Bird:enteredState()
end
function Player.Bird:update(dt)
Player.update(self, dt)
Player.gotoSpeed(self)
if self.userCanTransform and Input.pressed('up') then
self.vel = Vector(Player.birdFlappySpeedX, Player.birdFlappySpeedY)
animations.bird:pauseAtStart()
animations.bird:resume()
SFX.flap:play()
end
if DEBUG and Input.pressed('t') then
self:boost()
end
end
function Player.Bird:draw()
if not self.intro then
Player.draw(self)
animations.bird:update(1 / 60)
animations.bird:draw(sprites.bird, self.pos.x, self.pos.y, 0, 1, 1, 12, 12)
end
end
function Player.Bird:prepareLanding()
Player.prepareLanding(self)
self:gotoState('BirdToBall')
end
--============================================================================== PLAYER.BIRDUP
function Player.BirdToBall:update(dt)
Player.update(self, dt)
Player.gotoSpeed(self)
animations.birdToBall:update(dt)
if Input.isDown('up') and self.userCanTurn then
if self.vel:angleTo() > -math.pi / 2 then
self.vel:rotateInplace(-Player.ballAngularSpeed)
end
elseif Input.isDown('down') and self.userCanTurn then
if self.vel:angleTo() < math.pi / 2 then
self.vel:rotateInplace(Player.ballAngularSpeed)
end
end
end
function Player.BirdToBall:draw()
if not self.intro then
Player.draw(self)
animations.birdToBall:draw(sprites.birdToBall, self.pos.x, self.pos.y, 0, 1, 1, 12, 12)
end
end
--============================================================================== PLAYER.BIRDDOWN
function Player.BallToBird:update(dt)
Player.update(self, dt)
Player.gotoSpeed(self)
animations.ballToBird:update(dt)
end
function Player.BallToBird:draw()
if not self.intro then
Player.draw(self)
animations.ballToBird:draw(sprites.birdToBall, self.pos.x, self.pos.y, 0, 1, 1, 12, 12)
end
end
function Player.BallToBird:prepareLanding()
Player.prepareLanding(self)
self:gotoState('Ball')
end
return Player