diff --git a/lib/character/behaviors/map.lua b/lib/character/behaviors/map.lua index 07a2df8..2bd8565 100644 --- a/lib/character/behaviors/map.lua +++ b/lib/character/behaviors/map.lua @@ -39,14 +39,15 @@ end function mapBehavior:runTo(target) self.t0 = love.timer.getTime() self.runTarget = target - local charPos = self.position - local render = self.owner:has(Tree.behaviors.render) - if not render then return end - if target.x < charPos.x then - render.animation.side = LEFT - elseif target.x > charPos.x then - render.animation.side = RIGHT - end + self.owner:try(Tree.behaviors.render, + function(render) + if target.x < self.position.x then + render.animation.side = LEFT + elseif target.x > self.position.x then + render.animation.side = RIGHT + end + end + ) end function mapBehavior:update(dt) diff --git a/lib/character/behaviors/render.lua b/lib/character/behaviors/render.lua index bc43b98..73066bb 100644 --- a/lib/character/behaviors/render.lua +++ b/lib/character/behaviors/render.lua @@ -18,17 +18,20 @@ function renderBehavior:update(dt) end function renderBehavior:draw() - local ppm = Tree.level.camera.pixelsPerMeter - if not self.owner:has(Tree.behaviors.map) then return end - local position = self.owner:has(Tree.behaviors.map).displayedPosition - local state = self.owner:getState() + self.owner:try(Tree.behaviors.map, + function(map) + local ppm = Tree.level.camera.pixelsPerMeter + local position = map.displayedPosition + local state = self.owner:getState() - if Tree.level.selector.id == self.owner.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, - position.y + 0.5, nil, 1 / ppm * self.animation.side, 1 / ppm, 38, 47) - love.graphics.setColor(1, 1, 1) + self.animation.animationTable[state]:draw(Tree.assets.files.sprites.character[state], + position.x + 0.5, + position.y + 0.5, nil, 1 / ppm * self.animation.side, 1 / ppm, 38, 47) + love.graphics.setColor(1, 1, 1) + end + ) end return renderBehavior diff --git a/lib/character/character.lua b/lib/character/character.lua index 7ab99d0..cf709d3 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -55,8 +55,28 @@ function character:has(behavior) return self.behaviors[idx] or nil end +--- Если у персонажа есть поведение [behavior], применяет к нему [fn] +--- +--- Дальше meme для интеллектуалов +--- +--- *Я: мам купи мне >>=* +--- +--- *Мама: у нас дома есть >>=* +--- +--- *Дома:* +--- @generic T : Behavior +--- @generic V +--- @param behavior T +--- @param fn fun(behavior: T) : V | nil +--- @return V | nil +function character:try(behavior, fn) + local b = self:has(behavior) + if not b then return end + return fn(b) +end + --- usage: ---- addModules( {logic = logic.new(), graphics = graphics.new(), ...} ) +--- addModules( {logic.new(), graphics.new(), ...} ) --- --- or you may chain this if you are a wannabe haskell kiddo function character:addBehavior(modules)