03-01-2024, 11:32 AM
(This post was last modified: 03-01-2024, 02:59 PM by kevin.
Edit Reason: change to the "save_file function.
)
(02-29-2024, 04:25 PM)Marcus Wrote: Here's a small library that could be used for enemy movement in space shootemups (that's the only use I can think of), example included.
Excellent - I too will be using this library.
I've blatently pinched your example to use as the basis for a quick editor so that I can amend the array in real time, to change the path that is generated.
Here's the code for the editor in case it may be of use to anyone. Instructions are on screen, and the saved text file includes the x and y points at the top, which could be read into any N7 file, and also a full array, which could be copied into your N7 file manually.
Code:
' polyline_example.n7
' -------------------
' The polyline library can be used for ... enemy paths in space shootemups, I guess :)
#win32
include "polyline.n7"
set window "Polyline", 1024, 768
set redraw off
visible points = []
'base array to start - can be modified if required
points = [[100,100],[100,400],[400,400],[400,100],[100,100]]
' Create a polyline from the points.
path = PolyLine(points)
distance = 0
curved = true
move_now = false
move_point = 999
while not keydown(KEY_ESCAPE, true)
if keydown(KEY_S) then save_file()
for i = 0 to sizeof(points) - 1
if mousebutton(0)
if mousex() > points[i][0] - 8 and mousex() < points[i][0] + 8
if mousey() > points[i][1] - 8 and mousey() < points[i][1] + 8
move_now = true
move_point = i
endif
endif
elseif mousebutton(1) and i < sizeof(points) - 1
if mousex() > (points[i + 1][0] - points[i ][0]) / 2 + points[i ][0]- 8 and mousex() < (points[i + 1][0] - points[i ][0]) / 2 + points[i ][0]+ 8
if mousey() > (points[i + 1][1] - points[i][1]) / 2 + points[i ][1]- 8 and mousey() < (points[i + 1][1] - points[i][1]) / 2 + points[i ][1]+ 8
points = insert_into_array(points,i + 1,[mousex(),mousey()])
endif
endif
endif
next
if move_now = true
points[move_point][0] = mousex()
points[move_point][1] = mousey()
path = PolyLine(points)
if not mousebutton(0) then move_now = false
endif
' Toggle curved mode.
if keydown(KEY_SPACE, true) curved = not curved
' Move forward 4 pixels, wrap at length of path.
distance = (distance + 4)%path.GetLength()
' 'GetPoint' returns the position along the path at specified travel distance. If the second
' parameter is true, the point is computed for a composite bezier curve that passes through all
' the original points. If false, a position along the polyline is returned. If distance is
' outside the length of the polyline, unset is returned.
' Note that the function always returns the same array (but with different values, of course).
pos = path.GetPoint(distance, curved)
set color 0, 0, 0 , 24
cls
' Draw lines.
set color 255,255,255 ' 64, 64, 64
for i = 0 to sizeof(points) - 2
draw line points[i][0], points[i][1], points[i + 1][0], points[i + 1][1]
draw rect points[i][0] - 8, points[i][1] - 8,16,16
'set color 255,0,0
if i < sizeof(points) - 1
draw rect (points[i + 1][0] - points[i ][0]) / 2 + points[i ][0]- 8,
(points[i + 1][1] - points[i][1]) / 2 + points[i ][1]- 8,16,16,1
endif
set color 255,255,255
next
draw rect points[sizeof(points) - 1][0] - 8, points[sizeof(points) - 1][1] - 8,16,16
' Draw points.
set color 96, 96, 96
for i = 0 to sizeof(points) - 1 draw ellipse points[i][0], points[i][1], 2, 2, true
' Draw circle that moves along path.
set color 255, 255, 255
draw ellipse pos[0], pos[1], 5, 5, true
set caret width(primary)/2, height(primary) - 250
' Instructions.
set caret width(primary)/2, height(primary) - fheight()*12
center "Press and hold the left mouse button in a transparent rectangle to move the points." ;wln
center "Press and release the right mouse button in a solid rectangle to add a new point." ;wln
center "Press the S key to save to a text file";wln
center "Press space to toggle curve mode"
redraw
fwait 60
wend
###################################################################################################
function save_file()
newfile = savefiledialog("txt")
if instr(newfile, ".txt") < 0
newfile = newfile + ".txt"
endif
f = createfile(newfile)
if file(f)
write file f, sizeof(points)
wln file f
for i = 0 to sizeof(points) - 1
write file f , int(points[i][0]) + " "
write file f , int(points[i][1]) + " "
wln file f
next
wln file f ' now write out the full array, ready to be copy and pasted into a N7 file
write file f, "["
for i = 0 to sizeof(points) - 1
write file f ,"["
write file f , int(points[i][0]) + ","
if i < sizeof(points) - 1
write file f , int(points[i][1]) + "],"
else
write file f , int(points[i][1]) + "]"
endif
next
write file f, "]"
free file f
endif
endfunc
#########################################################################################
function insert_into_array(array,element,new_element)
temp = copy(array)
temp[sizeof(temp)] = new_element
temp2 = []
for i = 0 to element - 1
temp2[sizeof(temp2)] = temp[i]
next
temp2[element] = temp[sizeof(temp) - 1]
for i = element to sizeof(temp) - 1
temp2[sizeof(temp2)] = temp[i]
next
free key temp2,sizeof(temp2)-1
array = copy(temp2)
return array
endfunc
###########################################