summaryrefslogtreecommitdiff
path: root/Camera.lua
blob: 3b3f1a378a3cd7e757350c8253da3e1f53c4d207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
local component = require 'component'
local Transform = require 'Transform'

-- a view of the world
local Camera = component({Transform})

function Camera:use()
	local trans = self.obj:get(Transform):love()
	local cam_trans = love.math.newTransform(1920 / 2, 1080 / 2)
	love.graphics.applyTransform(cam_trans * trans:inverse())
end

function Camera:follow(obj)
	assert(obj:get(Transform))
	self.i.following = obj
end

function Camera:update(dt)
	if self.i.following then
		local this = self.obj:get(Transform)
		local follow = self.i.following:get(Transform)
		this.i.pos[1] = follow.i.pos[1]
		this.i.pos[2] = follow.i.pos[2]
	end
end

return Camera