A little too fast (no delay implemented) but here's what I scrounged up (still haven't tested darkfrei's one yet, just wanted to see if this idea worked first).
Code: Select all
local function choose(t)
return t[math.random(#t)]
end
local function point_distance(x,y,xx,yy)
return math.sqrt(((x-xx)^2)+(y-yy)^2)
end
local function point_direction(x,y,xx,yy)
return -math.atan2(yy-y, xx-x)
end
random_range = function(x,y)
return math.random((y and x or -x), y or x)
end
array_length = function(t)
return #t
end
function lengthdir_x(spd, ang)
return spd*math.cos(ang)
end
function lengthdir_y(spd, ang)
return spd*-math.sin(ang)
end
function draw_set_alpha(a)
local r,g,b = love.graphics.getColor()
love.graphics.setColor(r,g,b,a)
end
function set_color(rr,gg,bb,aa)
local r,g,b,a = love.graphics.getColor()
if not rr then
return r, g, b, a
end
love.graphics.setColor(rr,gg,bb,aa)
return r,g,b,a
end
function draw_line_width_colour(x1, y1, x2, y2,size,color1,color2)
local r,g,b,a = set_color(color1)
local p = love.graphics.getLineWidth()
love.graphics.setLineWidth(sizge or p)
love.graphics.line(x1,y1,x2,y2)
set_color(r,g,b,a)
love.graphics.setLineWidth(p)
end
function draw_circle_colour(x,y,r,c1,c2)
local r,g,b,a = set_color(c1)
love.graphics.circle("fill",x,y,r)
set_color(r,g,b,a)
end
local c_white = {1,1,1}
function draw_lightning(x1, y1, x2, y2, branches, size, color)
--draw_lightning(x, y, x2, y2, branches, size, colour)
--
--draws a lightning bolt from the given starting location to the given end location
--
--x = x of the bolt's start
--y = y of the bolt's start
--x2 = x of the bolt's end
--y2 = y of the bolt's end
--branches = true or false, if the lightning bolt branches into multiple smaller ones
--size = pixel width of the lightning
--colour = colour of the glow
--
--amusudan 23/5/2016
--
--feel free to use this in your project!
--
local dir = point_direction(x1,y1,x2,y2)
local length = point_distance(x1,y1,x2,y2) --error(length)
local colour = color;
local _size = size;
--make different segments
local point = {};
point[1] = 0;
local i2 = 2;
for i = 1, length do
if (math.random() < .06) then
point[i2] = i;
i2 = i2+1;
end
end
point[i2] = length;-- error(point[#point-0])
local points = array_length(point);
--draw segments
local i2 = 2
local difx = 0;
local difx2 = 0;
local dify = 0;
local ii =0
local dify2 = 0; --error(points)
local dis = 7
for i2 = 2, points do
local i2 = i2+ii
difx = random_range(dis)
dify = random_range(dis)
local xx = x1 + lengthdir_x(point[i2 - 1],dir);
local yy = y1 + lengthdir_y(point[i2 - 1],dir);
local xx2 = x1 + lengthdir_x(point[i2],dir);
local yy2 = y1 + lengthdir_y(point[i2],dir); --error(xx2..","..x1..",y2:"..yy2..","..y1..","..dir..","..point[i2])
--create a branch
if (math.random() < .15 and branches) then
local bdir = dir + choose({random_range(-45,-25),random_range(45,25)});
local blength = random_range(5,30);
draw_lightning(xx + difx2, yy + dify2, xx + difx2 + lengthdir_x(blength,bdir), yy + dify2 + lengthdir_y(blength,bdir), false, _size, colour)
end
--draw the glow of the lightning
set_color(1,1,1,.1)
draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 5,colour,colour);
draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 3,c_white,c_white);
draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size + 1,c_white,c_white);
draw_set_alpha(1)
--draw the white center of the lightning
draw_line_width_colour(xx + difx2,yy + dify2,xx2 + difx,yy2 + dify, size,c_white,c_white);
--ii = ii+1;
difx2 = difx;
dify2 = dify;
end
--draw a glowing circle
if (branches) then
draw_set_alpha(.91);
draw_circle_colour(x1,y1,size + 2.5,colour,colour,false);
draw_circle_colour(x1,y1,size + 1.5,colour,colour,false);
draw_circle_colour(x1,y1,size + .5,colour,colour,false);
draw_set_alpha(1);
draw_circle_colour(x1,y1,size,c_white,c_white,false);
end
end
love.draw = function()
love.graphics.scale(.5)
--love.graphics.translate(0,500)
local x = 100
local y = 100
local xx = 150
local yy = 300
--draw_line_width_colour(x,y,xx,yy,1,{0,1,0})
draw_lightning(x,y,xx,yy,true,10)
end