debug organized, thrusters use fuel

This commit is contained in:
Paul Liverman
2015-01-12 02:10:49 -08:00
parent 889fc83b07
commit eac25c9793
7 changed files with 204 additions and 137 deletions

61
src/Debug.lua Normal file
View File

@@ -0,0 +1,61 @@
local Debug = {}
local debug = {
-- values to use: above, below, none
hitRadius = "above", -- (magenta) where player can be hit
middleLines = "below", -- (red) midlines of screen
playerCenter = "none", -- (pink) center of player
fpsCounter = "above", -- (white) current fps in top left
fuelNumber = "above", -- (white) fuel/max fuel in numerical form
}
function Debug:drawAbove()
for k,v in pairs(debug) do
if v == "above" then
Debug[k]()
end
end
end
function Debug:drawBelow()
for k,v in pairs(debug) do
if v == "below" then
Debug[k]()
end
end
end
function Debug:hitRadius()
love.graphics.setLineWidth(1)
love.graphics.setColor(255, 0, 255) -- as much as I love this orange: 188, 128, 90, THIS NEEDS TO STAND OUT
love.graphics.arc("line", player.Ship.x, player.Ship.y, player.Ship.Hull.hitRadius, 0, math.pi*2, 20)
end
function Debug:middleLines()
love.graphics.setLineWidth(0.5)
love.graphics.setColor(200, 0, 0)
love.graphics.line(love.graphics.getWidth() / 2, 0, love.graphics.getWidth() / 2, love.graphics.getHeight())
love.graphics.line(0, love.graphics.getHeight() / 2, love.graphics.getWidth(), love.graphics.getHeight() / 2)
end
function Debug:playerCenter()
love.graphics.setColor(255, 100, 100)
love.graphics.arc("fill", player.Ship.x, player.Ship.y, 3, 0, math.pi*2, 20)
end
function Debug:fpsCounter()
love.graphics.setFont(Render.debugFont)
love.graphics.setColor(255, 255, 255)
love.graphics.print("FPS: "..love.timer.getFPS(), 3, 2)
end
function Debug:fuelNumber()
love.graphics.setFont(Render.debugFont)
local text = "Fuel: "..math.floor(player.Ship.Hull.fuelAmount).."/"..player.Ship.Hull.fuelCapacity
local width = Render.debugFont:getWidth(text)
love.graphics.setColor(100, 0, 0)
love.graphics.rectangle("fill", 1, love.graphics.getHeight() - 13, width+3, love.graphics.getHeight() -2)
love.graphics.setColor(255, 255, 255)
love.graphics.print(text, 2, love.graphics.getHeight() - 13)
end
return Debug

View File

