2014-10-18 11:23:10 +00:00
|
|
|
Class = require 'hump.class'
|
2014-10-18 12:14:51 +00:00
|
|
|
require 'entities/rectangle'
|
2014-10-20 19:20:58 +00:00
|
|
|
require 'entities/ground'
|
2014-10-18 12:49:39 +00:00
|
|
|
require 'entities/plane'
|
2014-10-19 00:27:25 +00:00
|
|
|
require 'entities/physicsentity'
|
2014-10-18 11:23:10 +00:00
|
|
|
require 'settings'
|
|
|
|
|
|
|
|
-- A level manages the level datastructure containing static blocks and level
|
|
|
|
-- specific data like global physics
|
|
|
|
|
|
|
|
-- Reset color to this before drawing each object
|
2014-10-18 20:27:40 +00:00
|
|
|
local draw_base_color = {255, 255, 255, 255}
|
2014-10-18 11:23:10 +00:00
|
|
|
|
|
|
|
Level = Class{
|
|
|
|
init = function(self)
|
|
|
|
self.name = 'Default'
|
|
|
|
self.entity_list = {}
|
|
|
|
|
|
|
|
self.world = love.physics.newWorld(GRAVITY_X, GRAVITY_Y, true)
|
2014-10-18 20:27:40 +00:00
|
|
|
|
2014-10-18 15:31:00 +00:00
|
|
|
-- Draw background to canvas so we don't redraw it every time
|
2014-10-26 18:36:43 +00:00
|
|
|
self.oldWidth, self.oldHeight = 0, 0
|
|
|
|
self:updateBackground()
|
2014-10-18 12:14:51 +00:00
|
|
|
|
2014-10-19 00:56:32 +00:00
|
|
|
self.makePlanes = { [1] = function()
|
2014-10-21 18:01:30 +00:00
|
|
|
return Plane(100, 400, INITIAL_PLANE_SPEED, 0,
|
2014-10-20 18:08:28 +00:00
|
|
|
255, 0, 0, -- red
|
|
|
|
self)
|
2014-10-19 00:56:32 +00:00
|
|
|
end,
|
|
|
|
[2] = function()
|
2014-10-26 18:36:43 +00:00
|
|
|
return Plane(levelWidth() - 100 - 100, 300, -INITIAL_PLANE_SPEED, 0,
|
2014-10-20 18:08:28 +00:00
|
|
|
0, 255, 0, -- green
|
|
|
|
self)
|
2014-10-19 00:56:32 +00:00
|
|
|
end }
|
|
|
|
|
|
|
|
self.planes = { [1] = self.makePlanes[1](),
|
|
|
|
[2] = self.makePlanes[2]() }
|
2014-10-26 18:36:43 +00:00
|
|
|
|
2014-10-20 19:20:58 +00:00
|
|
|
Ground(self);
|
2014-10-18 11:23:10 +00:00
|
|
|
end;
|
|
|
|
|
2014-10-26 18:36:43 +00:00
|
|
|
updateBackground = function(self)
|
|
|
|
local w, h = levelWidth(), levelHeight()
|
|
|
|
if w ~= self.oldWidth or h ~= self.oldHeight then
|
|
|
|
local background = love.graphics.newImage('resources/graphics/sky.png')
|
|
|
|
self.bgCanvas = love.graphics.newCanvas()
|
|
|
|
love.graphics.setCanvas(self.bgCanvas)
|
|
|
|
love.graphics.setColor(255, 255, 255, 255)
|
|
|
|
love.graphics.draw(background, 0, 0, 0,
|
|
|
|
levelWidth() / background:getWidth(),
|
|
|
|
levelHeight() / background:getHeight())
|
|
|
|
love.graphics.setCanvas()
|
|
|
|
|
|
|
|
self.oldWidth, self.oldHeight = w, h
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2014-10-19 00:56:32 +00:00
|
|
|
respawnPlayer = function(self, playerIdx)
|
|
|
|
self.planes[playerIdx] = self.makePlanes[playerIdx]()
|
|
|
|
end;
|
|
|
|
|
|
|
|
getPlane = function(self, playerIdx)
|
|
|
|
return self.planes[playerIdx]
|
2014-10-18 16:44:52 +00:00
|
|
|
end;
|
|
|
|
|
2014-10-18 11:23:10 +00:00
|
|
|
delete = function(self)
|
|
|
|
for key, entity in pairs(self.entity_list) do
|
|
|
|
entity:delete()
|
|
|
|
end
|
|
|
|
self.entity_list = nil
|
|
|
|
end;
|
|
|
|
|
|
|
|
activate = function(self)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
drawEntities = function(self)
|
|
|
|
for key, entity in pairs(self.entity_list) do
|
|
|
|
love.graphics.setColor(draw_base_color)
|
|
|
|
entity:draw()
|
|
|
|
end
|
|
|
|
love.graphics.setColor(draw_base_color)
|
|
|
|
end;
|
|
|
|
|
2014-10-18 20:27:40 +00:00
|
|
|
drawBackground = function(self)
|
2014-10-26 18:36:43 +00:00
|
|
|
self:updateBackground()
|
2014-10-18 20:27:40 +00:00
|
|
|
love.graphics.setColor(draw_base_color)
|
|
|
|
love.graphics.draw(self.bgCanvas)
|
|
|
|
end;
|
|
|
|
|
2014-10-18 11:23:10 +00:00
|
|
|
updateEntities = function(self, dt)
|
|
|
|
self.world:update(dt)
|
|
|
|
|
|
|
|
for key, entity in pairs(self.entity_list) do
|
2014-10-19 00:27:25 +00:00
|
|
|
if entity:isinstance(PhysicsEntity) then
|
2014-10-20 18:08:41 +00:00
|
|
|
local jump_window = 70
|
|
|
|
local jump_amount = 50
|
2014-10-26 18:36:43 +00:00
|
|
|
while entity.body:getX() > levelWidth() + jump_window do
|
|
|
|
entity.body:setX(entity.body:getX() - levelWidth() - jump_window - jump_amount)
|
2014-10-19 00:27:25 +00:00
|
|
|
end
|
2014-10-20 18:08:41 +00:00
|
|
|
while entity.body:getX() < -jump_window do
|
2014-10-26 18:36:43 +00:00
|
|
|
entity.body:setX(entity.body:getX() + levelWidth() + jump_window + jump_amount)
|
2014-10-19 00:27:25 +00:00
|
|
|
end
|
2014-10-18 22:03:42 +00:00
|
|
|
end
|
2014-10-18 11:23:10 +00:00
|
|
|
entity:update(dt)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
}
|