Challenge:
Version:
Level Editor
Winner?:
No
Code Snippet:
REM ======================= REM Top-view Race Challenge REM March 13, 2007 REM Author: phaelax REM ======================= rem 2d vector type type vector x as float y as float w as float endtype rem 2 vectors as line segment's endpoints, 2 vectors for control points rem a,c are the endpoints rem b,d are the respective control points type TrackSegment a as vector b as vector c as vector d as vector endtype rem vector points for hermite splines null = make vector2(1) null = make vector2(2) null = make vector2(3) null = make vector2(4) rem result vector null = make vector2(5) null = make vector2(6) rem array of hermite splines dim track(1) as TrackSegment rem add starting segment setPointA(1, 400,300,(rnd(200)-100)+400,(rnd(200)-100)+300) setPointC(1, 200,200,(rnd(200)-100)+200,(rnd(200)-100)+200) DO cls gosub _handle_points drawTrack(20) LOOP REM =============================================== REM REM =============================================== _handle_points: if mouseclick() = 2 and flag = 0 flag = 2 addPoint(mousex(), mousey()) endif if mouseclick() = 0 flag = 0 controlPoint = -1 endpoint = -1 endif _mc = mouseclick() ptRadius = 5 for t = 1 to array count(track()) rem draw endpoint ink rgb(0,255,0),0 circle track(t).c.x,track(t).c.y,ptRadius if t = 1 then circle track(t).a.x,track(t).a.y,ptRadius rem draw control point ink rgb(255,0,0),0 line track(t).b.x,track(t).b.y,track(t).a.x,track(t).a.y box track(t).b.x-3,track(t).b.y-3,track(t).b.x+3,track(t).b.y+3 ink rgb(0,0,255),0 circle track(t).d.x, track(t).d.y, 6 rem check if mouse has grabbed a control point if _mc = 1 and controlPoint = -1 and endpoint = -1 if mouseWithin(track(t).b.x-3,track(t).b.y-3,track(t).b.x+3,track(t).b.y+3) controlPoint = t endif endif rem if _mc = 1 and endpoint = -1 and controlPoint = -1 if mouseWithin(track(t).a.x-ptRadius,track(t).a.y-ptRadius,track(t).a.x+ptRadius,track(t).a.y+ptRadius) endPoint = t endif endif next t if controlPoint > -1 track(controlPoint).b.x = mousex() track(controlPoint).b.y = mousey() if controlPoint > 1 track(controlPoint-1).d = track(controlPoint).b endif endif if endPoint > -1 track(endPoint).a.x = mousex() track(endPoint).a.y = mousey() if endPoint > 1 track(endPoint-1).c = track(endPoint).a endif endif RETURN REM =============================================== REM splineStep: higher value equals smoother curves REM =============================================== function drawTrack(splineSteps) ink rgb(255,255,255),0 for t = 1 to array count(track()) rem create hermite spline for s = 1 to splineSteps rem get the 4 vectors set vector2 1,track(t).a.x,track(t).a.y set vector2 2,track(t).b.x,track(t).b.y set vector2 3,track(t).c.x,track(t).c.y set vector2 4,track(t).d.x,track(t).d.y time# = s hermite vector2 5,1,2,3,4,(time#-1.0)/splineSteps hermite vector2 6,1,2,3,4, time#/splineSteps rem draw a segment/step of this track's segment line x vector2(5), y vector2(5),x vector2(6), y vector2(6) next s next t endfunction REM =========================================== REM Convenience method for setting array values REM =========================================== function setPointA(index, x#,y#, cx#, cy#) track(index).a.x = x# track(index).a.y = y# track(index).b.x = cx# track(index).b.y = cy# endfunction REM =========================================== REM Convenience method for setting array values REM =========================================== function setPointC(index, x#,y#, cx#, cy#) track(index).c.x = x# track(index).c.y = y# track(index).d.x = cx# track(index).d.y = cy# endfunction REM =========================================== REM Adds a new segment to the end of the track REM =========================================== function addPoint(x as integer, y as integer) array insert at bottom track() i = array count(track()) print i track(i).a = track(i-1).c track(i).b = track(i-1).d track(i).c.x = x track(i).c.y = y track(i).d.x = x+20 track(i).d.y = y+20 endfunction REM =========================================== REM ummm, duh REM =========================================== function mouseWithin(x1,y1,x2,y2) if mousex()>x1 and mousex()<x2 and mousey()>y1 and mousey()<y2 then exitfunction 1 endfunction 0