Debian Codegraph @

CorsixTH/Lua/humanoid_action.lua

raw
1 --[[ Copyright (c) 2016 Albert "Alberth" Hofkamp
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 --! Humanoid action base class.
22 class "HumanoidAction"
23
24 ---@type HumanoidAction
25 local HumanoidAction = _G["HumanoidAction"]
26
27 --! Construct a humanoid action (base class constructor).
28 --!param name (str) Name of the action.
29 function HumanoidAction:HumanoidAction(name)
30 assert(type(name) == "string", "Invalid value for parameter 'name'")
31
32 self.name = name
33 self.count = nil -- 'nil' means 'forever' (until finished), else the number to perform.
34 self.must_happen = false -- If true, action cannot be skipped. Still can be interrupted/cancelled by pick up.
35 self.loop_callback = nil -- Periodic callback to check for termination conditions.
36 self.after_use = nil -- Callback for performing updates afterwards.
37 self.is_leaving = false -- Whether the humanoid is leaving.
38 self.no_truncate = false -- If set, disable shortening the action.
39 self.uninterruptible = false -- If true, can not be interrupted/cancelled by pickup.
40 end
41
42 --! Set the number of times the action should happen.
43 --!param count (int or nil) Set to 'nil' if 'forever', else integer count.
44 --!return (action) Returning self, for daisy-chaining.
45 function HumanoidAction:setCount(count)
46 assert(count == nil or type(count) == "number", "Invalid value for parameter 'count'")
47
48 self.count = count
49 return self
50 end
51
52 --! Set the 'must happen' flag (that is, action cannot be skipped).
53 --!param must_happen (bool) Whether or not the action must happen.
54 --!return (action) Returning self, for daisy-chaining.
55 function HumanoidAction:setMustHappen(must_happen)
56 assert(type(must_happen) == "boolean", "Invalid value for parameters 'must_happen'")
57
58 self.must_happen = must_happen
59 return self
60 end
61
62 --! Set the 'uninterruptible' flag (that is, action cannot be canceled or truncated by pickup).
63 --!param uninterruptible (bool) Whether or not the action uninterruptible.
64 --!return (action) Returning self, for daisy-chaining.
65 function HumanoidAction:setUninterruptible(uninterruptible)
66 assert(type(uninterruptible) == "boolean", "Invalid value for parameters 'uninterruptible'")
67
68 self.uninterruptible = uninterruptible
69 return self
70 end
71
72 --! Set the callback for checking termination conditions.
73 --!param loop_callback (func) Callback function that is called each iteration to check for
74 --! termination conditions.
75 --!return (action) Returning self, for daisy-chaining.
76 function HumanoidAction:setLoopCallback(loop_callback)
77 assert(loop_callback == nil or type(loop_callback) == "function",
78 "Invalid value for parameter 'loop_callback'")
79
80 self.loop_callback = loop_callback
81 return self
82 end
83
84 --! Set the callback for performing updates afterwards.
85 --!param after_use (func) Callback function that is called after the action ends.
86 --!return (action) Returning self, for daisy-chaining.
87 function HumanoidAction:setAfterUse(after_use)
88 assert(after_use == nil or type(after_use) == "function",
89 "Invalid value for parameter 'after_use'")
90
91 self.after_use = after_use
92 return self
93 end
94
95 --! Set whether the humanoid is leaving.
96 --!param is_leaving (bool) Whether or not the humanoid is leaving. If not specified, value is true.
97 --!return (action) Returning self, for daisy-chaining.
98 function HumanoidAction:setIsLeaving(is_leaving)
99 assert(type(is_leaving) == "boolean", "Invalid value for parameter 'is_leaving'")
100
101 self.is_leaving = is_leaving
102 return self
103 end
104
105 --! Do not allow truncating the action.
106 --!return (action) Returning self, for daisy-chaining.
107 function HumanoidAction:disableTruncate()
108 self.no_truncate = true
109 return self
110 end
111
112 function HumanoidAction:afterLoad(old, new)
113 if old < 112 and new >= 112 then
114 self.is_leaving = not not self.is_leaving
115 self.must_happen = not not self.must_happen
116 self.no_truncate = not not self.no_truncate
117 self.saved_must_happen = not not self.saved_must_happen
118 self.idle_must_happen = not not self.idle_must_happen
119
120 if self.name == "walk" then
121 self.is_entering = not not self.is_entering
122 end
123 end
124
125 if old < 168 and class.type(self) == "KnockDoorAction" then
126 -- When the knock-door animation is running in the action at the time of
127 -- save, the callback at the end of the animation doesn't have a valid
128 -- state variable and crashes.
129 if not self.state then self.state = 1 end
130 end
131 end