Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- StrawBlower |
3 | -- Class for all sprayers |
4 | -- |
5 | -- @author Stefan Geiger |
6 | -- @date 24/02/08 |
7 | -- |
8 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
9 | |
10 | source("dataS/scripts/vehicles/specializations/StrawBlowerDoorOpenEvent.lua"); |
11 | |
12 | StrawBlower = {}; |
13 | |
14 | function StrawBlower.prerequisitesPresent(specializations) |
15 | return SpecializationUtil.hasSpecialization(Fillable, specializations); |
16 | end |
17 | |
18 | function StrawBlower:load(xmlFile) |
19 | |
20 | self.setFillLevel = Utils.overwrittenFunction(self.setFillLevel, StrawBlower.setFillLevel); |
21 | self.strawBlowerBaleTriggerCallback = StrawBlower.strawBlowerBaleTriggerCallback; |
22 | self.setIsStrawBlowerDoorOpen = StrawBlower.setIsStrawBlowerDoorOpen; |
23 | |
24 | |
25 | self.strawBlowerDoorAnimation = getXMLString(xmlFile, "vehicle.strawBlowerDoor#animationName"); |
26 | self.strawBlowerDoorAnimationOpenSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.strawBlowerDoor#openSpeed"), 1); |
27 | self.strawBlowerDoorAnimationCloseSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.strawBlowerDoor#closeSpeed"), -self.strawBlowerDoorAnimationOpenSpeed); |
28 | self.isStrawBlowerDoorOpen = false; |
29 | |
30 | self.triggeredBales = {}; |
31 | |
32 | |
33 | if self.isServer then |
34 | self.strawBlowerTriggerId = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.strawBlowerBaleTrigger#index")); |
35 | if self.strawBlowerTriggerId ~= nil then |
36 | addTrigger(self.strawBlowerTriggerId, "strawBlowerBaleTriggerCallback", self); |
37 | end |
38 | end |
39 | |
40 | self.samples = {}; |
41 | local linkNode = Utils.indexToObject(self.components, Utils.getNoNil( getXMLString(xmlFile, "vehicle.strawBlowerSounds#linkNode"), "0>" ) ); |
42 | self.samples.startWork = Utils.loadSample(xmlFile, {}, "vehicle.strawBlowerSounds.startWork", nil, self.baseDirectory, linkNode); |
43 | self.samples.work = Utils.loadSample(xmlFile, {}, "vehicle.strawBlowerSounds.work", nil, self.baseDirectory, linkNode); |
44 | self.samples.stopWork = Utils.loadSample(xmlFile, {}, "vehicle.strawBlowerSounds.stopWork", nil, self.baseDirectory, linkNode); |
45 | |
46 | self.samples.work.startTime = -1; |
47 | self.samples.stopWork.stopTime = -1; |
48 | |
49 | if self.samples.startWork.sample ~= nil then |
50 | self.strawBlowerDoorAnimationOpenSpeed = self:getAnimationDuration(self.strawBlowerDoorAnimation) / self.samples.startWork.duration; |
51 | end |
52 | |
53 | -- we need to send the full fill level rather than percentages since we change the capacity |
54 | self.synchronizeFullFillLevel = true; |
55 | end |
56 | |
57 | function StrawBlower:delete() |
58 | if self.isServer then |
59 | if self.strawBlowerTriggerId ~= nil then |
60 | removeTrigger(self.strawBlowerTriggerId); |
61 | end |
62 | end |
63 | Utils.deleteSample(self.samples.startWork); |
64 | Utils.deleteSample(self.samples.work); |
65 | Utils.deleteSample(self.samples.stopWork); |
66 | end |
67 | |
68 | function StrawBlower:loadFromAttributesAndNodes(xmlFile, key, resetVehicles) |
69 | if not resetVehicles then |
70 | if self.strawBlowerDoorAnimation ~= nil then |
71 | local isDoorOpen = getXMLBool(xmlFile, key.."#isStrawBlowerDoorOpen"); |
72 | if isDoorOpen ~= nil then |
73 | self:setIsStrawBlowerDoorOpen(isDoorOpen, true); |
74 | if self.playAnimation ~= nil then |
75 | AnimatedVehicle.updateAnimationByName(self, self.strawBlowerDoorAnimation, 99999999); |
76 | if self.updateCylinderedInitial ~= nil then |
77 | self:updateCylinderedInitial(false); |
78 | end |
79 | end |
80 | end |
81 | end |
82 | self:setFillLevel(0, Fillable.FILLTYPE_UNKNOWN, true); |
83 | end |
84 | return BaseMission.VEHICLE_LOAD_OK; |
85 | end |
86 | |
87 | function StrawBlower:getSaveAttributesAndNodes(nodeIdent) |
88 | local attributes = ""; |
89 | if self.strawBlowerDoorAnimation ~= nil then |
90 | attributes = attributes.. 'isStrawBlowerDoorOpen="'.. tostring(self.isStrawBlowerDoorOpen)..'"'; |
91 | end |
92 | local nodes = ""; |
93 | return attributes, nodes; |
94 | end |
95 | |
96 | function StrawBlower:readStream(streamId, connection) |
97 | if self.strawBlowerDoorAnimation ~= nil then |
98 | local isStrawBlowerDoorOpen = streamReadBool(streamId); |
99 | self:setIsStrawBlowerDoorOpen(isStrawBlowerDoorOpen, true); |
100 | end |
101 | end |
102 | |
103 | function StrawBlower:writeStream(streamId, connection) |
104 | if self.strawBlowerDoorAnimation ~= nil then |
105 | streamWriteBool(streamId, self.isStrawBlowerDoorOpen); |
106 | end |
107 | end |
108 | |
109 | function StrawBlower:readUpdateStream(streamId, timestamp, connection) |
110 | end |
111 | |
112 | function StrawBlower:writeUpdateStream(streamId, connection, dirtyMask) |
113 | end |
114 | |
115 | function StrawBlower:mouseEvent(posX, posY, isDown, isUp, button) |
116 | end |
117 | |
118 | function StrawBlower:keyEvent(unicode, sym, modifier, isDown) |
119 | end |
120 | |
121 | function StrawBlower:update(dt) |
122 | if self.isActive then |
123 | if self:getIsActiveForInput() then |
124 | if self.strawBlowerDoorAnimation ~= nil then |
125 | if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then |
126 | self:setIsStrawBlowerDoorOpen(not self.isStrawBlowerDoorOpen); |
127 | end |
128 | end |
129 | end |
130 | end; |
131 | if self.isClient then |
132 | if self.samples.startWork.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.stopWork.sound3D ~= nil then |
133 | if self.samples.work.startTime ~= -1 and self.samples.work.startTime <= g_currentMission.time then |
134 | stopSample(self.samples.startWork.sound3D); |
135 | playSample(self.samples.work.sound3D, 0, 0, nil); |
136 | stopSample(self.samples.stopWork.sound3D); |
137 | setVisibility(self.samples.startWork.sound3D, false); |
138 | setVisibility(self.samples.work.sound3D, true); |
139 | setVisibility(self.samples.stopWork.sound3D, false); |
140 | self.samples.work.startTime = -1; |
141 | end |
142 | if self.samples.stopWork.stopTime ~= -1 and self.samples.stopWork.stopTime < g_currentMission.time then |
143 | self.samples.stopWork.stopTime = -1; |
144 | stopSample(self.samples.stopWork.sound3D); |
145 | setVisibility(self.samples.stopWork.sound3D, false); |
146 | end |
147 | end |
148 | end; |
149 | end |
150 | |
151 | function StrawBlower:updateTick(dt) |
152 | if self.isActive then |
153 | if self.isServer then |
154 | if self.currentStrawBlowerBale == nil then |
155 | local bale = next(self.triggeredBales); |
156 | if bale ~= nil then |
157 | self:setFillLevel(bale:getFillLevel(), bale:getFillType(), true); |
158 | |
159 | self.currentStrawBlowerBale = bale; |
160 | end |
161 | end |
162 | end |
163 | end |
164 | end |
165 | |
166 | function StrawBlower:draw() |
167 | if self.strawBlowerDoorAnimation ~= nil then |
168 | if self:getIsActiveForInput(true) then |
169 | if self.isStrawBlowerDoorOpen then |
170 | g_currentMission:addHelpButtonText(g_i18n:getText("close_door"), InputBinding.IMPLEMENT_EXTRA2); |
171 | else |
172 | g_currentMission:addHelpButtonText(g_i18n:getText("open_door"), InputBinding.IMPLEMENT_EXTRA2); |
173 | end |
174 | if self.fillLevel <= 0 and self:getCapacity() ~= 0 then |
175 | g_currentMission:addExtraPrintText(g_i18n:getText("FirstFillTheTool")); |
176 | end |
177 | end |
178 | end |
179 | end |
180 | |
181 | function StrawBlower:onDeactivateSounds() |
182 | end |
183 | |
184 | function StrawBlower:setFillLevel(superFunc, fillLevel, fillType, force) |
185 | |
186 | -- the capacity is always the fill level we have |
187 | self:setCapacity(math.max(self:getCapacity(), fillLevel)); |
188 | |
189 | if self.isServer then |
190 | local bale = self.currentStrawBlowerBale; |
191 | if bale ~= nil then |
192 | if fillLevel <= 0 then |
193 | if self.pendingDynamicMountObjects[bale] ~= nil then |
194 | self.pendingDynamicMountObjects[bale] = nil; |
195 | end; |
196 | if self.dynamicMountedObjects[bale] ~= nil then |
197 | self.dynamicMountedObjects[bale] = nil; |
198 | end; |
199 | bale:delete(); |
200 | self.currentStrawBlowerBale = nil; |
201 | self:setCapacity(1); |
202 | self.triggeredBales[bale] = nil; |
203 | elseif fillLevel < bale:getFillLevel() and fillType == bale:getFillType() then |
204 | bale:setFillLevel(fillLevel); |
205 | end |
206 | end |
207 | else |
208 | if fillLevel <= 0 then |
209 | self:setCapacity(1); |
210 | end |
211 | end |
212 | |
213 | superFunc(self, fillLevel, fillType, force); |
214 | end |
215 | |
216 | function StrawBlower:strawBlowerBaleTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId) |
217 | if onEnter then |
218 | -- this happens if a compound child of a deleted compound is entering |
219 | if otherActorId ~= 0 then |
220 | local object = g_currentMission:getNodeObject(otherActorId); |
221 | if object ~= nil then |
222 | if object:isa(Bale) then |
223 | if self.fillTypes[object:getFillType()] then |
224 | self.triggeredBales[object] = Utils.getNoNil(self.triggeredBales[object], 0) + 1; |
225 | end |
226 | end |
227 | end |
228 | end |
229 | elseif onLeave then |
230 | if otherActorId ~= 0 then |
231 | local object = g_currentMission:getNodeObject(otherActorId); |
232 | if object ~= nil then |
233 | local triggerCount = self.triggeredBales[object]; |
234 | if triggerCount ~= nil then |
235 | if triggerCount == 1 then |
236 | self.triggeredBales[object] = nil; |
237 | if object == self.currentStrawBlowerBale then |
238 | self.currentStrawBlowerBale = nil; |
239 | self:setFillLevel(0, Fillable.FILLTYPE_UNKNOWN, true); |
240 | end |
241 | else |
242 | self.triggeredBales[object] = triggerCount-1; |
243 | end |
244 | end |
245 | end |
246 | end |
247 | end |
248 | end |
249 | |
250 | function StrawBlower:setIsStrawBlowerDoorOpen(isStrawBlowerDoorOpen, noEventSend) |
251 | StrawBlowerDoorOpenEvent.sendEvent(self, isStrawBlowerDoorOpen, noEventSend) |
252 | self.isStrawBlowerDoorOpen = isStrawBlowerDoorOpen; |
253 | |
254 | if self.strawBlowerDoorAnimation ~= nil and self.playAnimation ~= nil then |
255 | if self.isStrawBlowerDoorOpen then |
256 | self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationOpenSpeed, nil, true); |
257 | else |
258 | self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationCloseSpeed, nil, true); |
259 | end |
260 | end |
261 | end |
262 | |
263 | function StrawBlower:onStartTip(tipTrigger, tipReferencePointIndex, noEventSend) |
264 | if self.samples.startWork.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.stopWork.sound3D ~= nil then |
265 | setVisibility(self.samples.startWork.sound3D, true); |
266 | setVisibility(self.samples.work.sound3D, false); |
267 | setVisibility(self.samples.stopWork.sound3D, false); |
268 | playSample(self.samples.startWork.sound3D, 1, 0, nil); |
269 | stopSample(self.samples.work.sound3D); |
270 | stopSample(self.samples.stopWork.sound3D); |
271 | self.samples.work.startTime = g_currentMission.time + self.samples.startWork.duration; |
272 | end |
273 | end |
274 | |
275 | function StrawBlower:onEndTip(noEventSend) |
276 | if self.samples.startWork.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.stopWork.sound3D ~= nil then |
277 | setVisibility(self.samples.startWork.sound3D, false); |
278 | setVisibility(self.samples.work.sound3D, false); |
279 | setVisibility(self.samples.stopWork.sound3D, true); |
280 | stopSample(self.samples.startWork.sound3D); |
281 | stopSample(self.samples.work.sound3D); |
282 | playSample(self.samples.stopWork.sound3D, 1, 0, nil); |
283 | self.samples.work.startTime = -1; |
284 | self.samples.stopWork.stopTime = g_currentMission.time + self.samples.stopWork.duration; |
285 | end |
286 | end
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de