Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --PHYSICS ENGINE
- local downForce = {}
- local physics = {
- collision = function(body1, body2)
- return(
- {
- hit = body1.AbsolutePosition.X + body1.AbsoluteSize.X > body2.AbsolutePosition.X and
- body2.AbsolutePosition.X + body2.AbsoluteSize.X > body1.AbsolutePosition.X and
- body1.AbsolutePosition.Y + body1.AbsoluteSize.Y > body2.AbsolutePosition.Y and
- body2.AbsolutePosition.Y + body2.AbsoluteSize.Y > body1.AbsolutePosition.Y,
- side = {
- left = body2.AbsolutePosition.X + body2.AbsoluteSize.X - body1.AbsolutePosition.X < 5,
- right = body1.AbsolutePosition.X + body1.AbsoluteSize.X - body2.AbsolutePosition.X < 5,
- top = body2.AbsolutePosition.Y + body2.AbsoluteSize.Y - body1.AbsolutePosition.Y < 5,
- bottom = body1.AbsolutePosition.Y + body1.AbsoluteSize.Y - body2.AbsolutePosition.Y < 5
- }
- }
- )
- end,
- updateGravity = function(mode, name, ...)
- if not downForce[name] then
- downForce[name] = 0
- end
- local args = unpack({...})
- if mode == "increase" then
- downForce[name] = downForce[name] + args.inc
- elseif mode == "reset" then
- downForce[name] = 0
- elseif mode == "jump" then
- downForce[name] = -10
- end
- return downForce[name]
- end
- }
- return physics
- --ACTUAL GAME STARTS HERE
- game.StarterGui:SetCoreGuiEnabled("All", false)
- local assets = {162359401, 85926303, 36527138}
- for i, v in pairs(assets) do
- game:GetService("ContentProvider"):Preload("rbxassetid://" .. tostring(v))
- end
- local player = game.Players.LocalPlayer
- repeat wait() until player.Character player.Character:Destroy()
- local mouse = player:GetMouse()
- local objects = script.Parent:WaitForChild("GameObjects")
- local physics = require(Workspace.Physics)
- local anchor = false
- local anchorPos;
- local isDown = {}
- local bullets = {}
- local enemies = {}
- local enemyBullets = {}
- local count = 0
- local score = 0
- local limit = 200
- physics.gravity = 5
- local player = {
- body = objects:WaitForChild("Player"),
- canJump = true,
- speed = 5,
- bulletSpeed = 10,
- upForce, downForce = 0, 0,
- lastSpot, lastShot;
- }
- function spawnBullet(side)
- if player.lastShot then
- if tick() - player.lastShot < 0.2 then
- return
- end
- end
- player.lastShot = tick()
- local velocity = side == "left" and -player.bulletSpeed or player.bulletSpeed
- local bullet = Instance.new("ImageLabel", objects)
- bullet.Size = UDim2.new(0, 50, 0, 10)
- bullet.Position = UDim2.new(
- player.body.Position.X.Scale,
- player.body.Position.X.Offset + (velocity * 6),
- player.body.Position.Y.Scale,
- player.body.Position.Y.Offset + ((player.body.Size.Y.Offset / 2) - (bullet.Size.Y.Offset / 2))
- )
- bullet.BackgroundTransparency = 1
- bullet.Image = "rbxassetid://85926303"
- bullet.ZIndex = 3
- table.insert(bullets, {body = bullet, speed = velocity, move = true, touch = function(hit)
- if hit.body.Name:find("Enemy") then
- hit.dead = true
- hit.body:Destroy()
- for i, v in pairs(enemies) do
- if v.body.Name == hit.body.Name then
- table.remove(enemies, i)
- break
- end
- end
- end
- end
- })
- end
- function spawnEnemyBullet(side, enemy)
- local velocity = side == "left" and 10 or -10
- local bullet = Instance.new("ImageLabel", objects)
- bullet.Size = UDim2.new(0, 50, 0, 10)
- bullet.Position = UDim2.new(
- enemy.body.Position.X.Scale,
- enemy.body.Position.X.Offset + (velocity * 6),
- enemy.body.Position.Y.Scale,
- enemy.body.Position.Y.Offset + ((enemy.body.Size.Y.Offset / 2) - (bullet.Size.Y.Offset / 2))
- )
- bullet.BackgroundTransparency = 1
- bullet.Image = "rbxassetid://36527138"
- bullet.ZIndex = 3
- table.insert(enemyBullets, {body = bullet, speed = velocity, move = true, touch = function(hit)
- if hit.body.Name == "Player" then
- hit.body:Destroy()
- wait(2)
- game:GetService("TeleportService"):Teleport(145098778)
- end
- end
- })
- end
- mouse.KeyDown:connect(function(key)
- isDown[key:lower()] = {
- true,
- key:lower() == "a" and UDim2.new(0, -player.speed, 0, 0) or
- key:lower() == "d" and UDim2.new(0, player.speed, 0, 0)
- }
- if key:byte() == 32 and player.canJump then
- anchor = false
- player.downForce = physics.updateGravity("jump", player.body.Name)
- elseif key:lower() == "l" then
- spawnBullet("right")
- elseif key:lower() == "k" then
- spawnBullet("left")
- end
- end)
- mouse.KeyUp:connect(function(key)
- isDown[key:lower()][1] = false
- end)
- local physicsBodies = {
- {body = objects:WaitForChild("Ground")},
- {body = objects:WaitForChild("Plat1")},
- {body = objects:WaitForChild("Plat2")},
- {body = script.Parent:WaitForChild("Wall1")},
- {body = script.Parent:WaitForChild("Wall2")},
- {body = script.Parent:WaitForChild("Wall4")},
- }
- function explode(bullet)
- coroutine.wrap(function()
- bullet.move = false
- bullet.body.Image = "rbxassetid://162359401"
- local size = bullet.body.Size
- local position = bullet.body.Position
- for x = 1, 50, 5 do
- wait()
- bullet.body.Size = size + UDim2.new(0, x, 0, x+3)
- bullet.body.Position = position - UDim2.new(0, x/2, 0, (x/2)+1.5)
- end
- bullet.body:Destroy()
- end)()
- end
- game:GetService("RunService").RenderStepped:connect(function()
- --handle physics and player movement
- for _, v in pairs(isDown) do
- if v[1] and v[2] then
- player.body.Position = player.body.Position + v[2]
- end
- end
- local gotHit;
- for _, object in pairs(physicsBodies) do
- local collision = physics.collision(object.body, player.body)
- local ignore;
- if collision.hit then
- if not collision.side.left and not collision.side.right then
- gotHit = true
- end
- if collision.side.top then
- anchor = true
- anchorPos = {object.body.Position.Y.Scale, object.body.Position.Y.Offset}
- end
- if not ignore then
- if player.lastSpot then
- player.body.Position = player.lastSpot
- end
- end
- end
- end
- if not gotHit then
- player.canJump = false
- player.lastSpot = player.body.Position
- player.downForce = physics.updateGravity("increase", player.body.Name, {inc = 0.5})
- else
- player.canJump = true
- player.downForce = physics.updateGravity("reset", player.body.Name)
- end
- if anchor then
- player.body.Position = UDim2.new(
- player.body.Position.X.Scale,
- player.body.Position.X.Offset,
- anchorPos[1],
- anchorPos[2] - player.body.Size.Y.Offset
- )
- physics.updateGravity("reset", player.body.Name)
- player.canJump = true
- end
- player.body.Position = player.body.Position + UDim2.new(0, 0, 0, player.downForce)
- --handle bullets
- for i, bullet in pairs(bullets) do
- if bullet.body and bullet.move then
- bullet.body.Position = bullet.body.Position + UDim2.new(0, bullet.speed, 0, 0)
- for _, object in pairs(physicsBodies) do
- if physics.collision(bullet.body, object.body).hit then
- explode(bullet)
- end
- end
- for _, enemy in pairs(enemies) do
- if physics.collision(bullet.body, enemy.body).hit then
- explode(bullet)
- bullet.touch(enemy)
- score = score + 1
- script.Parent:WaitForChild("Score").Text = score
- end
- end
- end
- end
- --handle enemies and enemy physics
- count = count + 1
- if count % limit == 0 then
- limit = limit - 1
- print("spawned enemy #" .. count)
- local enemy = player.body:clone()
- enemy.Parent = objects
- enemy.Name = "Enemy" .. tostring(count)
- enemy.BackgroundColor3 = Color3.new(1, 1, 1)
- enemy.BorderColor3 = Color3.new(1, 0, 0)
- enemy.Position = UDim2.new(0, math.random(60, 680), 0, -50)
- table.insert(enemies, {
- body = enemy,
- downForce = 0,
- lastSpot, hasLanded, dead;
- })
- end
- for _, enemy in pairs(enemies) do
- if enemy.body then
- local gotHit;
- enemy.lastSpot = enemy.body.Position
- enemy.downForce = physics.updateGravity("increase", enemy.body.Name, {inc = 0.1})
- for _, object in pairs(physicsBodies) do
- local collision = physics.collision(object.body, enemy.body)
- if collision.hit then
- gotHit = true
- if collision.side.top then
- enemy.body.Position = UDim2.new(
- enemy.body.Position.X.Scale,
- enemy.body.Position.X.Offset,
- object.body.Position.Y.Scale,
- object.body.Position.Y.Offset - enemy.body.Size.Y.Offset
- )
- end
- enemy.body.Position = enemy.lastSpot
- enemy.hasLanded = true
- end
- end
- if not gotHit then
- enemy.body.Position = enemy.body.Position + UDim2.new(0, 0, 0, enemy.downForce)
- end
- if enemy.hasLanded and not enemy["IS_SHOOTING"] then
- enemy["IS_SHOOTING"] = true
- coroutine.wrap(function()
- while wait(math.random(1, 5)) do
- if enemy.dead or not enemy.body then break end
- spawnEnemyBullet(math.random() < 0.5 and "left" or "right", enemy)
- end
- end)()
- end
- end
- end
- --handle enemy bullets
- for i, bullet in pairs(enemyBullets) do
- if bullet.move and bullet.body then
- bullet.body.Position = bullet.body.Position + UDim2.new(0, bullet.speed, 0, 0)
- for _, object in pairs(physicsBodies) do
- if physics.collision(bullet.body, object.body).hit then
- explode(bullet)
- end
- end
- if physics.collision(bullet.body, player.body).hit then
- explode(bullet)
- bullet.touch(player)
- end
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment