Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Line rider
--# Splines
Spline = class()
function Spline:init(x1,y1)
self.px = {}
self.py = {}
self.splines={}
self.x1 = x1 or 100
self.y1 = y1 or 100
self.x2 = self.x1+100
self.y2 = self.y1-100
self.x3 = self.x2+100
self.y3 = self.y2
self.x4 = self.x3+100
self.y4 = self.y3+100
self.n=40
end
function Spline:draw()
if PhysicsPaused then
pushStyle()
fill(127, 127, 127, 255)
ellipse(self.x1,self.y1,15)
ellipse(self.x2,self.y2,15)
ellipse(self.x3,self.y3,15)
ellipse(self.x4,self.y4,15)
popStyle()
pushStyle()
stroke(73, 73, 73, 80)
strokeWidth(7)
line(self.x1,self.y1,self.x2,self.y2)
line(self.x2,self.y2,self.x3,self.y3)
line(self.x3,self.y3,self.x4,self.y4)
popStyle()
end
local t=0
for i=1,self.n do
self.px[i] = (1-t)^3 * (self.x1) + 3*(1-t)^2 *t * (self.x2) + 3*(1-t)* t^2 *(self.x3)+ t^3*(self.x4)
self.py[i] = (1-t)^3 * (self.y1) + 3*(1-t)^2 *t * (self.y2) + 3*(1-t)* t^2 *(self.y3)+ t^3*(self.y4)
t=t+1/(self.n-1)
end
for i=1,self.n-1 do
pushMatrix()
pushStyle()
stroke(0, 0, 0, 255)
strokeWidth(5)
line(self.px[i],self.py[i],self.px[i+1],self.py[i+1])
popStyle()
popMatrix()
end
end
function Spline:touched(touch)
if tx>self.x1-40 and tx<self.x1+40 and ty>self.y1-40 and ty<self.y1+40 then
touchPoint=true
self.x1 = tx
self.y1 = ty
end
if tx>self.x2-40 and tx<self.x2+40 and ty>self.y2-40 and ty<self.y2+40 then
touchPoint=true
self.x2 = tx
self.y2 = ty
end
if tx>self.x3-40 and tx<self.x3+40 and ty>self.y3-40 and ty<self.y3+40 then
touchPoint=true
self.x3 = tx
self.y3 = ty
end
if tx>self.x4-40 and tx<self.x4+40 and ty>self.y4-40 and ty<self.y4+40 then
touchPoint=true
self.x4 = tx
self.y4 = ty
end
end
function Spline:MakePhysicsSplines()
for i=1,self.n-1 do
self.spline = physics.body(EDGE,vec2(self.px[i],self.py[i]), vec2(self.px[i+1],self.py[i+1]))
table.insert(self.splines,self.spline)
end
end
--# PhysicsPausedPlay
function PhysicsPausedPlayInit()
physics.pause()
splines={}
PhysicsPaused=true
CameraX,CameraY=0,0
SplineCount=readLocalData("SplineCount")
if SplineCount then
for i=1,SplineCount do
AddSpline()
end
for k,v in pairs(splines) do
v.x1=readLocalData("splinesX1"..k)
v.x2=readLocalData("splinesX2"..k)
v.x3=readLocalData("splinesX3"..k)
v.x4=readLocalData("splinesX4"..k)
v.y1=readLocalData("splinesY1"..k)
v.y2=readLocalData("splinesY2"..k)
v.y3=readLocalData("splinesY3"..k)
v.y4=readLocalData("splinesY4"..k)
end
elseif not SplineCount then
AddSpline()
end
FlagLX=readLocalData("FlagLX")
FlagLY=readLocalData("FlagLY")
FlagX=readLocalData("FlagX")
FlagY=readLocalData("FlagY")
FlagMove=Buttons(WIDTH-120,75,"Move To Flag",FlagMove)
StartMove=Buttons(WIDTH-120,150,"Move To Start",StartMove)
ASButton=Buttons(100,75,"Add Spline",AddSpline)
StartButton=Buttons(100,150,"Start",PhysicsResumePlay)
end
function PhysicsPausedPlayDraw()
background(255, 255, 255, 255)
pushMatrix()
translate(WIDTH/2+CameraX,HEIGHT/2+CameraY)
pushStyle()
fill(0, 0, 0, 255)
ellipse(0,0,40)
for k,v in pairs(splines) do
v:draw()
end
popStyle()
if FlagLX then
pushStyle()
fill(0, 0, 0, 100)
ellipse(FlagX,FlagY,40)
stroke(0, 0, 0, 102)
strokeWidth(4)
line(FlagX,FlagY,FlagX+FlagLX/10,FlagY+FlagLY/10)
popStyle()
end
popMatrix()
ASButton:draw()
StartButton:draw()
FlagMove:draw()
StartMove:draw()
end
function PhysicsPausedPlayTouched(touch)
tx=touch.x-CameraX-WIDTH/2
ty=touch.y-CameraY-HEIGHT/2
for k,v in pairs(splines) do
if not Moving then
v:touched(touch)
end
end
if not touchPoint then
CameraX = CameraX + touch.deltaX
CameraY = CameraY + touch.deltaY
Moving=true
end
if touch.state==ENDED then
touchPoint=false
Moving=false
end
ASButton:touched(touch)
StartButton:touched(touch)
FlagMove:touched(touch)
StartMove:touched(touch)
end
--# PhysicsResumePlay
function PhysicsResumePlayInit()
physics.resume()
PhysicsPaused=false
CameraX,CameraY=0,0
ResetButton=Buttons(100,75,"Reset",Reset)
FlagButton=Buttons(100,150,"Flag",Flag)
end
function PhysicsResumePlayDraw()
background(255, 255, 255, 255)
pushMatrix()
translate(WIDTH/2-Player.x,HEIGHT/2-Player.y)
fill(0, 0, 0, 255)
ellipse(Player.x,Player.y,40)
for k,v in pairs(splines) do
v:draw()
end
if FlagLX then
pushStyle()
fill(0, 0, 0, 100)
ellipse(FlagX,FlagY,40)
strokeWidth(4)
stroke(0, 0, 0, 100)
line(FlagX,FlagY,FlagX+FlagLX/10,FlagY+FlagLY/10)
popStyle()
end
popMatrix()
ResetButton:draw()
FlagButton:draw()
end
function PhysicsResumePlayTouched(touch)
ResetButton:touched(touch)
FlagButton:touched(touch)
end
--# OtherFunctions
function AddSpline()
S=Spline(200-WIDTH/2-CameraX,150-HEIGHT/2-CameraY)
table.insert(splines,S)
end
function PhysicsResumePlay()
local SplineCount=0
for k,v in pairs(splines) do
saveLocalData("splinesX1"..k,v.x1)
saveLocalData("splinesX2"..k,v.x2)
saveLocalData("splinesX3"..k,v.x3)
saveLocalData("splinesX4"..k,v.x4)
saveLocalData("splinesY1"..k,v.y1)
saveLocalData("splinesY2"..k,v.y2)
saveLocalData("splinesY3"..k,v.y3)
saveLocalData("splinesY4"..k,v.y4)
SplineCount = SplineCount + 1
end
saveLocalData("SplineCount",SplineCount)
PhysicsBodies()
PhysicsResumePlayInit()
GameState="PhysicsResumePlay"
end
function PhysicsBodies()
Player=physics.body(CIRCLE,20)
Player.x=0
Player.y=0
Player.restitution=0
Player.gravityScale=1
Player.friction=0
for k,v in pairs(splines) do
v:MakePhysicsSplines()
end
end
function FlagMove()
if FlagLX then
CameraX=-FlagX
CameraY=-FlagY
end
end
function StartMove()
CameraX=0
CameraY=0
end
function Reset()
restart()
end
function Flag()
FlagDirn=Player.linearVelocity
saveLocalData("FlagLX",FlagDirn.x)
saveLocalData("FlagLY",FlagDirn.y)
saveLocalData("FlagX",Player.x)
saveLocalData("FlagY",Player.y)
FlagX=readLocalData("FlagX")
FlagY=readLocalData("FlagY")
FlagLX=readLocalData("FlagLX")
FlagLY=readLocalData("FlagLY")
end
--# Buttons
Buttons = class()
function Buttons:init(x,y,txt,funct,l,h)
self.x = x
self.y = y
self.txt = txt
self.funct = funct
self.l=l
self.h=h
self.colour=color(31,64,104,0)
end
function Buttons:draw()
fill(0, 0, 0, 255)
fontSize(30)
font("AmericanTypewriter-Bold")
text(self.txt,self.x,self.y)
self.Sl,self.Sh = textSize(self.txt)
if self.l == nil then
self.l = self.Sl + 20
end
if self.h==nil then
self.h=self.Sh+20
end
pushStyle()
fill(self.colour)
strokeWidth(5)
stroke(31, 64, 104, 198)
rectMode(CENTER)
rect(self.x,self.y,self.l,self.h)
end
function Buttons:touched(touch)
tx = touch.x
ty = touch.y
if tx<=self.x + self.l/2 and tx>=self.x - self.l/2 and
ty>=self.y - self.h/2 and ty<=self.y + self.h/2 then
if touch.state==BEGAN then
self.colour=color(31, 64, 104, 100)
elseif touch.state == ENDED then
self.colour=color(31,64,104,0)
self.funct()
end
end
if touch.state==ENDED then
self.colour=color(31,64,104,0)
end
end
--# Main
function setup()
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_ANY)
GameState="PhysicsPausedPlay"
PhysicsPausedPlayInit()
end
function draw()
if GameState=="PhysicsPausedPlay" then
PhysicsPausedPlayDraw()
elseif GameState=="PhysicsResumePlay" then
PhysicsResumePlayDraw()
end
end
function touched(touch)
if GameState=="PhysicsPausedPlay" then
PhysicsPausedPlayTouched(touch)
elseif GameState=="PhysicsResumePlay" then
PhysicsResumePlayTouched(touch)
end
if touch.x>WIDTH-10 and touch.y>HEIGHT-10 then
close()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.