-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
273 lines (201 loc) · 5.44 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
--
-- player.lua
-- defines the Player class
--
require 'resources'
Player = {}
Player.__index = Player
local gravity = 1500
local stepSize = 3
function Player.new()
local self = {}
setmetatable(self, Player)
self.img = love.graphics.newImage('/data/images/walkR1.png')
-- position
self.x = 10
self.y = 400
-- collisions
self.width = self.img:getWidth()
self.height = self.img:getHeight()
self.rect = HC.rectangle(self.x,self.y,self.width,self.height)
-- jumping
self.y_vel = 0
self.jump_fuel = 0.5
self.jump_fuel_max = 0.5
self.jump_height = -500
-- state
self.state = "jumping"
self.health = 10
self.walking = false
self.x_platform_speed = 0
self.damaged = false
self.active = false
-- walkcycle
self:loadImages()
self.dwalk = 0 -- time elapsed
self.imgNum = 1 -- image rotation number
self.ddamage = 0 -- damage inflicted at discrete intervals
return self
end
function Player:update(dt, bg)
local dx, dy
-- move left
if love.keyboard.isDown('left') and self.x > 0 then
dx = -stepSize
-- move right
elseif love.keyboard.isDown('right') and self.x < 900 then
dx = stepSize
else
dx = 0
end
-- add platform speed
dx = dx + self.x_platform_speed
dy = self.y_vel * dt
self.y_vel = self.y_vel + gravity * dt
-- jump
if self.jump_fuel > 0 and love.keyboard.isDown('up') then
-- begin jump
if self.state == 'standing' then
self.y_vel = self.jump_height
self.state = 'jumping'
end
-- continue jumping
self.jump_fuel = self.jump_fuel - dt
self.y_vel = self.y_vel + 0.6 * self.jump_height * (dt / self.jump_fuel_max)
end
self:move(bg, dt, dx, dy)
-- change the game state, if necessary
if self.health <= 0 then
return 'gameover'
end
return 'play'
end
function Player:draw()
local health = love.graphics.newText(fonts['fippsM'], {black,self.health})
love.graphics.draw(health, WINDOW_W - health:getWidth() - 10, 10)
love.graphics.draw(self.img, self.x, self.y)
end
function Player:move(bg, dt, dx, dy)
-- move left
if dx < 0 and self.x > 0 then
if self.x - bg.x <= 200
or (bg.width + bg.x <= WINDOW_W and self.x > 200) then
self.x = self.x + dx
elseif bg.x < 200 then
bg:shiftWorld(dx)
end
-- move right
elseif dx > 0 and self.x < WINDOW_W - self.width then
if self.x < 200 or bg.width + bg.x <= WINDOW_W then
self.x = self.x + dx
elseif bg.x > WINDOW_W - bg.width then
bg:shiftWorld(dx)
end
end
-- gravity
self.y = self.y + dy
-- update collision rect
self.rect:moveTo(self.x + self.width/2.0, self.y + self.height/2.0)
--
-- collision checking
--
for i=1, #bg.entities do
thing = bg.entities[i]
local x,y = thing.rect:center()
if self.rect:collidesWith(thing.rect) and thing.active then
if thing.type == 'block' then
-- if landing on top of the block
if dy > 0 and self.y + self.height < y - thing.height/4 then
self.y = y - thing.height/2 - self.height
self.y_vel = 0
self.jump_fuel = self.jump_fuel_max
self.x_platform_speed = thing.vel.x
self.state = 'standing'
self:updateHealth(dt, thing)
end
elseif thing.type == 'item' then
self:updateHealth(dt, thing)
thing:kill()
elseif thing.type == 'enemy' then
-- if landing on top of the enemy
if dy >= 0 and self.y + self.height < y - thing.height/4 then
self.y = y - thing.height/2 - self.height
self.y_vel = 0
self.jump_fuel = self.jump_fuel_max
self.x_platform_speed = thing.vel.x
self.state = 'standing'
-- if hitting the side of the enemy
else
self:updateHealth(dt, thing)
end
end
end
end
-- update walkcycle image
self:walkcycle(dt, dx)
end
function Player:kill()
self.active = false
end
function Player:keypressed(bg)
-- check keypresses
end
function Player:updateHealth(dt, thing)
self.ddamage = self.ddamage + dt
if thing.health >= 0 then
self.health = self.health + thing.health
-- take damage at discrete time intervals
elseif self.damaged == false then
self.health = self.health + thing.health
self.damaged = true
end
-- reset time counter
if self.ddamage > 0.5 then
self.damaged = false
self.ddamage = self.ddamage - 0.5
end
end
function Player:loadImages()
-- load images for walkcycles
self.walkL = {}
self.walkL[1] = love.graphics.newImage('data/images/walkL1.png')
self.walkL[2] = love.graphics.newImage('data/images/walkL2.png')
self.walkL[3] = love.graphics.newImage('data/images/walkL3.png')
self.walkL[4] = love.graphics.newImage('data/images/walkL2.png')
self.walkR = {}
self.walkR[1] = love.graphics.newImage('data/images/walkR1.png')
self.walkR[2] = love.graphics.newImage('data/images/walkR2.png')
self.walkR[3] = love.graphics.newImage('data/images/walkR3.png')
self.walkR[4] = love.graphics.newImage('data/images/walkR2.png')
end
function Player:walkcycle(dt, dx)
self.dwalk = self.dwalk + dt
-- if jumping
if self.state == 'jumping' then
if dx < 0 then
self.img = self.walkL[2]
elseif dx > 0 then
self.img = self.walkR[2]
end
end
-- while walking
if dx ~= 0 and self.state == 'standing' and self.dwalk > 0.3 then
if dx < 0 then
self.img = self.walkL[self.imgNum]
else
self.img = self.walkR[self.imgNum]
end
-- increment imgNum
self.imgNum = self.imgNum + 1
if self.imgNum > 4 then self.imgNum = 1 end
self.dwalk = self.dwalk - 0.3
end
end
function Player:isNearFriend(friends)
for i=1, #friends do
if self.rect:collidesWith(friends[i].rect) then
return friends[i]
end
end
return false
end