| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
class "IdleAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type IdleAction |
| 24 |
local IdleAction = _G["IdleAction"] |
| 25 |
|
| 26 |
function IdleAction:IdleAction() |
| 27 |
self:HumanoidAction("idle") |
| 28 |
self.direction = nil -- Direction of standing idle. |
| 29 |
self.on_interrupt = nil -- Function to call at an interrupt. |
| 30 |
end |
| 31 |
|
| 32 |
--! Set the direction of facing while standing idle. |
| 33 |
--!param direction (string) Direction of facing. |
| 34 |
--!return (action) Self, for daisy-chaining. |
| 35 |
function IdleAction:setDirection(direction) |
| 36 |
assert(direction == nil or |
| 37 |
direction == "north" or direction == "south" or |
| 38 |
direction == "east" or direction == "west", |
| 39 |
"Invalid value for parameter 'direction'") |
| 40 |
|
| 41 |
self.direction = direction |
| 42 |
return self |
| 43 |
end |
| 44 |
|
| 45 |
--! Set the function to call on interrupt. |
| 46 |
--!param on_interrupt (function) Function to call on interrupt. |
| 47 |
--!return (action) Self, for daisy-chaining. |
| 48 |
function IdleAction:setOnInterrupt(on_interrupt) |
| 49 |
assert(on_interrupt == nil or type(on_interrupt) == "function", |
| 50 |
"Invalid value for parameter 'on_interrupt'") |
| 51 |
|
| 52 |
self.on_interrupt = on_interrupt |
| 53 |
return self |
| 54 |
end |
| 55 |
|
| 56 |
local action_idle_interrupt = permanent"action_idle_interrupt"( function(_, humanoid) |
| 57 |
humanoid:setTimer(1, humanoid.finishAction) |
| 58 |
end) |
| 59 |
|
| 60 |
local action_timer = permanent"action_idle_timer"( function(humanoid) |
| 61 |
local action = humanoid:getCurrentAction() |
| 62 |
if action.after_use then |
| 63 |
action.after_use() |
| 64 |
action.must_happen = true |
| 65 |
end |
| 66 |
humanoid:finishAction() |
| 67 |
end) |
| 68 |
|
| 69 |
local function action_idle_start(action, humanoid) |
| 70 |
local direction = action.direction or humanoid.last_move_direction |
| 71 |
local anims = humanoid.walk_anims |
| 72 |
if direction == "north" then |
| 73 |
humanoid:setAnimation(anims.idle_north, 0) |
| 74 |
elseif direction == "east" then |
| 75 |
humanoid:setAnimation(anims.idle_east, 0) |
| 76 |
elseif direction == "south" then |
| 77 |
humanoid:setAnimation(anims.idle_east, 1) |
| 78 |
elseif direction == "west" then |
| 79 |
humanoid:setAnimation(anims.idle_north, 1) |
| 80 |
end |
| 81 |
local map, x, y = humanoid.th:getTile() |
| 82 |
humanoid.th:setTile(map, x, y, humanoid:getDrawingLayer()) |
| 83 |
humanoid:setSpeed(0, 0) |
| 84 |
if action.count then |
| 85 |
humanoid:setTimer(action.count, action_timer) |
| 86 |
action.must_happen = true |
| 87 |
end |
| 88 |
-- If an interrupt is already specified for the idle action don't replace it |
| 89 |
if action.must_happen and not action.on_interrupt then |
| 90 |
action.on_interrupt = action_idle_interrupt |
| 91 |
end |
| 92 |
if action.loop_callback then |
| 93 |
action:loop_callback(humanoid) |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
return action_idle_start |