Camera: More intuitive functions names

This commit is contained in:
Matthias Richter 2011-11-16 13:45:20 +01:00
parent 31760b00c7
commit cd3df0c49f

View File

@ -39,14 +39,16 @@ end
function camera:rotate(phi) function camera:rotate(phi)
self.rot = self.rot + phi self.rot = self.rot + phi
return self
end end
function camera:translate(t) function camera:move(p,q)
self.pos = self.pos + t p = type(p) == "number" and vector(p,q) or p
self.pos = self.pos + p
return self
end end
camera.move = camera.translate
function camera:predraw() function camera:attach()
local center = vector(love.graphics.getWidth(), love.graphics.getHeight()) / (self.zoom * 2) local center = vector(love.graphics.getWidth(), love.graphics.getHeight()) / (self.zoom * 2)
love.graphics.push() love.graphics.push()
love.graphics.scale(self.zoom) love.graphics.scale(self.zoom)
@ -55,30 +57,33 @@ function camera:predraw()
love.graphics.translate((-self.pos):unpack()) love.graphics.translate((-self.pos):unpack())
end end
function camera:postdraw() function camera:detach()
love.graphics.pop() love.graphics.pop()
end end
function camera:draw(func) function camera:draw(func)
self:predraw() self:attach()
func() func()
self:postdraw() self:detach()
end end
function camera:toCameraCoords(p) function camera:cameraCoords(p, q)
p = type(p) == "number" and vector(p,q) or p
local w,h = love.graphics.getWidth(), love.graphics.getHeight() local w,h = love.graphics.getWidth(), love.graphics.getHeight()
local p = (p - self.pos):rotate_inplace(self.rot) p = (p - self.pos):rotate_inplace(self.rot)
return vector(p.x * self.zoom + w/2, p.y * self.zoom + h/2) return vector(p.x * self.zoom + w/2, p.y * self.zoom + h/2)
end end
function camera:toWorldCoords(p) function camera:worldCoords(p, q)
p = type(p) == "number" and vector(p,q) or p
local w,h = love.graphics.getWidth(), love.graphics.getHeight() local w,h = love.graphics.getWidth(), love.graphics.getHeight()
local p = vector((p.x-w/2) / self.zoom, (p.y-h/2) / self.zoom):rotate_inplace(-self.rot) p = vector((p.x-w/2) / self.zoom, (p.y-h/2) / self.zoom):rotate_inplace(-self.rot)
return p + self.pos return p + self.pos
end end
function camera:mousepos() function camera:mousepos()
return self:toWorldCoords(vector(love.mouse.getPosition())) return self:worldCoords(vector(love.mouse.getPosition()))
end end
-- the module -- the module