implement character:try
This commit is contained in:
parent
1b12b2c470
commit
947787ff44
@ -39,14 +39,15 @@ end
|
|||||||
function mapBehavior:runTo(target)
|
function mapBehavior:runTo(target)
|
||||||
self.t0 = love.timer.getTime()
|
self.t0 = love.timer.getTime()
|
||||||
self.runTarget = target
|
self.runTarget = target
|
||||||
local charPos = self.position
|
self.owner:try(Tree.behaviors.render,
|
||||||
local render = self.owner:has(Tree.behaviors.render)
|
function(render)
|
||||||
if not render then return end
|
if target.x < self.position.x then
|
||||||
if target.x < charPos.x then
|
render.animation.side = LEFT
|
||||||
render.animation.side = LEFT
|
elseif target.x > self.position.x then
|
||||||
elseif target.x > charPos.x then
|
render.animation.side = RIGHT
|
||||||
render.animation.side = RIGHT
|
end
|
||||||
end
|
end
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mapBehavior:update(dt)
|
function mapBehavior:update(dt)
|
||||||
|
|||||||
@ -18,17 +18,20 @@ function renderBehavior:update(dt)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function renderBehavior:draw()
|
function renderBehavior:draw()
|
||||||
local ppm = Tree.level.camera.pixelsPerMeter
|
self.owner:try(Tree.behaviors.map,
|
||||||
if not self.owner:has(Tree.behaviors.map) then return end
|
function(map)
|
||||||
local position = self.owner:has(Tree.behaviors.map).displayedPosition
|
local ppm = Tree.level.camera.pixelsPerMeter
|
||||||
local state = self.owner:getState()
|
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],
|
self.animation.animationTable[state]:draw(Tree.assets.files.sprites.character[state],
|
||||||
position.x + 0.5,
|
position.x + 0.5,
|
||||||
position.y + 0.5, nil, 1 / ppm * self.animation.side, 1 / ppm, 38, 47)
|
position.y + 0.5, nil, 1 / ppm * self.animation.side, 1 / ppm, 38, 47)
|
||||||
love.graphics.setColor(1, 1, 1)
|
love.graphics.setColor(1, 1, 1)
|
||||||
|
end
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
return renderBehavior
|
return renderBehavior
|
||||||
|
|||||||
@ -55,8 +55,28 @@ function character:has(behavior)
|
|||||||
return self.behaviors[idx] or nil
|
return self.behaviors[idx] or nil
|
||||||
end
|
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:
|
--- 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
|
--- or you may chain this if you are a wannabe haskell kiddo
|
||||||
function character:addBehavior(modules)
|
function character:addBehavior(modules)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user