-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.lua
236 lines (187 loc) · 4.46 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
-- Player
-- Represents the player
Player = {start = {x=0, y=0}, direction = 1, direction_y = 1, energy = 180, books = 0, brain = 0}
player_r_img = love.graphics.newImage("img/josef.png")
player_l_img = love.graphics.newImage("img/josef_left.png")
function Player:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Player:with(position, world)
self.start = position
self.body = love.physics.newBody(world.physics, position.x*32, position.y*32, 10, 0)
self.shape = love.physics.newRectangleShape(self.body, -16, -16, 32, 32, 0)
self.shape:setFriction(1.0)
self.type = "player"
self.shape:setData(self)
return self
end
function Player:draw(x, y)
local r, b = self.body:getPosition()
local l, t = r-32, b-32
local x1,y1,x2,y2 = self.shape:getPoints()
if player.direction == -1 then
love.graphics.draw(player_l_img, x1-x, y1-y)
else
love.graphics.draw(player_r_img, x1-x, y1-y)
end
end
function Player:move_y(delta, world)
local x,y = self.body:getPosition()
if delta > 0 then
self.direction_y = 1
else
self.direction_y = -1
end
self.body:setY(y + delta)
wall = self:is_collided_y(y+delta,world)
if wall then
if world:find_deadly(x-32,x,y+delta) then
-- DEATH
self.energy = 0
end
self:avoid_y(wall)
return true
end
end
function Player:move(delta, world)
local x = self.body:getX()
if delta > 0 then
self.direction = 1
else
self.direction = -1
end
self.body:setX(x + delta)
wall = self:is_collided(x+delta,world)
if wall then
self:avoid(wall)
return true
end
end
function Player:is_collided(x, world)
local xp, y = self.body:getPosition()
if self.direction == 1 then
wall = world:find_wall(x+1,y-1)
if wall then
return wall
end
wall = world:find_wall(x+1,y-31)
if wall then
return wall
end
end
if self.direction == -1 then
wall = world:find_wall(x-33,y-1)
if wall then
return wall
end
wall = world:find_wall(x-33,y-31)
if wall then
return wall
end
end
for i=1,#elevators do
elevator = elevators[i]
if elevator:is_collided_with_player(player, world) then
return elevator
end
end
end
function Player:is_collided_y(y,world)
local x, yp = self.body:getPosition()
if self.direction_y == 1 then
wall = world:find_wall(x-1,y+1)
if wall then
return wall
end
wall = world:find_wall(x-31,y+1)
if wall then
return wall
end
end
if self.direction_y == -1 then
wall = world:find_wall(x-1,y-33)
if wall then
return wall
end
wall = world:find_wall(x-31,y-33)
if wall then
return wall
end
end
for i=1,#elevators do
elevator = elevators[i]
if elevator:is_collided_with_player_y(player, world) then
return elevator
end
end
end
function Player:rest()
local vx, vy = self.body:getLinearVelocity()
self.body:setLinearVelocity(0, vy)
end
function Player:align()
local x, y = self.body:getPosition()
x = math.floor(x+0.5)
local diff = x % 4
if diff < 2 then
x = x - diff
else
x = x + (4-diff)
end
self.body:setX(x)
end
function Player:burn(world)
local x, y = self.body:getPosition()
local px = x
if self.direction < 0 then
x = x - 4
px = px - 31
else
x = x - 32 + 4
px = px - 1
end
local wall = world:find_wall(px, y+1)
local wall_break = world:find_wall(x, y+1)
if wall == nil or wall.visible == false or wall.tile_type == Type.EMPTY or wall == wall_break then
return
end
if wall_break and wall_break.tile_type == Type.BREAKABLE then
wall_break.visible = false
end
return wall_break
end
function Player:avoid(wall)
local r, b = self.body:getPosition()
local l, t = r - 32, b - 32
local wr, wb = wall.body:getPosition()
local wl, wt = wr - (wall.width * 32), wb - 32
if (l > wl and l < wr) or (r > wl and r < wr) then
if self.direction == 1 then
self.body:setX(wr-32)
else
self.body:setX(wr+32)
end
end
end
function Player:avoid_y(wall)
local r, b = self.body:getPosition()
local l, t = r - 32, b - 32
local wr, wb = wall.body:getPosition()
local wl, wt = wr - (wall.width * 32), wb - 32
if (t > wt and t < wb) or (b > wt and b < wb) then
if self.direction_y == 1 then
self.body:setY(wb-32)
else
self.body:setY(wb+32)
end
end
end
function Player:getX()
return self.body:getX()
end
function Player:getY()
return self.body:getY()
end