-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRessource.lua
244 lines (211 loc) · 6.72 KB
/
Ressource.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
local Ressource_usedIndex = {}
Ressource = Tile:extend
{
class = "Ressource",
props = {"x", "y", "rotation", "image", "width", "height", "currentPain", "controller", "description", "deaths", "quality"},
sync_high = {"currentPain"},
sync_low = {"controller", "deaths", "quality"},
owner = 0,
image = '/assets/graphics/ressource.png',
currentPain = 0,
maxPain = config.ressourceHealth,
t = Text:new{
font = 12,
text = "",
x = 0,
y = 0,
tint = {0,0,0},
},
controller = "",
nextController = "",
targetable = true,
deaths = 0,
quality = 0,
-- UiBar
painBar = nil,
movable = false,
onDraw = function(self)
if the.app.view.layers.ui:contains(self.t) == false then the.app.view.layers.ui:add(self.t) end
end,
onNew = function (self)
self:mixin(GameObject)
self:mixin(GameObjectCommons)
self:mixin(FogOfWarObject)
self.width = 64
self.height = 64
self:updateQuad()
object_manager.create(self)
--print("NEW RESSOURCE", self.x, self.y, self.width, self.height)
the.app.view.layers.characters:add(self)
self.painBar = UiBar:new{
x = self.x, y = self.y,
dx = 0, dy = self.height,
currentValue = self.currentPain, maxValue = self.maxPain,
width = self.width,
}
drawDebugWrapper(self)
self:every(config.xpGainsEachNSeconds, function()
if self:isLocal() then self:giveXP() end
end)
-- over time tracking
self:every(config.trackingOverTimeTimeout, function()
if self:isLocal() and the.phaseManager and the.phaseManager.phase == "playing" then
track("resource_ot", self.oid, self.description, self.currentPain, self.controller)
end
end)
local amount = #config.ressourceQualityTable
local count = 0
for k,v in pairs(Ressource_usedIndex) do count = count + 1 end
if count == amount then Ressource_usedIndex = {} end
local randomNumber = nil
while true do
randomNumber = math.random(1,amount)
if not Ressource_usedIndex[randomNumber] then break end
end
Ressource_usedIndex[randomNumber] = true
self.quality = config.ressourceQualityTable[randomNumber]
the.ressourceObjects[self] = true
the.gridIndexCollision:insertAt(self.x,self.y,self)
end,
gainPain = function (self, str, source_oid)
--print(self.oid, "gain pain", str)
self.currentPain = self.currentPain + str
self:updatePain()
-- dmg tracking
object_manager.send(source_oid, "inc", "resource_dmg", str)
end,
showDamage = function (self, str)
self:showDamageWithOffset (str, 30)
end,
receiveBoth = function (self, message_name, ...)
--print(self.oid, "receives message", message_name, "with", ...)
if message_name == "damage" then
local str, source_oid = ...
--print("RESSOURCE DAMAGE", str)
self:showDamage(str)
elseif message_name == "heal" then
local str, source_oid = ...
--print("RESSOURCE HEAL", -str)
self:showDamage(-str)
elseif message_name == "damage_over_time" then
local str, duration, ticks, source_oid = ...
--~ print("RESSOURCE DAMAGE_OVER_TIME", str, duration, ticks, source_oid)
local oldDeaths = self.deaths
for i=0,ticks do
self:after(duration / ticks * i, function()
if self.deaths == oldDeaths then
self:showDamage(str)
end
end)
end
elseif message_name == "heal_over_time" then
local str, duration, ticks, source_oid = ...
local oldDeaths = self.deaths
--print("RESSOURCE HEAL_OVER_TIME", str, duration, ticks)
for i=0,ticks do
self:after(duration / ticks * i, function()
if self.deaths == oldDeaths then
self:showDamage(-str)
end
end)
end
end
end,
receiveLocal = function (self, message_name, ...)
--print(self.oid, "receives message", message_name, "with", ...)
if message_name == "damage" then
local str, source_oid = ...
--~ print("RESSOURCE DAMANGE", str, source_oid)
self:controllerChanger(source_oid)
self:gainPain(str, source_oid)
elseif message_name == "heal" then
local str, source_oid = ...
--print("RESSOURCE HEAL", -str)
self:controllerChanger(source_oid)
self:gainPain(-str, source_oid)
elseif message_name == "damage_over_time" then
local str, duration, ticks, source_oid = ...
--~ print("RESSOURCE DAMAGE_OVER_TIME", str, duration, ticks, source_oid)
local oldDeaths = self.deaths
for i=0,ticks do
self:after(duration / ticks * i, function()
if self.deaths == oldDeaths then
self:controllerChanger(source_oid)
self:gainPain(str, source_oid)
end
end)
end
elseif message_name == "heal_over_time" then
local str, duration, ticks, source_oid = ...
--print("RESSOURCE HEAL_OVER_TIME", str, duration, ticks)
local oldDeaths = self.deaths
for i=0,ticks do
self:after(duration / ticks * i, function()
if self.deaths == oldDeaths then
self:controllerChanger(source_oid)
self:gainPain(-str, source_oid)
end
end)
end
end
end,
controllerChanger = function (self, source_oid)
local source = object_manager.get(source_oid)
if source then
self.nextController = source.team
else
self.nextController = "unknown"
end
--~ print("CONTROLLER CHANGE", source_oid, self.nextController)
end,
updatePain = function (self)
if self.currentPain < 0 then self.currentPain = 0 end
if self.currentPain > self.maxPain then
self.currentPain = self.maxPain
--self:die()
self:changeController()
end
end,
changeController = function(self)
--~ print("REALLY CHANGE CONTROLLER", self.nextController)
self.controller = self.nextController
self.currentPain = 0
self.deaths = self.deaths + 1
end,
giveXP = function(self)
if self.controller then
object_manager.visit(function(oid,obj)
if obj.team and obj.team == self.controller then
object_manager.send(oid, "xp", config.xpPerRessourceTick * self.quality, CHARACTER_XP_RESOURCE)
end
end)
end
end,
onUpdateBoth = function (self)
self.painBar.currentValue = self.currentPain
self.painBar:updateBar()
self.painBar.bar.alpha = self.alpha
self.painBar.background.alpha = self.alpha
self.painBar.x = self.x
self.painBar.y = self.y
local name = "nobody"
if self.controller ~= "" then
name = self.controller
end
self.t.text = "Lvl " .. self.quality .. " ressource \nOwned by: " .. name
self.t.x = self.x - self.width /4
self.t.y = self.y - self.t.height - 20
self.t.alpha = self.alpha
self.t.width = 120
the.ressources[self.description] = name or "none"
self:updateFogAlpha()
end,
onDieBoth = function (self)
the.gridIndexCollision:removeObject(self)
self.painBar:die()
self.painBar = nil
self.t:die()
self.t = nil
the.ressourceObjects[self] = nil
end,
}