scenes/levels: some general cleanup

This commit is contained in:
Kazhnuz 2019-03-03 15:41:20 +01:00
parent fc53a491f6
commit 5d27343e29
4 changed files with 90 additions and 69 deletions

View File

@ -1,10 +1,13 @@
local CameraSystem = Object:extend()
-- INIT FUNCTIONS
-- Initialize the camera system
function CameraSystem:new(scene, x, y)
self.scene = 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( )
self.screenWidth = width
self.screenHeight = height
@ -15,10 +18,75 @@ function CameraSystem:new(scene, x, y)
self:limit()
end
-- WRAPPER and UTILS
-- Access data from the camera
function CameraSystem:floorCoord()
self.view.x, self.view.y = utils.math.floorCoord(self.view.x, self.view.y)
end
function CameraSystem:getCoord()
local camx, camy, camh, camw
camx = self.view.x - (self.viewWidth/2)
camy = self.view.y - (self.viewHeight/2)
camw = self.viewWidth
camh = self.viewHeight
return camx, camy, camw, camh
end
function CameraSystem:getScale()
return self.view.scale
end
function CameraSystem:getScreenCoord()
local camx, camy, camh, camw
camx = self.view.x - (self.screenWidth/2)
camy = self.view.y - (self.screenHeight/2)
camw = self.screenWidth
camh = self.screenHeight
return camx, camy, camw, camh
end
function CameraSystem:worldCoord(x, y, ox, oy, w, h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
return self.view:worldCoords(x, y, ox, oy, w, h)
end
function CameraSystem:cameraCoords(x, y)
return self.view:cameraCoords(x, y, ox, oy, w, h)
end
function CameraSystem:getVisibleEntities()
local camx, camy, camw, camh = self:getScreenCoord()
local visibleThings, len = self.world:queryRect(camx-64, camy-64, camw+128, camh+128)
return visibleThings, len
end
function CameraSystem:isInsideView(x, y, w, h, border)
local camx, camy, camw, camh = self:getCoord()
local border = border or 0
if (x + w + border >= camx) and (x < camx + camw + border)
and (y + h + border >= camy) and (y < camy + camh + border) then
return true
else
return false
end
end
function CameraSystem:attach()
self.view:attach()
end
function CameraSystem:detach()
self.view:detach()
end
-- UPDATE and MOVE functions
-- Move and update the camera system
function CameraSystem:update(dt)
self:followPlayer(self.scene.playermanager.activePlayer)
self:limit()
@ -55,10 +123,6 @@ function CameraSystem:followPlayer(id)
end
end
function CameraSystem:debugDrawCameraPlayerZone()
--
end
function CameraSystem:move(x, y)
self.view.x = x
self.view.y = y
@ -86,64 +150,4 @@ function CameraSystem:limit()
self.view.x, self.view.y = camx, camy
end
function CameraSystem:getCoord()
local camx, camy, camh, camw
camx = self.view.x - (self.viewWidth/2)
camy = self.view.y - (self.viewHeight/2)
camw = self.viewWidth
camh = self.viewHeight
return camx, camy, camw, camh
end
function CameraSystem:getScale()
return self.view.scale
end
function CameraSystem:getScreenCoord()
local camx, camy, camh, camw
camx = self.view.x - (self.screenWidth/2)
camy = self.view.y - (self.screenHeight/2)
camw = self.screenWidth
camh = self.screenHeight
return camx, camy, camw, camh
end
function CameraSystem:worldCoord(x, y, ox, oy, w, h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
return self.view:worldCoords(x, y, ox, oy, w, h)
end
function CameraSystem:cameraCoords(x, y)
return self.view:cameraCoords(x, y, ox, oy, w, h)
end
function CameraSystem:getVisibleEntities()
local camx, camy, camw, camh = self:getScreenCoord()
local visibleThings, len = self.world:queryRect(camx-64, camy-64, camw+128, camh+128)
return visibleThings, len
end
function CameraSystem:isInsideView(x, y, w, h, border)
local camx, camy, camw, camh = self:getCoord()
local border = border or 0
if (x + w + border >= camx) and (x < camx + camw + border)
and (y + h + border >= camy) and (y < camy + camh + border) then
return true
else
return false
end
end
function CameraSystem:attach()
self.view:attach()
end
function CameraSystem:detach()
self.view:detach()
end
return CameraSystem

View File

@ -4,6 +4,9 @@ local World = require "scenes.levels.controller.world"
local Camera = require "scenes.levels.controller.camera"
local PlayerManager = require "scenes.levels.controller.players"
-- INIT FUNCTIONS
-- Initialize and launch the level
function Level:new()
self:reset()
end
@ -64,6 +67,7 @@ function Level:launchMission()
end
-- UPDATE FUNCTIONS
-- Update the level
function Level:update(dt)
self.keys = core.input.keys
@ -77,12 +81,11 @@ end
-- DRAW FUNCTIONS
-- draw level elements
function Level:draw(dt)
love.graphics.setColor(1, 1, 1, 1) -- On reset la couleur.
utils.graphics.resetColor()
-- 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.world:draw()

View File

@ -13,6 +13,11 @@ function PlayerManager:new(scene)
self.gold = 0
end
-- PLAYER FUNCTIONS
-- Handle virtual players
-- TODO: Gérer la manière dont le joueur va avoir une équipe de cochons
function PlayerManager:addPlayer(pigID)
-- Enregistrer le joueur n'est pas le rajouter à une liste des objets qui existe,
-- mais juste insérer ses informations les plus importantes afin d'aider le jeu
@ -78,6 +83,9 @@ function PlayerManager:playerHaveObject(id)
end
end
-- UPDATE FUNCTIONS
-- Update the death timer and respawn the player when it's done
function PlayerManager:update(dt)
if (self.deathTimer > 0) then
self.deathTimer = self.deathTimer - dt
@ -87,6 +95,9 @@ function PlayerManager:update(dt)
end
end
-- DRAW FUNCTIONS
-- Functions made to draw the HUD
function PlayerManager:drawHUD(dt)
utils.graphics.resetColor()
local hp = 0
@ -116,5 +127,4 @@ function PlayerManager:drawHUD(dt)
utils.graphics.resetColor()
end
return PlayerManager

View File

@ -159,6 +159,8 @@ end
-- All function to draw the map, world and entities
function World:draw()
-- Ona attache puis détache la caméra pour dessiner le monde, afin que celui
-- reste "fixe" tandis que le jouer bouge.
self:drawBackgroundColor()
self.scene.camera:attach()
self:drawMap()
@ -177,6 +179,8 @@ function World:drawEntities()
end
function World:drawMap()
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos
local tx, ty = self.scene.camera:getCoord()
local scale = self.scene.camera:getScale()
local tx = tx