public
Last active

Line rider

  • Download Gist
Line rider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
 
--# 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
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.