fysplane/level.lua

95 lines
2.9 KiB
Lua
Raw Normal View History

2014-10-18 11:23:10 +00:00
Class = require 'hump.class'
2014-10-18 12:14:51 +00:00
require 'entities/rectangle'
require 'entities/plane'
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
-- Draw background to canvas so we don't redraw it every time
self.background = love.graphics.newImage('resources/graphics/sky.png')
self.bgCanvas = love.graphics.newCanvas()
love.graphics.setCanvas(self.bgCanvas)
love.graphics.draw(self.background)
love.graphics.setCanvas()
2014-10-18 12:14:51 +00:00
2014-10-19 00:56:32 +00:00
self.makePlanes = { [1] = function()
return Plane(100, 100, INITIAL_PLANE_SPEED, 0, self)
end,
[2] = function()
return Plane(love.window.getWidth() - 100 - 100, 100, -INITIAL_PLANE_SPEED, 0, self)
end }
self.planes = { [1] = self.makePlanes[1](),
[2] = self.makePlanes[2]() }
2014-10-18 12:59:39 +00:00
self:insertGround()
2014-10-18 11:23:10 +00:00
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]
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)
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
if entity:isinstance(PhysicsEntity) then
while entity.body:getX() > love.window.getWidth() + 200 do
entity.body:setX(entity.body:getX() - love.window.getWidth() - 300)
end
while entity.body:getX() < -200 do
entity.body:setX(entity.body:getX() + love.window.getWidth() + 300)
end
2014-10-18 22:03:42 +00:00
end
2014-10-18 11:23:10 +00:00
entity:update(dt)
end
end;
2014-10-18 12:59:39 +00:00
insertGround = function(self)
groundImg = love.graphics.newImage('tiles/grasstop.png')
2014-10-19 01:35:07 +00:00
for i = -10, love.window.getWidth() / 16 + 10, 1 do
2014-10-18 13:05:34 +00:00
Rectangle(i * 16, love.window.getHeight(), self, "static", 0, 16, 16, 0, groundImg)
2014-10-18 12:59:39 +00:00
end
end;
2014-10-18 11:23:10 +00:00
}