Changes on Lights

- adjusted lights so they are called to do something, instead of it 
being handled on game world 
- lights has only coordinate arguments + a table with all other optional 
ones.
- renamed a lot of lights components to radius instaed of range, as it 
was agreed before elsewhere
This commit is contained in:
lustlion 2022-03-17 00:37:14 +01:00
parent 0486787b98
commit f670f6bc87
3 changed files with 49 additions and 38 deletions

View File

@ -79,19 +79,7 @@ function drawGameworldDarkness()
love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0)
for _, light in pairs(LoadedObjects.Lights) do
if light.range ~= 0 then
local position = {
x = (light.pos.x - Camera.pos.x) / game.scale,
y = (light.pos.y - Camera.pos.y) / game.scale
}
local range = (light.range + light.flicker_value) / game.scale
love.graphics.circle(
"fill",
position.x,
position.y,
range
)
end
light:drawClear()
end
Canvas.Darkness:endDrawing()
Canvas.Darkness:draw()
@ -99,23 +87,7 @@ end
function drawGameworldLights()
for _, light in pairs(LoadedObjects.Lights) do
if light.range ~= 0 then
love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
shader.circle_gradient:send("pos_x",- Camera.pos.x + light.pos.x)
shader.circle_gradient:send("pos_y",- Camera.pos.y + light.pos.y)
shader.circle_gradient:send("range",light.range)
shader.circle_gradient:send("scale",game.scale)
love.graphics.setShader(shader.circle_gradient)
love.graphics.circle(
"fill",
- Camera.pos.x + light.pos.x,
- Camera.pos.y + light.pos.y,
light.range
)
love.graphics.setShader()
end
light:drawShine()
end
end

View File

@ -1,16 +1,17 @@
Light = {}
LoadedObjects.Lights = {}
function Light:new(x,y,range,flicker,color,lum)
function Light:new(x,y,data)
local o = {}
o.pos = {
x = x,
y = y
}
o.range = range
o.lum = lum or 1
o.color = color or {1,1,1}
o.flicker_amount = flicker or 2
o.radius = data.radius
o.shine_radius = data.shine_radius or 0
o.lum = data.lum or 1
o.color = data.color or {1,1,1}
o.flicker_amount = data.flicker or 2
o.flicker_value = 0
o.dim = 0
o.flicker_time = 60/12
@ -45,3 +46,39 @@ function Light:flicker()
self.flicker_value = math.min(math.max(self.flicker_value, -self.flicker_amount), self.flicker_amount)
end
end
function Light:drawClear()
if self.radius ~= 0 then
local position = {
x = (self.pos.x - Camera.pos.x) / game.scale,
y = (self.pos.y - Camera.pos.y) / game.scale
}
local radius = (self.radius + self.flicker_value) / game.scale
love.graphics.circle(
"fill",
position.x,
position.y,
radius
)
end
end
function Light:drawShine()
if self.radius ~= 0 then
love.graphics.setColor(self.color[1],self.color[2],self.color[3],1)
shader.circle_gradient:send("pos_x",- Camera.pos.x + self.pos.x)
shader.circle_gradient:send("pos_y",- Camera.pos.y + self.pos.y)
shader.circle_gradient:send("radius",self.shine_radius)
shader.circle_gradient:send("scale",game.scale)
love.graphics.setShader(shader.circle_gradient)
love.graphics.circle(
"fill",
- Camera.pos.x + self.pos.x,
- Camera.pos.y + self.pos.y,
self.radius
)
love.graphics.setShader()
end
end

View File

@ -2,7 +2,7 @@ shader = {}
shader.circle_gradient = love.graphics.newShader[[
uniform float pos_x;
uniform float pos_y;
uniform float range;
uniform float radius;
uniform float scale;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
@ -12,11 +12,13 @@ shader.circle_gradient = love.graphics.newShader[[
float distance_x = pos_x - screen_coords.x / scale;
float distance_y = pos_y - screen_coords.y / scale;
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) ;
if (distance < range){
float alpha = 1-(5*distance/range);
if (distance < radius){
float alpha = 1-(5*distance/radius);
if (pixel.a > alpha){
pixel.a = alpha;
}
} else {
pixel.a = 0;
}
return pixel * color * color;
}