From 38536f6d59a128ad40f7a76b39280becc1cf827f Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Sun, 14 Sep 2025 20:57:20 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D0=B5=D0=B9=20=D0=BF=D0=B5=D1=80=D1=81=D0=BE=D0=BD=D0=B0=D0=B6?= =?UTF-8?q?=D0=B0,=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=201:=20-=20=D1=81?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B0=D0=BA=D1=81=D0=B8=D1=81=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9=20-=20?= =?UTF-8?q?=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B0=20=D0=BD=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=BE=D0=BD=D0=B0=D0=B6=D0=B0=20=D0=B2=D0=BD?= =?UTF-8?q?=D1=83=D1=82=D1=80=D0=B8=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/character/character.lua | 28 +++++++++++++++++++++++----- lib/character/graphics.lua | 11 +++++------ lib/character/logic.lua | 8 ++++---- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/character/character.lua b/lib/character/character.lua index fd44a80..5345a91 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -28,16 +28,34 @@ local function spawn(name, template, spriteDir, position, size, level) char.id = characterId characterId = characterId + 1 - - char.logic = (require 'lib.character.logic').new(char.id, position, size) - char.graphics = (require 'lib.character.graphics').new(char.id, spriteDir) - char.info = (require "lib/character/info").new(name, template, level) - char = setmetatable(char, character) + + char:addModules( + { + --logic = (require 'lib.character.logic').new(char.id, position, size), + graphics = (require 'lib.character.graphics').new(char.id, spriteDir), + info = (require "lib/character/info").new(name, template, level) + } + ) Tree.level.characters[char.id] = char return char end +--- usage: +--- addModules( {logic = logic.new(), graphics = graphics.new(), ...} ) +function character:addModules(modules) + for key, module in pairs(modules) do + module.owner = self + self[key] = module + end +end + +--- геттеры и сеттеры для "внешних" данных +--- @return CharacterState +function character:getState() + return self.logic.state or "idle" +end + --- @param path Deque function character:followPath(path) self.logic:followPath(path) diff --git a/lib/character/graphics.lua b/lib/character/graphics.lua index e644247..f966431 100644 --- a/lib/character/graphics.lua +++ b/lib/character/graphics.lua @@ -1,5 +1,5 @@ --- @class Graphics ---- @field id Id +--- @field owner Character --- @field animation Animation local graphics = {} graphics.__index = graphics @@ -14,16 +14,15 @@ local function new(id, spriteDir) end function graphics:update(dt) - local state = Tree.level.characters[self.id].logic.state - self.animation.animationTable[state]:update(dt) + self.animation.animationTable[self.owner:getState()]:update(dt) end function graphics:draw() local ppm = Tree.level.camera.pixelsPerMeter - local position = Tree.level.characters[self.id].logic.mapLogic.displayedPosition - local state = Tree.level.characters[self.id].logic.state + local position = self.owner.logic.mapLogic.displayedPosition + local state = self.owner:getState() - if Tree.level.selector.id == self.id then love.graphics.setColor(0.5, 1, 0.5) end + if Tree.level.selector.id == self.owner.id then love.graphics.setColor(0.5, 1, 0.5) end self.animation.animationTable[state]:draw(Tree.assets.files.sprites.character[state], position.x + 0.5, diff --git a/lib/character/logic.lua b/lib/character/logic.lua index c3778a5..e5e0564 100644 --- a/lib/character/logic.lua +++ b/lib/character/logic.lua @@ -3,7 +3,7 @@ local utils = require "lib.utils.utils" --- @alias CharacterState "idle"|"run"|"attack"|"hurt" --- @class Logic ---- @field id Id +--- @field owner Character --- @field mapLogic MapLogic --- @field state CharacterState local logic = {} @@ -23,7 +23,7 @@ end --- @param state CharacterState function logic:setState(state) self.state = state - Tree.level.characters[self.id].graphics.animation:setState(state, (state ~= "idle" and state ~= "run") and function() + self.owner.graphics.animation:setState(state, (state ~= "idle" and state ~= "run") and function() self:setState("idle") end or nil) end @@ -45,9 +45,9 @@ function logic:runTo(target) self.mapLogic.runTarget = target local charPos = self.mapLogic.position if target.x < charPos.x then - Tree.level.characters[self.id].graphics.animation.side = LEFT + self.owner.graphics.animation.side = LEFT elseif target.x > charPos.x then - Tree.level.characters[self.id].graphics.animation.side = RIGHT + self.owner.graphics.animation.side = RIGHT end end