@thebugel Here is the code with the screen scrolling code removed. I also added code so that all the points don’t have to be stations. What controls a point being a station is the first entry in each line of the dest table. I made that entry a vec3. It contains the x,y values plus a 1 or 0. A 1 means it’s a station, a 0 is just a connecting point. I also changed code in setup() as to where the train starts. Just change the “at” value to a dest table offset of the point you want the train to start. I have it starting from the 1st point in the dest table. I also added code in drawStations() to show the x,y value next to the connecting point or station. This will help when setting up the points and stations. Once you get the points set up, that line of code can be commented out. I didn’t do anything with the time, I’m not sure what you have in mind for that. I also added several more points and stations to the dest table so I could test it more. Remove what you don’t need.
displayMode(FULLSCREEN)
function setup()
rectMode(CENTER)
setDest()
at=1 -- starting train position, dest table offset
x=dest[at][1].x
y=dest[at][1].y
setAtDest()
totalTime=0
startTimer=false
arrived=true
speed=2
end
function draw()
background(40, 40, 50)
showTime()
drawTracks()
drawStations()
drawTrain()
checkArrived()
checkAllStations()
end
function touched(t)
if done and t.tapCount==2 then -- restart
restart()
end
if t.state==BEGAN and arrived then
startTimer=true
v1=vec2(t.x,t.y)
-- is touched point a valid destination
for a,b in pairs(dest[at]) do
d=v1:dist(vec2(b.x,b.y))
if d<20 then
-- valid destination
currDest=vec2(b.x,b.y)
-- get dest table offset of destination
for c,d in pairs(dest) do
if d[1].x==b.x and d[1].y==b.y then
at=c -- dest table offset
break
end
end
-- determine new speed and direction of train
v1=vec2(x,y)
d=v1:dist(vec2(b.x,b.y))
dx=(b.x-x)/d*speed
dy=(b.y-y)/d*speed
arrived=false
break
end
end
end
end
function setDest() -- table of the destination points
dest = {
{vec3(370,538,0),vec2(370,314),vec2(370,90)},
{vec3(370,314,0),vec2(370,538),vec2(594,314),vec2(370,90)},
{vec3(594,314,1),vec2(370,314),vec2(594,700),vec2(594,200)},
{vec3(370,90,0),vec2(370,314),vec2(370,538),vec2(100,90)},
{vec3(594,700,1),vec2(200,700),vec2(594,314)},
{vec3(200,700,0),vec2(200,200),vec2(594,700)},
{vec3(200,200,1),vec2(200,700),vec2(594,200)},
{vec2(594,200,0),vec2(594,314),vec2(200,200)},
{vec3(100,90,1),vec2(370,90)}
}
end
function drawTracks()
stroke(255)
strokeWidth(3)
for z=1,#dest do
for s=2,#dest[z] do
line(dest[z][1].x,dest[z][1].y,dest[z][s].x,dest[z][s].y)
end
end
end
function drawTrain()
fill(255)
ellipse(x,y,30)
end
function drawStations()
for a,b in pairs(dest) do
fill(255)
-- display x,y values on the screen
text("("..b[1].x..","..b[1].y..")",b[1].x+40,b[1].y-30)
-- comment above line when text not needed
strokeWidth(0)
if b[1].z==1 then -- a station
fill(255)
if atDest[a]==true then
fill(0,255,0)
end
rect(b[1].x,b[1].y,40,40)
else
ellipse(b[1].x,b[1].y,10)
end
end
end
function checkArrived()
-- check if train arrived at destination
if not arrived then
v1=vec2(currDest.x,currDest.y)
d=v1:dist(vec2(x,y))
if d>3 then -- train hasn't arrived yet
x=x+dx
y=y+dy
else
x=currDest.x -- train arrived, set exact dest x,y position
y=currDest.y
arrived=true
atDest[at]=true
end
end
end
function setAtDest()
atDest={}
for z=1,#dest do
atDest[z]=false
end
atDest[at]=true
end
function checkAllStations()
done=true
for z=1,#atDest do
if dest[z][1].z==1 and atDest[z]==false then
done=false
end
end
end
function showTime()
fill(255)
if not startTimer then
et=ElapsedTime
end
print(done)
if not done then
totalTime=string.format("%0.2f",ElapsedTime-et)
else
text("All stations reached in",WIDTH/2,HEIGHT-50)
text("Double tap screen to restart",WIDTH/2,HEIGHT-160)
end
text(totalTime.." seconds",WIDTH/2,HEIGHT-90)
end