-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedShields.lua
162 lines (130 loc) · 3.92 KB
/
LinkedShields.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--
-- Copyright (c) 2005 Pandemic Studios, LLC. All rights reserved.
--
LinkedShields = {
--------------------------------------
-- Fields that need to be set on creation (i.e. when New is called)
--
objs = nil, --set this to a list of object names which share the same shield
maxShield = 200000, --maximum shield value
addShield = 10000, --regeneration rate for the shields
controllerObject = nil, --the object that, if blown up, will take down the shields
--------------------------------------
-- Internal state variables
--
--------------------------------------
-- Methods
--
New = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
-- initializes all this stuff
Init = function(self)
self.bAllShieldsDown = false
---------------------------------------------------------------
-- callback for syncing all shields when one shield is damaged.
local AffectAllShields = function(object, objs, damage)
local cur, max, add = GetObjectShield(object)
for i, obj in ipairs(objs) do
SetProperty(obj, "CurShield", cur)
if ( cur == 0.0 ) then
SetProperty(obj, "AddShield", 0)
end
end
end
local CheckAllShieldsDown = function(object)
local AllShieldsDown = function()
if not self.bAllShieldsDown then
self.bAllShieldsDown = true
if IsObjectAlive(self.controllerObject) then
KillObject(self.controllerObject)
end
self:OnAllShieldsDown()
end
end
local cur, max, add = GetObjectShield(object)
if ( cur == 0.0 ) then
AllShieldsDown()
end
end
---------------------------------------------------------------
local DisableShields = function(objs)
for i, obj in ipairs(objs) do
-- just do it for one. the callback on shield health change
-- will take care of the rest.
local cur, max, add = GetObjectShield(obj)
-- shields are already disabled. exit out.
if ( cur == 0.0 ) then
return
end
SetProperty(obj, "CurShield", 0)
SetProperty(obj, "AddShield", 0)
end
end
-- enable all shields. reset health.
local EnableShields = function(objs)
for i, obj in objs do
SetProperty(obj, "AddShield", self.addShield / table.getn(objs))
SetProperty(obj, "CurShield", self.maxShield)
end
self.bAllShieldsDown = false
self:OnAllShieldsUp()
end
-------------------
-- init stuff
for i, obj in ipairs(self.objs) do
SetProperty(obj, "CurShield", self.maxShield)
SetProperty(obj, "MaxShield", self.maxShield)
SetProperty(obj, "AddShield", self.addShield / table.getn(self.objs))
OnObjectDamageName(
function(object, damage)
AffectAllShields(object, self.objs, damage)
CheckAllShieldsDown(object)
end,
obj)
end
if ( self.controllerObject ) then
OnObjectKillName(
function()
--if the shields aren't already down...
if not self.bAllShieldsDown then
DisableShields(self.objs)
end
self:OnControlObjectDestroyed()
end,
self.controllerObject)
OnObjectRespawnName(
function()
EnableShields(self.objs)
end,
self.controllerObject)
end
end,
ChangeAddShield = function(self, x)
for i, obj in ipairs(self.objs) do
SetProperty(obj, "AddShield", x)
end
end,
}
--------------------------------------
-- Overridable callbacks
--
function LinkedShields:OnControlObjectDestroyed()
end
function LinkedShields:OnAllShieldsUp()
end
function LinkedShields:OnAllShieldsDown()
end
--------------------------------------
-- Util functions (mainly for space combat)
--
--objs: a table of object names
--trueOrFalse: whether to turn the lockOn effect on or off
function EnableLockOn(objs, trueOrFalse)
for i, object in pairs(objs) do
EnableBuildingLockOn(object, trueOrFalse)
end
end