Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local world = {}
- world.__index = world
- local runService = game:GetService("RunService")
- local function getPoints(Obj)
- local top = Obj.Position + UDim2.new(0,0,0,Obj.Size.Y.Offset/2)
- local bottom = Obj.Position - UDim2.new(0,0,0,Obj.Size.Y.Offset/2)
- local left = Obj.Position - UDim2.new(0,Obj.Size.X.Offset/2,0,0)
- local right = Obj.Position + UDim2.new(0,Obj.Size.X.Offset/2,0,0)
- return {
- ["Top"] = top,
- ["Bottom"] = bottom,
- ["Left"] = left,
- ["Right"] = right
- }
- end
- function world.new()
- return setmetatable({
- _Thread = nil,
- _World = {},
- _Yield = false,
- Colliding = {}
- },world)
- end
- function world:GetObjects()
- return self._World
- end
- function world:GetCollisions()
- return self.Colliding
- end
- function world:Pause()
- self._Yield = true
- end
- function world:Run()
- self._Yield = false
- if not self._Thread then
- self._Thread = coroutine.create(function()
- runService.RenderStepped:Connect(function()
- if self._Yield then
- coroutine.yield()
- end
- for _,obj in pairs(self._World) do
- if not self.Colliding[obj] then
- self.Colliding[obj] = {}
- end
- for _,anObj in pairs(self._World) do
- if anObj ~= obj then
- local oP = getPoints(obj)
- local aOP = getPoints(anObj)
- if aOP.Top.Y.Offset >= oP.Bottom.Y.Offset and aOP.Top.Y.Offset <= oP.Top.Y.Offset or aOP.Bottom.Y.Offset <= oP.Bottom.Y.Offset and aOP.Bottom.Y.Offset >= oP.Top.Y.Offset then
- if aOP.Right.X.Offset >= oP.Left.X.Offset and aOP.Right.X.Offset <= oP.Right.X.Offset or aOP.Left.X.Offset <= oP.Right.X.Offset and aOP.Left.X.Offset >= oP.Left.X.Offset then
- if not table.find(self.Colliding[obj], anObj) then
- table.insert(self.Colliding[obj], anObj)
- end
- end
- end
- end
- end
- end
- end)
- end)
- end
- coroutine.resume(self._Thread)
- end
- function world:RemoveObject(obj)
- local pos = table.find(self._World, obj)
- table.remove(self._World, pos)
- obj:Destroy()
- end
- local object = {}
- object.__index = object
- function world:AddObject(Size)
- local obj = Instance.new("ImageLabel")
- obj.Size = Size
- obj.AnchorPoint = Vector2.new(0.5,0.5)
- obj.Name = "Object"..#self._World+1
- self._World[#self._World+1] = obj
- return setmetatable({
- Object = obj
- },object)
- end
- function object:Destroy()
- local pos = table.find(self._World, self.Object)
- table.remove(self._World, pos)
- self.Object:Destroy()
- self.Object = nil
- end
- return world
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement