Browse Source

animation and thrusters

tags/earlywindows
Madiwka 3 years ago
parent
commit
e8de7b5340
9 changed files with 207 additions and 16 deletions
  1. +118
    -0
      entities/ship/prerenderpath.lua
  2. +6
    -4
      entities/ship/ship.lua
  3. +8
    -2
      levels/level1.lua
  4. +6
    -0
      levels/level2.lua
  5. +6
    -0
      levels/level3.lua
  6. +11
    -6
      levels/level4.lua
  7. +37
    -4
      levels/levelgeneral.lua
  8. +8
    -0
      levels/selectlv.lua
  9. +7
    -0
      src/GUI.lua

+ 118
- 0
entities/ship/prerenderpath.lua View File

@@ -0,0 +1,118 @@
ship = Class{}


function ship:init(x, y, image)
self.x = x
self.y = y
self.ox = x
self.oy = y
self.dy = 0
self.dx = 5
self.image = love.graphics.newImage(image)
self.width = self.image:getWidth()
self.height = self.image:getHeight()
self.rotation = 1.5708
self.vector = 1.5708
self.color = {1,1,1,1}
self.path = {}
self.dottimer = 0.5
self.fuel = 0
end
function ship:newPathDot(dotx, doty)
return {
x = dotx,
y = doty
}
end
function ship:update(dt)
if not shipIsHit then
self.dottimer = self.dottimer - dt
if self.dottimer < 0 then
table.insert(self.path, self:newPathDot(self.x, self.y))
self.dottimer = 0.2
end
if love.timer.getFPS() < 20 then
self.path = {}
end
self.x = self.x + self.dx/2
self.y = self.y + self.dy/2
if self.dx ~= 0 then
self.vector = math.atan( self.dy/ self.dx)
end
--[[if love.keyboard.isDown('s') then
self.speed = math.sqrt(self.dx^2 + self.dy^2)
if self.speed > 0 then
self.speed = self.speed - 0.5
end
self.dx = math.cos(self.vector) * self.speed
self.dy = math.sin(self.vector) * self.speed
end ]]--
if love.keyboard.isDown('w') and self.fuel > 0 then
self.fuel = self.fuel - 0.5
self.speed = math.sqrt(self.dx^2 + self.dy^2)
self.speed = self.speed + 0.5
self.dx = math.cos(self.vector) * self.speed
self.dy = math.sin(self.vector) * self.speed
end
--[[
if love.keyboard.isDown('left') then
self.dx = self.dx - 0.5
end
if love.keyboard.isDown('right') then
self.dx = self.dx + 0.5
end
if love.keyboard.isDown('up') then
self.dy = self.dy - 0.5
end
if love.keyboard.isDown('down') then
self.dy = self.dy + 0.5
end
]]--
--print(self.speed)
--[[
if love.keyboard.isDown('right') then
self.rotation = self.rotation + 10
elseif love.keyboard.isDown('left') then
self.rotation = self.rotation - 10
end]]--

--print("rotation:" .. self.rotation)
--print("speed:" .. self.dx .. " " .. self.dy)
if self.dx < 0 then
self.vector = self.vector - 3.14159
end
self.vector = self.vector + 1.5708
end
end

function ship:draw()

-- Draw the `self.canvas` to screen
love.graphics.setColor(unpack(self.color))
--print("DAW" .. camera.x)
love.graphics.draw(self.image, self.x, self.y, self.vector, 1, 1, self.width/2, self.height/2)
for i in ipairs(self.path) do
if i > 1 then
love.graphics.setColor(0.9,0.9,0.9,1)
--print("DOING".. i)
love.graphics.line(self.path[i].x, self.path[i].y, self.path[i-1].x, self.path[i-1].y)
end
end
love.graphics.setColor(1,1,1,1)
end

function ship:reset()
self.x = self.ox
self.y = self.oy
self.dy = 0
self.dx = 5
self.rotation = 1.57
self.canvas = love.graphics.newCanvas(WINDOW_WIDTH, WINDOW_HEIGHT)
self.vector = 1.56
self.speed = 0
self.path = {}
self.dottimer = 0.5
end

+ 6
- 4
entities/ship/ship.lua View File

@@ -16,6 +16,7 @@ self.vector = 1.5708
self.color = {1,1,1,1}
self.path = {}
self.dottimer = 0.5
self.fuel = 0
end
function ship:newPathDot(dotx, doty)
return {
@@ -28,7 +29,7 @@ function ship:update(dt)
self.dottimer = self.dottimer - dt
if self.dottimer < 0 then
table.insert(self.path, self:newPathDot(self.x, self.y))
self.dottimer = 0.5
self.dottimer = 0.2
end
if love.timer.getFPS() < 20 then
self.path = {}
@@ -38,15 +39,16 @@ function ship:update(dt)
if self.dx ~= 0 then
self.vector = math.atan( self.dy/ self.dx)
end
if love.keyboard.isDown('s') then
--[[if love.keyboard.isDown('s') then
self.speed = math.sqrt(self.dx^2 + self.dy^2)
if self.speed > 0 then
self.speed = self.speed - 0.5
end
self.dx = math.cos(self.vector) * self.speed
self.dy = math.sin(self.vector) * self.speed
end
if love.keyboard.isDown('w') then
end ]]--
if love.keyboard.isDown('w') and self.fuel > 0 then
self.fuel = self.fuel - 0.5
self.speed = math.sqrt(self.dx^2 + self.dy^2)
self.speed = self.speed + 0.5
self.dx = math.cos(self.vector) * self.speed


+ 8
- 2
levels/level1.lua View File

@@ -3,6 +3,8 @@ local levelLoaded = false
local M = {}
function level1.load()
shipsleft = 1
thrusterMax = 50
firstShip.fuel = 50
local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
planetsleft = 3
gameStatus = "setup"
@@ -27,9 +29,9 @@ function level1.load()
end
))
table.insert(guibutts, menu:addButton("To menu", function ()
level1.goBack()
level.goBack()
end))
table.insert(planets, planet(700, 200, 50, 0.3, planetImage, "nodelete"))
-- table.insert(planets, planet(700, 200, 50, 0.3, planetImage, "nodelete"))
end
function level1.reset()
firstShip:reset()
@@ -40,6 +42,7 @@ function level1.reset()
table.insert(planets, planet(700, 200, 50, 0.3, planetImage))
shipsleft = 1
shipIsHit = false
firstShip.fuel = 50
planetsleft = 3
end
function level1.GUIControl()
@@ -50,5 +53,8 @@ function level1.GUIControl()
VCAM.x = VCAM.x + 10
end
end
function level1.goBack()
levelgeneral.goBack()
end
return level1


+ 6
- 0
levels/level2.lua View File

@@ -5,6 +5,8 @@ function level2.load()
shipsleft = 1
local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
planetsleft = 3
thrusterMax = 100
firstShip.fuel = 100
gameStatus = "setup"
playbutts = {}
guibutts = {}
@@ -36,6 +38,7 @@ function level2.reset()
for k in pairs(planets) do
planets[k] = nil
end
firstShip.fuel = 100
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
@@ -50,5 +53,8 @@ function level2.GUIControl()
VCAM.x = VCAM.x + 10
end
end
function level2.goBack()
levelgeneral.goBack()
end
return level2


+ 6
- 0
levels/level3.lua View File

@@ -7,6 +7,8 @@ function level3.load()
planetsleft = 3
gameStatus = "setup"
playbutts = {}
thrusterMax = 150
firstShip.fuel = 150
guibutts = {}
VCAM.x, VCAM.y = WINDOW_WIDTH/2, WINDOW_HEIGHT/2
explosions = {}
@@ -43,6 +45,7 @@ function level3.reset()
end
local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
shipsleft = 1
firstShip.fuel = 150
shipIsHit = false
planetsleft = 3
end
@@ -54,5 +57,8 @@ function level3.GUIControl()
VCAM.x = VCAM.x + 10
end
end
function level3.goBack()
levelgeneral.goBack()
end
return level3


+ 11
- 6
levels/level4.lua View File

