fysplane/level_state.lua

210 lines
5 KiB
Lua
Raw Normal View History

2014-10-18 11:23:10 +00:00
Gamestate = require 'hump.gamestate'
require 'level'
require 'player'
2014-10-18 20:27:40 +00:00
require 'scoreboard'
2014-10-18 22:46:27 +00:00
require 'entities/chaingunpowerup'
require 'entities/plane'
require 'chaingunmode'
2014-10-18 22:51:57 +00:00
require 'entities/plane'
2014-10-19 00:08:25 +00:00
require 'machinegun'
2014-10-18 11:23:10 +00:00
level_state = {}
local players = {
[1] = Player(1, 'flux'),
[2] = Player(2, 'Nicd')
}
2014-10-18 20:27:40 +00:00
local scoreboards = {}
local level_music = nil
local current_level = nil
2014-10-18 18:59:00 +00:00
local paused = false
2014-10-18 11:23:10 +00:00
2014-10-18 22:46:27 +00:00
local POWERUP_POSSIBILITY = 0.001
2014-10-18 11:23:10 +00:00
function level_state:init()
end
function level_state:enter(previous, level_file)
entity_id = 1
if level_music == nil then
2014-10-19 00:51:26 +00:00
level_music = love.audio.newSource("resources/audio/Beavis II.mp3")
level_music:setVolume(0.5)
level_music:setLooping(true)
level_music:play()
2014-10-18 11:23:10 +00:00
end
love.graphics.setBackgroundColor({0, 0, 0, 255})
current_level = Level()
current_level.world:setCallbacks(begin_contact, end_contact, pre_solve, post_solve)
players[1]:setPlane(current_level:getPlane(1))
2014-10-18 22:46:27 +00:00
players[2]:setPlane(current_level:getPlane(2))
2014-10-18 20:27:40 +00:00
-- Set up scoreboards
for i = 1, #players, 1 do
local i0 = i - 1
local x = 20 + (i0 * SCOREBOARD_WIDTH) + (i0 * SCOREBOARD_MARGIN)
scoreboards[i] = Scoreboard(x, 20, players[i])
end
2014-10-18 22:46:27 +00:00
ChaingunPowerUp(750, 750, current_level)
2014-10-18 11:23:10 +00:00
end
function level_state:draw()
2014-10-18 20:27:40 +00:00
current_level:drawBackground()
for i = 1, #scoreboards, 1 do
scoreboards[i]:draw()
end
2014-10-18 11:23:10 +00:00
current_level:drawEntities()
end
function level_state:update(dt)
if current_level.reset == true then
resetLevel()
return
end
2014-10-18 22:46:27 +00:00
-- Generate powerups
local r = love.math.random()
if r <= POWERUP_POSSIBILITY then
local x = love.math.random(love.window.getWidth())
local y = love.math.random(love.window.getHeight())
ChaingunPowerUp(x, y, current_level)
end
2014-10-18 18:59:00 +00:00
if not paused then
current_level:updateEntities(dt)
end
2014-10-19 00:56:32 +00:00
2014-10-19 01:00:00 +00:00
if players[1]:getPlane() == nil then
print("HOP")
2014-10-19 00:56:32 +00:00
current_level:respawnPlayer(1)
players[1]:setPlane(current_level:getPlane(1))
end
2014-10-19 01:00:00 +00:00
if players[2]:getPlane() == nil then
print("HIP")
2014-10-19 00:56:32 +00:00
current_level:respawnPlayer(2)
players[2]:setPlane(current_level:getPlane(2))
end
2014-10-18 11:23:10 +00:00
end
function level_state:focus(bool)
end
function level_state:leave(bool)
current_level:delete()
current_level = nil
end
function level_state:keypressed(key, unicode)
print('Somebody pressed ' .. key)
2014-10-18 11:23:10 +00:00
-- Ctrl + R restarts current level.
if key == "r"
2014-10-18 12:59:39 +00:00
and (love.keyboard.isDown("lctrl")
or love.keyboard.isDown("rctrl")) then
2014-10-18 11:23:10 +00:00
current_level.reset = true
2014-10-18 18:59:00 +00:00
elseif key == " " then
paused = not paused
2014-10-18 11:23:10 +00:00
else
for id, player in pairs(players) do
player:press(key)
end
2014-10-18 11:23:10 +00:00
end
end
function level_state:keyreleased(key, unicode)
print('Somebody released ' .. key)
2014-10-18 11:43:22 +00:00
for id, player in pairs(players) do
player:release(key)
end
2014-10-18 11:23:10 +00:00
end
function level_state:mousepressed(x, y, button)
end
function begin_contact(a, b, coll)
2014-10-18 22:46:27 +00:00
local aObj = a:getUserData()
local bObj = b:getUserData()
if aObj ~= nil and bObj ~= nil then
if aObj:isinstance(Plane) and bObj:isinstance(ChaingunPowerUp) then
bObj.deleteLater = true
aObj:setPowerUpMode(ChaingunMode())
2014-10-18 23:37:41 +00:00
coll:setEnabled(false)
2014-10-18 22:46:27 +00:00
elseif bObj:isinstance(Plane) and aObj:isinstance(ChaingunPowerUp) then
aObj.deleteLater = true
bObj:setPowerUpMode(ChaingunMode())
2014-10-18 23:37:41 +00:00
coll:setEnabled(false)
2014-10-18 22:51:57 +00:00
else
aObj:wasHit()
bObj:wasHit()
2014-10-19 00:08:25 +00:00
local plane = nil
local other = nil
if aObj:isinstance(Plane) then
plane = aObj
other = bObj
elseif bObj:isinstance(Plane) then
plane = bObj
other = aObj
end
2014-10-19 01:35:07 +00:00
if other and other:isinstance(Plane) then
plane:receiveDamage(1000)
other:receiveDamage(1000)
2014-10-19 01:45:38 +00:00
plane:getOwner():addScore(SUICIDE_SCORE)
other:getOwner():addScore(SUICIDE_SCORE)
2014-10-19 01:35:07 +00:00
elseif plane then
2014-10-19 00:08:25 +00:00
if other:getOwner() == nil then
-- Collision with ground or other plane
plane:receiveDamage(1000)
2014-10-19 01:45:38 +00:00
plane:getOwner():addScore(SUICIDE_SCORE)
2014-10-19 00:08:25 +00:00
elseif other:getOwner().id ~= plane.id then
for key, gun in pairs(GUNS) do
2014-10-19 01:45:38 +00:00
if other:isinstance(gun['projectile']) and plane.health > 0 then
2014-10-19 00:08:25 +00:00
plane:receiveDamage(gun['damage'])
2014-10-19 01:45:38 +00:00
if plane.health == 0 then
other:getOwner():getOwner():addScore(KILL_SCORE)
end
2014-10-19 00:08:25 +00:00
end
end
end
end
2014-10-18 22:46:27 +00:00
end
end
2014-10-18 11:23:10 +00:00
end
function end_contact(a, b, coll)
end
function pre_solve(a, b, coll)
end
function end_solve(a, b, coll)
end
function resetLevel()
Gamestate.switch(level_state)
end