GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2021-04-26 16:13:56

maras
Member
Registered: 2021-04-25
Posts: 28
Website

draw_line_pattern

Made this for fun but then found out that some people are actually looking for it

Expand/// draw_line_pattern(x1, y1, x2, y2, pattern, anim_spd, width)
// 
// draws a line with a given pattern
// 
// pattern usage: 
// ["20-","10x"]             -> 20px line, 10px space
// ["5-","10x","5-","30x"]   -> 5px line, 10px space, 5px line, 30px space
// 
// animation:
// positive number moves the line one way
// negative number the other way
// 
// 	x1		x from, real
// 	y1		y from, real
// 	x2		x to, real
// 	y2		y to, real
// 	pattern	        line pattern, array
// 	anim_spd	line shifting speed, real 
// 	width	        line width in pixels, real
// 	
// made for GMS2 but can be easily modified to make it work in older versions
//
/// GMLscripts.com/license

function draw_line_pattern(x1, y1, x2, y2, pattern, anim_spd, width) {
	
	// clamp the width
	width = max(1, width);

	// pattern is not an array? draw a regular line then
	if !is_array(pattern)
		{
		draw_line_width(x1, y1, x2, y2, width);
		return 0;
		}
	
	var dir = point_direction(x1, y1, x2, y2);
	var dis = point_distance(x1, y1, x2, y2);
	
	// prepare the pattern stuff
	var patt_num = array_length(pattern);
	var patt_arr = [];
	var patt_empty = ord("x");
	var patt_shift = 0;
	
	if patt_num != 0
		{
		// loop through the pattern
		for(var p = 0; p < patt_num; p++)
			{
			// decide if space or line
			var pstr = pattern[p];
			var o = string_ord_at(pstr, string_length(pstr)) != patt_empty;
			var n = real(string_delete(pstr, string_length(pstr), 1));
			
			// add the result into a final array
			repeat(n) patt_arr[array_length(patt_arr)] = o;
			}

		// animation shift value
		patt_shift = floor(get_timer() * 0.00001 * abs(anim_spd/width)) % array_length(patt_arr);
		}
	else // the array is empty? draw a regular line
		{
		draw_line_width(x1, y1, x2, y2, width);
		return 0;
		}
	
	// finally draw the pattern from the final array
	for(var i = 0; i < dis/width; i++)
		{
		// if the current patter is not a space
		if patt_arr[patt_shift]
			{
			// get the coords
			var px = x1+dcos(dir) * i * width;
			var py = y1-dsin(dir) * i * width;

			// and draw it
			if width == 1 draw_point(px, py);
			else draw_rectangle(px-width*0.5, py-width*0.5, px+width*0.5, py+width*0.5, false);
			}
		
		// shift the array 
		if sign(anim_spd) != 0 patt_shift -= sign(anim_spd);
		else patt_shift++;
		
		if patt_shift > array_length(patt_arr)-1 patt_shift = 0;
		else if patt_shift < 0 patt_shift = array_length(patt_arr)-1;
		}
        
    return 0;
}

Example:
dlp.gif

Last edited by maras (2021-04-27 03:23:17)


I'm on the official GM discord > maras_cz

Offline

#2 2021-05-25 14:37:19

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: draw_line_pattern

PKGPP1A.gif

Very interesting idea for a script. All the string manipulation makes it a lot slower than it could be. I might make some changes to improve that.

It also looks like you've found a bug in the forum's image tag parser. I'll have to look into that.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB