GauHelldragon

Spaceshooty

Feb 22nd, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.73 KB | None | 0 0
  1.  
  2. function love.load()
  3.     images = {
  4.         ship1 = love.graphics.newImage("ship1.png"),
  5.         ship2 = love.graphics.newImage("ship2.png"),
  6.         laser = love.graphics.newImage("laser.png"),
  7.         rock = love.graphics.newImage("rock.png")
  8.     }
  9.    
  10.     love.graphics.setBackgroundColor(0,0,0)
  11.     love.graphics.setPointStyle("smooth")
  12.     myFont = love.graphics.newFont(20)
  13.     love.graphics.setFont(myFont)
  14.     SetupParticles()
  15.    
  16.    
  17.     world = love.physics.newWorld(0, 0)
  18.     world:setCallbacks(beginContact, endContact, preSolve, postSolve)
  19.    
  20.     shipBody = love.physics.newBody(world, 400,300, "dynamic")
  21.     shipBody:setLinearDamping(0.1)
  22.     shipShape = love.physics.newRectangleShape(31, 34)
  23.     shipFix = love.physics.newFixture(shipBody, shipShape)
  24.     shipFix:setCategory(3)
  25.     shipFix:setMask(2)
  26.     rockets = 0
  27.     destroyRock = 0
  28.     destroyLaser = 0
  29.     countdown = -1
  30.     level = 0
  31.     dead = 0
  32.     stars = {}   -- table which will hold our stars
  33.     max_stars = 100   -- how many stars we want
  34.     for i=1, max_stars do   -- generate the coords of our stars
  35.         local x = math.random(5, love.graphics.getWidth()-5) + 0.5  -- generate a "random" number for the x coord of this star
  36.         local y = math.random(5, love.graphics.getHeight()-5) + 0.5  -- both coords are limited to the screen size, minus 5 pixels of padding
  37.         local c = math.random(100,255)
  38.         stars[i] = {x, y,c}   -- stick the values into the table
  39.     end
  40.     make_rocks(5)
  41. end
  42.    
  43. function love.draw()
  44.     for i=1, #stars do   -- loop through all of our stars
  45.         local c = stars[i][3]
  46.         love.graphics.setColor(c,c,c)
  47.         love.graphics.point(stars[i][1], stars[i][2])   -- draw each point
  48.     end
  49.     love.graphics.setColor(255,255,255)
  50.    
  51.     for i, v in ipairs(lasers) do
  52.         love.graphics.draw(images.laser,v.b:getX(),v.b:getY(),v.b:getAngle(),1,1,15,1)
  53.         --local v1,v2,v3,v4 = v.f:getBoundingBox()
  54.         --love.graphics.rectangle("line",v1,v2,v3-v1,v4-v2)
  55.     end
  56.  
  57.     for i, v in ipairs(rocks) do
  58.         love.graphics.draw(images.rock,v.body:getX(),v.body:getY(),v.body:getAngle(),v.size,v.size,v.ox,v.oy)
  59.         --local v1,v2,v3,v4 = v.fix:getBoundingBox()
  60.         --love.graphics.rectangle("line",v1,v2,v3-v1,v4-v2)
  61.        
  62.     end
  63.  
  64.    
  65.     if dead == 0 then
  66.         local shipImage = images.ship1
  67.         if rockets == 1 then
  68.             shipImage = images.ship2
  69.         end    
  70.         love.graphics.draw(shipImage, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 15, 17)
  71.     else
  72.         love.graphics.print("You are dead!",350,200)
  73.         if dead > 4 then   
  74.             love.graphics.print("Press Space to restart.",310,230)
  75.         end
  76.     end
  77.    
  78.     love.graphics.draw(rockParticles,0,0)
  79.     love.graphics.draw(bigRocks,0,0)
  80.     love.graphics.draw(shipBoom,0,0)
  81.    
  82.     if ( countdown > 0 ) then
  83.         love.graphics.print("Prepare for next level!",300,200)
  84.            
  85.     end
  86.    
  87. --  if rockets == 1 then
  88.     --  love.graphics.draw(images.ship2, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 17, 15)
  89. --  else
  90. --      love.graphics.draw(images.ship1, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 17, 15)
  91. --  end
  92. end
  93.  
  94. laser_cooldown = 0
  95. engine_cooldown = 0
  96. lasers = {}
  97. rocks = {}
  98.  
  99. function fire_laser()
  100.     local laser = {}
  101.     local x = shipBody:getX()
  102.     local y = shipBody:getY()
  103.     local a = shipBody:getAngle()
  104.     laser.b = love.physics.newBody(world,x,y,"dynamic")
  105.     laser.b:setBullet(true)
  106.     laser.s = love.physics.newRectangleShape(25,3)
  107.     laser.f = love.physics.newFixture(laser.b,laser.s)
  108.     laser.f:setMask(3)
  109.     laser.f:setCategory(2)
  110.    
  111.     laser.b:setAngle(a)
  112.     local laserXSpeed = math.cos(a) * 100
  113.     local laserYSpeed = math.sin(a) * 100
  114.    
  115.     laser.b:applyLinearImpulse(laserXSpeed,laserYSpeed)
  116.    
  117.     table.insert(lasers,laser)
  118.     lasersound = love.audio.newSource("laser.wav","static")
  119.     love.audio.play(lasersound)
  120. end
  121.    
  122. function love.update(dt)
  123.     if laser_cooldown > 0 then
  124.         laser_cooldown = laser_cooldown - dt
  125.         if ( laser_cooldown < 0 ) then
  126.             laser_cooldown = 0
  127.         end
  128.     end
  129.     if engine_cooldown > 0 then
  130.         engine_cooldown = engine_cooldown - dt
  131.         if ( engine_cooldown < 0 ) then
  132.             engine_cooldown = 0
  133.         end
  134.     end
  135.     checkRocks()
  136.     shipBody:setAngularVelocity(0)
  137.     world:update(dt)
  138.     rockParticles:update(dt)
  139.     bigRocks:update(dt)
  140.     shipBoom:update(dt)
  141.     if dead > 0 then
  142.         dead = dead + dt
  143.     end
  144.     if #rocks <= 0 then
  145.         if ( countdown == -1 ) then
  146.             countdown = 6
  147.             youwin = love.audio.newSource("level.wav","static")
  148.             love.audio.play(youwin)
  149.            
  150.         end
  151.        
  152.         if ( countdown > 0 ) then
  153.             countdown = countdown - dt
  154.         end
  155.         if ( countdown <= 0 ) then 
  156.             countdown = -1
  157.             level = level + 2
  158.             make_rocks(5+level)
  159.             shipBody:setPosition(400,300)
  160.         end
  161.     end
  162.        
  163.    
  164.     if love.keyboard.isDown(" ") and laser_cooldown <= 0 then
  165.         if dead == 0  then
  166.             laser_cooldown = 0.5
  167.             fire_laser()
  168.         elseif dead > 4 then
  169.             love.filesystem.load("main.lua")()
  170.             love.load()
  171.         end
  172.     end
  173.    
  174.     for i, v in ipairs(lasers) do
  175.         if v.b:getX() < 0 or v.b:getX() > 800 or v.b:getY() < 0 or v.b:getY() > 600 then
  176.             table.remove(lasers,i)
  177.         end
  178.     end
  179.    
  180.     if love.keyboard.isDown("right") or love.keyboard.isDown("left") then
  181.         local delta = -1
  182.         if love.keyboard.isDown("right") then
  183.             delta = 1
  184.         end
  185.         local a = shipBody:getAngle()
  186.         a = a + ( dt * 5 * delta )
  187.         shipBody:setAngle(a)   
  188.     end
  189.     if love.keyboard.isDown("up") then
  190.         rockets = 1
  191.         local xForce = math.cos(shipBody:getAngle()) * 200
  192.         local yForce = math.sin(shipBody:getAngle()) * 200
  193.        
  194.         shipBody:applyForce(xForce,yForce)
  195.         if engine_cooldown <= 0 then
  196.             local enginehum = love.audio.newSource("engine.wav","static")
  197.             enginehum:play()
  198.             engine_cooldown = 0.25
  199.         end
  200.        
  201.         --if enginehum:isStopped() then
  202.         --  enginehum:play()
  203.         --end
  204.        
  205.     else
  206.         rockets = 0
  207.         --enginehum:stop()
  208.     end
  209.     if shipBody:getX() < 0 then
  210.         shipBody:setX(shipBody:getX() + 800)
  211.     end
  212.     if shipBody:getY() < 0 then
  213.         shipBody:setY(shipBody:getY() + 600)
  214.     end
  215.    
  216.     if shipBody:getX() > 800 then
  217.         shipBody:setX(shipBody:getX() - 800)
  218.     end
  219.    
  220.     if shipBody:getY() > 600 then
  221.         shipBody:setY(shipBody:getY() - 600)
  222.     end
  223.     for i, v in ipairs(rocks) do
  224.         if v.body:getX() < -20 then
  225.             v.body:setX(v.body:getX() + 840)
  226.         end
  227.         if v.body:getY() < -20 then
  228.             v.body:setY(v.body:getY() + 640)
  229.         end
  230.        
  231.         if v.body:getX() > 820 then
  232.             v.body:setX(v.body:getX() - 840)
  233.         end
  234.        
  235.         if v.body:getY() > 620 then
  236.             v.body:setY(v.body:getY() - 640)
  237.         end
  238.    
  239.     end
  240.    
  241.    
  242. end
  243.  
  244. function make_rocks(total_rocks)
  245.     for i = 1, total_rocks do
  246.  
  247.        
  248.         local x = 0
  249.         local y = 0
  250.         local s = math.random() + 0.5
  251.         if ( math.random(0,1) == 1 ) then
  252.             x = math.random(0,800)
  253.         else
  254.             y = math.random(0,600)
  255.         end
  256.         createNewRock(x,y,s)
  257.    
  258.     end
  259. end
  260.    
  261. function createNewRock(x,y,s)
  262.        
  263.        
  264.     rock = {}
  265.     rock.a = math.random() * 2 * math.pi
  266.     rock.size = s  
  267.     rock.body = love.physics.newBody(world, x, y, "dynamic")
  268.     rock.shape = love.physics.newPolygonShape(14*rock.size ,0,
  269.                                               31*rock.size ,0,
  270.                                               45*rock.size ,5*rock.size,
  271.                                               58*rock.size ,31*rock.size,
  272.                                               46*rock.size ,42*rock.size,
  273.                                               11*rock.size ,41*rock.size,
  274.                                               0            ,22*rock.size)
  275.     --rock.shape = love.physics.newRectangleShape(59*rock.size,43*rock.size)
  276.     rock.fix = love.physics.newFixture(rock.body,rock.shape)
  277.     rock.fix:setCategory(1)
  278.    
  279.     rock.body:setAngle(rock.a)
  280.     rock.ox = 0
  281.     rock.oy = 0
  282.     rock.body:applyAngularImpulse(math.random() * 200 - 100)
  283.     rock.body:applyLinearImpulse(math.random() * 200 - 200, math.random() * 200 - 100)
  284.    
  285.     rock.health = rock.size * rock.size * 100
  286.    
  287.     table.insert(rocks,rock)
  288. end
  289.    
  290. function beginContact(a, b, coll)
  291.    
  292. end
  293.  
  294. function endContact(a, b, coll)
  295.    
  296. end
  297.  
  298. function preSolve(a, b, coll)
  299.    
  300. end
  301.  
  302. function postSolve(a, b, coll)
  303.     if ( a:getCategory() == 1 and b:getCategory() == 2 ) or ( a:getCategory() == 2 and b:getCategory() == 1 ) then
  304.         local laserFix = a
  305.         local rockFix = b
  306.         if b:getCategory() == 2 then
  307.             laserFix = b
  308.             rockFix = a
  309.         end
  310.         --deleteLaser(laserFix)
  311.         destroyLaser = laserFix
  312.         local x1, y1, x2, y2 = coll:getPositions()
  313.         rockParticles:setPosition(x1,y1)
  314.         rockParticles:start()
  315.         local rock,i = findRock(rockFix)
  316.         rock.health = rock.health - 25
  317.         local hit = love.audio.newSource("hit.wav","static")
  318.         hit:play()
  319.        
  320.         if rock.health <= 0 then
  321.             local x,y = rock.body:getPosition()
  322.             bigRocks:setPosition(x,y)
  323.             bigRocks:start()
  324.             if rock.size >= 1 then
  325.                 newRockSize = rock.size - 0.5
  326.                 makeNewRock = 1
  327.                 newRockX = x
  328.                 newRockY = y
  329.             end
  330.             destroyRock = i
  331.             local boom = love.audio.newSource("boom.wav","static")
  332.             boom:play()
  333.            
  334.         end    
  335.     end
  336.     if ( a:getCategory() == 1 and b:getCategory() == 3 ) or ( a:getCategory() == 3 and b:getCategory() == 1 ) then
  337.         if dead == 0 then
  338.             dead = 1
  339.             local boom = love.audio.newSource("boom.wav","static")
  340.             boom:play()
  341.             local x,y = shipBody:getPosition()
  342.             shipBoom:setPosition(x,y)
  343.             shipBoom:start()
  344.         end
  345.            
  346.    
  347.    
  348.     end
  349. end
  350. function checkRocks()
  351.     if  makeNewRock == 1  then
  352.         makeNewRock = 0
  353.         createNewRock(newRockX,newRockY,newRockSize)
  354.         createNewRock(newRockX,newRockY,newRockSize)
  355.     end
  356.     if  destroyRock ~= 0  then
  357.         destroyARock(destroyRock)
  358.         destroyRock = 0
  359.     end
  360.     if destroyLaser ~= 0 then
  361.         deleteLaser(destroyLaser)
  362.         destroyLaser = 0
  363.     end
  364. end
  365. function destroyARock(i)
  366.     local rock = rocks[i]
  367.     rock.body:destroy()
  368.     table.remove(rocks,i)
  369. end
  370. function deleteLaser(laserFix)
  371.     for i, v in ipairs(lasers) do
  372.         if v.f == laserFix then
  373.             v.b:destroy()
  374.             table.remove(lasers,i)
  375.         end
  376.     end
  377. end
  378. function findRock(rockFix)
  379.     for  i, v in ipairs(rocks) do
  380.         if v.fix == rockFix then
  381.             return v,i
  382.         end
  383.     end
  384. end
  385.  
  386.  
  387.  
  388.  
  389.    
  390. function SetupParticles()
  391.     rockParticles = love.graphics.newParticleSystem(images.rock,100)
  392.     rockParticles:setEmissionRate(20)
  393.     rockParticles:setSpeed(50,80)
  394.     rockParticles:setSizes(0.4)
  395.     rockParticles:setSizeVariation(0.5)
  396.     rockParticles:setColors(150,150,150,200,150,150,150,0)
  397.     rockParticles:setLifetime(0.25)
  398.     rockParticles:setParticleLife(1)
  399.     rockParticles:setRotation(0,2*math.pi)
  400.     rockParticles:setSpread(2*math.pi)
  401.     rockParticles:setSpin(-1,1,1)
  402.    
  403.     bigRocks = love.graphics.newParticleSystem(images.rock,100)
  404.     bigRocks:setEmissionRate(20)
  405.     bigRocks:setSpeed(50,60)
  406.     bigRocks:setSizes(0.6)
  407.     bigRocks:setSizeVariation(0.5)
  408.     bigRocks:setColors(200,200,200,200,200,200,150,0)
  409.     bigRocks:setLifetime(0.5)
  410.     bigRocks:setParticleLife(1)
  411.     bigRocks:setRotation(0,2*math.pi)
  412.     bigRocks:setSpread(2*math.pi)
  413.     bigRocks:setSpin(-1,1,1)
  414.    
  415.     shipBoom = love.graphics.newParticleSystem(images.rock,100)
  416.     shipBoom:setEmissionRate(30)
  417.     shipBoom:setSpeed(50,60)
  418.     shipBoom:setSizes(0.2,0.6)
  419.     shipBoom:setSizeVariation(0.5)
  420.     shipBoom:setColors(200,200,20,200, 200,20,20,150, 200,20,20,100, 150,20,20,50, 20,20,20,0)
  421.     shipBoom:setLifetime(3)
  422.     shipBoom:setParticleLife(1)
  423.     shipBoom:setRotation(0,2*math.pi)
  424.     shipBoom:setSpread(2*math.pi)
  425.     shipBoom:setSpin(-1,1,1)
  426.    
  427.    
  428. end
Add Comment
Please, Sign In to add comment