Skip to content

Commit f30fa6a

Browse files
authored
Merge pull request #2 from flamendless/master
Changed how lights are stored. Dynamic pool of light is more efficient.
2 parents 7570a16 + 97556e8 commit f30fa6a

1 file changed

Lines changed: 12 additions & 49 deletions

File tree

luven/luven.lua

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,6 @@ luven.lightShapes = {}
267267
-- /// Luven utils local functions
268268
-- ///////////////////////////////////////////////
269269

270-
local function getLastEnabledLightIndex()
271-
for i = NUM_LIGHTS, 1, -1 do
272-
if (currentLights[i].enabled) then
273-
return i
274-
end
275-
end
276-
277-
return 0
278-
end
279-
280270
local function drawLights()
281271
lg.setCanvas(lightMap)
282272
lg.setBlendMode("add")
@@ -286,9 +276,9 @@ local function drawLights()
286276
local oldR, oldG, oldB, oldA = lg.getColor()
287277

288278
-- lastActiveLightIndex updated in luven.update()
289-
for i = 1, lastActiveLightIndex do
290-
if (currentLights[i].enabled) then
291-
local light = currentLights[i]
279+
for i = 1, #currentLights do
280+
local light = currentLights[i]
281+
if (light.enabled) then
292282
lgSetColor(light.color)
293283
lgDraw(light.shape.sprite, light.x, light.y, light.angle, light.scaleX * light.power, light.scaleY * light.power, light.shape.originX, light.shape.originY)
294284
end
@@ -300,17 +290,6 @@ local function drawLights()
300290
lg.setCanvas()
301291
end
302292

303-
local function getNextId()
304-
for i = 1, NUM_LIGHTS do
305-
local light = currentLights[i]
306-
if (light.enabled == false) then
307-
return i
308-
end
309-
end
310-
311-
return 1 -- first index
312-
end
313-
314293
local function randomFloat(min, max)
315294
return min + love.math.random() * (max - min)
316295
end
@@ -354,10 +333,6 @@ function luven.init(screenWidth, screenHeight, useCamera)
354333
luven.registerLightShape("cone", luvenPath .. "lights/cone.png", 0)
355334

356335
lightMap = lg.newCanvas(screenWidth, screenHeight)
357-
358-
for i = 1, NUM_LIGHTS do
359-
currentLights[i] = { enabled = false }
360-
end
361336
end
362337

363338
-- param : color = { r, g, b, a (1) } (Values between 0 - 1)
@@ -390,9 +365,7 @@ function luven.update(dt)
390365
cameraUpdate(dt)
391366
end
392367

393-
lastActiveLightIndex = getLastEnabledLightIndex()
394-
395-
for i = 1, lastActiveLightIndex do
368+
for i = 1, #currentLights do
396369
local light = currentLights[i]
397370
if (light.enabled) then
398371
if (light.type == lightTypes.flickering) then
@@ -432,7 +405,7 @@ function luven.drawEnd()
432405
end
433406

434407
function luven.removeAllLights()
435-
for _, v in pairs(currentLights) do
408+
for _, v in ipairs(currentLights) do
436409
if (v.enabled) then
437410
luven.removeLight(v.id)
438411
end
@@ -449,15 +422,7 @@ function luven.dispose()
449422
end
450423

451424
function luven.getLightCount()
452-
local count = 0
453-
454-
for i = 1, lastActiveLightIndex do
455-
if (currentLights[i].enabled) then
456-
count = count + 1
457-
end
458-
end
459-
460-
return count
425+
return #currentLights
461426
end
462427

463428
-- ///////////////////////////////////////////////
@@ -508,11 +473,10 @@ function luven.addNormalLight(x, y, color, power, lightShape, angle, sx, sy)
508473
assertPositiveNumber(functionName, "sx", sx)
509474
assertPositiveNumber(functionName, "sy", sy)
510475

511-
local id = getNextId()
476+
local id = #currentLights + 1
477+
currentLights[id] = {}
512478
local light = currentLights[id]
513479

514-
clearTable(light)
515-
516480
light.id = id
517481
light.x = x
518482
light.y = y
@@ -556,11 +520,10 @@ function luven.addFlickeringLight(x, y, colorRange, powerRange, speedRange, ligh
556520
assertPositiveNumber(functionName, "sx", sx)
557521
assertPositiveNumber(functionName, "sy", sy)
558522

559-
local id = getNextId()
523+
local id = #currentLights + 1
524+
currentLights[id] = {}
560525
local light = currentLights[id]
561526

562-
clearTable(light)
563-
564527
light.id = id
565528
light.x = x
566529
light.y = y
@@ -602,9 +565,9 @@ function luven.addFlashingLight(x, y, color, maxPower, speed, lightShape, angle,
602565
assertPositiveNumber(functionName, "sx", sx)
603566
assertPositiveNumber(functionName, "sy", sy)
604567

605-
local id = getNextId()
568+
local id = #currentLights + 1
569+
currentLights[id] = {}
606570
local light = currentLights[id]
607-
clearTable(light)
608571

609572
light.id = id
610573
light.x = x

0 commit comments

Comments
 (0)