From c02b521b650491160ce43ece34f5c7300f80811e Mon Sep 17 00:00:00 2001
From: Madiwka3 <madi.turgunov.03@gmail.com>
Date: Sun, 18 Apr 2021 21:05:48 +0600
Subject: [PATCH] Level 2

---
 levels/level2.lua   | 134 ++++++++++++++++++++++++++++++++++++++++++++
 levels/selectlv.lua |   5 ++
 2 files changed, 139 insertions(+)
 create mode 100644 levels/level2.lua

diff --git a/levels/level2.lua b/levels/level2.lua
new file mode 100644
index 0000000..47cff4c
--- /dev/null
+++ b/levels/level2.lua
@@ -0,0 +1,134 @@
+level2 = Class{}
+local levelLoaded = false
+local M = {}
+function level2.update(dt)
+    if not levelLoaded then 
+        shipsleft = 1
+        local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
+        planetsleft = 3
+        gameStatus = "setup"
+        playbutts = {}
+        guibutts = {}
+        VCAM.x, VCAM.y = WINDOW_WIDTH/2, WINDOW_HEIGHT/2
+        explosions = {}
+        shipIsHit = false
+        guimenu = mainMenu()
+        reachedGoal = false
+        lvlbase = base(1800, 500)
+        levelLoaded = true
+        table.insert(playbutts, menu:addButton("Return to setup", function()
+            gameStatus = "setup"
+            level2.reset()
+        end ))
+        table.insert(guibutts, menu:addButton("Release brake!", function ()
+            selectedItem = "none"
+            gameStatus = "play"
+        end 
+        ))
+        table.insert(guibutts, menu:addButton("To menu", function ()
+            level2.goBack()
+        end)) 
+        table.insert(planets, planet(700, 500, 50, 0.3, planetImage))
+        
+    end 
+    if reachedGoal then 
+        if levelsBeaten < 2 then 
+            levelsBeaten = 2
+        end
+        print("levelsBeaten is " .. levelsBeaten)
+        love.filesystem.write("save", levelsBeaten)
+        level2.goBack()
+    end
+    camera:update(dt)
+    lvlbase:update(dt)
+    --print(camera.x .. " " .. camera.y)
+    for i, explosion in ipairs(explosions) do 
+        explosion:update(dt)
+        if explosion.killed then 
+            table.remove(explosions, i)
+            gameStatus = "setup"
+            level2.reset()
+        end
+    end
+    if gameStatus == "play" then
+        camera.x, camera.y = firstShip.x - firstShip.height*4, firstShip.y- firstShip.width
+        --print(camera.x .. firstShip.x)
+        if shipIsHit then 
+            if #explosions == 0 then 
+                table.insert(explosions, explosion(firstShip.x, firstShip.y, 100, {1,1,1,1}))
+            end 
+            
+        end 
+    firstShip:update(dt)
+    
+    for i in ipairs(planets) do 
+        planets[i]:update(dt)
+    end
+else 
+    camera:follow(VCAM.x, VCAM.y)
+end
+    level2.GUIControl()
+    
+    
+end 
+
+function level2.draw()
+    love.graphics.setColor(1,1,1,1)
+    camera:attach()
+    firstShip:draw()
+    lvlbase:draw()
+    for i in ipairs(planets) do 
+        planets[i]:draw(dt)
+    end
+    --love.graphics.rectangle("fill",VCAM.x,VCAM.y,30,30)
+    if shipIsHit then 
+        for i, explosion in ipairs(explosions) do 
+            explosion:render()
+            --print("exploding")
+        end
+    end
+    camera:detach()
+    
+    camera:draw()
+   
+    if gameStatus == "setup" then 
+    GUIDraw("left")
+    elseif gameStatus == "play" then 
+        guimenu:butt(playbutts, WINDOW_WIDTH, WINDOW_HEIGHT, 1100, WINDOW_HEIGHT-50, 40, WINDOW_WIDTH/3)
+    end
+    
+    
+    
+    
+end 
+function level2.goBack()
+    level2.reset()
+    gameStatus = "setup"
+    firstShip.path = {}
+    levelLoaded = false
+    for k in pairs(planets) do
+        planets[k] = nil
+    end
+    gameState = "selectlv"
+end 
+function level2.reset()
+    firstShip:reset()
+    for k in pairs(planets) do
+        planets[k] = nil
+    end
+    local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
+    table.insert(planets, planet(700, 500, 50, 0.3, planetImage))
+    shipsleft = 1
+    shipIsHit = false
+    planetsleft = 3
+end 
+function level2.GUIControl()
+    if (love.keyboard.isDown('a') and VCAM.x > WINDOW_WIDTH/2) then 
+        VCAM.x = VCAM.x - 10
+    end
+    if (love.keyboard.isDown('d'))  then
+        VCAM.x = VCAM.x + 10
+    end
+end 
+return level2
+
diff --git a/levels/selectlv.lua b/levels/selectlv.lua
index 7c08b78..2a29d3c 100644
--- a/levels/selectlv.lua
+++ b/levels/selectlv.lua
@@ -3,6 +3,11 @@ levels = {}
 table.insert(levels, menu:addButton("Level 1", function ()
 gameState = "level1"
 end ))
+table.insert(levels, menu:addButton("Level 2", function ()
+    if levelsBeaten > 0 then 
+    gameState = "level2"
+    end
+    end ))