From 9abb55dd1da7dc747b65414d024020da2bb60bf5 Mon Sep 17 00:00:00 2001 From: Ivan Yuriev Date: Sun, 3 Aug 2025 04:19:04 +0300 Subject: [PATCH] moved values into the global tree --- character.lua | 3 ++- lib/asset_bundle.lua | 7 +++++++ lib/camera.lua | 3 +-- lib/level.lua | 1 + lib/tree.lua | 15 ++++----------- main.lua | 41 +++++++---------------------------------- 6 files changed, 22 insertions(+), 48 deletions(-) diff --git a/character.lua b/character.lua index 14858d2..a1e3179 100644 --- a/character.lua +++ b/character.lua @@ -122,7 +122,8 @@ function Character:update(dt) self.animationTable[self.state]:update(dt) end -function Character:draw(camera) +function Character:draw() + local camera = Tree.level.camera self.animationTable[self.state]:draw(AssetBundle.files.sprites.character[self.state], self.position.x, self.position.y, nil, 1 / camera.pixelsPerMeter, 1 / camera.pixelsPerMeter, 38, 47) end diff --git a/lib/asset_bundle.lua b/lib/asset_bundle.lua index 7ff31b6..c7cf06d 100644 --- a/lib/asset_bundle.lua +++ b/lib/asset_bundle.lua @@ -1,8 +1,14 @@ +--- @class AssetBundle +--- @field root string Path to the tree root +--- @field files table local AssetBundle = { root = "/assets", files = {} } +--- Loads assets into the tree. +--- Calls [onFileLoading] whenever a file is loaded. +--- @param onFileLoading? fun(path: string): AssetBundle | nil function AssetBundle:load(onFileLoading) local callback = onFileLoading or function(path) print("[AssetBundle]: loading " .. path) @@ -29,6 +35,7 @@ function AssetBundle:load(onFileLoading) end self.files = enumerate(self.root) + return self end function AssetBundle.loadFile(path) diff --git a/lib/camera.lua b/lib/camera.lua index 7e2f1b0..524e4b0 100644 --- a/lib/camera.lua +++ b/lib/camera.lua @@ -1,5 +1,4 @@ local Vec3 = require "lib/vec3" -local tree = require "lib/tree" local EPSILON = 0.001 @@ -25,7 +24,7 @@ end ---@todo Отрефакторить и вынести кнопки управления в controls, не должно быть таких ужасных проверок function camera:update(dt) - local ps = tree.instance().panning + local ps = Tree.panning if ps.delta:length() > 0 then local worldDelta = ps.delta:scale(1 / (self.pixelsPerMeter * self.scale)):scale(dt):scale(self.speed) self.velocity = self.velocity + worldDelta diff --git a/lib/level.lua b/lib/level.lua index 57234aa..d0a2043 100644 --- a/lib/level.lua +++ b/lib/level.lua @@ -18,6 +18,7 @@ function level:update(dt) utils.each(self.characters, function(el) el:update(dt) end) + self.camera:update(dt) end return { diff --git a/lib/tree.lua b/lib/tree.lua index 74f74cb..cb1a843 100644 --- a/lib/tree.lua +++ b/lib/tree.lua @@ -3,15 +3,8 @@ --- В love.update обновлять, в love.draw читать -local tree - -local function instance() - tree = tree or { - panning = require "lib/panning" - } - return tree -end - -return { - instance = instance +Tree = { + panning = require "lib/panning", + assets = (require "lib/asset_bundle"):load(), + level = (require "lib/level"):new() -- для теста у нас только один уровень, который сразу же загружен } diff --git a/main.lua b/main.lua index 155e902..e348b46 100644 --- a/main.lua +++ b/main.lua @@ -1,50 +1,33 @@ -- CameraLoader = require 'lib/camera' require "character" - -local camera = require 'lib/camera' -local Vec3 = require "lib/vec3" -local tree = require "lib/tree" -local AssetBundle = require "lib/asset_bundle" +require "lib/tree" function love.conf(t) t.console = true end function love.load() - AssetBundle:load() - - Camera = camera.new() - Camera.position = Vec3({}) -- PlayerFaction = Faction -- Hero1 = Character:create("Petya", 10) -- Hero2 = Character:create("Andrysha", 12) - Hero = CreateCharacter("Hero", AssetBundle.files.sprites.character) + Hero = CreateCharacter("Hero", Tree.assets.files.sprites.character) -- PlayerFaction.characters = { Hero1, Hero2 } love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true }) end ----@todo Вынести это в свое поле в дереве глобального состояния уровня --- local cameraPos = Vec3({}) --- Camera:lockPosition(cameraPos.x, cameraPos.y, Camera.smooth.damped(10)) - function love.update(dt) - Camera:update(dt) - tree.instance().panning:update(dt) - + Tree.panning:update(dt) + Tree.level:update(dt) Hero:update(dt) end function love.draw() - Camera:attach() + Tree.level.camera:attach() - local uiSize = 0.2 - - local windowWidth = love.graphics.getWidth() - local windowHeight = love.graphics.getHeight() * (1 - uiSize) local width = 20 local height = 12 @@ -54,17 +37,7 @@ function love.draw() love.graphics.setColor(244 / 255, 67 / 255, 54 / 255, 1) love.graphics.rectangle('fill', 0, 0, 1, 1) love.graphics.setColor(1, 1, 1, 1) - Hero:draw(Camera) + Hero:draw() - Camera:detach() + Tree.level.camera:detach() end - -local Level = {} -Level.kakaya_ta_hren = 10 -Level.map = {} - -local v = Vec3({ 0, 1, 2 }) - --- Faction -> Character --- calculate_order() --- calculate_order()