-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelControl.lua
235 lines (216 loc) · 6.72 KB
/
levelControl.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
function createWalls(x, y, width, height)
--local wall param gets wall info from the level data
local wall = world:newRectangleCollider(x, y, width, height, {collision_class = "Wall"})
wall:setType('static')
table.insert(walls, wall)
end
function createToilets(x, y, width, height)
local toilet = world:newRectangleCollider(x, y, width, height, {collision_class = "Toilets"})
toilet:setType('static')
table.insert(toilets, toilet)
end
function createHDoor(x, y, width, height)
local door = world:newRectangleCollider(x, y, width, height, {collision_class = "Doors"})
door:setType('static')
door.locked = true
table.insert(hDoors, door)
end
function createVDoor(x, y, width, height)
local door = world:newRectangleCollider(x, y, width, height, {collision_class = "Doors"})
door:setType('static')
door.locked = true
table.insert(vDoors, door)
end
function createKeys(x, y, width, height)
local key = world:newRectangleCollider(x, y, width + 20, height, {collision_class = "Keys"})
key:setType('static')
key.collected = false
key.animation = animations.key
table.insert(keys, key)
end
function toiletUpdate(dt)
if gameMode == 1 then
local toiletCollider = world:queryRectangleArea(player:getX() + 30, player:getY() + 30, 10, 10, {'Toilets'})
if #toiletCollider > 0 then
toiletFlag = true
pauseTrigger()
end
end
end
function keyDraw()
for i,k in ipairs(keys) do
local kx, ky = k:getPosition()
k.animation:draw(sprites.keyGraphic, kx+5, ky+10, nil, nil, nil, sprites.keyGraphic:getWidth(), sprites.keyGraphic:getHeight())
end
end
function hDoorUpdate(dt)
if gameMode == 1 then
for i,d in ipairs(hDoors) do
local doorHCollider = world:queryRectangleArea(d:getX()-10, d:getY()-10, 20, 20, {'Player'})
if #doorHCollider > 0 then
if player.hasKey then
player.hasKey = false
d.locked = false
love.audio.play(sounds.doorOpen)
end
end
end
for i=#hDoors,1,-1 do
local d = hDoors[i]
if d.locked == false then
hDoors[i]:destroy()
table.remove(hDoors,i)
end
end
end
end
function vDoorUpdate(dt)
if gameMode == 1 then
for i,d in ipairs(vDoors) do
local doorVCollider = world:queryRectangleArea(d:getX()-10, d:getY()-10, 20, 20, {'Player'})
if #doorVCollider > 0 then
if player.hasKey then
player.hasKey = false
d.locked = false
love.audio.play(sounds.doorOpen)
end
end
end
for i=#vDoors,1,-1 do
local d = vDoors[i]
if d.locked == false then
vDoors[i]:destroy()
table.remove(vDoors,i)
end
end
end
end
function drawDoors()
for i, d in ipairs(vDoors) do
local dx, dy = d:getPosition()
love.graphics.draw(sprites.doorGraphic, dx+5, dy-20, 1.5708, nil, nil)
--love.graphics.draw()
end
for i, d in ipairs(hDoors) do
local dx, dy = d:getPosition()
love.graphics.draw(sprites.doorGraphic, dx-20, dy)
end
end
function keyUpdate(dt)
if gameMode == 1 then
if #keys > 0 then
for i,k in ipairs(keys) do
k.animation:update(dt)
local keyCollider = world:queryRectangleArea(k:getX() + 5, k:getY(), 10, 10, {'Player'})
if #keyCollider > 0 then
if player.hasKey == false then
player.hasKey = true
k.collected = true
love.audio.play(sounds.key)
end
end
end
for i=#keys,1,-1 do
local k = keys[i]
if k.collected then
keys[i]:destroy()
table.remove(keys,i)
end
end
--debug
if player.hasKey then
debugKey = 1
else
debugKey = 0
end
end
end
end
function clearRoom()
local i = #walls
while i > -1 do
if walls[i] ~= nil then
walls[i]:destroy()
end
table.remove(walls, i)
i = i -1
end
local i = #keys
while i > -1 do
if keys[i] ~= nil then
keys[i]:destroy()
end
table.remove(keys, i)
i = i -1
end
local i = #toilets
while i > -1 do
if toilets[i] ~= nil then
toilets[i]:destroy()
end
table.remove(toilets, i)
i = i -1
end
local d = #hDoors
while d > -1 do
if hDoors[d] ~= nil then
hDoors[d]:destroy()
end
table.remove(hDoors, d)
d = d -1
end
local d = #vDoors
while d > -1 do
if vDoors[d] ~= nil then
vDoors[d]:destroy()
end
table.remove(vDoors, d)
d = d -1
end
local i = #ghosts
while i > -1 do
if ghosts[i] ~= nil then
ghosts[i]:destroy()
end
table.remove(ghosts, i)
i = i -1
end
player.hasKey = false
end
function miss()
player.misses = player.misses + 1
--gameMode = 2
if player.misses <=2 then
loadroom(roomNumber)
else
end
end
function loadRoom(roomNumber)
clearRoom()
gameMap = sti("rooms/room" .. roomNumber .. ".lua")
for i, obj in pairs(gameMap.layers["Walls"].objects) do
createWalls(obj.x, obj.y, obj.width, obj.height)
end
for i, obj in pairs(gameMap.layers["Toilets"].objects) do
createToilets(obj.x, obj.y, obj.width, obj.height)
end
if gameMap.layers["HoriDoors"] then
for i, obj in pairs(gameMap.layers["HoriDoors"].objects) do
createHDoor(obj.x, obj.y, obj.width, obj.height)
end
end
if gameMap.layers["VertDoors"] then
for i, obj in pairs(gameMap.layers["VertDoors"].objects) do
createVDoor(obj.x, obj.y, obj.width, obj.height)
end
end
for i, obj in pairs(gameMap.layers["Start"].objects) do
player:setPosition(obj.x + 40, obj.y + 40)
end
if gameMap.layers["Keys"] then
for i, obj in pairs(gameMap.layers["Keys"].objects) do
createKeys(obj.x, obj.y, obj.width, obj.height)
end
end
setPoopTimer()
end