| 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 "SpawnAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type SpawnAction |
| 24 |
local SpawnAction = _G["SpawnAction"] |
| 25 |
|
| 26 |
--! Spawn an entity. |
| 27 |
--!param mode (str) Mode of spawning: "spawn" or "despawn" |
| 28 |
--!param point (table x, y, optional direction) Position and optional face direction of spawning or despawning. |
| 29 |
function SpawnAction:SpawnAction(mode, point) |
| 30 |
assert(mode == "spawn" or mode == "despawn", "Invalid value for parameter 'mode'") |
| 31 |
assert(type(point) == "table" and |
| 32 |
type(point.x) == "number" and type(point.y) == "number", |
| 33 |
"Invalid value for parameter 'point'") |
| 34 |
|
| 35 |
self:HumanoidAction("spawn") |
| 36 |
self.mode = mode -- mode of spawning: "spawn" or "despawn" |
| 37 |
self.point = point |
| 38 |
self.offset = nil -- Offset in position?? |
| 39 |
end |
| 40 |
|
| 41 |
--! Set the offset of spawning. |
| 42 |
--! |
| 43 |
--! These two values specifies how many tiles away the humanoid should start |
| 44 |
--! walking before actually spawning in the destination tile. Default is x and |
| 45 |
--! y values are 2, and should not be set less than or equal to 0. Only one of |
| 46 |
--! x or y offsets are used depending on the initial walk direction of the |
| 47 |
--! newly spawned humanoid. |
| 48 |
--!param offset (table x, y) Position offset. |
| 49 |
--!return (action) Return self for daisy chaining. |
| 50 |
function SpawnAction:setOffset(offset) |
| 51 |
assert(type(offset) == "table" and |
| 52 |
(offset.x == nil or (type(offset.x) == "number" and offset.x > 0)) and |
| 53 |
(offset.y == nil or (type(offset.y) == "number" and offset.y > 0)), |
| 54 |
"Invalid value for parameter 'offset'") |
| 55 |
|
| 56 |
self.offset = offset |
| 57 |
return self |
| 58 |
end |
| 59 |
|
| 60 |
local orient_opposite = { |
| 61 |
north = "south", |
| 62 |
west = "east", |
| 63 |
east = "west", |
| 64 |
south = "north", |
| 65 |
} |
| 66 |
|
| 67 |
local action_spawn_despawn = permanent"action_spawn_despawn"( function(humanoid) |
| 68 |
if humanoid.hospital then |
| 69 |
humanoid:despawn() |
| 70 |
end |
| 71 |
humanoid.world:destroyEntity(humanoid) |
| 72 |
end) |
| 73 |
|
| 74 |
local function action_spawn_start(action, humanoid) |
| 75 |
assert(action.mode == "spawn" or action.mode == "despawn", "spawn action given invalid mode: " .. action.mode) |
| 76 |
local x, y = action.point.x, action.point.y |
| 77 |
|
| 78 |
-- For despawning, insert a walk to the despawning position if the humanoid is |
| 79 |
-- not there. |
| 80 |
if action.mode == "despawn" and (humanoid.tile_x ~= x or humanoid.tile_y ~= y) then |
| 81 |
humanoid:queueAction(WalkAction(action.point.x, action.point.y):setMustHappen(action.must_happen), 0) |
| 82 |
return |
| 83 |
end |
| 84 |
action.must_happen = true |
| 85 |
|
| 86 |
local anims = humanoid.walk_anims |
| 87 |
local walk_dir = action.point.direction |
| 88 |
if action.mode == "spawn" then |
| 89 |
walk_dir = orient_opposite[walk_dir] |
| 90 |
end |
| 91 |
local offset_x = 2 |
| 92 |
local offset_y = 2 |
| 93 |
if action.offset then |
| 94 |
offset_x = action.offset.x and action.offset.x or 2 |
| 95 |
offset_y = action.offset.y and action.offset.y or 2 |
| 96 |
end |
| 97 |
assert(offset_x > 0 and offset_y > 0, "Spawning needs to be done from an adjacent tile.") |
| 98 |
local anim, flag, speed_x, speed_y, duration |
| 99 |
if walk_dir == "east" then |
| 100 |
anim, flag, speed_x, speed_y, duration = anims.walk_east , 0, 4, 2, 10 * offset_x |
| 101 |
elseif walk_dir == "west" then |
| 102 |
anim, flag, speed_x, speed_y, duration = anims.walk_north, 1, -4, -2, 10 * offset_x |
| 103 |
elseif walk_dir == "south" then |
| 104 |
anim, flag, speed_x, speed_y, duration = anims.walk_east , 1, -4, 2, 10 * offset_y |
| 105 |
else--if walk_dir == "north" then |
| 106 |
anim, flag, speed_x, speed_y, duration = anims.walk_north, 0, 4, -2, 10 * offset_y |
| 107 |
end |
| 108 |
humanoid.last_move_direction = walk_dir |
| 109 |
humanoid:setAnimation(anim, flag) |
| 110 |
local pos_x, pos_y = 0, 0 |
| 111 |
if action.mode == "spawn" then |
| 112 |
pos_x = -speed_x * duration |
| 113 |
pos_y = -speed_y * duration |
| 114 |
humanoid:setTimer(duration, humanoid.finishAction) |
| 115 |
else |
| 116 |
humanoid:setTimer(duration, action_spawn_despawn) |
| 117 |
end |
| 118 |
humanoid:setTilePositionSpeed(x, y, pos_x, pos_y, speed_x, speed_y) |
| 119 |
end |
| 120 |
|
| 121 |
return action_spawn_start |