Debian Codegraph @

CorsixTH/Lua/humanoid_actions/knock_door.lua

raw
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 "KnockDoorAction" (HumanoidAction)
22
23 ---@type KnockDoorAction
24 local KnockDoorAction = _G["KnockDoorAction"]
25
26 --! Constructor for knocking on the door action.
27 --!param door (Object) Door to knock on.
28 --!param direction (string) Direction of facing.
29 function KnockDoorAction:KnockDoorAction(humanoid, door, direction)
30 assert(class.is(humanoid, Humanoid), "Invalid value for parameter 'humanoid'")
31 assert(class.is(door, Door), "Invalid value for parameter 'door'")
32 assert(direction == "north" or direction == "south" or
33 direction == "east" or direction == "west",
34 "Invalid value for parameter 'direction'")
35
36 self:HumanoidAction("knock_door")
37 self.humanoid = humanoid
38 self.door = door -- Door to knock on.
39 self.direction = direction -- Direction of facing.
40 self.state = nil -- Next step in the animation.
41 end
42
43 function KnockDoorAction:start()
44 self.state = 0
45 self:update()
46 end
47
48
49 local function action_knock_door_start(action, humanoid)
50 assert(humanoid == action.humanoid)
51 action:start()
52 end
53
54 local action_knock_door_tick = permanent"action_knock_door_tick"( function(humanoid)
55 local action = humanoid:getCurrentAction()
56 action:update()
57 end)
58
59
60 function KnockDoorAction:update()
61 if self.state == 0 then
62 local direction = self.direction
63 local anims = self.humanoid.door_anims
64 local door = self.door
65 self.must_happen = true
66 local anim = anims.knock_north
67 local flag_mirror = (direction == "west" or direction == "south") and 1 or 0
68 if direction == "east" or direction == "south" then
69 anim = anims.knock_east
70 end
71 self.humanoid:setAnimation(anim, flag_mirror)
72 self.humanoid:setTilePositionSpeed(self.humanoid.tile_x, self.humanoid.tile_y)
73 self.humanoid:setTimer(TheApp.animation_manager:getAnimLength(anim), action_knock_door_tick)
74 self.humanoid.user_of = door
75 door:setUser(self.humanoid)
76 door.th:makeVisible()
77
78 self.state = 1
79 return
80
81 elseif self.state == 1 then
82 local door = self.humanoid.user_of
83 door:removeUser(self.humanoid)
84 self.humanoid.user_of = nil
85 door:getRoom():tryAdvanceQueue()
86 self.humanoid:finishAction()
87
88 self.state = nil
89 return
90 end
91
92 error("Illegal state " .. tostring(self.state) .. " in KnockDoorAction:update")
93 end
94
95 return action_knock_door_start