Advertisement
Guest User

Untitled

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