-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedDestroyables.lua
71 lines (60 loc) · 1.82 KB
/
LinkedDestroyables.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--
-- Copyright (c) 2005 Pandemic Studios, LLC. All rights reserved.
--
--------------------------------------------
-- LinkedDestroyables contains a list of "object sets", each of which contains one or more destroyable objects.
-- When all the objects in one set are destroyed, it automatically kills the rest of the objects in all the other sets.
LinkedDestroyables =
{
objectSets = nil, --Must be set to a two-dimensional table of objects. Each row must be a list of object names,
--which represents a so-called "object set"
New = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
Init = function(self)
--------------------
-- Local functions
--
local ObjectKilled = function(setIndex)
for _, objectName in pairs(self.objectSets[setIndex]) do
if IsObjectAlive(objectName) then
return
end
end
--if you reach reach here, all the objects in the set are dead, so
--we need to now go and kill all the objects in the other sets
for setIdx, set in pairs(self.objectSets) do
if setIdx ~= setIndex then
for _, objectName in pairs(set) do
if IsObjectAlive(objectName) then
KillObject(objectName)
end
end
end
end
--callback notifier
self:OnSetDestroyed()
end
--------------------
-- Initialization logic
--
for setIdx, set in pairs(self.objectSets) do
--create a callback for each object in the set
for _, objectName in pairs(set) do
local whichSetIndex = setIdx --silly lua and your closure rules
OnObjectKillName(
function (objectPtr, killer)
ObjectKilled(whichSetIndex)
end,
objectName
)
end
end
end,
}
function LinkedDestroyables:OnSetDestroyed()
--override me!
end