scenes/levels: split world object

This commit is contained in:
Kazhnuz 2019-03-03 15:27:34 +01:00
parent 3ce78cacc5
commit b6d5f19a13
5 changed files with 53 additions and 45 deletions

View File

@ -2,7 +2,7 @@ local CameraSystem = Object:extend()
function CameraSystem:new(scene, x, y)
self.scene = scene
self.world = scene
self.world = scene.world
self.view = Camera(x, y, 1, 0, true)
--local width, height = love.graphics.getDimensions()
local width, height, flags = love.window.getMode( )

View File

@ -1,6 +1,6 @@
Level = Object:extend() -- On créer la classe des entitées, c'est la classe de base
require "scenes.levels.controller.world"
local World = require "scenes.levels.controller.world"
local Camera = require "scenes.levels.controller.camera"
require "scenes.levels.controller.debug"
PlayerManager = require "scenes.levels.controller.players"
@ -47,7 +47,8 @@ function Level:loadMission(levelID, missionID)
self.startx = missiondatas.startx
self.starty = missiondatas.starty
self:initWorld()
self.world = World(self, self.mapfile)
self.world:load()
assets:setMusic(missiondatas.music)
self:launchMission()
@ -74,7 +75,7 @@ function Level:update(dt)
self.keys = core.input.keys
if (self.pause == false) then
self.playermanager:update(dt)
self:updateWorld(dt)
self.world:update(dt)
assets:update(dt)
self.camera:update(dt)
end
@ -89,7 +90,7 @@ function Level:draw(dt)
-- Ona attache puis détache la caméra pour dessiner le monde, afin que celui
-- reste "fixe" tandis que le jouer bouge.
self.camera:floorCoord()
self:drawWorld()
self.world:draw()
if (self.pause == false) then
self:drawHUD()

View File

@ -6,7 +6,7 @@ function PlayerManager:new(scene)
self.players = {}
self.deathTimer = -100
self.activePlayer = -1
self.startx, self.starty = self.scene:getStartPosition()
self.startx, self.starty = self.scene.world:getStartPosition()
end
function PlayerManager:addPlayer(pigID)
@ -27,7 +27,7 @@ function PlayerManager:spawnPlayer(playerID)
end
function PlayerManager:getPlayers()
local itemList = self.scene:getEntities()
local itemList = self.scene.world:getEntities()
local playerList = {}
for i,v in ipairs(itemList) do
@ -40,7 +40,7 @@ function PlayerManager:getPlayers()
end
function PlayerManager:getPlayerByID(id)
local itemList = self.scene:getEntities()
local itemList = self.scene.world:getEntities()
local player
if (id == nil) then

View File

@ -1,21 +1,26 @@
local World = Object:extend()
local Obj = require "scenes.levels.entities"
-- INIT FUNCTIONS
-- All functions to init the world and the map
function Level:initWorld()
self.map = Sti("assets/maps/" .. self.mapfile .. ".lua")
function World:new(scene, mapfile)
self.scene = scene
self.map = Sti("assets/maps/" .. mapfile .. ".lua")
self.obj = Obj
self.entities = Bump.newWorld(50)
self.backcolor = self.map.backgroundcolor or {0, 0, 0}
self.activeObjects = 0
end
function World:load()
self:loadCollisions()
self:loadEntities()
end
function Level:loadEntities()
function World:loadEntities()
for k, objectlayer in pairs(self.map.layers) do
if (self:isEntityLayer(objectlayer.name)) then
for k, object in pairs(objectlayer.objects) do
@ -27,11 +32,11 @@ function Level:loadEntities()
end
end
function Level:isEntityLayer(layername)
function World:isEntityLayer(layername)
return (self.obj.index[layername] ~= nil)
end
function Level:addEntityByNameOnGrid(name, x, y, w, h)
function World:addEntityByNameOnGrid(name, x, y, w, h)
local objWidth, objHeight = 16, 16
local cellHor = math.ceil(w / objWidth)
local cellVert = math.ceil(h / objHeight)
@ -42,11 +47,11 @@ function Level:addEntityByNameOnGrid(name, x, y, w, h)
end
end
function Level:addEntityByName(name, x, y)
self.obj.index[name](self, x, y)
function World:addEntityByName(name, x, y)
self.obj.index[name](self.scene, x, y)
end
function Level:loadCollisions()
function World:loadCollisions()
for k, objectlayer in pairs(self.map.layers) do
if self:isCollisionLayer(objectlayer.name) then
for k, object in pairs(objectlayer.objects) do
@ -57,7 +62,7 @@ function Level:loadCollisions()
end
end
function Level:getStartPosition()
function World:getStartPosition()
local startx, starty
for k, objectlayer in pairs(self.map.layers) do
if objectlayer.name == "playerstart" then
@ -71,72 +76,72 @@ function Level:getStartPosition()
return startx, starty
end
function Level:isCollisionLayer(layername)
function World:isCollisionLayer(layername)
return (self.obj.collisions[layername] == true)
end
function Level:addCollision(x, y, w, h, name)
function World:addCollision(x, y, w, h, name)
-- Rajouter un block solide dans le monde physique
if w == 0 then
w = 1
end
self.obj.Collision(self, x, y, w, h, name)
self.obj.Collision(self.scene, x, y, w, h, name)
end
-- ENTITY MANAGEMENT FUNCTIONS
-- All BUMP2D wrappers
function Level:addEntity(entity)
function World:addEntity(entity)
return self.entities:add(entity, entity.x, entity.y, entity.w, entity.h)
end
function Level:moveEntity(entity, x, y, filter)
function World:moveEntity(entity, x, y, filter)
return self.entities:move(entity, x, y, filter)
end
function Level:removeEntity(entity)
function World:removeEntity(entity)
return self.entities:remove(entity)
end
function Level:queryRect(x, y, w, h)
function World:queryRect(x, y, w, h)
return self.entities:queryRect(x, y, w, h)
end
function Level:countEntities()
function World:countEntities()
return self.entities:countItems()
end
function Level:getEntities()
function World:getEntities()
return self.entities:getItems()
end
-- MAP FUNCTIONS
-- All map wrappers
function Level:getDimensions()
function World:getDimensions()
return self.map.width * self.map.tilewidth,
self.map.height * self.map.tileheight
end
function Level:setBackgroundColor(r, g, b)
function World:setBackgroundColor(r, g, b)
self.backcolor = {r, g, b}
end
function Level:getBackgroundColor()
function World:getBackgroundColor()
return self.backcolor[1]/256, self.backcolor[2]/256, self.backcolor[3]/256
end
-- UPDATE FUNCTIONS
-- All update functions
function Level:updateWorld(dt)
function World:update(dt)
self:updateEntities(dt)
self:updateMap(dt)
end
function Level:updateEntities(dt)
function World:updateEntities(dt)
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self.camera:getVisibleEntities()
local visibleThings, len = self.scene.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
@ -146,24 +151,24 @@ function Level:updateEntities(dt)
self.activeObjects = len
end
function Level:updateMap(dt)
function World:updateMap(dt)
self.map:update(dt)
end
-- DRAW FUNCTIONS
-- All function to draw the map, world and entities
function Level:drawWorld()
function World:draw()
self:drawBackgroundColor()
self.camera:attach()
self.scene.camera:attach()
self:drawMap()
self:drawEntities()
self.camera:detach()
self.scene.camera:detach()
end
function Level:drawEntities()
function World:drawEntities()
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self.camera:getVisibleEntities()
local visibleThings, len = self.scene.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
@ -171,9 +176,9 @@ function Level:drawEntities()
end
end
function Level:drawMap()
local tx, ty = self.camera:getCoord()
local scale = self.camera:getScale()
function World:drawMap()
local tx, ty = self.scene.camera:getCoord()
local scale = self.scene.camera:getScale()
local tx = tx
local ty = ty
@ -183,9 +188,11 @@ function Level:drawMap()
self.map:draw(-tx, -ty, scale, scale)
end
function Level:drawBackgroundColor()
function World:drawBackgroundColor()
local r, g, b = self.backcolor[1], self.backcolor[2], self.backcolor[3]
love.graphics.setColor(r/256, g/256, b/256)
love.graphics.rectangle("fill", 0, 0, 480, 272)
utils.graphics.resetColor()
end
return World

View File

@ -8,10 +8,10 @@ function Entity:new(level, collType, x, y, w, h) -- On enregistre une nouvelle e
end
function Entity:initPhysics(level, collType, x, y, w, h)
self.level = level
self.world = level
self.level = level
self.world = level.world
self.camera = level.camera
self.obj = self.world.obj
self.obj = self.world.obj
self.collType = collType
self.gacc = 500