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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
--- Used to manage physical world and everything inside it: clouds, platforms, nauts, background etc.
-- TODO: Possibly move common parts of `World` and `Menu` to abstract class `Scene`.
World = require "not.Scene":extends()
require "not.Platform"
require "not.Player"
require "not.Effect"
require "not.Decoration"
require "not.Ray"
require "not.Cloud"
require "not.CloudGenerator"
require "not.Layer"
--- ZA WARUDO!
-- TODO: Missing documentation on most of World's methods.
function World:new (map, nauts)
love.physics.setMeter(64)
self.world = love.physics.newWorld(0, 9.81*64, true)
self.world:setCallbacks(self:getContactCallbacks())
self.lastNaut = false
self.entities = {}
self.map = map
self:initLayers()
self:buildMap()
self:spawnNauts(nauts)
self.camera = Camera(self.map.center.x, self.map.center.y, self)
musicPlayer:play(self.map.theme)
end
-- The end of the world
function World:delete ()
for _,entity in pairs(self.entities) do
entity:delete()
end
for layer in self.layers() do
layer:delete()
end
self.world:destroy()
collectgarbage()
end
--- Custom iterator for layers table.
-- Iterates over elements in reversed order. Doesn't pay attention to any changes in table.
local
function layersIterator (layers)
local i = layers.n + 1
return function ()
i = i - 1
return layers[i]
end
end
--- Layers in World may exists as two references. Every reference is stored inside `instance.layers`.
-- First reference is indexed with number, it exists for every layer.
-- Second reference is indexed with string, it exists only for selected layers.
-- Mentioned special layers are initialized in this method.
-- Additionally layer count is stored inside `instance.layers.n`.
-- Layers are drawn in reverse order, meaning that `instance.layers[1]` will be on the top.
-- Calling `instance.layers` will return iterator.
function World:initLayers ()
local width, height = love.graphics.getDimensions()
self.layers = setmetatable({}, {__call = layersIterator})
self.layers.n = 0
self.layers.rays = self:addLayer(320, 180, 0, 1)
self.layers.tags = self:addLayer(width, height)
self.layers.platforms = self:addLayer(width, height)
self.layers.effects = self:addLayer(width, height)
self.layers.heroes = self:addLayer(width, height)
self.layers.decorations = self:addLayer(width, height)
self.layers.clouds = self:addLayer(width, height)
end
--- Builds map using one of tables frin config files located in `config/maps/` directory.
-- TODO: Clean World@buildMap. Possibly explode into more methods.
function World:buildMap ()
for _,op in pairs(self.map.create) do
if op.platform then
local path = string.format("config/platforms/%s.lua", op.platform)
local config = love.filesystem.load(path)()
self:createPlatform(op.x, op.y, config.shape, config.sprite, config.animations)
end
if op.decoration then
self:createDecoration(op.x, op.y, op.decoration)
end
if op.background then
local width, height = love.graphics.getDimensions()
local image = love.graphics.newImage(op.background)
local x = image:getWidth() / -2
local y = image:getHeight() / -2
local bg = self:createDecoration(x, y, op.background)
if op.animations then
bg:setAnimationsList(op.animations)
end
bg.layer = self:addLayer(width, height, op.ratio)
end
if op.clouds then
local width, height = love.graphics.getDimensions()
local animations = op.animations
if type(animations) == "string" then
animations = require("config.animations." .. animations)
end
local cg = CloudGenerator(op.clouds, animations, op.count, self)
if op.ratio then
cg.layer = self:addLayer(width, height, op.ratio)
end
self:insertEntity(cg)
cg:run(op.count, true)
end
end
end
-- Spawn all the nauts for the round
function World:spawnNauts (nauts)
for _,naut in pairs(nauts) do
local x,y = self:getSpawnPosition()
local spawn = self:createNaut(x, y, naut[1])
spawn:assignControllerSet(naut[2])
end
end
-- Get respawn location
function World:getSpawnPosition ()
local n = love.math.random(1, #self.map.respawns)
return self.map.respawns[n].x, self.map.respawns[n].y
end
-- TODO: Possibly automate calculation of new layer's dimensions based on scale.
function World:addLayer (width, height, ratio, scale)
local layer = Layer(width, height)
local n = self.layers.n + 1
if ratio then
layer.ratio = ratio
end
if scale then
layer.scale = scale
end
self.layers[n] = layer
self.layers.n = n
return layer
end
-- TODO: Standardize `create*` methods with corresponding constructors. Pay attention to both params' order and names.
function World:createPlatform (x, y, polygon, sprite, animations)
local p = Platform(animations, polygon, x, y, self, sprite)
table.insert(self.entities, p)
p.layer = self.layers.platforms
return p
end
function World:createNaut (x, y, name)
local h = Player(name, x, y, self)
table.insert(self.entities, h)
h.layer = self.layers.heroes
return h
end
-- TODO: Sprites' in general don't take actual Image in constructor. That is not only case of Decoration.
function World:createDecoration (x, y, sprite)
local d = Decoration(x, y, self, sprite)
table.insert(self.entities, d)
d.layer = self.layers.decorations
return d
end
function World:createEffect (name, x, y)
local e = Effect(name, x, y, self)
table.insert(self.entities, e)
e.layer = self.layers.effects
return e
end
function World:createRay (naut)
local r = Ray(naut, self)
table.insert(self.entities, r)
r.layer = self.layers.rays
return r
end
function World:insertCloud (cloud)
table.insert(self.entities, cloud)
if not cloud.layer then
cloud.layer = self.layers.clouds
end
return cloud
end
--- Verbose wrapper for inserting entities into entities table.
-- @param entity entity to insert
function World:insertEntity (entity)
if entity then
table.insert(self.entities, entity)
return entity
end
end
--- Searches entities for those which return true with filtering function.
-- @param filter function with entity as parameter
-- @return table containing results of search
function World:getEntities (filter)
local result = {}
for _,entity in pairs(self.entities) do
if filter(entity) then
table.insert(result, entity)
end
end
return result
end
--- Counts entities returning true with filtering function.
-- @param filter function with entity as parameter
-- @return entity count
function World:countEntities (filter)
local count = 0
for _,entity in pairs(self.entities) do
if filter(entity) then
count = count + 1
end
end
return count
end
function World:getCloudsCount ()
return self:countEntities(function (entity)
return entity:is(Cloud)
end)
end
function World:getCloudsCountFrom (generator)
return self:countEntities(function (entity)
return entity:is(Cloud) and entity.generator == generator
end)
end
function World:getNautsAll ()
return self:getEntities(function (entity)
return entity:is(require("not.Hero")) and not entity.body:isDestroyed()
end)
end
function World:getNautsPlayable ()
return self:getEntities(function (entity)
return entity:is(require("not.Hero")) and entity.lives > -1
end)
end
function World:getNautsAlive ()
return self:getEntities(function (entity)
return entity:is(require("not.Hero")) and entity.isAlive
end)
end
-- get Map name
function World:getMapName ()
return self.map.name
end
-- Event: when player is killed
function World:onNautKilled (naut)
self.camera:startShake()
self:createRay(naut)
local nauts = self:getNautsPlayable()
if self.lastNaut then
sceneManager:removeTopScene()
sceneManager:changeScene(Menu())
elseif #nauts < 2 then
self.lastNaut = true
naut:playSound(5, true)
sceneManager:addScene(Menu("win"))
end
end
-- LÖVE2D callbacks
-- Update ZU WARUDO
function World:update (dt)
self.world:update(dt)
self.camera:update(dt)
for key,entity in pairs(self.entities) do
if entity:update(dt) then
table.remove(self.entities, key):delete()
end
end
-- TODO: Possibly rename Camera@sum because this code part in World doesn't make sense without reading further.
self.camera:sum(self.map.center.x, self.map.center.y)
for _,hero in pairs(self:getNautsAll()) do
self.camera:sum(hero:getPosition())
end
-- Some additional debug info.
local stats = love.graphics.getStats()
dbg_msg = string.format("%sMap: %s\nClouds: %d\nLoaded: %d\nMB: %.2f", dbg_msg, self.map.filename, self:getCloudsCount(), stats.images, stats.texturememory / 1024 / 1024)
end
function World:draw ()
for _,entity in pairs(self.entities) do
if entity.draw and entity.layer then
entity.layer:renderToWith(self.camera, entity.draw, entity, debug)
end
if entity.drawTag then
self.layers.tags:renderToWith(self.camera, entity.drawTag, entity, debug)
end
end
for layer in self.layers() do
layer:draw()
layer:clear()
end
-- TODO: Debug information could possibly get its own layer so it could follow flow of draw method.
if debug then
local center = self.map.center
local ax, ay, bx, by = self.camera:getBoundaries()
love.graphics.setLineWidth(1 / getScale())
love.graphics.setLineStyle("rough")
self.camera:push()
self.camera:scale()
self.camera:translate()
love.graphics.setColor(130,130,130)
love.graphics.line(ax,center.y,bx,center.y)
love.graphics.line(center.x,ay,center.x,by)
love.graphics.setColor(200,200,200)
love.graphics.line(ax,0,bx,0)
love.graphics.line(0,ay,0,by)
self.camera:pop()
end
-- TODO: Draw method beyond this point is a very, very dark place (portraits drawing to review).
local scale = getScale()
for _,naut in pairs(self:getNautsAll()) do
-- I have no idea where to place them T_T
-- let's do: bottom-left, bottom-right, top-left, top-right
local w, h = love.graphics.getWidth()/scale, love.graphics.getHeight()/scale
local y, e = 1, 1
if _ < 3 then y, e = h-33, 0 end
naut:drawHUD(1+(_%2)*(w-34), y, scale, e)
end
end
--- Wraps World's beginContact and endContact to functions usable as callbacks for Box2D's world.
-- Only difference in new functions is absence of self as first argument.
-- @return wrapper for beginContact
-- @return wrapper for endContact
function World:getContactCallbacks ()
local b = function (a, b, coll)
self:beginContact(a, b, coll)
end
local e = function (a, b, coll)
self:endContact(a, b, coll)
end
return b, e
end
-- TODO: Review current state of Box2D callbacks (again).
-- TODO: Stop using magical numbers in Box2D callbacks.
-- [1] -> Platform
-- [2] -> Hero
-- [3] -> Punch sensor
function World:beginContact (a, b, coll)
if a:getCategory() == 1 then
local x,y = coll:getNormal()
if y < -0.6 then
b:getUserData():land()
end
local vx, vy = b:getUserData().body:getLinearVelocity()
if math.abs(x) == 1 or (y < -0.6 and x == 0) then
b:getUserData():playSound(3)
end
end
if a:getCategory() == 3 then
if b:getCategory() == 2 then
b:getUserData():damage(a:getUserData()[2])
end
if b:getCategory() == 3 then
a:getBody():getUserData():damage(b:getUserData()[2])
b:getBody():getUserData():damage(a:getUserData()[2])
local x1,y1 = b:getBody():getUserData():getPosition()
local x2,y2 = a:getBody():getUserData():getPosition()
local x = (x2 - x1) / 2 + x1 - 12
local y = (y2 - y1) / 2 + y1 - 15
self:createEffect("clash", x, y)
end
end
if b:getCategory() == 3 then
if a:getCategory() == 2 then
a:getUserData():damage(b:getUserData()[2])
end
end
end
function World:endContact (a, b, coll)
if a:getCategory() == 1 then
b:getUserData().inAir = true
end
end
-- Controller callbacks
function World:controlpressed (set, action, key)
if key == "f6" and debug then
local filename = self.map.filename
local map = love.filesystem.load(filename)()
map.filename = filename
local nauts = {}
for _,naut in pairs(self:getNautsAll()) do
table.insert(nauts, {naut.name, naut:getControllerSet()})
end
local new = World(map, nauts)
sceneManager:changeScene(new)
end
if key == "escape" then
sceneManager:addScene(Menu("pause"))
self:setInputDisabled(true)
self:setSleeping(true)
end
for k,naut in pairs(self:getNautsAll()) do
naut:controlpressed(set, action, key)
end
end
function World:controlreleased (set, action, key)
for k,naut in pairs(self:getNautsAll()) do
naut:controlreleased(set, action, key)
end
end
|