diff options
| author | the lemons <citrons@mondecitronne.com> | 2022-08-27 01:23:20 -0500 |
|---|---|---|
| committer | the lemons <citrons@mondecitronne.com> | 2022-08-27 01:23:20 -0500 |
| commit | 8bb18a89060ecd581a06238ce3fa5d2170ee3a15 (patch) | |
| tree | 25baba9d2c267b986adfe7af661b3cd99a32b346 /Camera.lua | |
| parent | 001272dbcbaa1110541e0666af3cea1c97bc199f (diff) | |
implement component system
Diffstat (limited to 'Camera.lua')
| -rw-r--r-- | Camera.lua | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -1,23 +1,26 @@ -local game = require 'game' +local component = require 'component' +local Transform = require 'Transform' -local Camera = game.Object:extend() +-- a view of the world +local Camera = component({Transform}) function Camera:use() - local x, y = unpack(self.pos) - local scale = 1 / self.scale - local transform = love.math.newTransform( - -x + 1920 / 2, -y + 1080 / 2, -self.rot, scale, scale) - love.graphics.applyTransform(transform) + 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) - self.following = obj + assert(obj:get(Transform)) + self.i.following = obj end function Camera:update(dt) - if self.following then - self.pos[1] = self.following.pos[1] - self.pos[2] = self.following.pos[2] + 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 |