@@ -8,12 +8,14 @@ function level4.load()
gameStatus = "setup"
playbutts = {}
guibutts = {}
thrusterMax = 200
firstShip.fuel = 200
VCAM.x, VCAM.y = WINDOW_WIDTH/2, WINDOW_HEIGHT/2
explosions = {}
shipIsHit = false
guimenu = mainMenu()
reachedGoal = false
lvlbase = base(400, 300)
lvlbase = base(-300, 300)
levelLoaded = true
table.insert(playbutts, menu:addButton("Return to setup", function()
gameStatus = "setup"
@@ -29,9 +31,8 @@ function level4.load()
table.insert(guibutts, menu:addButton("To menu", function ()
levelgeneral.goBack()
end))
table.insert(planets, planet(900, 400, 50, 0.3, planetImage, "nodelete"))
table.insert(planets, planet(700, 300, 50, 0.3, planetImage, "nodelete"))
table.insert(planets, planet(900, 200, 50, 0.3, planetImage, "nodelete"))
table.insert(planets, planet(-200, 400, 50, 0.3, planetImage, "nodelete"))
table.insert(planets, planet(-200, 200, 50, 0.3, planetImage, "nodelete"))
end

function level4.reset()
@@ -43,16 +44,20 @@ function level4.reset()
end
local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
shipsleft = 1
firstShip.fuel = 200
shipIsHit = false
planetsleft = 3
end
function level4.GUIControl()
if (love.keyboard.isDown('a') and VCAM.x > WINDOW_WIDTH/2) then
if (love.keyboard.isDown('a') and VCAM.x > -WINDOW_WIDTH) then
VCAM.x = VCAM.x - 10
end
if (love.keyboard.isDown('d')) then
if (love.keyboard.isDown('d') and VCAM.x < WINDOW_WIDTH*2) then
VCAM.x = VCAM.x + 10
end
end
function level4.goBack()
levelgeneral.goBack()
end
return level4


+ 37
- 4
levels/levelgeneral.lua View File

@@ -1,19 +1,29 @@
levelgeneral = Class{}
local levelLoaded = false
local M = {}
local thrusterMax = 0
local animationComplete = false
local frame = 0
function levelgeneral.update(dt)
if not levelLoaded then
level = require("levels/level" .. currentLevel)
level.load()
frame = 0
animationComplete = false
levelLoaded = true
end
if reachedGoal then
if love.keyboard.isDown('return') then
animationComplete = true
end
if saveData.levelsBeaten < currentLevel then
saveData.levelsBeaten = currentLevel
end
--print("saveData.levelsBeaten is " .. saveData.levelsBeaten)
if animationComplete then
love.filesystem.write("save", serialize(saveData))
levelgeneral.goBack()
end
end
camera:update(dt)
if lvlbase ~= nil then
@@ -47,13 +57,13 @@ else
end
levelgeneral.GUIControl()
end

function levelgeneral.draw()
love.graphics.setColor(1,1,1,1)
camera:attach()
firstShip:draw()

if lvlbase ~= nil then
lvlbase:draw()
end
@@ -67,14 +77,37 @@ function levelgeneral.draw()
--print("exploding")
end
end
if reachedGoal then
love.graphics.clear(0,0,0,1)
love.graphics.setColor(30/255, 30/255, 30/255, 1)
if frame < WINDOW_WIDTH then
love.graphics.circle("fill", firstShip.x, firstShip.y, WINDOW_WIDTH - frame)
end
frame = frame + 20
end
firstShip:draw()
camera:detach()
camera:draw()
if gameStatus == "setup" then
if reachedGoal then
love.graphics.setColor(1,1,1,1-1/frame)
love.graphics.setFont(smallfont)
love.graphics.printf("Press Enter to continue",0, 600, WINDOW_WIDTH, "center")
end
if gameStatus == "setup" and not reachedGoal then
GUIDraw("left")
elseif gameStatus == "play" then
if not reachedGoal then
love.graphics.printf("Thrusters: ", 0, WINDOW_HEIGHT-50, 600, "center")
local m = smallfont:getWidth("Thrusters: ")
local n = smallfont:getHeight("Thrusters: ")
love.graphics.setColor(1,0,0,1)
love.graphics.rectangle("fill",m + 100, WINDOW_HEIGHT-50, thrusterMax/2, n)
love.graphics.setColor(0,1,0,1)
love.graphics.rectangle("fill",m + 100, WINDOW_HEIGHT-50, firstShip.fuel/2, n)
love.graphics.setColor(1,1,1,1)
guimenu:butt(playbutts, WINDOW_WIDTH, WINDOW_HEIGHT, 1100, WINDOW_HEIGHT-50, 40, WINDOW_WIDTH/3)
end
end


+ 8
- 0
levels/selectlv.lua View File

@@ -22,6 +22,14 @@ table.insert(levels, menu:addButton("Level 2", function ()
currentLevel = 3
end
end ))
table.insert(levels, menu:addButton("Level 4", function ()
if saveData.levelsBeaten > 2 then
menuLoaded = false
objReset()
gameState = "levelgeneral"
currentLevel = 4
end
end ))





+ 7
- 0
src/GUI.lua View File

@@ -53,7 +53,14 @@ function GUIDraw(mode)
if selectedItem == "ship" and mx < menuX then
local shipW = shipImage:getWidth()
local shipH = shipImage:getHeight()
if VCAM.x > WINDOW_WIDTH/2-1 then
love.graphics.draw(shipImage,10,my, 1.5708, 1, 1, shipW/2, shipH/2)
elseif VCAM.x > -WINDOW_WIDTH/2+(WINDOW_WIDTH-(menuX-firstShip.height/2)) then
local timex, timey = camera:toCameraCoords(250, 0)
love.graphics.draw(shipImage,timex,my, 1.5708, 1, 1, shipW/2, shipH/2)
else
love.graphics.draw(shipImage,menuX-firstShip.height/2,my, 1.5708, 1, 1, shipW/2, shipH/2)
end
if love.keyboard.mouseisReleased then
love.keyboard.mouseisReleased = false
firstShip.x = 250


Loading…
Cancel
Save