@thebugel Here’s the updated code. The screen scrolls to keep the moving train in view. I added a table to check if all stations were stopped at. I added a timer to see how long it takes to stop at all the stations. So you can try adding the changes on your own or see how they were done here. The program layout got changed also, I put more of the code in seperate functions.
displayMode(FULLSCREEN)
function setup()
at=5
setDest()
setAtDest()
totalTime=0
startTimer=false
arrived=true
speed=2
x=100
y=600
mx=0
my=0
end
function draw()
background(40, 40, 50)
showTime()
translate(mx,my)
drawTracks()
drawStations()
drawTrain()
checkArrived()
checkAllStations()
end
function touched(t)
if done and t.tapCount==2 then -- restart
restart()
end
if t.state==MOVING and arrived then -- scroll screen
mx=mx+t.deltaX
my=my+t.deltaY
end
if t.state==BEGAN and arrived then
startTimer=true
ms=5 -- set move screen speed at 5 pixels for off screen speed
v1=vec2(t.x-mx,t.y-my)
-- 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 = {
{vec2(300,400),vec2(300,600),vec2(300,800),vec2(1700,400)},
{vec2(300,600),vec2(300,400),vec2(300,800),vec2(500,600),vec2(100,600)},
{vec2(300,800),vec2(300,400),vec2(300,600),vec2(1700,1200)},
{vec2(500,600),vec2(300,600),vec2(100,600)},
{vec2(100,600),vec2(300,600),vec2(500,600)},
{vec2(1700,400),vec2(300,400),vec2(1700,1200),vec2(1000,600)},
{vec2(1700,1200),vec2(1700,400),vec2(300,800),vec2(1000,600)},
{vec2(1000,600),vec2(1700,1200),vec2(1700,400)}
}
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,20)
end
function drawStations()
for a,b in pairs(dest) do
fill(255)
strokeWidth(0)
if atDest[a]==true then
fill(0, 255, 0, 255)
ellipse(b[1].x,b[1].y,40)
else
ellipse(b[1].x,b[1].y,20)
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
-- check if on screen train is at the edges
if x+mx>50 and x+mx<WIDTH-50 and y+my>50 and y+my<HEIGHT-50 then
ms=3 -- set move screen speed to 3 pixels
end
-- move screen to put or keep the train in view
if x+mx<50 then
mx=mx+ms
end
if x+mx>WIDTH-50 then
mx=mx-ms
end
if y+my<50 then
my=my+ms
end
if y+my>HEIGHT-50 then
my=my-ms
end
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 atDest[z]==false then
done=false
end
end
end
function showTime()
fill(255)
if not startTimer then
et=ElapsedTime
end
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