Sprache Deutsch Language English

Script Dokumentation LS 2015 - WaterTrailer (Patch 1.3)

Script Dokumentation Übersicht

scripts/vehicles/specializations/WaterTrailer.lua

Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1--
2-- Water Trailer
3--
4--
5-- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
6
7source("dataS/scripts/vehicles/specializations/WaterTrailerSetIsFillingEvent.lua");
8
9WaterTrailer = {};
10
11function WaterTrailer.prerequisitesPresent(specializations)
12 return SpecializationUtil.hasSpecialization(Fillable, specializations);
13end;
14
15function WaterTrailer:load(xmlFile)
16
17 self.setIsWaterTrailerFilling = SpecializationUtil.callSpecializationsFunction("setIsWaterTrailerFilling");
18 self.removeWaterTrailerFillTrigger = SpecializationUtil.callSpecializationsFunction("removeWaterTrailerFillTrigger");
19 self.addWaterTrailerFillTrigger = SpecializationUtil.callSpecializationsFunction("addWaterTrailerFillTrigger");
20
21 self.isWaterTrailerFilling = false;
22 self.waterTrailerActivatableAdded = false;
23 self.waterTrailerActivatable = WaterTrailerActivatable:new(self);
24
25 self.waterTrailerFillTriggers = {};
26
27
28 self.fillLitersPerSecond = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fillLitersPerSecond"), 500);
29
30 local waterTrailerFillTriggerNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.waterTrailerFillTrigger#index"));
31
32 if waterTrailerFillTriggerNode ~= nil then
33 self.waterTrailerFillTrigger = WaterTrailerFillTrigger:new(waterTrailerFillTriggerNode, self);
34 end;
35
36 if self.isClient then
37 self.sampleRefuel = Utils.loadSample(xmlFile, {}, "vehicle.refuelSound", "$data/maps/sounds/refuel.wav", self.baseDirectory, self.components[1].node);
38 end;
39end;
40
41function WaterTrailer:delete()
42 for _, trigger in pairs(self.waterTrailerFillTriggers) do
43 trigger:onVehicleDeleted(self);
44 end
45 if self.waterTrailerActivatableAdded then
46 g_currentMission:removeActivatableObject(self.waterTrailerActivatable);
47 self.waterTrailerActivatableAdded = false;
48 end;
49 if self.waterTrailerFillTrigger ~= nil then
50 self.waterTrailerFillTrigger:delete();
51 self.waterTrailerFillTrigger = nil;
52 end;
53 if self.isClient then
54 Utils.deleteSample(self.sampleRefuel);
55 end;
56end;
57
58function WaterTrailer:readStream(streamId, connection)
59 local isWaterTrailerFilling = streamReadBool(streamId);
60 self:setIsWaterTrailerFilling(isWaterTrailerFilling, true);
61end;
62
63function WaterTrailer:writeStream(streamId, connection)
64 streamWriteBool(streamId, self.isWaterTrailerFilling);
65end;
66
67function WaterTrailer:mouseEvent(posX, posY, isDown, isUp, button)
68end;
69
70function WaterTrailer:keyEvent(unicode, sym, modifier, isDown)
71end;
72
73function WaterTrailer:update(dt)
74end;
75
76function WaterTrailer:updateTick(dt)
77 if self:getIsActive() or self.isWaterTrailerFilling then
78 local _,y,_ = getWorldTranslation(self.components[1].node);
79 local isNearWater = (y <= g_currentMission.waterY + 0.2);
80 local waterTrailerFillTrigger = nil;
81 if not isNearWater then
82 -- find the first trigger which is activable
83 for i=1, table.getn(self.waterTrailerFillTriggers) do
84 local trigger = self.waterTrailerFillTriggers[i];
85 if trigger:getIsActivatable(self) then
86 isNearWater = true;
87 waterTrailerFillTrigger = trigger;
88 break;
89 end
90 end
91 end
92 if isNearWater then
93 if not self.waterTrailerActivatableAdded then
94 g_currentMission:addActivatableObject(self.waterTrailerActivatable);
95 self.waterTrailerActivatableAdded = true;
96 end;
97 else
98 if self.waterTrailerActivatableAdded then
99 g_currentMission:removeActivatableObject(self.waterTrailerActivatable);
100 self.waterTrailerActivatableAdded = false;
101 end;
102 end;
103
104 if self.isServer then
105 -- stop filling if not near the water anymore
106 if self.isWaterTrailerFilling then
107 if not isNearWater then
108 self:setIsWaterTrailerFilling(false);
109 end
110 end
111
112 if self.isWaterTrailerFilling then
113 local delta = self.fillLitersPerSecond*dt*0.001;
114
115 if waterTrailerFillTrigger ~= nil then
116 delta = waterTrailerFillTrigger:fillWater(self, delta);
117 else
118 if self:allowFillType(Fillable.FILLTYPE_WATER, false) then
119 local oldFillLevel = self:getFillLevel(Fillable.FILLTYPE_WATER);
120 self:setFillLevel(oldFillLevel + delta, Fillable.FILLTYPE_WATER, true);
121 delta = self:getFillLevel(Fillable.FILLTYPE_WATER) - oldFillLevel;
122 else
123 delta = 0;
124 end
125 end
126 if delta <= 0 then
127 self:setIsWaterTrailerFilling(false);
128 end;
129 end;
130 end;
131 end;
132end;
133
134function WaterTrailer:draw()
135 if self.isClient then
136 if self.fillLevel <= 0 and self:getCapacity() ~= 0 then
137 g_currentMission:addExtraPrintText(g_i18n:getText("FirstFillTheTool"));
138 end;
139 end;
140end;
141
142function WaterTrailer:onDetach()
143 if self.waterTrailerActivatableAdded then
144 g_currentMission:removeActivatableObject(self.waterTrailerActivatable);
145 self.waterTrailerActivatableAdded = false;
146 end;
147end;
148
149function WaterTrailer:onLeave()
150 if self.waterTrailerActivatableAdded then
151 g_currentMission:removeActivatableObject(self.waterTrailerActivatable);
152 self.waterTrailerActivatableAdded = false;
153 end;
154end;
155
156function WaterTrailer:setIsWaterTrailerFilling(isFilling, noEventSend)
157 WaterTrailerSetIsFillingEvent.sendEvent(self, isFilling, noEventSend)
158 self.isWaterTrailerFilling = isFilling;
159 if self.isClient and self.sampleRefuel ~= nil then
160 if isFilling then
161 Utils.play3DSample(self.sampleRefuel);
162 else
163 Utils.stop3DSample(self.sampleRefuel);
164 end;
165 end;
166end;
167
168function WaterTrailer:addWaterTrailerFillTrigger(trigger)
169 table.insert(self.waterTrailerFillTriggers, trigger);
170end;
171
172function WaterTrailer:removeWaterTrailerFillTrigger(trigger)
173 for i=1, table.getn(self.waterTrailerFillTriggers) do
174 if self.waterTrailerFillTriggers[i] == trigger then
175 table.remove(self.waterTrailerFillTriggers, i);
176 break;
177 end;
178 end;
179end;
180
181WaterTrailerActivatable = {}
182local WaterTrailerActivatable_mt = Class(WaterTrailerActivatable);
183
184function WaterTrailerActivatable:new(trailer)
185 local self = {};
186 setmetatable(self, WaterTrailerActivatable_mt);
187
188 self.trailer = trailer;
189 self.activateText = "unknown";
190
191 return self;
192end;
193
194
195function WaterTrailerActivatable:getIsActivatable()
196 if self.trailer:getIsActiveForInput() and self.trailer:getFillLevel(Fillable.FILLTYPE_WATER) < self.trailer:getCapacity() and self.trailer:allowFillType(Fillable.FILLTYPE_WATER, false) then
197 self:updateActivateText();
198 return true;
199 end
200 return false;
201end
202
203function WaterTrailerActivatable:onActivateObject()
204 self.trailer:setIsWaterTrailerFilling(not self.trailer.isWaterTrailerFilling);
205 self:updateActivateText();
206 g_currentMission:addActivatableObject(self);
207end;
208
209function WaterTrailerActivatable:drawActivate()
210 -- TODO draw icon
211end;
212
213function WaterTrailerActivatable:updateActivateText()
214 if self.trailer.isWaterTrailerFilling then
215 self.activateText = string.format(g_i18n:getText("stop_refill_OBJECT"), self.trailer.typeDesc);
216 else
217 self.activateText = string.format(g_i18n:getText("refill_OBJECT"), self.trailer.typeDesc);
218 end;
219end;
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
Script Dokumentation Übersicht