уменьшение связности подмодулей персонажа, часть 1:
- синтаксис для добавления модулей - ссылка на персонажа внутри модуля
This commit is contained in:
parent
b7bd164269
commit
38536f6d59
@ -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)
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user