Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PolyLine library
#3
(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

###########################################
Reply


Messages In This Thread
PolyLine library - by Marcus - 02-29-2024, 04:25 PM
RE: PolyLine library - by aliensoldier - 02-29-2024, 04:54 PM
RE: PolyLine library - by kevin - 03-01-2024, 11:32 AM
RE: PolyLine library - by Marcus - 03-01-2024, 02:40 PM
RE: PolyLine library - by kevin - 03-01-2024, 03:02 PM
RE: PolyLine library - by Marcus - 03-01-2024, 06:32 PM
RE: PolyLine library - by kevin - 03-01-2024, 08:05 PM
RE: PolyLine library - by Marcus - 03-02-2024, 01:23 PM
RE: PolyLine library - by johnno56 - 03-01-2024, 09:36 PM
RE: PolyLine library - by Marcus - 03-01-2024, 10:03 PM
RE: PolyLine library - by kevin - 03-02-2024, 04:48 PM
RE: PolyLine library - by Marcus - 03-02-2024, 06:20 PM
RE: PolyLine library - by 1micha.elok - 03-05-2024, 06:53 AM
RE: PolyLine library - by kevin - 03-05-2024, 08:42 AM
RE: PolyLine library - by 1micha.elok - 03-05-2024, 11:15 AM
RE: PolyLine library - by Marcus - 03-05-2024, 08:52 PM
RE: PolyLine library - by Marcus - 03-08-2024, 08:21 PM
RE: PolyLine library - by 1micha.elok - 03-09-2024, 02:53 AM
RE: PolyLine library - by Marcus - 03-09-2024, 07:43 AM
RE: PolyLine library - by Marcus - 03-10-2024, 10:10 AM
RE: PolyLine library - by 1micha.elok - 03-11-2024, 12:52 AM

Forum Jump:


Users browsing this thread: 5 Guest(s)