@@ -2,9 +2,7 @@ local class = require "lib.middleclass"
local Engine = class('Engine')
-- have hull limit rotationImpulse? yeah probably (take damage if above Hull's rotationImpulseLimit, but do warn about this so people don't usually do it)
-- have a fuel effeciency multiplayer here
function Engine:initialize(maximumSpeed, spaceDrag, speedLowerLimit, degreeLock, rotationImpulse, rotationDrag, rotationLowerLimit, Thrusters)
function Engine:initialize(maximumSpeed, spaceDrag, speedLowerLimit, degreeLock, rotationImpulse, rotationDrag, rotationLowerLimit)
self.maximumSpeed = maximumSpeed or 0 and console.d("Engine created with no maximumSpeed.")
self.spaceDrag = spaceDrag or 0 and console.d("Engine created with no spaceDrag.")
self.speedLowerLimit = speedLowerLimit or 0 and console.d("Engine created with no speedLowerLimit.")
@@ -12,18 +10,6 @@ function Engine:initialize(maximumSpeed, spaceDrag, speedLowerLimit, degreeLock,
self.rotationImpulse = rotationImpulse or 0 and console.e("Engine created with no rotationImpulse.")
self.rotationDrag = rotationDrag or 0 and console.d("Engine created with no rotationDrag.")
self.rotationLowerLimit = rotationLowerLimit or 0 and console.d("Engine created with no rotationLowerLimit.")
self.Thrusters = Thrusters or {} and console.d("Engine created with no Thrusters.")
end
--[[
function Thruster:draw(shipX, shipY, shipRotation)
love.graphics.setColor(self.thrusterColor)
love.graphics.line(shipX + self.thrusterX*math.cos(shipRotation) - self.thrusterY*math.sin(shipRotation),
shipY + self.thrusterX*math.sin(shipRotation) + self.thrusterY*math.cos(shipRotation),
shipX + self.thrusterX*math.cos(shipRotation) - (self.thrusterY+self.thrusterLength)*math.sin(shipRotation),
shipY + self.thrusterX*math.sin(shipRotation) + (self.thrusterY+self.thrusterLength)*math.cos(shipRotation))
end
]]
return Engine

View File

@@ -2,12 +2,18 @@ local class = require "lib.middleclass"
local Hull = class('Hull')
function Hull:initialize(imgFile, imgScale, imgColor)
function Hull:initialize(imgFile, imgScale, hitRadius, fuelCapacity, imgColor)
-- image stuff
self.image = love.graphics.newImage(imgFile) or love.graphics.newImage("images/no_hull.png") and console.e("Hull created with no image.")
self.imgHalfWidth = self.image:getWidth() / 2
self.imgHalfHeight = self.image:getHeight() / 2
self.imgScale = imgScale or 1 and console.d("Hull created with no scale.")
self.imgColor = imgColor or {255, 255, 255} and console.d("Hull created with no color (this is fine and normal).")
-- physics stuff
self.hitRadius = hitRadius or self.image:getWidth() * 0.9 and console.d("Hull created with no hitRadius.") --defaults to 90% imgWidth
self.fuelCapacity = fuelCapacity or 1000 and console.d("Hull created with no fuelCapacity.")
self.fuelAmount = fuelCapacity or 1000
end
return Hull

View File

@@ -34,13 +34,8 @@ local directions = {
}
function Ship:accelerate(direction, dt)
-- get amount of thrust based on which Thrusters fire that way
local thrust = 0
for i=1,#self.Thrusters do
if self.Thrusters[i].direction == direction then
thrust = thrust + self.Thrusters[i].thrust
end
end
local thrust = Ship.static:fireThrusters(self, direction)
if not thrust then return end -- if false, exit, out of fuel
-- here is where we actually apply acceleration
self.v.x = self.v.x + thrust * math.cos(self.currentRotation - (self.currentRotation % self.Engine.degreeLock) + directions[direction]) * dt
@@ -57,4 +52,39 @@ function Ship:accelerate(direction, dt)
end
end
function Ship:rotate(direction, dt)
local haveFuel = Ship.static:fireThrusters(self, direction)
if direction == "rotateleft" and haveFuel then
self.rotationSpeed = self.rotationSpeed - self.Engine.rotationImpulse * dt
elseif direction == "rotateright" and haveFuel then
self.rotationSpeed = self.rotationSpeed + self.Engine.rotationImpulse * dt
end
end
-- Returns thrust generated or FALSE if ran out of fuel.
function Ship.static:fireThrusters(self, direction)
-- get amount of thrust and fuel usage based on which Thrusters fire in direction
local thrust, fuelUsage = 0, 0
--if direction == "rotateleft" or direction == "rotateright" then
--look in the Engine..why do we need to do that?
--else
for i=1,#self.Thrusters do
if self.Thrusters[i].direction == direction then
thrust = thrust + self.Thrusters[i].thrust
fuelUsage = fuelUsage + self.Thrusters[i].thrust * self.Thrusters[i].fuelEfficiency / 600 --60 for framerate, and 1/10 a 'unit'
end
end
-- use fuel, then check if out
self.Hull.fuelAmount = self.Hull.fuelAmount - fuelUsage
if self.Hull.fuelAmount < 0 then
self.Hull.fuelAmount = 0
console.d("Ran out of fuel!")
return false
end
-- finally return thrust amount
return thrust
end
return Ship

View File

@@ -1,23 +1,22 @@
local forward = -math.pi/2
local backward = math.pi/2
local left = math.pi
local right = 0
local class = require "lib.middleclass"
local Thruster = class('Thruster')
function Thruster:initialize(thrust, direction, thrusterX, thrusterY, thrusterColor, thrusterLength)
function Thruster:initialize(thrust, direction, thrusterX, thrusterY, thrusterColor, thrusterLength, fuelEfficiency)
self.thrust = thrust or 0 and console.e("Thruster created with no thrust.")
self.direction = direction or 0 and console.d("Thruster created with no direction.")
self.thrusterX = thrusterX or nil and console.e("Thruster created with incomplete location.")
self.thrusterY = thrusterY or nil and console.e("Thruster created with incomplete location.")
self.thrusterColor = thrusterColor or {255, 255, 255} and console.d("Thruster created with no color.")
self.thrusterLength = thrusterLength or 2 and console.d("Thruster created with no length.")
self.fuelEfficiency = fuelEfficiency or 1 and console.d("Thruster created with no fuelEfficiency.")
end
function Thruster:draw(shipX, shipY, shipRotation)
love.graphics.setColor(self.thrusterColor)
love.graphics.setLineWidth(2)
if self.direction == "forward" then
love.graphics.line(shipX + self.thrusterX*math.cos(shipRotation) - self.thrusterY*math.sin(shipRotation),
shipY + self.thrusterX*math.sin(shipRotation) + self.thrusterY*math.cos(shipRotation),

View File

@@ -1,9 +1,9 @@
function love.conf(t)
t.identity = nil -- The name of the save directory (string)
t.identity = "SpaceLove" -- The name of the save directory (string)
--t.version = "0.9.0" -- The LÖVE version this game was made for (string)
t.console = true -- Attach a console (boolean, Windows only)
t.window.title = "SpaceLove" -- The window title (string)
t.window.title = "SpaceLove v0.0.0.whatever" -- The window title (string)
t.window.icon = nil -- Filepath to an image to use as the window's icon (string)
t.window.width = 1280 -- The window width (number)
t.window.height = 720 -- The window height (number)

View File

@@ -1,9 +1,3 @@
local player
local Render = {
jitter = false,
jitterLevel = 2,
}
--very very temporary
console = {
e = function() end,
@@ -11,36 +5,42 @@ console = {
}
-- Classes
local Player = require "player"
local Player = require "Player"
local Ship = require "Ship"
local Hull = require "Hull"
local Shield = require "Shield"
local Engine = require "Engine"
local Thruster = require "Thruster"
function love.load()
-- Create the player
local Debug = require "Debug"
-- GLOBALS
Render = {
jitter = false,
jitterLevel = 2,
debugFont = love.graphics.newFont(10),
hudFont = love.graphics.newFont("fonts/Audimat Mono Regular.ttf", 16),
}
player = Player(Ship(
Hull("images/fighter1.png", 1.5),
Hull("images/fighter1.png", 1.5, 16, 10000),
Shield("images/shieldhit1.png", 1.6),
Engine(40000, 0.95, 10, 7.5, 33, 0.93, 0.5,
Engine(40000, 0.95, 10, 7.5, 33, 0.93, 0.5),
{
Thruster(0, "rotateleft", 4.4, -16, {250, 200, 200}, 0.4),
Thruster(0, "rotateleft", -11, 14.3, {255, 230, 230}, 0.6),
Thruster(0, "rotateleft", 13, 11, {250, 200, 200}, 0.6),
Thruster(0, "rotateright", -4.8, -16, {250, 200, 200}, 0.4),
Thruster(0, "rotateright", 10.4, 14.3, {255, 230, 230}, 0.6),
Thruster(0, "rotateright", -13.6, 11, {250, 200, 200}, 0.6),
}),
{
Thruster(1300, "forward", -8.5, 16.5, {160, 250, 255}, 2.4),
Thruster(1300, "forward", 8.5, 16.5, {160, 250, 255}, 2.4),
Thruster(650, "backward", -13, 1.8, {200, 240, 255}, 1.8),
Thruster(650, "backward", 13, 1.8, {200, 240, 255}, 1.8),
Thruster(320, "left", 6, -4.4, {220, 230, 250}, 0.6),
Thruster(540, "left", 17.5, 5.5, {220, 230, 250}, 1.2),
Thruster(320, "right", -6, -4.4, {220, 230, 250}, 0.6),
Thruster(540, "right", -17.5, 5.5, {220, 230, 250}, 1.2),
Thruster(1300, "forward", -8.5, 16.5, {160, 250, 255}, 2.4, 0.7),
Thruster(1300, "forward", 8.5, 16.5, {160, 250, 255}, 2.4, 0.7),
Thruster(650, "backward", -13, 1.8, {200, 240, 255}, 1.8, 0.8),
Thruster(650, "backward", 13, 1.8, {200, 240, 255}, 1.8, 0.8),
Thruster(320, "left", 6, -4.4, {220, 230, 250}, 0.6, 1),
Thruster(540, "left", 17.5, 5.5, {220, 230, 250}, 1.2, 0.9),
Thruster(320, "right", -6, -4.4, {220, 230, 250}, 0.6, 1),
Thruster(540, "right", -17.5, 5.5, {220, 230, 250}, 1.2, 0.9),
Thruster(90, "rotateleft", 4.4, -16, {250, 200, 200}, 0.4, 1.1),
Thruster(110, "rotateleft", -11, 14.3, {255, 230, 230}, 0.6, 1),
Thruster(110, "rotateleft", 13, 11, {250, 200, 200}, 0.6, 1),
Thruster(90, "rotateright", -4.8, -16, {250, 200, 200}, 0.4, 1.1),
Thruster(110, "rotateright", 10.4, 14.3, {255, 230, 230}, 0.6, 1),
Thruster(110, "rotateright", -13.6, 11, {250, 200, 200}, 0.6, 1),
},
{
x = love.graphics.getWidth() / 2,
@@ -54,18 +54,12 @@ function love.load()
}
))
--font!
love.graphics.setNewFont("fonts/Audimat Mono Regular.ttf", 16)
--line width!
love.graphics.setLineWidth(2)
end
function love.update(dt)
-- player rotation input
if love.keyboard.isDown('q') or love.keyboard.isDown('a') then
player.Ship.rotationSpeed = player.Ship.rotationSpeed - player.Ship.Engine.rotationImpulse * dt
player.Ship:rotate("rotateleft", dt)
elseif love.keyboard.isDown('e') or love.keyboard.isDown('d') then
player.Ship.rotationSpeed = player.Ship.rotationSpeed + player.Ship.Engine.rotationImpulse * dt
player.Ship:rotate("rotateright", dt)
end
-- player rotationSpeed adjustment
@@ -114,16 +108,8 @@ function love.update(dt)
end
function love.draw()
-- FPS in title
--change this to be a line displayed on screen
love.window.setTitle("FPS: "..love.timer.getFPS())
-- Red Lines for debug purposes
---[[
love.graphics.setColor(255, 0, 0)
love.graphics.line(love.graphics.getWidth() / 2, 0, love.graphics.getWidth() / 2, love.graphics.getHeight())
love.graphics.line(0, love.graphics.getHeight() / 2, love.graphics.getWidth(), love.graphics.getHeight() / 2)
--]]
-- Debug below!
Debug:drawBelow()
-- Ship draw
love.graphics.setColor(255, 255, 255)
@@ -138,56 +124,55 @@ function love.draw()
--love.graphics.draw(player.shieldhit, player.x - player.shieldHalfWidth, player.y - player.shieldHalfHeight, player.currentRotation - (player.currentRotation % player.degreeLock), player.shieldScale, player.shieldScale, player.shieldHalfWidth, player.shieldHalfHeight)
-- Thruster draws
if love.keyboard.isDown('w') then
if love.keyboard.isDown('w') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "forward" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
elseif love.keyboard.isDown('s') then
elseif love.keyboard.isDown('s') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "backward" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
end
if love.keyboard.isDown('j') then
if love.keyboard.isDown('j') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "left" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
elseif love.keyboard.isDown('l') then
elseif love.keyboard.isDown('l') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "right" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
end
if love.keyboard.isDown('q') or love.keyboard.isDown('a') then
for i=1,#player.Ship.Engine.Thrusters do
if player.Ship.Engine.Thrusters[i].direction == "rotateleft" then
player.Ship.Engine.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
if love.keyboard.isDown('q') and player.Ship.Hull.fuelAmount > 0 or love.keyboard.isDown('a') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "rotateleft" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
elseif love.keyboard.isDown('e') or love.keyboard.isDown('d') then
for i=1,#player.Ship.Engine.Thrusters do
if player.Ship.Engine.Thrusters[i].direction == "rotateright" then
player.Ship.Engine.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
elseif love.keyboard.isDown('e') and player.Ship.Hull.fuelAmount > 0 or love.keyboard.isDown('d') and player.Ship.Hull.fuelAmount > 0 then
for i=1,#player.Ship.Thrusters do
if player.Ship.Thrusters[i].direction == "rotateright" then
player.Ship.Thrusters[i]:draw(player.Ship.x, player.Ship.y, player.Ship.currentRotation - (player.Ship.currentRotation % player.Ship.Engine.degreeLock))
end
end
end
-- Fuel UI draw
love.graphics.setColor(175, 255, 255)
love.graphics.setFont(Render.hudFont)
love.graphics.print("FUEL", 3, love.graphics.getHeight() - 30)
love.graphics.setLineWidth(2)
love.graphics.rectangle("line", 3, love.graphics.getHeight() - 13, 125, 10)
love.graphics.setColor(175, 255, 25) --change to be based on fuel amount
love.graphics.rectangle("fill", 4, love.graphics.getHeight() - 12, 120, 8) --max width is 123
love.graphics.rectangle("fill", 4, love.graphics.getHeight() - 12, player.Ship.Hull.fuelAmount / player.Ship.Hull.fuelCapacity * 123, 8) --max width is 123
-- Pink arc for debug purposes
---[[
love.graphics.setColor(255, 100, 100)
love.graphics.arc("fill", player.Ship.x, player.Ship.y, 3, 0, math.pi*2, 20)
--]]
-- Debug above!
Debug:drawAbove()
end