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!
//
var dir = point_direction(x1,y1,x2,y2);
var length = point_distance(x1,y1,x2,y2);
var colour = color;
var _size = size;
//make different segments
var point;
point[0] = 0;
var i2 = 1;
for (var i = 0; i < length; i++) {
if (random(1) < .06) {
point[i2] = i;
i2++;
}
}
point[i2] = length;
var points = array_length(point);
//draw segments
var i2 = 1
var difx = 0;
var difx2 = 0;
var dify = 0;
var dify2 = 0;
for (i2 = 1; i2 < points; i2++) {
difx = random_range(-7,7)
dify = random_range(-7,7)
var xx = x1 + lengthdir_x(point[i2 - 1],dir);
var yy = y1 + lengthdir_y(point[i2 - 1],dir);
var xx2 = x1 + lengthdir_x(point[i2],dir);
var yy2 = y1 + lengthdir_y(point[i2],dir);
//create a branch
if (random(1) < .15 && branches) {
var bdir = dir + choose(random_range(-45,-25),random_range(45,25));
var 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)
}
//draw the glow of the lightning
draw_set_alpha(.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);
i2++;
difx2 = difx;
dify2 = dify;
}
//draw a glowing circle
if (branches) {
draw_set_alpha(.1);
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);
}
}