From a96780e04d22c916921be996a6b99487efc4e569 Mon Sep 17 00:00:00 2001 From: the lemons Date: Sat, 27 Aug 2022 02:49:55 -0500 Subject: store the current camera as an attribute of the world --- Bolvis.lua | 1 + Camera.lua | 4 ++++ main.lua | 6 ++---- world.lua | 3 +-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Bolvis.lua b/Bolvis.lua index 8d53c44..cf6a7e8 100644 --- a/Bolvis.lua +++ b/Bolvis.lua @@ -20,6 +20,7 @@ local Hand = component( function Bolvis:init(inst) inst.hand = self.obj.world:object(Hand {bolvis = self.obj}) inst.camera = self.obj.world:object(Camera {following = self.obj}) + inst.camera[Camera]:use() inst.blink = 10 end diff --git a/Camera.lua b/Camera.lua index 3b3f1a3..5a9c87b 100644 --- a/Camera.lua +++ b/Camera.lua @@ -5,6 +5,10 @@ local Transform = require 'Transform' local Camera = component({Transform}) function Camera:use() + self.obj.world.camera = self +end + +function Camera: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()) diff --git a/main.lua b/main.lua index 872499b..98ad56f 100644 --- a/main.lua +++ b/main.lua @@ -17,9 +17,7 @@ function love.load() love.mouse.setRelativeMode(true) level = world() - local bolvis = level:object(Transform {pos = {960, 540}}, Bolvis()) - camera = bolvis[Bolvis].i.camera[Camera] - + level:object(Transform {pos = {960, 540}}, Bolvis()) level:object(Transform {pos = {400, 540}}, Sprite {name = 'utah-teapot'}) level:object(Transform {pos = {600, 540}}, Sprite {name = 'apioform'}) level:object(Transform {pos = {960, 300}}, Sprite {name = 'test'}) @@ -51,7 +49,7 @@ end function love.draw() love.graphics.applyTransform(screen_transform()) - camera:use() + level.camera:transform() local to_draw = {} for o in level:iterate(Drawable) do table.insert(to_draw, o) diff --git a/world.lua b/world.lua index a219589..ec35d07 100644 --- a/world.lua +++ b/world.lua @@ -74,8 +74,7 @@ function obj_proto:require(ct, instance) end local world_proto = {} -local world_mt = - {__index = world_proto, __newindex = function()assert(false)end} +local world_mt = {__index = world_proto} local function world() local w = setmetatable({objects = {}, components = {}}, world_mt) -- cgit v1.2.3