ForeverDev

Untitled

Nov 12th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. function round(n, mult)
  2.     mult = mult or 1
  3.     return math.floor((n + mult/2)/mult) * mult
  4. end
  5.  
  6. local PhysicsBody = {}
  7.  
  8. function PhysicsBody.__index(self, key)
  9.     return PhysicsBody[key] or self.body[key]
  10. end
  11.  
  12. function PhysicsBody.__newindex(self, key, value)
  13.     self.body[key] = value 
  14. end
  15.  
  16. function PhysicsBody:isColliding(other)
  17.     return
  18.         self.body.AbsolutePosition.X + self.body.AbsoluteSize.X > other.body.AbsolutePosition.X and
  19.         other.body.AbsolutePosition.X + other.body.AbsoluteSize.X > self.body.AbsolutePosition.X and
  20.         self.body.AbsolutePosition.Y + self.body.AbsoluteSize.Y > other.body.AbsolutePosition.Y and
  21.         other.body.AbsolutePosition.Y + other.body.AbsoluteSize.Y > self.body.AbsolutePosition.Y
  22.    
  23. end
  24.  
  25. function PhysicsBody:edges(other)
  26.     return
  27.         math.abs(self.body.AbsolutePosition.X + self.body.AbsoluteSize.X - other.body.AbsolutePosition.X) < 5,
  28.         math.abs(other.body.AbsolutePosition.X + other.body.AbsoluteSize.X - self.body.AbsolutePosition.X) < 5,
  29.         math.abs(self.body.AbsolutePosition.Y + self.body.AbsoluteSize.Y - other.body.AbsolutePosition.Y) < 5,
  30.         math.abs(other.body.AbsolutePosition.Y + other.body.AbsoluteSize.Y - self.body.AbsolutePosition.Y) < 5
  31.    
  32. end
  33.  
  34. function PhysicsBody:move(udim)
  35.     if type(udim) == "table" then
  36.         self.body.Position = self.body.Position + UDim2.new(udim.sx or 0, udim.x or 0, udim.sy or 0, udim.y or 0)
  37.     else
  38.         self.body.Position = udim
  39.     end
  40. end
  41.  
  42. function PhysicsBody:setGravity(force)
  43.     rawset(self, "GRAVITY_FORCE", force)
  44.     rawset(self, "pull", force)
  45. end
  46.  
  47. function PhysicsBody:applyGravity()
  48.     if self.GRAVITY_FORCE then
  49.         self.pull = self.pull + self.GRAVITY_FORCE
  50.     end
  51.     self:move{y = self.pull}
  52. end
  53.  
  54. function PhysicsBody:jump()
  55.     coroutine.wrap(function()
  56.         self:spin(180)
  57.         rawset(self, "isJumping", true)
  58.         wait(0.2)
  59.         rawset(self, "isJumping", false)
  60.     end)()
  61. end
  62.  
  63. function PhysicsBody:spin(degrees)
  64.     coroutine.wrap(function()
  65.         if rawget(self, "spinning") then
  66.             repeat wait() until not rawget(self, "spinning")
  67.         end
  68.         rawset(self, "spinning", true)
  69.         for i = 1, degrees, 8 do
  70.             if self then
  71.                 self.body.Rotation = self.body.Rotation + 8
  72.                 wait()
  73.             end
  74.         end
  75.         self.body.Rotation = round(self.body.Rotation, degrees)
  76.         rawset(self, "spinning", false)
  77.     end)()
  78. end
  79.  
  80. function PhysicsBody:destroy()
  81.     rawget(self, "body"):Destroy()
  82.     self = nil
  83. end
  84.  
  85. function PhysicsBody.new(properties)
  86.     properties.position[4] = properties.position[4] - 75
  87.     local body = Instance.new("TextLabel", script.Parent:WaitForChild("ScreenGui"):WaitForChild("GameScreen"))
  88.     body.Size = UDim2.new(unpack(properties.size))
  89.     body.Position = UDim2.new(unpack(properties.position))
  90.     body.Text = ""
  91.     body.ZIndex = 3
  92.     if properties.airJump then
  93.         coroutine.wrap(function()
  94.             while wait() do
  95.                 if body then
  96.                     body.Rotation = body.Rotation + 3
  97.                 end
  98.             end
  99.         end)()
  100.     end
  101.     for i, v in pairs(properties.flags or {}) do
  102.         body[i] = v
  103.     end
  104.     return setmetatable({
  105.         height = properties.height or 0,
  106.         elevator = properties.elevator,
  107.         airJump = properties.airJump,
  108.         superJumper = properties.superJumper,
  109.         jumper = properties.jumper,
  110.         finish = properties.finish,
  111.         fake = properties.fake,
  112.         hasBeenHit = false,
  113.         body = body,
  114.         hit = properties.hit or function() end
  115.     }, PhysicsBody)
  116. end
  117.  
  118. setmetatable(PhysicsBody, {__call = function(self, properties) return self.new(properties) end})
  119.  
  120. return PhysicsBody
Advertisement
Add Comment
Please, Sign In to add comment