You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
love2d-tank/main.lua

217 lines
4.7 KiB

Object = require("libs/classic")
4 months ago
require("constants")
WF = require("libs/windfield")
STI = require("libs/sti")
World = WF.newWorld(0, 0) --no gravity
--World:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames
World:addCollisionClass("Player1")
World:addCollisionClass("Bullet1")
World:addCollisionClass("Player2")
World:addCollisionClass("Bullet2")
World:addCollisionClass("Wall")
3 months ago
World:addCollisionClass("PowerUp")
3 months ago
require("gui")
3 months ago
require("powerups/powerup")
require("powerups/speed")
require("restart")
3 months ago
require("truncate")
require("player")
require("bullet")
require("mapsloader")
require("Game/UpdateGame")
require("Menu/UpdateMenu")
require("Pause/UpdatePause")
require("Win/UpdateWin")
require("Game/DrawGame")
require("Menu/DrawMenu")
require("Pause/DrawPause")
require("Win/DrawWin")
require("Game/GameKeyPressed")
require("Menu/MenuKeyPressed")
require("Pause/PauseKeyPressed")
require("Win/WinKeyPressed")
4 months ago
--Fonts used in the game
GameFont = love.graphics.newFont("assets/Daydream.ttf", 60)
DebugFont = love.graphics.newFont("assets/Daydream.ttf", 12)
MenuFont = love.graphics.newFont("assets/Daydream.ttf", 45)
Walls = {}
-- Music streaming
musicMenu = love.audio.newSource("music/menu.mp3", "stream") or nil
musicBattle = love.audio.newSource("music/battle.mp3", "stream") or nil
--musicStory = love.audio.newSource("music/story.mp3", "stream") or nil
musicPause = musicBattle:clone()
musicPause:setFilter({
type = "lowpass",
volume = 0.7,
highgain = 0.4,
})
function love.run()
4 months ago
if love.load then
love.load(love.arg.parseGameArguments(arg), arg)
end
-- We don't want the first frame's dt to include time taken by love.load.
4 months ago
if love.timer then
love.timer.step()
end
local dt = 0
-- Main loop time.
return function()
-- Process events.
if love.event then
love.event.pump()
4 months ago
for name, a, b, c, d, e, f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
4 months ago
love.handlers[name](a, b, c, d, e, f)
end
end
-- Update dt, as we'll be passing it to update
4 months ago
if love.timer then
dt = love.timer.step()
end
-- Call update and draw
4 months ago
if love.update then
love.update(dt)
end -- will pass 0 if love.timer is disabled
if love.graphics and love.graphics.isActive() then
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
4 months ago
if love.draw then
love.draw()
end
love.graphics.present()
end
4 months ago
if love.timer then
love.timer.sleep(0.001)
end
end
end
3 months ago
function love.resized()
--TOD0: fix scaling!
end
function love.load()
4 months ago
-- Set a random seed
love.math.setRandomSeed(love.timer.getTime())
3 months ago
-- Figure out the X_SCALE and Y_SCALE
local curWidth, curHeight = love.graphics.getDimensions()
_G.X_SCALE = truncate(curWidth / GAME_WORLD_DIM_WIDTH, 3)
_G.Y_SCALE = truncate(curHeight / GAME_WORLD_DIM_HEIGHT, 3)
--Game values (reset after each load)
7 months ago
HEALTH = 3
P1_DELAY = 0.5
P2_DELAY = 0.5
--Bullet lists
Bullets1 = {}
Bullets2 = {}
4 months ago
3 months ago
--PowerUp stuff
PowerUps = {}
PUTimeToPlace = _G.PU_TIMER
7 months ago
DebugFlag = false
EnableKeyPress1 = true
KeyPressTime1 = 0
KeyDelay1 = P1_DELAY
7 months ago
EnableKeyPress2 = true
KeyPressTime2 = 0
KeyDelay2 = P2_DELAY
7 months ago
3 months ago
--TODO: two player speeds for upgrades
3 months ago
UserPlayer1 = Player(1, 1000, 100, HEALTH, "assets/player1.png", 12000)
UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", 12000)
--STI Map loading
4 months ago
LoadMap(_G.CUR_LEVEL)
7 months ago
end
function love.keypressed(key)
if _G.GAMESTATE == "MENU" then
MenuKeyPressed(key)
end
if _G.GAMESTATE == "PAUSE" then
PauseKeyPressed(key)
end
if _G.GAMESTATE == "GAME" then
GameKeyPressed(key)
end
if _G.GAMESTATE == "WIN" then
WinKeyPressed(key)
end
7 months ago
end
function love.update(dt)
if _G.GAMESTATE == "MENU" then
UpdateMenu(dt)
-- Handle music
3 months ago
if not musicMenu:isPlaying() and not _G.MUTED then
musicMenu:setVolume(0.5)
love.audio.play(musicMenu)
end
end
if _G.GAMESTATE == "PAUSE" then
UpdatePause(dt)
-- Handle music
3 months ago
if not musicPause:isPlaying() and not _G.MUTED then
4 months ago
musicBattle:setVolume(0)
musicPause:setVolume(0.5)
love.audio.play(musicBattle)
love.audio.play(musicPause)
end
end
if _G.GAMESTATE == "GAME" then
UpdateGame(dt)
-- Handle music
3 months ago
if not musicBattle:isPlaying() and not _G.MUTED then
4 months ago
musicBattle:setVolume(0.5)
musicPause:setVolume(0)
love.audio.play(musicBattle)
love.audio.play(musicPause)
end
end
4 months ago
if _G.GAMESTATE == "WIN" then
UpdateWin(dt)
4 months ago
end
7 months ago
end
function love.draw()
if _G.GAMESTATE == "MENU" then
DrawMenu()
7 months ago
end
if _G.GAMESTATE == "PAUSE" then
DrawPause()
end
if _G.GAMESTATE == "GAME" then
DrawGame()
end
if _G.GAMESTATE == "WIN" then
DrawWin()
end
7 months ago
end