уменьшение связности подмодулей персонажа, часть 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
|
char.id = characterId
|
||||||
characterId = characterId + 1
|
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 = 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
|
Tree.level.characters[char.id] = char
|
||||||
return char
|
return char
|
||||||
end
|
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
|
--- @param path Deque
|
||||||
function character:followPath(path)
|
function character:followPath(path)
|
||||||
self.logic:followPath(path)
|
self.logic:followPath(path)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
--- @class Graphics
|
--- @class Graphics
|
||||||
--- @field id Id
|
--- @field owner Character
|
||||||
--- @field animation Animation
|
--- @field animation Animation
|
||||||
local graphics = {}
|
local graphics = {}
|
||||||
graphics.__index = graphics
|
graphics.__index = graphics
|
||||||
@ -14,16 +14,15 @@ local function new(id, spriteDir)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function graphics:update(dt)
|
function graphics:update(dt)
|
||||||
local state = Tree.level.characters[self.id].logic.state
|
self.animation.animationTable[self.owner:getState()]:update(dt)
|
||||||
self.animation.animationTable[state]:update(dt)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function graphics:draw()
|
function graphics:draw()
|
||||||
local ppm = Tree.level.camera.pixelsPerMeter
|
local ppm = Tree.level.camera.pixelsPerMeter
|
||||||
local position = Tree.level.characters[self.id].logic.mapLogic.displayedPosition
|
local position = self.owner.logic.mapLogic.displayedPosition
|
||||||
local state = Tree.level.characters[self.id].logic.state
|
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],
|
self.animation.animationTable[state]:draw(Tree.assets.files.sprites.character[state],
|
||||||
position.x + 0.5,
|
position.x + 0.5,
|
||||||
|
|||||||
@ -3,7 +3,7 @@ local utils = require "lib.utils.utils"
|
|||||||
--- @alias CharacterState "idle"|"run"|"attack"|"hurt"
|
--- @alias CharacterState "idle"|"run"|"attack"|"hurt"
|
||||||
|
|
||||||
--- @class Logic
|
--- @class Logic
|
||||||
--- @field id Id
|
--- @field owner Character
|
||||||
--- @field mapLogic MapLogic
|
--- @field mapLogic MapLogic
|
||||||
--- @field state CharacterState
|
--- @field state CharacterState
|
||||||
local logic = {}
|
local logic = {}
|
||||||
@ -23,7 +23,7 @@ end
|
|||||||
--- @param state CharacterState
|
--- @param state CharacterState
|
||||||
function logic:setState(state)
|
function logic:setState(state)
|
||||||
self.state = 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")
|
self:setState("idle")
|
||||||
end or nil)
|
end or nil)
|
||||||
end
|
end
|
||||||
@ -45,9 +45,9 @@ function logic:runTo(target)
|
|||||||
self.mapLogic.runTarget = target
|
self.mapLogic.runTarget = target
|
||||||
local charPos = self.mapLogic.position
|
local charPos = self.mapLogic.position
|
||||||
if target.x < charPos.x then
|
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
|
elseif target.x > charPos.x then
|
||||||
Tree.level.characters[self.id].graphics.animation.side = RIGHT
|
self.owner.graphics.animation.side = RIGHT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user