Advertisement
Guest User

Untitled

a guest
Feb 20th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.95 KB | None | 0 0
  1.  
  2. --# Main
  3. -- APlayer
  4. -- Use this function to perform your initial setup
  5. function setup()
  6.     displayMode(FULLSCREEN)
  7.     ply = Player(WIDTH/2,HEIGHT/2,vec2(120,120),180)
  8.     dt=1 -- or 60/(1/DeltaTime), experimental
  9.     tchs = {}
  10.     pts = {}
  11.     pts[0] = vec2()
  12.     --level generator
  13.     for i=1,1000 do
  14.         table.insert(pts,pts[i-1]+vec2(math.random(10,20),math.sin(i/10)*10+
  15.         noise(i/2,i*2)*(5+i/100)))
  16.     end
  17.     lvm = TMesh(pts,10)
  18.     parameter.integer("SurfaceTexType",1,10,1,function()
  19.         if SurfaceTexType == 1 then
  20.             lvm.m.texture = readImage("Cargo Bot:Register Slot")
  21.         end
  22.         if SurfaceTexType == 2 then
  23.             lvm.m.texture = readImage("Cargo Bot:Game Area Floor")
  24.         end
  25.         if SurfaceTexType == 3 then
  26.             lvm.m.texture = readImage("Planet Cute:Brown Block")  
  27.         end
  28.         if SurfaceTexType == 4 then
  29.             lvm.m.texture = readImage("Planet Cute:Dirt Block")  
  30.         end
  31.         if SurfaceTexType == 5 then
  32.             lvm.m.texture = readImage("Planet Cute:Grass Block")  
  33.         end
  34.         if SurfaceTexType == 6 then
  35.             lvm.m.texture = readImage("Planet Cute:Stone Block")  
  36.         end
  37.         if SurfaceTexType == 7 then
  38.             lvm.m.texture = readImage("Planet Cute:Wall Block")  
  39.         end
  40.         if SurfaceTexType == 8 then
  41.             lvm.m.texture = readImage("Planet Cute:Water Block")  
  42.         end
  43.         if SurfaceTexType == 9 then
  44.             lvm.m.texture = readImage("Planet Cute:Wood Block")  
  45.         end
  46.         if SurfaceTexType == 10 then
  47.             lvm.m.texture = readImage("Platformer Art:Water")  
  48.         end
  49.     end)
  50.     parameter.integer("GroundTexType",1,2,1,function()
  51.         if GroundTexType == 1 then
  52.             lvm.t.texture = readImage("Cargo Bot:Starry Background")
  53.         end
  54.         if GroundTexType == 2 then
  55.             lvm.t.texture = readImage("Tyrian Remastered:Panel Opening 2")
  56.         end
  57.     end)
  58.     bvm = mesh()
  59.     bvm.texture = readImage("Space Art:UFO")
  60.     balls={}
  61.      for i=1,50 do
  62.         balls[i]={}
  63.         balls[i].body = physics.body(CIRCLE,math.random(5,10))
  64.         balls[i].body.position = ply:getPos()+vec2(math.random(-50,50),500+math.random(0,500))
  65.         balls[i].r = bvm:addRect(10,10,10,10)
  66.     end
  67.     physics.continuous = true
  68.     physics.iterations(10,8)
  69.     floor = physics.body(CHAIN,false,unpack(pts))
  70.     floor.info = "level"
  71.     floor.categories = {0,2}
  72.     scr = vec2(WIDTH/2,HEIGHT/2)
  73. end
  74.  
  75. function treadImage(str)
  76.     local tbl = {}
  77.     tbl.img = readImage(str)
  78.     tbl.str = str
  79.     return tbl
  80. end
  81.  
  82. function moveScreen()
  83.     local ball = ply:getPos()-vec2(WIDTH/2,HEIGHT/3)
  84.     spos = ball
  85.     if scr.x~=ball.x and scr.y~=ball.y then
  86.         scr = scr + ((vec2(WIDTH/2,HEIGHT/2)-ball)-scr)
  87.     end
  88.     local mt = vec2(WIDTH,HEIGHT*0.3)*0.5-vec2(WIDTH,HEIGHT*0.3)*0.5
  89.     translate(-ball.x+mt.x,-ball.y+mt.y)
  90. end
  91.  
  92. function touched(t)
  93.     if t.state == BEGAN then
  94.         local jpos,jsize = ply.joypos,ply.joysize
  95.         local epos,esize = ply.epos,ply.esize
  96.         if vec2(t.x,t.y):dist(jpos) < jsize/2 then
  97.             table.insert(tchs,{t,ply,"ply"})
  98.         elseif vec2(t.x,t.y):dist(epos) < esize/2 then
  99.             table.insert(tchs,{t,ply,"jump"})
  100.         else
  101.             table.insert(tchs,{t,ply,"look"})
  102.         end
  103.     end
  104.     for k,v in pairs(tchs) do
  105.         if t.id == v[1].id then
  106.             tchs[k][1] = t
  107.             tchs[k][2]:touched(tchs[k])
  108.             if v[1].state == ENDED then
  109.                 tchs[k] = nil
  110.             end
  111.         end
  112.     end
  113.    
  114. end
  115.  
  116. function collide(c) ply:collide(c) end
  117.  
  118. function sign(x)
  119.     if x>0 then return 1 elseif x < 0 then return -1 else return 0 end
  120. end
  121. function angleOfPoint( pt )
  122.    local x, y = pt.x, pt.y
  123.    local radian = math.atan2(y,x)
  124.    local angle = radian*180/math.pi
  125.    if angle < 0 then angle = 360 + angle end
  126.    return angle
  127. end
  128. -- This function gets called once every frame
  129. function draw()
  130.     -- This sets a dark background color
  131.     background(40, 40, 50)
  132.  
  133.     -- This sets the line thickness
  134.     strokeWidth(5)
  135.  
  136.     -- Do your drawing here
  137.     pushMatrix()
  138.     moveScreen()
  139.     lvm:draw()
  140.     for k,v in pairs(balls) do
  141.         bvm:setRect(v.r,v.body.x,v.body.y,v.body.radius*2,v.body.radius*2,math.rad(v.body.angle))
  142.     end
  143.     bvm:draw()
  144.     ply:draw()
  145.     popMatrix()
  146.     ply:drawJoystick()
  147. end
  148.  
  149. flipshadr = {vS = [[//
  150. // A basic vertex shader
  151. //
  152.  
  153. //This is the current model * view * projection matrix
  154. // Codea sets it automatically
  155. uniform mat4 modelViewProjection;
  156.  
  157. //This is the current mesh vertex position, color and tex coord
  158. // Set automatically
  159. attribute vec4 position;
  160. attribute vec4 color;
  161. attribute vec2 texCoord;
  162.  
  163. //This is an output variable that will be passed to the fragment shader
  164. varying lowp vec4 vColor;
  165. varying highp vec2 vTexCoord;
  166.  
  167. void main()
  168. {
  169.     //Pass the mesh color to the fragment shader
  170.     vColor = color;
  171.     vTexCoord = texCoord;
  172.    
  173.     //Multiply the vertex position by our combined transform
  174.     gl_Position = modelViewProjection * position;
  175. }
  176. ]],fS = [[
  177. //
  178. // A basic fragment shader
  179. //
  180.  
  181. //Default precision qualifier
  182. precision highp float;
  183.  
  184. //This represents the current texture on the mesh
  185. uniform lowp sampler2D texture;
  186. uniform highp vec2 flip;
  187.  
  188. //The interpolated vertex color for this fragment
  189. varying lowp vec4 vColor;
  190.  
  191. //The interpolated texture coordinate for this fragment
  192. varying highp vec2 vTexCoord;
  193.  
  194. void main()
  195. {
  196.     //Sample the texture at the interpolated coordinate
  197.     lowp vec4 col = vec4(0,0,0,0);
  198.     mediump vec2 tx = vTexCoord.st;
  199.     if(flip.x==-1.0) tx.x = 1.0 - tx.x;
  200.     if(flip.y==-1.0) tx.y = 1.0 - tx.y;
  201.     col = texture2D( texture, tx)
  202.     * vColor;
  203.     //Set the output color to the texture color
  204.     gl_FragColor = col;
  205. }
  206. ]]}
  207.  
  208. --# Player
  209. Player = class()
  210.  
  211. function Player:init(x,y,jpos,jsize)
  212.     self.pos = vec2(x,y)
  213.     self.vel = vec2(0,0)
  214.     self.alive = true
  215.     self.plyv =  {vec2(-13,-11),vec2(-14,21),vec2(-8,27),
  216.     vec2(-1,29),vec2(7,27),vec2(15,20),
  217.     vec2(15,-9),vec2(10,-15),vec2(1,-18),
  218.     vec2(-8,-16)}
  219.     self.rca = nil
  220.     self.rcb = nil
  221.     self.ply = physics.body(POLYGON,unpack(self.plyv))
  222.     self.ply.friction = 0.5
  223.     self.ply.restitution = 0.4
  224.     self.ply.position = vec2(x,y)
  225.     self.ply.type = STATIC
  226.     self.ply.linearVelocity = vec2(0,0)
  227.     self.ply.categories = {4}
  228.     self.ply.info = "ply"
  229.     local verts = {vec2(-13,14),vec2(-16,4),vec2(-23,0),
  230.     vec2(-17,0),vec2(-13,-17),vec2(-5,-22),
  231.     vec2(10,-22),vec2(16,-15),vec2(19,0),
  232.     vec2(24,0),vec2(20,4),vec2(15,14),
  233.     vec2(5,19),vec2(-4,19)}
  234.     self.head = physics.body(POLYGON,unpack(verts))
  235.     self.head.friction = 0.5
  236.     self.head.density = 1
  237.     self.head.restitution = 0.4
  238.     self.head.position = vec2(x,y+35)
  239.     self.head.type = STATIC
  240.     self.head.info = "head"
  241.     self.head.categories = {4}
  242.     self.head.linearVelocity = vec2()
  243.     self.neck = physics.joint(REVOLUTE,self.ply,self.head,self.head.position+vec2(0,-30))
  244.     self.neck.enableLimit = true
  245.     self.neck.lowerLimit = -45
  246.     self.neck.upperLimit = 45
  247.     self.plym = mesh()
  248.     self.plyr = self.plym:addRect(x,y,40,80,0)
  249.     self.plym.shader = shader(flipshadr.vS,flipshadr.fS)
  250.     self.plym.shader.flip = vec2(-1,1)
  251.     self.plym.texture = readImage("Small World:Grass Patch")
  252.     self.headm = mesh()
  253.     self.headr = self.headm:addRect(x,y,60,60)
  254.     self.headm.shader = shader(flipshadr.vS,flipshadr.fS)
  255.     self.headm.shader.flip = vec2(-1,1)
  256.     self.headm.texture = readImage("Small World:Grass Patch")
  257.     self.joypos = jpos
  258.     self.epos = vec2(WIDTH-jsize/2-25,jpos.y)
  259.     self.esize = jsize
  260.     self.joysize = jsize
  261.     self.jactive = false
  262.     self.joff = vec2()
  263.     self.lookpos = vec2(x+600,y)
  264.     self.speed = 450
  265.     self.jump = nil
  266.     self.dir = 1
  267.     self.lp = false
  268.     self.mdir = self.dir
  269.     local verts = {vec2(-9,0),vec2(-9,-4),vec2(-5,-6),vec2(0,-6),vec2(5,-6),vec2(9,-5),vec2(10,-3),vec2(10,3),vec2(7,6),vec2(4,5),vec2(2,4),vec2(-2,5),vec2(-6,4)}
  270.     for i=1,#verts do
  271.         verts[i] = verts[i]*1.5
  272.     end
  273.     self.sj = nil
  274.     self.lt = physics.body(POLYGON,unpack(verts))
  275.     self.lt.position = vec2(x-10,y-50)
  276.     self.lt.angularDamping = 0.1
  277.     self.lt.friction = 1.5
  278.     self.lt.type = STATIC
  279.     self.lt.bullet = true
  280.     self.lt.density = 1
  281.     self.lt.categories = {3}
  282.     self.lt.mask = {0,1}
  283.     self.lt.info = "foot"
  284.     self.rt = physics.body(POLYGON,unpack(verts))
  285.     self.rt.position = vec2(x+10,y-50)
  286.     self.rt.angularDamping = 0.1
  287.     self.rt.friction = 1.5
  288.     self.rt.bullet = true
  289.     self.rt.type = STATIC
  290.     self.rt.density = 1
  291.     self.rt.categories = {3}
  292.     self.rt.mask = {0,1}
  293.     self.rt.info = "foot"
  294.     self.lm = mesh()
  295.     self.lm.texture = readImage("Small World:Grass Patch")
  296.     self.lt.info = self.lm:addRect(self.lt.x,self.lt.y,10,30)
  297.     self.rt.info = self.lm:addRect(self.rt.x,self.rt.y,10,30)
  298.     self.lm.shader = shader(flipshadr.vS,flipshadr.fS)
  299.     self.lm.shader.flip = vec2(-self.dir,1)
  300.     self.lm:setColors(255,255,255,255)
  301.     self.hm = mesh()
  302.     self.hm.texture = readImage("Small World:Grass Patch")
  303.     self.hm.shader = shader(flipshadr.vS,flipshadr.fS)
  304.     self.hm.shader.flip = vec2(-1,self.dir)
  305.     self.hr = self.hm:addRect(x+30,y,20,18)
  306.     self.wt = -2
  307.     self.dt = vec2(x,y)
  308.     self.recoil = vec2()
  309.     self.prevang = 0
  310.     self.lookvel = vec2()
  311.     self.tw = nil
  312.     self.killed = false
  313.     self.ktime = 0
  314.     self.dtt = nil
  315.     self.rv = 0
  316.     self.tp = nil
  317.     self.ftouch = false
  318.     self:flipTex(true)
  319.     self.tbox = nil
  320.     self:setState(DYNAMIC)
  321. end
  322.  
  323. function Player:isAlive()
  324.     return self.alive
  325. end
  326.  
  327. function Player:getState()
  328.     if (self.ply.type+self.lt.type+self.rt.type+self.head.type)/4 == DYNAMIC then return DYNAMIC else return STATIC end
  329. end
  330.  
  331. function Player:setState(state)
  332.     self.ply.type = state self.lt.type = state self.rt.type = state self.head.type = state
  333. end
  334.  
  335. function Player:getPos()
  336.     return self.ply.position
  337. end
  338.  
  339. function Player:setPos(x,y)
  340.     local pos = vec2(x,y)
  341.     self.ply.position = pos+vec2(0,30)
  342.     self.head.position = self.ply.position+vec2(0,40)
  343.     self.lt.position = self.ply.position+vec2(-10,-50)
  344.     self.rt.position = self.ply.position+vec2(10,-50)
  345. end
  346.  
  347. function Player:joyOffset()
  348.     return self.joff
  349. end
  350.  
  351. function Player:getVelocity()
  352.     return self.ply.linearVelocity
  353. end
  354.  
  355. function Player:flipTex(flip)
  356.     if self.dir == nil then return end
  357.     local dir = -self.dir
  358.     if flip then dir = -dir end
  359.     self.plym.shader.flip = vec2(-dir,1)
  360.     self.headm.shader.flip = vec2(-dir,1)
  361.     self.hm.shader.flip = vec2(-1,dir)
  362.     self.lm.shader.flip = vec2(-dir,1)
  363. end
  364.  
  365. function Player:stopActions()
  366.     self.joff = vec2()
  367.     self.jactive = false
  368.     self.vel = vec2()
  369.     self.jump = nil
  370. end
  371.  
  372. function Player:walk(vel)
  373.     local ply = self.ply
  374.     self.vel = vec2(vel.x,ply.linearVelocity.y)
  375.     if sign(self.vel.x) ~= 0 then
  376.         if sign(self.vel.x)~=self.dir then
  377.             self.lookpos = ply.position
  378.             self.mdir = sign(self.vel.x)
  379.             --self.dir = self.mdir
  380.         end
  381.     end
  382.     --self.wt = self.wt+0.001*(dt*0.7+0.3)*self.vel.x*0.6
  383.     --self.wt = self.wt+self.vel.x*0.0006
  384. end
  385.  
  386. function Player:applyForce(force)
  387.     self.ply:applyForce(force*self.ply.mass*dt)
  388.     self.lt:applyForce(force*self.lt.mass*dt*0.5)
  389.     self.rt:applyForce(force*self.rt.mass*dt*0.5)
  390.     self.head:applyForce(force*self.head.mass*dt*0.05)
  391. end
  392.  
  393. function Player:applyLinearImpulse(force)
  394.     self.ply:applyLinearImpulse(((force-self.ply.linearVelocity/32)*self.ply.mass)/32)
  395.     self.lt:applyLinearImpulse(((force-self.lt.linearVelocity/32)*self.lt.mass)/32)
  396.     self.rt:applyLinearImpulse(((force-self.rt.linearVelocity/32)*self.rt.mass)/32)
  397.     --self.head:applyLinearImpulse(force*self.head.mass)
  398. end
  399.  
  400. function Player:setVelocity(vel)
  401.     self.ply.linearVelocity = vel
  402.     self.lt.linearVelocity = vel
  403.     self.rt.linearVelocity = vel
  404.     self.head.linearVelocity = vel
  405. end
  406.  
  407.  
  408. function Player:correctFeet()
  409.     if self.lt.angle > 40 then
  410.         self.lt.angle = 45
  411.     elseif self.lt.angle < -45 then
  412.         self.lt.angle = -45
  413.     end
  414.     if self.rt.angle > 45  then
  415.         self.rt.angle = 45
  416.     elseif self.rt.angle < -45 then
  417.         self.rt.angle = -45
  418.     end
  419. end
  420.  
  421. function Player:destroy()
  422.     self.rt:destroy()
  423.     self.lt:destroy()
  424.     self.ply:destroy()
  425.     self.head:destroy()
  426.     self.neck:destroy()
  427. end
  428.  
  429. function Player:kill()
  430.     if not self.alive or playing == "death" then return end
  431.     self.ply.fixedRotation = false
  432.     while self.ply.angle > 180 do
  433.         self.ply.angle = self.ply.angle - 360
  434.     end
  435.     while self.ply.angle < -180 do
  436.         self.ply.angle = self.ply.angle + 360
  437.     end
  438.     while self.lt.angle > 180 do
  439.         self.lt.angle = self.lt.angle - 360
  440.     end
  441.     while self.lt.angle < -180 do
  442.         self.lt.angle = self.lt.angle + 360
  443.     end
  444.     while self.rt.angle > 180 do
  445.         self.rt.angle = self.rt.angle - 360
  446.     end
  447.     while self.rt.angle < -180 do
  448.         self.rt.angle = self.rt.angle + 360
  449.     end
  450.     while self.head.angle > 180 do
  451.         self.head.angle = self.head.angle - 360
  452.     end
  453.     while self.head.angle < -180 do
  454.         self.head.angle = self.head.angle + 360
  455.     end
  456.     self:stopActions()
  457.     self.alive = false
  458.     self.killed = true
  459.     self.lookvel = vec2()
  460.     self.neck.lowerLimit = -80
  461.     self.neck.upperLimit = 80
  462.     self.head.density = 0.5
  463.     self.head.angularVelocity = 0
  464.     self:stopWalk()
  465.     if self.tw then tween.stop(self.tw) self.tw = nil end
  466.     self.tw = tween.delay(1.5,function() self:revive() end)
  467. end
  468.  
  469. function Player:findLegs()
  470.     local found = true
  471.     local ply,lt,rt = self.ply,self.lt,self.rt
  472.     if lt.position:dist(ply.position) > 40 then
  473.         local rcl = physics.raycast(lt.position,lt.position+(ply.position-lt.position):normalize()*60-vec2(0,-10),0,1)
  474.         if rcl and rcl.fraction<0.7 then
  475.             lt:applyForce(rcl.normal*(1-rcl.fraction)*7.5+vec2(0,15)+vec2((lt.position-ply.position).x,0):normalize()*5)
  476.         elseif not rcl or (rcl and rcl.fraction>0.7) then
  477.             lt:applyForce(vec2((ply.position-lt.position).x,0):normalize()*30-vec2(lt.linearVelocity.x/10,0))
  478.             if lt.y<ply.y-5 then
  479.                 lt:applyForce(vec2(0,15))
  480.             end
  481.         end
  482.         found = false
  483.     end
  484.     if rt.position:dist(ply.position) > 40 then
  485.         local rcr = physics.raycast(rt.position,rt.position+(ply.position-rt.position):normalize()*60-vec2(0,-10),0,1)
  486.         if rcr and rcr.fraction<0.7 then
  487.             rt:applyForce(rcr.normal*(1-rcr.fraction)*7.5+vec2(0,15)+vec2((rt.position-ply.position).x,0):normalize()*5)
  488.         elseif not rcr or (rcr and rcr.fraction>0.7) then
  489.             rt:applyForce(vec2((ply.position-rt.position).x,0):normalize()*30-vec2(rt.linearVelocity.x/10,0))
  490.             if rt.y<ply.y-5 then
  491.                 rt:applyForce(vec2(0,15))
  492.             end
  493.         end
  494.         found = false
  495.     end
  496.     if found then
  497.         local rc = physics.raycast(ply.position,ply.position-vec2(0,50),0)
  498.         if rc then
  499.             ply.linearVelocity = vec2(0,20)
  500.             self.head.linearVelocity = vec2(0,80)
  501.             local ang = (0-ply.angle)
  502.             while ang > 180 do
  503.                 ang = ang - 360
  504.             end
  505.             while ang < -180 do
  506.                 ang = ang + 360
  507.             end
  508.             --ply.angularVelocity = ply.angularVelocity+ang-ply.angularVelocity/10
  509.             if rc.fraction<0.6 then
  510.                 found = false
  511.             end
  512.         else
  513.             found = true
  514.         end
  515.     end
  516.     local pla = ply.angle
  517.     while pla > 180 do
  518.         pla = pla - 360
  519.     end
  520.     while pla < -180 do
  521.         pla = pla + 360
  522.     end
  523.     if found then
  524.         if pla>-90 and pla<90 then
  525.             found = true
  526.         else
  527.             found = false
  528.         end
  529.     end
  530.     if ((self.lt.position+self.rt.position)/2-self.ply.position):len()>900 then
  531.         playing = nil
  532.         cod = "You died from not being able to find your legs."
  533.     end
  534.     if not found and not self.killed then
  535.         tween.delay(0.03,function() self:findLegs() end)
  536.     else
  537.         self.alive = true
  538.         for i=1,10 do
  539.             tween.delay(0.02*i,function() self:setVelocity(vec2()) end)
  540.         end
  541.         self.killed = false
  542.         ply.angle = pla
  543.     end
  544. end
  545.  
  546. function Player:revive()
  547.     self.killed = false
  548.     if self.tw then tween.stop(self.tw) self.tw = nil end
  549.     if ((self.lt.position+self.rt.position)/2-self.ply.position):len()<900 then
  550.         --self.alive = true
  551.         --self:setPos(self.ply.x,self.ply.y+40)
  552.         self:findLegs()
  553.         self.neck.lowerLimit = -45
  554.         self.neck.upperLimit = 45
  555.         self.head.density = 0.5
  556.     end
  557. end
  558.  
  559. function Player:collide(c)
  560.     if not c.bodyA then return end
  561.     if c.bodyA.info and c.bodyA.info == "head" then
  562.         if c.normalImpulse>4 then
  563.             self:kill()
  564.         end
  565.     end
  566. end
  567.  
  568. function Player:papplyForce(force)
  569.     self.ply.linearVelocity = self.ply.linearVelocity*0.98 + force
  570. end
  571.  
  572. function Player:raycast(pos,pos2,...)
  573.     local rc = physics.raycastAll(pos,pos2,...)
  574.     local cent = {nil,math.huge}
  575.     if not rc then return nil end
  576.     for k,v in pairs(rc) do
  577.         if v.body.info ~= "foot" then
  578.             if v.point:dist(pos)<cent[2] then
  579.                 cent = {v,v.point:dist(pos)}
  580.             end
  581.         end
  582.     end
  583.     return cent[1]
  584. end
  585.  
  586. function Player:getViewAngle()
  587.     return angleOfPoint(self.lookpos-self:getPos())
  588. end
  589.  
  590. function Player:draw()
  591.     local pl = self.ply
  592.     local lt = self.lt
  593.     local rt = self.rt
  594.     local rtang = math.rad(self.rt.angle)
  595.     local ltang = math.rad(self.lt.angle)
  596.     local head = self.head
  597.     local jump = self.jump
  598.     local alive = self.alive
  599.     local joff = self.joff
  600.     local norm = vec2()
  601.     local lv = 0
  602.     local angadd = 0
  603.     local sp = self:getPos()
  604.     local pos = sp
  605.     local dir = self.dir
  606.     local tp = vec2(sp.x+(head.x-sp.x),sp.y)
  607.     self.tp = tp
  608.     local lp = self.lookpos
  609.     local dtt = (lp-pos):normalize()*math.min(math.max(lp:dist(sp),1),40)
  610.     if sign(lp.x-sp.x)~=self.dir then
  611.         self.dir = sign(lp.x-sp.x)
  612.         if not self.dir then
  613.             self.dir = 1
  614.         else
  615.             self:flipTex(true)
  616.             self.lm.shader.flip = vec2(-self.dir,1)
  617.         end
  618.     end
  619.     if dtt ~= dtt then
  620.         dtt = vec2(40,25)
  621.     end
  622.    
  623.     if not self.ftouch and self.vel:len()>0 then
  624.         --self:kill()
  625.         self.ftouch = true
  626.     end
  627.     self.dtt = dtt
  628.     pos = pos+dtt
  629.     ellipse(lp.x,lp.y,20)
  630.     local ptos = dtt
  631.     local ang = math.rad(angleOfPoint(lp-sp))
  632.     local ptang = ang
  633.     local npos = vec2()
  634.     local gpos = pos + self.recoil/10
  635.     self.hm:setRect(self.hr,gpos.x-dtt.x*0.5,gpos.y-dtt.y*0.5,20,18,ang)
  636.     local kneepos = pl.position+vec2(0,-60)+pl.linearVelocity/32
  637.     if joff.y<-0.4 then
  638.         -- kneepos = pl.position+vec2(0,-45)
  639.     end
  640.     local wt = self.wt
  641.     local vel = self.vel
  642.     local plvel = pl.linearVelocity
  643.     --Foot gap distance
  644.     local len = math.min(10+20*(math.abs(vel.x)/600+0.3),40)
  645.     if joff.y<-0.4 then
  646.         len = math.min(10+15*(math.abs(vel.x)/1000+0.4),30)
  647.     end
  648.     self.plym:setRect(self.plyr,sp.x,sp.y,30,40,math.rad(pl.angle))
  649.     self.headm:setRect(self.headr,head.x,head.y,50,50,math.rad(head.angle))
  650.     pushStyle()
  651.     strokeWidth(5)
  652.     --Toe and Heel raycast
  653.     self.rcat = self:raycast(lt.position+vec2(1*dir,-5):rotate(math.rad(lt.angle)),
  654.     lt.position+vec2(9*dir,-16):rotate(math.rad(lt.angle)),0)
  655.     self.rcah = self:raycast(lt.position+vec2(-1*dir,-5):rotate(math.rad(lt.angle)),
  656.     lt.position+vec2(-9*dir,-16):rotate(math.rad(lt.angle)),0)
  657.     self.rcbt = self:raycast(rt.position+vec2(1*dir,-5):rotate(math.rad(rt.angle)),
  658.     rt.position+vec2(9*dir,-16):rotate(math.rad(rt.angle)),0)
  659.     self.rcbh = self:raycast(rt.position+vec2(-1*dir,-5):rotate(math.rad(rt.angle)),
  660.     rt.position+vec2(-9*dir,-16):rotate(math.rad(rt.angle)),0)
  661.     --Debug drawing for feet
  662.     local p1,p2,p3,p4
  663.     p1,p2 = lt.position+vec2(9*dir,-16):rotate(math.rad(lt.angle)),rt.position+vec2(9*dir,-16):rotate(math.rad(rt.angle))
  664.     p3,p4 = lt.position+vec2(1*dir,-5):rotate(math.rad(lt.angle)),rt.position+vec2(1*dir,-5):rotate(math.rad(rt.angle))
  665.     if self.rcat then
  666.         p1 = self.rcat.point
  667.     end
  668.     line(p3.x,p3.y,p1.x,p1.y)
  669.     if self.rcbt then
  670.         p2 = self.rcbt.point
  671.     end
  672.     line(p4.x,p4.y,p2.x,p2.y)
  673.     stroke(255,50,50)
  674.     p1,p2 = lt.position+vec2(-9*dir,-16):rotate(math.rad(lt.angle)),rt.position+vec2(-9*dir,-16):rotate(math.rad(rt.angle))
  675.     p3,p4 = lt.position+vec2(1*dir,-5):rotate(math.rad(lt.angle)),rt.position+vec2(-1*dir,-5):rotate(math.rad(rt.angle))
  676.     if self.rcah then
  677.         p1 = self.rcah.point
  678.     end
  679.     line(p3.x,p3.y,p1.x,p1.y)
  680.     if self.rcbh then
  681.         p2 = self.rcbh.point
  682.     end
  683.     line(p4.x,p4.y,p2.x,p2.y)
  684.     popStyle()
  685.     --End debug drawing
  686.    
  687.     local rc = self.rcat or self.rcah
  688.     local rc2 = self.rcbt or self.rcbh
  689.     if self.jump and rc and rc2 then
  690.         rc = nil
  691.         rc2 = nil
  692.         self.lt:applyForce(vec2(0,-self.jump.y/5))
  693.         self.rt:applyForce(vec2(0,-self.jump.y/5))
  694.         --self:papplyForce(vec2(0,0))
  695.         tween.delay(0.01,function()
  696.             if self.jump then
  697.                 self:setVelocity(vec2((self.vel.x*3)+pl.linearVelocity.x/32,self.jump.y*5))
  698.                 self.lt:applyForce(vec2(0,self.jump.y/5))
  699.                 self.rt:applyForce(vec2(0,self.jump.y/5))
  700.             end
  701.         end)
  702.     end
  703.     local angg = 0
  704.     local pla = pl.angle
  705.     while pla > 180 do
  706.         pla = pla - 360
  707.     end
  708.     while pla < -180 do
  709.         pla = pla + 360
  710.     end
  711.     --Upright the body
  712.     if alive then
  713.         local ang = (angg-pla)
  714.         while ang > 180 do
  715.             ang = ang - 360
  716.         end
  717.         while ang < -180 do
  718.             ang = ang + 360
  719.         end
  720.         pl.angularVelocity = (ang-pl.angularVelocity/10)*dt*2
  721.     end
  722.     local fpos = ((rt.position+lt.position)/2)+vec2(0,60)
  723.     local ltpos,rtpos = lt.position,rt.position
  724.     local rcd = self:raycast(pl.position+vec2(0,-30):rotate(math.rad(pl.angle)),pl.position+vec2(0,-120):rotate(math.rad(pl.angle)),0)
  725.     if (rc or rc2) and alive and not jump then
  726.         kneepos = pl.position+vec2(0,-60)
  727.         self.hm:setRect(self.hr,gpos.x-dtt.x*0.5,gpos.y-dtt.y*0.5,20,18,math.rad(angleOfPoint(lp-sp)))
  728.         self.prevang = ang
  729.         self:papplyForce(vec2(self.recoil.x*5,self.recoil.y*2.5))
  730.         local plv = vec2(pl.linearVelocity.x,pl.linearVelocity.y*2)
  731.         if joff.y<-0.4 then
  732.            
  733.         else
  734.            
  735.         end
  736.         if rcd then
  737.             norm = rcd.normal
  738.         end
  739.         local pf = false
  740.         if rc and rc2 then
  741.             if rc.body == rc2.body then
  742.                 if rc.body.mass>3 and rc2.body.mass>3 then
  743.                     pf = true
  744.                 end
  745.             end
  746.         end
  747.         local rca = rcd
  748.         if pf and rca then
  749.             pl:applyLinearImpulse(rca.body.linearVelocity*rca.body.mass*0.1*pl.mass/42)
  750.             lt:applyLinearImpulse(rca.body.linearVelocity*rca.body.mass*0.1*lt.mass/36)
  751.             rt:applyLinearImpulse(rca.body.linearVelocity*rca.body.mass*0.1*rt.mass/36)
  752.         end
  753.         if self.ktime>0.4 then self:kill() self:revive() end
  754.         if norm:len() ~= norm:len() then norm = vec2(0,1) end
  755.         local ang = (angleOfPoint(norm)-90)
  756.         local gad = (ang/90)*-dir
  757.         local gda = (ang/90)*dir
  758.         gda = norm.x*dir
  759.         local grad = 1+(ang/90)*-dir
  760.         if rcd and rcd.body.type == DYNAMIC then
  761.             ang = 0
  762.             grad = 1
  763.             gad = 0
  764.             gda = 0
  765.             norm.y = 1
  766.             norm.x = 0
  767.         end
  768.         self:applyLinearImpulse(vec2(0,(-vel.x)*0.2*
  769.         (grad)):rotate(math.rad(ang*0.9+90))/(1+math.abs(pl.x-(lt.x+rt.x)/2)*0.1))
  770.         local move = 1
  771.         if self.joff:len()>0 then move = 0 end
  772.         local rcf,rcf2
  773.         if rc then
  774.             if self.rcat or self.rcah then
  775.                 rcf = true
  776.             end
  777.         end
  778.         if rc2 then
  779.             if self.rcbt or self.rcbh then
  780.                 rcf2 = true
  781.             end
  782.         end
  783.         --If feet are not touching the ground, this needs tweaking
  784.         if math.cos(self.wt-gda)>=0.01 then
  785.             self.wt = wt + (math.sin(self.wt+math.pi/2-gda)*dir)*move*0.1
  786.         end
  787.         if math.cos(self.wt+math.pi-gda)>=0.01 then
  788.             self.wt = wt + (math.sin(self.wt-math.pi/2-gda)*dir)*move*0.1
  789.         end
  790.        
  791.         --Debug drawing
  792.         pushStyle()
  793.         pushMatrix()
  794.         translate(pl.x,pl.y)
  795.         strokeWidth(3)
  796.         stroke(255)
  797.         fill(255)
  798.         text(math.ceil(gda*100)/100,0,100)
  799.         line(100,100,100,200)
  800.         line(90,100-math.min(math.cos(self.wt-math.rad(ang)),0)*100,110,100-math.min(math.cos(self.wt-math.rad(ang))*100,0))
  801.         text(math.ceil(math.min(math.cos(self.wt-math.rad(ang)),0)*100)/100,70,100-math.min(math.cos(self.wt-math.rad(ang)),0)*100)
  802.         line(150,100,150,200)
  803.         line(140,100-math.min(math.cos(self.wt+math.pi-math.rad(ang)),0)*100,160,100-math.min(math.cos(self.wt+math.pi-math.rad(ang))*100,0))
  804.         text(math.ceil(math.min(math.cos(self.wt+math.pi-math.rad(ang)),0)*100)/100,180,100-math.min(math.cos(self.wt+math.pi-math.rad(ang)),0)*100)
  805.         popMatrix()
  806.         popStyle()
  807.         --End debug drawing
  808.         --This makes the feet turn, wt = walking time, grad = floor gradient
  809.         if self.joff.x~=0 and not self.jump then
  810.             self.wt = self.wt + (self.vel.x*0.00055)*((grad-1)*0.5+1)
  811.         end
  812.         --Check if the joystick is being used
  813.         if self.jactive then
  814.             self:walk(self.joff*self.speed)
  815.         end
  816.         --A bit of grip, can be made stronger but this works well
  817.         if rcd then
  818.             pl:applyForce(pl.mass*((rcd.point-pl.position)))
  819.         end
  820.         --The footwork
  821.         if not self.jump then
  822.             if rc or not rc2 then
  823.                 local pros
  824.                 local pnorm
  825.                 fpos = nil
  826.                 --Check if the foot should be touching the floor, gda = ground angle from -1 to 1, 1 being 90
  827.                 if math.cos(self.wt)<=gda then
  828.                     if self.rcat then
  829.                         pros = self.rcat.point
  830.                         lt.angularVelocity = lt.angularVelocity+5*-dir-lt.angularVelocity/20
  831.                         if not self.rcah then
  832.                             lt:applyForce(lt.mass*((pros-lt.position)*4-lt.linearVelocity))
  833.                         end
  834.                     end
  835.                     if self.rcah then
  836.                         pros = self.rcah.point
  837.                         lt.angularVelocity = lt.angularVelocity+5*dir-lt.angularVelocity/20
  838.                         if not self.rcat then
  839.                             lt:applyForce(lt.mass*((pros-lt.position)*4-lt.linearVelocity))
  840.                         end
  841.                     end
  842.                     if self.rcat and self.rcah then
  843.                         pnorm = (self.rcat.normal+self.rcah.normal)/2
  844.                         pros = (self.rcat.point+self.rcah.point)/2+pnorm*0
  845.                         ltpos = pros
  846.                         lt:applyLinearImpulse(lt.mass*((pros-lt.position)*16-lt.linearVelocity)/32)
  847.                         lt.linearVelocity=lt.linearVelocity*0.1
  848.                     end
  849.                 else
  850.                     ltpos = (vec2(math.sin(self.wt)*(len+5),
  851.                     math.cos(self.wt)*math.max(len,0)):rotate(math.rad(ang))+kneepos)
  852.                 end
  853.                 --pros = ((pros+(lt.position+rt.position)/2+pl.position+vec2(0,120))/3)
  854.                 local pti = vec2(self.vel.x*0.035,0):rotate(math.rad(ang))
  855.                 pti.y = math.min(pti.y,0)
  856.                 pl:applyForce(pl.mass*(((lt.position+rt.position)/2+vec2(pti.x,60+pti.y)-pl.position)*32-
  857.                 pl.linearVelocity))
  858.                 rtpos = (vec2(math.sin(self.wt+math.pi)*(len+5),
  859.                 math.cos(self.wt+math.pi)*math.max(len,0)):rotate(math.rad(ang))+kneepos+pti)
  860.             end
  861.             if rc2 or not rc then
  862.                 local pros
  863.                 local pnorm
  864.                 fpos = nil
  865.                 if math.cos(self.wt+math.pi)<=gda then
  866.                     if self.rcbt then
  867.                         pros = self.rcbt.point
  868.                         rt.angularVelocity = rt.angularVelocity+5*-dir-rt.angularVelocity/20
  869.                         if not self.rcbh then
  870.                             rt:applyForce(rt.mass*((pros-rt.position)*4-rt.linearVelocity))
  871.                         end
  872.                     end
  873.                     if self.rcbh then
  874.                         pros = self.rcbh.point
  875.                         rt.angularVelocity = rt.angularVelocity+5*dir-rt.angularVelocity/20
  876.                         if not self.rcbt then
  877.                             rt:applyForce(rt.mass*((pros-rt.position)*4-rt.linearVelocity))
  878.                         end
  879.                     end
  880.                     if self.rcbt and self.rcbh then
  881.                         pnorm = (self.rcbt.normal+self.rcbh.normal)/2
  882.                         pros = (self.rcbt.point+self.rcbh.point)/2+pnorm*0
  883.                         rtpos = pros
  884.                         rt:applyLinearImpulse(rt.mass*((pros-rt.position)*16-rt.linearVelocity)/32)
  885.                         rt.linearVelocity=rt.linearVelocity*0.1
  886.                     end
  887.                 else
  888.                     rtpos = (vec2(math.sin(self.wt+math.pi)*(len+5),
  889.                     math.cos(self.wt+math.pi)*math.max(len,0)):rotate(math.rad(ang))+kneepos)
  890.                 end
  891.                 --pros = ((pros+(lt.position+rt.position)/2+pl.position+vec2(0,120))/3)
  892.                 local pti = vec2(self.vel.x*0.035,0):rotate(math.rad(ang))
  893.                 pti.y = math.min(pti.y,0)
  894.                 pl:applyForce(pl.mass*(((lt.position+rt.position)/2+vec2(pti.x,60+pti.y)-pl.position)*32-
  895.                 pl.linearVelocity))
  896.                 ltpos = (vec2(math.sin(self.wt)*(len+5),
  897.                 math.cos(self.wt)*math.max(len,0)):rotate(math.rad(ang))+kneepos+pti)
  898.             end
  899.         end
  900.         local mng = ang--+math.sin(self.wt)*15+(15*dir)
  901.         lt.angularVelocity = lt.mass*((mng-lt.angle)*32-lt.angularVelocity)
  902.         rt.angularVelocity = rt.mass*((mng-rt.angle)*32-rt.angularVelocity)
  903.     elseif alive then
  904.         ltpos = (vec2(math.sin(self.wt)*len*0.5*dir,0)+kneepos)
  905.         rtpos = (vec2(math.sin(self.wt+math.pi)*len*0.5*dir,0)+kneepos)
  906.         self:papplyForce(self.recoil*0.55)
  907.         --Gravity does not work so well so we need to give it a little push
  908.         if pl.linearVelocity.y<=0 then
  909.             pl.linearVelocity.y = pl.linearVelocity.y*1.1
  910.         end
  911.         if lt.linearVelocity.y<=0 then
  912.             lt.linearVelocity.y = lt.linearVelocity.y*1.1
  913.         end
  914.         if rt.linearVelocity.y<=0 then
  915.             rt.linearVelocity.y = rt.linearVelocity.y*1.1
  916.         end
  917.         self:applyForce(vec2(0,-20))
  918.         --Simple foot correction
  919.         local mng = 0
  920.         lt.angularVelocity = lt.mass*((mng-lt.angle)*32-lt.angularVelocity)
  921.         rt.angularVelocity = rt.mass*((mng-rt.angle)*32-rt.angularVelocity)
  922.         if math.cos(self.wt)>0.1 and math.cos(self.wt+math.pi)>0.1 then
  923.             self.wt = wt + 0.1*dir*move
  924.         end
  925.        
  926.         self:walk(self.joff*self.speed*0.2)
  927.     end
  928.     if alive then
  929.         local px = lp.x-(pl.x)
  930.         if px == 0 then px = 1 end
  931.         if math.abs(px)<200 then
  932.             px = sign(px)*200
  933.         end
  934.         if head.angle > 60 then
  935.             head.angle = 60
  936.         end
  937.         if head.angle < -60 then
  938.             head.angle = -60
  939.         end
  940.         local angle = angleOfPoint(vec2(sign(px)*200,lp.y+50-pl.y))+90-100*sign(px)
  941.         while angle > 180 do
  942.             angle = angle - 360
  943.         end
  944.         while angle < -180 do
  945.             angle = angle + 360
  946.         end
  947.         local ang = (angle-head.angle)
  948.         --ply angle normalising
  949.         while ang > 180 do
  950.             ang = ang - 360
  951.         end
  952.         while ang < -180 do
  953.             ang = ang + 360
  954.         end
  955.         head.angularVelocity = (ang*25-head.angularVelocity/10)
  956.         while math.abs(head.angularVelocity)>300 do
  957.             head.angularVelocity = head.angularVelocity*0.9
  958.         end
  959.         --text(math.ceil(ang),head.x,head.y+50)
  960.         --text(math.ceil(head.angularVelocity),head.x,head.y+100)
  961.     end
  962.     --Apply the force luke!
  963.     if alive then
  964.         if fpos then
  965.             pl:applyForce(pl.mass*((fpos-pl.position)*32-pl.linearVelocity/32))
  966.             lt:applyForce(lt.mass*((ltpos-lt.position)*32-lt.linearVelocity))
  967.             rt:applyForce(rt.mass*((rtpos-rt.position)*32-rt.linearVelocity))
  968.         else
  969.             pl:applyForce(pl.mass*(pl.linearVelocity/32))
  970.             lt:applyLinearImpulse(lt.mass*((ltpos-lt.position)*32-lt.linearVelocity)/32)
  971.             rt:applyLinearImpulse(rt.mass*((rtpos-rt.position)*32-rt.linearVelocity)/32)
  972.         end
  973.     end
  974.     --Draw body
  975.     self.lm:setRect(self.lt.info,lt.x,lt.y-2,27,17,ltang)
  976.     self.lm:setRect(self.rt.info,rt.x,rt.y-2,27,17,rtang)
  977.     self.lm:draw()
  978.     self.plym:draw()
  979.     self.headm:draw()
  980.     self.hm:draw()
  981.     if not self.lp then
  982.         local lookpos = sp+vec2(self.mdir,0):normalize()*400
  983.         local dis = self.lookpos:dist(lookpos)
  984.         self:setLookPos(self.lookpos + (lookpos-self.lookpos):normalize()*math.min(dis*0.05*dt,30))
  985.     end
  986.     if alive then
  987.         self:correctFeet()
  988.     end
  989.     if self.mdir~=self.dir then self.mdir = -self.dir end
  990. end
  991.  
  992. function Player:drawJoystick()
  993.     pushStyle()
  994.     local joyp = self.joypos
  995.     local jsize = self.joysize
  996.     tint(255,200)
  997.     sprite("Documents:flatdark",joyp.x,joyp.y,jsize,jsize)
  998.     if self.jactive then
  999.         tint(200,200)
  1000.     else
  1001.         tint(255,255)
  1002.     end
  1003.     sprite("Documents:flatjoy",joyp.x+self.joff.x*(jsize/2),
  1004.     joyp.y+self.joff.y*(jsize/2),jsize*0.6)
  1005.     popStyle()
  1006.     pushStyle()
  1007.     if self.jump then
  1008.         fill(100,40)
  1009.         stroke(50,100)
  1010.         strokeWidth(5)
  1011.     else
  1012.         fill(100,70)
  1013.         stroke(50,70)
  1014.         strokeWidth(3)
  1015.     end
  1016.     ellipse(self.epos.x,self.epos.y,self.esize)
  1017.     popStyle()
  1018. end
  1019.  
  1020. function Player:setLookPos(pos)
  1021.     self.lookpos = pos
  1022.     local dis = (pos-(vec2(self.ply.x,self.ply.y)))
  1023.     if math.abs(dis.x)<10 then
  1024.         self.lookpos.x = pos.x + dis:normalize().x*10
  1025.     end
  1026.     if math.abs(dis.x)>0 then
  1027.         self.dir = (vec2(dis.x,0):normalize().x)
  1028.         self.mdir = self.dir
  1029.         self:flipTex(true)
  1030.         --self.lm.shader.flip = vec2(-self.dir,1)
  1031.     end
  1032.     --end
  1033.     self.rv = 1
  1034. end
  1035.  
  1036. function Player:stopWalk()
  1037.     self.jactive,self.joff,self.vel = false,vec2(),vec2()
  1038. end
  1039.  
  1040. function Player:touched(touch)
  1041.     t = touch[1]
  1042.     local p = vec2(t.x,t.y)+vec2(WIDTH/2,HEIGHT/2)-scr
  1043.     local jpos,jsize = self.joypos,self.joysize
  1044.     local epos,esize = self.epos,self.esize
  1045.     if touch[3] == "ply" then
  1046.         if vec2(t.x,t.y):dist(jpos) < jsize/2 and t.state == BEGAN then
  1047.             self.jactive = true
  1048.             self.joff = (vec2(t.x,t.y)-jpos)/(jsize*0.5)
  1049.         end
  1050.        
  1051.         if self.jactive then
  1052.             self.joff = (vec2(t.x,t.y)-jpos)/(jsize*0.5)
  1053.             if self.joff:len() > 1 then
  1054.                 self.joff = self.joff:normalize()
  1055.             end
  1056.             if t.state == ENDED or t.state == CANCELLED then
  1057.                 self.jactive,self.joff,self.vel = false,vec2(),vec2()
  1058.             end
  1059.         end
  1060.     end
  1061.     if touch[3] == "look" then
  1062.         self:setLookPos(p)
  1063.         self.lp = true
  1064.         if t.state == ENDED then
  1065.             self.lp = false
  1066.         end
  1067.     end
  1068.     if touch[3] == "jump" and not self.tbox then
  1069.         if vec2(t.x,t.y):dist(epos) < esize/2 and t.state == BEGAN then
  1070.             self.jump = vec2(0,100)
  1071.             tween.delay(0.1,function() self.jump = nil end)
  1072.         end
  1073.     end
  1074. end
  1075.  
  1076. --# TMesh
  1077. TMesh = class()
  1078.  
  1079. function TMesh:init(points,width)
  1080.     self.points = points
  1081.     self.width = width+15
  1082.     self.m = mesh()
  1083.     self.p1,self.p2 = nil,nil
  1084.     self.t = mesh()
  1085.     self.mp = {}
  1086.     self.finished = true
  1087.     local p = points
  1088.     local mp = {}
  1089.     local mu = {p[1]-vec2(0,800)}
  1090.     local pl = #p
  1091.     --Create lower ground vertices from given points
  1092.     for i=1,pl do
  1093.         --if (i%2) == 0 or i == pl then
  1094.             mu[math.floor(i)+1] = p[i]+vec2(0,-self.width/3)
  1095.         --end
  1096.     end
  1097.     --Create surface layer two triangles each segment
  1098.     for i=2,pl do
  1099.             table.insert(mp,p[i-1]+vec2(0,-self.width/2))
  1100.             table.insert(mp,p[i]+vec2(0,-self.width/2))
  1101.             table.insert(mp,p[i-1]+vec2(0,25))
  1102.            
  1103.             table.insert(mp,p[i-1]+vec2(0,25))
  1104.             table.insert(mp,p[i]+vec2(0,-self.width/2))
  1105.             table.insert(mp,p[i]+vec2(0,25))
  1106.     end
  1107.     mu[#mu+1] = p[#p]-vec2(0,1500) --This makes sure the lower ground ends far below view
  1108.     self.p1 = mp
  1109.     self.p2 = triangulate(mu)
  1110. end
  1111.  
  1112. function TMesh:draw()
  1113.     self.t:draw()
  1114.     self.m:draw()
  1115.     if self.finished then  
  1116.         self.m.texture = readImage("Cargo Bot:Register Slot") --Surface mesh
  1117.         self.t.texture = readImage("Cargo Bot:Starry Background")
  1118.         self.m.vertices = self.p1
  1119.         self.t.vertices = self.p2
  1120.         self.m:setColors(255,255,255,255)
  1121.         self.t:setColors(255,255,255,255)
  1122.         local tv = {}
  1123.         local gl = 0
  1124.         local p1 = self.p1
  1125.         local seglen = 70
  1126.         --This plots the texture coordinates for the surface, change seglen to change surface image length
  1127.         for i=1,#p1,6 do
  1128.             if p1[i+3] and p1[i+5] then
  1129.             local len = 0
  1130.             local lent = p1[i]:dist(p1[i+1])
  1131.             local lenb = p1[i+3]:dist(p1[i+5])
  1132.             if lenb>lent then len = lenb else len = lent end
  1133.             local texlen = len/seglen
  1134.             tv[i] = vec2(gl,0.1)
  1135.             tv[i+1] = vec2(gl+texlen,0.1)
  1136.             tv[i+2] = vec2(gl,1)
  1137.             tv[i+3] = vec2(gl,1)
  1138.             tv[i+4] = vec2(gl+texlen,0.1)
  1139.             tv[i+5] = vec2(gl+texlen,1)
  1140.             gl = gl + texlen
  1141.             end
  1142.         end
  1143.  
  1144.         self.m.texCoords = tv
  1145.         self.m.shader = shader(ttexshdr.vS,ttexshdr.fS)
  1146.  
  1147.         tv = {}
  1148.         for k,v in pairs(self.p2) do
  1149.             table.insert(tv,vec2(v.x/96,v.y/96))
  1150.         end
  1151.         self.t.texCoords = tv
  1152.         self.t.shader = shader(ttexshdr.vS,ttexshdr.fS)
  1153.         self.finished = nil
  1154.     end
  1155. end
  1156.  
  1157. ttexshdr = {}
  1158. ttexshdr.vS =
  1159. [[
  1160.  
  1161. //This is the current model * view * projection matrix
  1162. // Codea sets it automatically
  1163. uniform mat4 modelViewProjection;
  1164.  
  1165. //This is the current mesh vertex position, color and tex coord
  1166. // Set automatically
  1167. attribute vec4 position;
  1168. attribute vec4 color;
  1169. attribute vec2 texCoord;
  1170.  
  1171. //This is an output variable that will be passed to the fragment shader
  1172. varying lowp vec4 vColor;
  1173. varying highp vec2 vTexCoord;
  1174.  
  1175. void main()
  1176. {
  1177.     //Pass the mesh color to the fragment shader
  1178.     vColor = color;
  1179.     vTexCoord = texCoord;
  1180.    
  1181.     //Multiply the vertex position by our combined transform
  1182.     gl_Position = modelViewProjection * position;
  1183. }
  1184. ]]
  1185. ttexshdr.fS =
  1186. [[
  1187.  
  1188. //Default precision qualifier
  1189. precision highp float;
  1190.  
  1191. //This represents the current texture on the mesh
  1192. uniform lowp sampler2D texture;
  1193.  
  1194. //The interpolated vertex color for this fragment
  1195. varying lowp vec4 vColor;
  1196.  
  1197. //The interpolated texture coordinate for this fragment
  1198. varying highp vec2 vTexCoord;
  1199.  
  1200. void main()
  1201. {
  1202.     //Sample the texture at the interpolated coordinate
  1203.     lowp vec4 col = texture2D( texture, vec2(mod(vTexCoord.x,1.0), mod(vTexCoord.y,1.0))) * vColor;
  1204.  
  1205.  
  1206.     //Set the output color to the texture color
  1207.     gl_FragColor = col;
  1208. }
  1209. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement