ForeverDev

Untitled

Sep 7th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.06 KB | None | 0 0
  1. --PHYSICS ENGINE
  2.  
  3. local downForce = {}
  4.  
  5. local physics = {
  6.     collision = function(body1, body2)
  7.         return(
  8.             {
  9.                 hit =   body1.AbsolutePosition.X + body1.AbsoluteSize.X > body2.AbsolutePosition.X and
  10.                         body2.AbsolutePosition.X + body2.AbsoluteSize.X > body1.AbsolutePosition.X and
  11.                         body1.AbsolutePosition.Y + body1.AbsoluteSize.Y > body2.AbsolutePosition.Y and
  12.                         body2.AbsolutePosition.Y + body2.AbsoluteSize.Y > body1.AbsolutePosition.Y,
  13.                            
  14.                 side = {
  15.                     left =      body2.AbsolutePosition.X + body2.AbsoluteSize.X - body1.AbsolutePosition.X < 5,
  16.                     right =     body1.AbsolutePosition.X + body1.AbsoluteSize.X - body2.AbsolutePosition.X < 5,
  17.                     top =       body2.AbsolutePosition.Y + body2.AbsoluteSize.Y - body1.AbsolutePosition.Y < 5,
  18.                     bottom =    body1.AbsolutePosition.Y + body1.AbsoluteSize.Y - body2.AbsolutePosition.Y < 5
  19.                 }
  20.             }
  21.         )
  22.     end,
  23.     updateGravity = function(mode, name, ...)
  24.         if not downForce[name] then
  25.             downForce[name] = 0
  26.         end
  27.         local args = unpack({...})
  28.         if mode == "increase" then
  29.             downForce[name] = downForce[name] + args.inc
  30.         elseif mode == "reset" then
  31.             downForce[name] = 0
  32.         elseif mode == "jump" then
  33.             downForce[name] = -10
  34.         end
  35.         return downForce[name]
  36.     end
  37. }
  38.  
  39. return physics
  40.  
  41. --ACTUAL GAME STARTS HERE
  42.  
  43. game.StarterGui:SetCoreGuiEnabled("All", false)
  44.  
  45. local assets = {162359401, 85926303, 36527138}
  46. for i, v in pairs(assets) do
  47.     game:GetService("ContentProvider"):Preload("rbxassetid://" .. tostring(v))
  48. end
  49.  
  50. local player = game.Players.LocalPlayer
  51. repeat wait() until player.Character player.Character:Destroy()
  52. local mouse = player:GetMouse()
  53. local objects = script.Parent:WaitForChild("GameObjects")
  54. local physics = require(Workspace.Physics)
  55. local anchor = false
  56. local anchorPos;
  57. local isDown = {}
  58. local bullets = {}
  59. local enemies = {}
  60. local enemyBullets = {}
  61. local count = 0
  62. local score = 0
  63. local limit = 200
  64.  
  65. physics.gravity = 5
  66.  
  67. local player = {
  68.     body = objects:WaitForChild("Player"),
  69.     canJump = true,
  70.     speed = 5,
  71.     bulletSpeed = 10,
  72.     upForce, downForce = 0, 0,
  73.     lastSpot, lastShot;
  74. }
  75.  
  76. function spawnBullet(side)
  77.     if player.lastShot then
  78.         if tick() - player.lastShot < 0.2 then
  79.             return
  80.         end
  81.     end
  82.     player.lastShot = tick()
  83.     local velocity = side == "left" and -player.bulletSpeed or player.bulletSpeed
  84.     local bullet = Instance.new("ImageLabel", objects)
  85.     bullet.Size = UDim2.new(0, 50, 0, 10)
  86.     bullet.Position = UDim2.new(
  87.         player.body.Position.X.Scale,
  88.         player.body.Position.X.Offset + (velocity * 6),
  89.         player.body.Position.Y.Scale,
  90.         player.body.Position.Y.Offset + ((player.body.Size.Y.Offset / 2) - (bullet.Size.Y.Offset / 2))
  91.     )
  92.     bullet.BackgroundTransparency = 1
  93.     bullet.Image = "rbxassetid://85926303"
  94.     bullet.ZIndex = 3
  95.     table.insert(bullets, {body = bullet, speed = velocity, move = true, touch = function(hit)
  96.         if hit.body.Name:find("Enemy") then
  97.             hit.dead = true
  98.             hit.body:Destroy()
  99.             for i, v in pairs(enemies) do
  100.                 if v.body.Name == hit.body.Name then
  101.                     table.remove(enemies, i)
  102.                     break
  103.                 end
  104.             end
  105.         end
  106.     end
  107.     })
  108. end
  109.  
  110. function spawnEnemyBullet(side, enemy)
  111.     local velocity = side == "left" and 10 or -10
  112.     local bullet = Instance.new("ImageLabel", objects)
  113.     bullet.Size = UDim2.new(0, 50, 0, 10)
  114.     bullet.Position = UDim2.new(
  115.         enemy.body.Position.X.Scale,
  116.         enemy.body.Position.X.Offset + (velocity * 6),
  117.         enemy.body.Position.Y.Scale,
  118.         enemy.body.Position.Y.Offset + ((enemy.body.Size.Y.Offset / 2) - (bullet.Size.Y.Offset / 2))
  119.     )
  120.     bullet.BackgroundTransparency = 1
  121.     bullet.Image = "rbxassetid://36527138"
  122.     bullet.ZIndex = 3
  123.     table.insert(enemyBullets, {body = bullet, speed = velocity, move = true, touch = function(hit)
  124.         if hit.body.Name == "Player" then
  125.             hit.body:Destroy()
  126.             wait(2)
  127.             game:GetService("TeleportService"):Teleport(145098778)
  128.         end
  129.     end
  130.     })
  131. end
  132.  
  133. mouse.KeyDown:connect(function(key)
  134.     isDown[key:lower()] = {
  135.         true,
  136.         key:lower() == "a" and UDim2.new(0, -player.speed, 0, 0) or
  137.         key:lower() == "d" and UDim2.new(0, player.speed, 0, 0)
  138.     }
  139.     if key:byte() == 32 and player.canJump then
  140.         anchor = false
  141.         player.downForce = physics.updateGravity("jump", player.body.Name)
  142.     elseif key:lower() == "l" then
  143.         spawnBullet("right")
  144.     elseif key:lower() == "k" then
  145.         spawnBullet("left")
  146.     end
  147. end)
  148.  
  149. mouse.KeyUp:connect(function(key)
  150.     isDown[key:lower()][1] = false
  151. end)
  152.  
  153. local physicsBodies = {
  154.     {body = objects:WaitForChild("Ground")},
  155.     {body = objects:WaitForChild("Plat1")},
  156.     {body = objects:WaitForChild("Plat2")},
  157.     {body = script.Parent:WaitForChild("Wall1")},
  158.     {body = script.Parent:WaitForChild("Wall2")},
  159.     {body = script.Parent:WaitForChild("Wall4")},
  160. }
  161.  
  162. function explode(bullet)
  163.     coroutine.wrap(function()
  164.         bullet.move = false
  165.         bullet.body.Image = "rbxassetid://162359401"
  166.         local size = bullet.body.Size
  167.         local position = bullet.body.Position
  168.         for x = 1, 50, 5 do
  169.             wait()
  170.             bullet.body.Size = size + UDim2.new(0, x, 0, x+3)
  171.             bullet.body.Position = position - UDim2.new(0, x/2, 0, (x/2)+1.5)
  172.         end
  173.         bullet.body:Destroy()
  174.     end)()
  175. end
  176.  
  177. game:GetService("RunService").RenderStepped:connect(function()
  178.     --handle physics and player movement
  179.     for _, v in pairs(isDown) do
  180.         if v[1] and v[2] then
  181.             player.body.Position = player.body.Position + v[2]
  182.         end
  183.     end
  184.     local gotHit;
  185.     for _, object in pairs(physicsBodies) do
  186.         local collision = physics.collision(object.body, player.body)
  187.         local ignore;
  188.         if collision.hit then
  189.             if not collision.side.left and not collision.side.right then
  190.                 gotHit = true
  191.             end
  192.             if collision.side.top then
  193.                 anchor = true
  194.                 anchorPos = {object.body.Position.Y.Scale, object.body.Position.Y.Offset}
  195.             end
  196.             if not ignore then
  197.                 if player.lastSpot then
  198.                     player.body.Position = player.lastSpot
  199.                 end
  200.             end
  201.         end
  202.     end
  203.     if not gotHit then
  204.         player.canJump = false
  205.         player.lastSpot = player.body.Position
  206.         player.downForce = physics.updateGravity("increase", player.body.Name, {inc = 0.5})
  207.     else
  208.         player.canJump = true
  209.         player.downForce = physics.updateGravity("reset", player.body.Name)
  210.     end
  211.     if anchor then
  212.         player.body.Position = UDim2.new(
  213.             player.body.Position.X.Scale,
  214.             player.body.Position.X.Offset,
  215.             anchorPos[1],
  216.             anchorPos[2] - player.body.Size.Y.Offset
  217.         )
  218.         physics.updateGravity("reset", player.body.Name)
  219.         player.canJump = true
  220.     end
  221.     player.body.Position = player.body.Position + UDim2.new(0, 0, 0, player.downForce)
  222.    
  223.     --handle bullets
  224.     for i, bullet in pairs(bullets) do
  225.         if bullet.body and bullet.move then
  226.             bullet.body.Position = bullet.body.Position + UDim2.new(0, bullet.speed, 0, 0)
  227.             for _, object in pairs(physicsBodies) do
  228.                 if physics.collision(bullet.body, object.body).hit then
  229.                     explode(bullet)
  230.                 end
  231.             end
  232.             for _, enemy in pairs(enemies) do
  233.                 if physics.collision(bullet.body, enemy.body).hit then
  234.                     explode(bullet)
  235.                     bullet.touch(enemy)
  236.                     score = score + 1
  237.                     script.Parent:WaitForChild("Score").Text = score
  238.                 end
  239.             end
  240.         end
  241.     end
  242.    
  243.     --handle enemies and enemy physics
  244.     count = count + 1
  245.     if count % limit == 0 then
  246.         limit = limit - 1
  247.         print("spawned enemy #" .. count)
  248.         local enemy = player.body:clone()
  249.         enemy.Parent = objects
  250.         enemy.Name = "Enemy" .. tostring(count)
  251.         enemy.BackgroundColor3 = Color3.new(1, 1, 1)
  252.         enemy.BorderColor3 = Color3.new(1, 0, 0)
  253.         enemy.Position = UDim2.new(0, math.random(60, 680), 0, -50)
  254.         table.insert(enemies, {
  255.             body = enemy,
  256.             downForce = 0,
  257.             lastSpot, hasLanded, dead;
  258.         })
  259.     end
  260.     for _, enemy in pairs(enemies) do
  261.         if enemy.body then
  262.             local gotHit;
  263.             enemy.lastSpot = enemy.body.Position
  264.             enemy.downForce = physics.updateGravity("increase", enemy.body.Name, {inc = 0.1})
  265.             for _, object in pairs(physicsBodies) do
  266.                 local collision = physics.collision(object.body, enemy.body)
  267.                 if collision.hit then
  268.                     gotHit = true
  269.                     if collision.side.top then
  270.                         enemy.body.Position = UDim2.new(
  271.                             enemy.body.Position.X.Scale,
  272.                             enemy.body.Position.X.Offset,
  273.                             object.body.Position.Y.Scale,
  274.                             object.body.Position.Y.Offset - enemy.body.Size.Y.Offset
  275.                         )
  276.                     end
  277.                     enemy.body.Position = enemy.lastSpot
  278.                     enemy.hasLanded = true
  279.                 end
  280.             end
  281.             if not gotHit then
  282.                 enemy.body.Position = enemy.body.Position + UDim2.new(0, 0, 0, enemy.downForce)
  283.             end
  284.             if enemy.hasLanded and not enemy["IS_SHOOTING"] then
  285.                 enemy["IS_SHOOTING"] = true
  286.                 coroutine.wrap(function()
  287.                     while wait(math.random(1, 5)) do
  288.                         if enemy.dead or not enemy.body then break end
  289.                         spawnEnemyBullet(math.random() < 0.5 and "left" or "right", enemy)
  290.                     end
  291.                 end)()
  292.             end
  293.         end
  294.     end
  295.    
  296.     --handle enemy bullets
  297.     for i, bullet in pairs(enemyBullets) do
  298.         if bullet.move and bullet.body then
  299.             bullet.body.Position = bullet.body.Position + UDim2.new(0, bullet.speed, 0, 0)
  300.             for _, object in pairs(physicsBodies) do
  301.                 if physics.collision(bullet.body, object.body).hit then
  302.                     explode(bullet)
  303.                 end
  304.             end
  305.             if physics.collision(bullet.body, player.body).hit then
  306.                 explode(bullet)
  307.                 bullet.touch(player)
  308.             end
  309.         end
  310.     end
  311. end)
Advertisement
Add Comment
Please, Sign In to add comment