Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function round(n, mult)
- mult = mult or 1
- return math.floor((n + mult/2)/mult) * mult
- end
- local PhysicsBody = {}
- function PhysicsBody.__index(self, key)
- return PhysicsBody[key] or self.body[key]
- end
- function PhysicsBody.__newindex(self, key, value)
- self.body[key] = value
- end
- function PhysicsBody:isColliding(other)
- return
- self.body.AbsolutePosition.X + self.body.AbsoluteSize.X > other.body.AbsolutePosition.X and
- other.body.AbsolutePosition.X + other.body.AbsoluteSize.X > self.body.AbsolutePosition.X and
- self.body.AbsolutePosition.Y + self.body.AbsoluteSize.Y > other.body.AbsolutePosition.Y and
- other.body.AbsolutePosition.Y + other.body.AbsoluteSize.Y > self.body.AbsolutePosition.Y
- end
- function PhysicsBody:edges(other)
- return
- math.abs(self.body.AbsolutePosition.X + self.body.AbsoluteSize.X - other.body.AbsolutePosition.X) < 5,
- math.abs(other.body.AbsolutePosition.X + other.body.AbsoluteSize.X - self.body.AbsolutePosition.X) < 5,
- math.abs(self.body.AbsolutePosition.Y + self.body.AbsoluteSize.Y - other.body.AbsolutePosition.Y) < 5,
- math.abs(other.body.AbsolutePosition.Y + other.body.AbsoluteSize.Y - self.body.AbsolutePosition.Y) < 5
- end
- function PhysicsBody:move(udim)
- if type(udim) == "table" then
- self.body.Position = self.body.Position + UDim2.new(udim.sx or 0, udim.x or 0, udim.sy or 0, udim.y or 0)
- else
- self.body.Position = udim
- end
- end
- function PhysicsBody:setGravity(force)
- rawset(self, "GRAVITY_FORCE", force)
- rawset(self, "pull", force)
- end
- function PhysicsBody:applyGravity()
- if self.GRAVITY_FORCE then
- self.pull = self.pull + self.GRAVITY_FORCE
- end
- self:move{y = self.pull}
- end
- function PhysicsBody:jump()
- coroutine.wrap(function()
- self:spin(180)
- rawset(self, "isJumping", true)
- wait(0.2)
- rawset(self, "isJumping", false)
- end)()
- end
- function PhysicsBody:spin(degrees)
- coroutine.wrap(function()
- if rawget(self, "spinning") then
- repeat wait() until not rawget(self, "spinning")
- end
- rawset(self, "spinning", true)
- for i = 1, degrees, 8 do
- if self then
- self.body.Rotation = self.body.Rotation + 8
- wait()
- end
- end
- self.body.Rotation = round(self.body.Rotation, degrees)
- rawset(self, "spinning", false)
- end)()
- end
- function PhysicsBody:destroy()
- rawget(self, "body"):Destroy()
- self = nil
- end
- function PhysicsBody.new(properties)
- properties.position[4] = properties.position[4] - 75
- local body = Instance.new("TextLabel", script.Parent:WaitForChild("ScreenGui"):WaitForChild("GameScreen"))
- body.Size = UDim2.new(unpack(properties.size))
- body.Position = UDim2.new(unpack(properties.position))
- body.Text = ""
- body.ZIndex = 3
- if properties.airJump then
- coroutine.wrap(function()
- while wait() do
- if body then
- body.Rotation = body.Rotation + 3
- end
- end
- end)()
- end
- for i, v in pairs(properties.flags or {}) do
- body[i] = v
- end
- return setmetatable({
- height = properties.height or 0,
- elevator = properties.elevator,
- airJump = properties.airJump,
- superJumper = properties.superJumper,
- jumper = properties.jumper,
- finish = properties.finish,
- fake = properties.fake,
- hasBeenHit = false,
- body = body,
- hit = properties.hit or function() end
- }, PhysicsBody)
- end
- setmetatable(PhysicsBody, {__call = function(self, properties) return self.new(properties) end})
- return PhysicsBody
Advertisement
Add Comment
Please, Sign In to add comment