109 | function Wipers:loadWiperFromXML(xmlFile, key, wiper) |
110 | local animName = getXMLString(xmlFile, key .. "#animName") |
111 | if animName ~= nil then |
112 | if self:getAnimationExists(animName) then |
113 | wiper.animName = animName |
114 | wiper.animDuration = self:getAnimationDuration(animName) |
115 | wiper.states = {} |
116 | local j = 0 |
117 | while true do |
118 | local stateKey = string.format("%s.state(%d)", key, j) |
119 | if not hasXMLProperty(xmlFile, stateKey) then |
120 | break |
121 | end |
122 | |
123 | local state = {} |
124 | state.animSpeed = getXMLFloat(xmlFile, stateKey .. "#animSpeed") |
125 | state.animPause = getXMLFloat(xmlFile, stateKey .. "#animPause") |
126 | if state.animSpeed ~= nil and state.animPause ~= nil then |
127 | state.animPause = state.animPause * 1000 |
128 | table.insert(wiper.states, state) |
129 | end |
130 | |
131 | j = j + 1 |
132 | end |
133 | else |
134 | g_logManager:xmlWarning(self.configFileName, "Animation '%s' not defined for wiper '%s'!", animName, key) |
135 | return false |
136 | end |
137 | else |
138 | g_logManager:xmlWarning(self.configFileName, "Missing animation for wiper '%s'!", key) |
139 | return false |
140 | end |
141 | |
142 | local numStates = #wiper.states |
143 | if numStates > 0 then |
144 | local stepSize = 1.0 / numStates |
145 | local curMax = stepSize |
146 | |
147 | for _,state in ipairs(wiper.states) do |
148 | state.maxRainValue = curMax |
149 | curMax = curMax + stepSize |
150 | end |
151 | |
152 | wiper.nextStartTime = nil |
153 | else |
154 | g_logManager:xmlWarning(self.configFileName, "No states defined for wiper '%s'!", key) |
155 | return false |
156 | end |
157 | |
158 | return true |
159 | end |
36 | function Wipers:onLoad(savegame) |
37 | local spec = self.spec_wipers |
38 | |
39 | spec.wipers = {} |
40 | |
41 | local i = 0 |
42 | while true do |
43 | local key = string.format("vehicle.wipers.wiper(%d)", i) |
44 | if not hasXMLProperty(self.xmlFile, key) then |
45 | break |
46 | end |
47 | |
48 | local wiper = {} |
49 | if self:loadWiperFromXML(self.xmlFile, key, wiper) then |
50 | table.insert(spec.wipers, wiper) |
51 | end |
52 | |
53 | i = i + 1 |
54 | end |
55 | |
56 | spec.hasWipers = #spec.wipers > 0 |
57 | spec.lastRainScale = 0.0 |
58 | end |
62 | function Wipers:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
63 | local spec = self.spec_wipers |
64 | |
65 | if spec.hasWipers and self:getIsControlled() then |
66 | spec.lastRainScale = g_currentMission.environment.weather:getRainFallScale() |
67 | |
68 | for _, wiper in pairs(spec.wipers) do |
69 | local stateIdToUse = 0 |
70 | if self:getIsActiveForWipers() then |
71 | if spec.lastRainScale > 0 then |
72 | for stateIndex,state in ipairs(wiper.states) do |
73 | if spec.lastRainScale <= state.maxRainValue then |
74 | stateIdToUse = stateIndex |
75 | break |
76 | end |
77 | end |
78 | end |
79 | end |
80 | |
81 | if Wipers.forcedState ~= 0 then |
82 | stateIdToUse = MathUtil.clamp(Wipers.forcedState, 1, #wiper.states) |
83 | end |
84 | |
85 | if stateIdToUse > 0 then |
86 | local currentState = wiper.states[stateIdToUse] |
87 | |
88 | if self:getAnimationTime(wiper.animName) == 1 then |
89 | self:playAnimation(wiper.animName, -currentState.animSpeed, 1, true) |
90 | end |
91 | |
92 | if wiper.nextStartTime == nil or wiper.nextStartTime < g_currentMission.time then |
93 | if not self:getIsAnimationPlaying(wiper.animName) then |
94 | self:playAnimation(wiper.animName, currentState.animSpeed, 0, true) |
95 | wiper.nextStartTime = nil |
96 | end |
97 | end |
98 | |
99 | if wiper.nextStartTime == nil then |
100 | wiper.nextStartTime = g_currentMission.time + (wiper.animDuration / currentState.animSpeed) + currentState.animPause |
101 | end |
102 | end |
103 | end |
104 | end |
105 | end |