Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- WaterTrailerFillTrigger class |
2 | -- |
3 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
4 | |
5 | WaterTrailerFillTrigger = {} |
6 | |
7 | local WaterTrailerFillTrigger_mt = Class(WaterTrailerFillTrigger); |
8 | |
9 | function WaterTrailerFillTrigger:onCreate(id) |
10 | g_currentMission:addNonUpdateable(WaterTrailerFillTrigger:new(id)); |
11 | end; |
12 | |
13 | function WaterTrailerFillTrigger:new(nodeId, trailer) |
14 | local self = {}; |
15 | setmetatable(self, WaterTrailerFillTrigger_mt); |
16 | |
17 | self.triggerId = nodeId; |
18 | addTrigger(nodeId, "triggerCallback", self); |
19 | |
20 | self.priceScale = Utils.getNoNil(getUserAttribute(nodeId, "priceScale"), 1); |
21 | |
22 | self.trailer = trailer; |
23 | |
24 | self.triggeredTrailers = {}; |
25 | |
26 | self.isEnabled = true; |
27 | |
28 | return self; |
29 | end; |
30 | |
31 | function WaterTrailerFillTrigger:delete() |
32 | for trailer, _ in pairs(self.triggeredTrailers) do |
33 | if trailer.removeWaterTrailerFillTrigger ~= nil then |
34 | trailer:removeWaterTrailerFillTrigger(self); |
35 | end |
36 | end |
37 | removeTrigger(self.triggerId); |
38 | end; |
39 | |
40 | function WaterTrailerFillTrigger:onVehicleDeleted(vehicle) |
41 | self.triggeredTrailers[vehicle] = nil; |
42 | end |
43 | |
44 | function WaterTrailerFillTrigger:fillWater(vehicle, delta) |
45 | |
46 | if not vehicle:allowFillType(Fillable.FILLTYPE_WATER, false) then |
47 | return 0; |
48 | end |
49 | |
50 | if self.trailer ~= nil then |
51 | local trailerWaterFillLevel = self.trailer:getFillLevel(Fillable.FILLTYPE_WATER); |
52 | if trailerWaterFillLevel > 0 then |
53 | delta = math.min(delta, trailerWaterFillLevel); |
54 | if delta <= 0 then |
55 | return 0; |
56 | end |
57 | else |
58 | return 0; |
59 | end |
60 | end |
61 | |
62 | local oldFillLevel = vehicle:getFillLevel(Fillable.FILLTYPE_WATER); |
63 | vehicle:setFillLevel(oldFillLevel + delta, Fillable.FILLTYPE_WATER); |
64 | delta = vehicle:getFillLevel(Fillable.FILLTYPE_WATER) - oldFillLevel; |
65 | |
66 | if delta > 0 then |
67 | if self.trailer ~= nil then |
68 | self.trailer:setFillLevel(self.trailer:getFillLevel(Fillable.FILLTYPE_WATER) - delta, Fillable.FILLTYPE_WATER); |
69 | elseif self.priceScale > 0 then |
70 | local price = delta * g_waterPricePerLiter * self.priceScale; |
71 | g_currentMission.missionStats:updateStats("expenses", price); |
72 | g_currentMission:addSharedMoney(-price, "other"); |
73 | end |
74 | end |
75 | return delta; |
76 | end |
77 | |
78 | function WaterTrailerFillTrigger:getIsActivatable(vehicle) |
79 | if self.trailer ~= nil then |
80 | if self.trailer:getFillLevel(Fillable.FILLTYPE_WATER) <= 0 then |
81 | return false; |
82 | end |
83 | end |
84 | if not vehicle:allowFillType(Fillable.FILLTYPE_WATER, false) then |
85 | return false; |
86 | end |
87 | return true; |
88 | end |
89 | |
90 | function WaterTrailerFillTrigger:triggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId) |
91 | if self.isEnabled and (onEnter or onLeave) then |
92 | |
93 | local waterTrailer = g_currentMission.objectToTrailer[otherShapeId]; |
94 | |
95 | if waterTrailer ~= nil and waterTrailer ~= self.trailer and waterTrailer.addWaterTrailerFillTrigger ~= nil and waterTrailer.removeWaterTrailerFillTrigger ~= nil then |
96 | if onEnter then |
97 | self.triggeredTrailers[waterTrailer] = waterTrailer; |
98 | waterTrailer:addWaterTrailerFillTrigger(self); |
99 | else -- onLeave |
100 | self.triggeredTrailers[waterTrailer] = nil; |
101 | waterTrailer:removeWaterTrailerFillTrigger(self); |
102 | end; |
103 | end; |
104 | end; |
105 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de