26 | function StrawBlower:load(savegame) |
27 | |
28 | self.setUnitFillLevel = Utils.overwrittenFunction(self.setUnitFillLevel, StrawBlower.setUnitFillLevel); |
29 | self.strawBlowerBaleTriggerCallback = StrawBlower.strawBlowerBaleTriggerCallback; |
30 | self.setIsStrawBlowerDoorOpen = StrawBlower.setIsStrawBlowerDoorOpen; |
31 | |
32 | |
33 | self.strawBlowerDoorAnimation = getXMLString(self.xmlFile, "vehicle.strawBlowerDoor#animationName"); |
34 | self.strawBlowerDoorAnimationOpenSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.strawBlowerDoor#openSpeed"), 1); |
35 | self.strawBlowerDoorAnimationCloseSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.strawBlowerDoor#closeSpeed"), -self.strawBlowerDoorAnimationOpenSpeed); |
36 | self.isStrawBlowerDoorOpen = false; |
37 | |
38 | self.triggeredBales = {}; |
39 | |
40 | if self.isServer then |
41 | self.strawBlowerTriggerId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.strawBlowerBaleTrigger#index")); |
42 | if self.strawBlowerTriggerId ~= nil then |
43 | addTrigger(self.strawBlowerTriggerId, "strawBlowerBaleTriggerCallback", self); |
44 | end |
45 | end |
46 | |
47 | self.samples = {}; |
48 | local linkNode = Utils.indexToObject(self.components, Utils.getNoNil( getXMLString(self.xmlFile, "vehicle.strawBlowerSounds#linkNode"), "0>" ) ); |
49 | self.samples.workStart = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.startWork", nil, self.baseDirectory, linkNode); |
50 | self.samples.work = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.work", nil, self.baseDirectory, linkNode); |
51 | self.samples.workStop = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.stopWork", nil, self.baseDirectory, linkNode); |
52 | |
53 | self.samples.work.startTime = -1; |
54 | self.samples.workStop.stopTime = -1; |
55 | |
56 | if self.samples.workStart.sample ~= nil then |
57 | self.strawBlowerDoorAnimationOpenSpeed = self:getAnimationDuration(self.strawBlowerDoorAnimation) / self.samples.workStart.duration; |
58 | end |
59 | |
60 | self.strawBlower = {}; |
61 | self.strawBlower.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#fillUnitIndex"), 1); |
62 | self.strawBlower.unloadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#unloadInfoIndex"), 1); |
63 | self.strawBlower.loadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#loadInfoIndex"), 1); |
64 | self.strawBlower.dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#dischargeInfoIndex"), 1); |
65 | |
66 | |
67 | -- we need to send the full fill level rather than percentages since we change the capacity |
68 | self.synchronizeFullFillLevel = true; |
69 | |
70 | if savegame ~= nil and not savegame.resetVehicles then |
71 | if self.strawBlowerDoorAnimation ~= nil then |
72 | local isDoorOpen = getXMLBool(savegame.xmlFile, savegame.key.."#isStrawBlowerDoorOpen"); |
73 | if isDoorOpen ~= nil then |
74 | self:setIsStrawBlowerDoorOpen(isDoorOpen, true); |
75 | if self.playAnimation ~= nil then |
76 | AnimatedVehicle.updateAnimationByName(self, self.strawBlowerDoorAnimation, 99999999); |
77 | if self.updateCylinderedInitial ~= nil then |
78 | self:updateCylinderedInitial(false); |
79 | end |
80 | end |
81 | end |
82 | end |
83 | self:setUnitFillLevel(self.strawBlower.fillUnitIndex, 0, FillUtil.FILLTYPE_UNKNOWN); |
84 | end |
85 | end |
158 | function StrawBlower:update(dt) |
159 | if self.isActive then |
160 | if self:getIsActiveForInput() then |
161 | if self.strawBlowerDoorAnimation ~= nil then |
162 | if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then |
163 | self:setIsStrawBlowerDoorOpen(not self.isStrawBlowerDoorOpen); |
164 | end |
165 | end |
166 | end |
167 | end |
168 | if self.isClient then |
169 | if self.samples.workStart.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.workStop.sound3D ~= nil then |
170 | if self.samples.work.startTime ~= -1 and self.samples.work.startTime <= g_currentMission.time then |
171 | SoundUtil.stop3DSample(self.samples.workStart); |
172 | SoundUtil.play3DSample(self.samples.work); |
173 | SoundUtil.stop3DSample(self.samples.workStop); |
174 | self.samples.work.startTime = -1; |
175 | end |
176 | if self.samples.workStop.stopTime ~= -1 and self.samples.workStop.stopTime < g_currentMission.time then |
177 | self.samples.workStop.stopTime = -1; |
178 | SoundUtil.stop3DSample(self.samples.workStop); |
179 | end |
180 | end |
181 | end |
182 | end |
204 | function StrawBlower:draw() |
205 | if self.strawBlowerDoorAnimation ~= nil then |
206 | if self:getIsActiveForInput(true) then |
207 | if self.isStrawBlowerDoorOpen then |
208 | g_currentMission:addHelpButtonText(g_i18n:getText("action_closeDoor"), InputBinding.IMPLEMENT_EXTRA2, nil, GS_PRIO_NORMAL); |
209 | else |
210 | g_currentMission:addHelpButtonText(g_i18n:getText("action_openDoor"), InputBinding.IMPLEMENT_EXTRA2, nil, GS_PRIO_NORMAL); |
211 | end |
212 | if self:getUnitFillLevel(self.strawBlower.fillUnitIndex) <= 0 and self:getUnitCapacity(self.strawBlower.fillUnitIndex) ~= 0 then |
213 | g_currentMission:addExtraPrintText(g_i18n:getText("info_firstFillTheTool")); |
214 | end |
215 | end |
216 | end |
217 | end |
229 | function StrawBlower:setUnitFillLevel(superFunc, fillUnitIndex, fillLevel, fillType, force) |
230 | if fillUnitIndex == self.strawBlower.fillUnitIndex then |
231 | -- the capacity is always the fill level we have |
232 | self:setUnitCapacity(self.strawBlower.fillUnitIndex, math.max(self:getUnitCapacity(self.strawBlower.fillUnitIndex), fillLevel)); |
233 | |
234 | if self.isServer then |
235 | local bale = self.currentStrawBlowerBale; |
236 | if bale ~= nil then |
237 | if fillLevel <= 0 then |
238 | if self.pendingDynamicMountObjects[bale] ~= nil then |
239 | self.pendingDynamicMountObjects[bale] = nil; |
240 | end |
241 | if self.dynamicMountedObjects[bale] ~= nil then |
242 | self.dynamicMountedObjects[bale] = nil; |
243 | end |
244 | bale:delete(); |
245 | self.currentStrawBlowerBale = nil; |
246 | self:setUnitCapacity(self.strawBlower.fillUnitIndex, 1); |
247 | self.triggeredBales[bale] = nil; |
248 | elseif fillLevel < bale:getFillLevel() and fillType == bale:getFillType() then |
249 | bale:setFillLevel(fillLevel); |
250 | end |
251 | end |
252 | else |
253 | if fillLevel <= 0 then |
254 | self:setUnitCapacity(self.strawBlower.fillUnitIndex, 1); |
255 | end |
256 | end |
257 | end |
258 | superFunc(self, fillUnitIndex, fillLevel, fillType, force); |
259 | end |
269 | function StrawBlower:strawBlowerBaleTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId) |
270 | if onEnter then |
271 | -- this happens if a compound child of a deleted compound is entering |
272 | if otherActorId ~= 0 then |
273 | local object = g_currentMission:getNodeObject(otherActorId); |
274 | if object ~= nil then |
275 | if object:isa(Bale) then |
276 | if self:getUnitFillTypes(self.strawBlower.fillUnitIndex)[object:getFillType()] then |
277 | self.triggeredBales[object] = Utils.getNoNil(self.triggeredBales[object], 0) + 1; |
278 | end |
279 | end |
280 | end |
281 | end |
282 | elseif onLeave then |
283 | if otherActorId ~= 0 then |
284 | local object = g_currentMission:getNodeObject(otherActorId); |
285 | if object ~= nil then |
286 | local triggerCount = self.triggeredBales[object]; |
287 | if triggerCount ~= nil then |
288 | if triggerCount == 1 then |
289 | self.triggeredBales[object] = nil; |
290 | if object == self.currentStrawBlowerBale then |
291 | self.currentStrawBlowerBale = nil; |
292 | self:setUnitFillLevel(self.strawBlower.fillUnitIndex, 0, FillUtil.FILLTYPE_UNKNOWN, true); |
293 | end |
294 | else |
295 | self.triggeredBales[object] = triggerCount-1; |
296 | end |
297 | end |
298 | end |
299 | end |
300 | end |
301 | end |
307 | function StrawBlower:setIsStrawBlowerDoorOpen(isStrawBlowerDoorOpen, noEventSend) |
308 | StrawBlowerDoorOpenEvent.sendEvent(self, isStrawBlowerDoorOpen, noEventSend) |
309 | self.isStrawBlowerDoorOpen = isStrawBlowerDoorOpen; |
310 | |
311 | if self.strawBlowerDoorAnimation ~= nil and self.playAnimation ~= nil then |
312 | local curTime = self:getAnimationTime(self.strawBlowerDoorAnimation); |
313 | if self.isStrawBlowerDoorOpen then |
314 | self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationOpenSpeed, curTime, true); |
315 | else |
316 | self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationCloseSpeed, curTime, true); |
317 | end |
318 | end |
319 | end |