66 | function ReverseDriving:onLoad(savegame) |
67 | local spec = self.spec_reverseDriving |
68 | |
69 | XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.reverseDriving.steering#reversedIndex", "vehicle.reverseDriving.steeringWheel#node") --FS17 to FS19 |
70 | XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.reverseDriving.steering#reversedNode", "vehicle.reverseDriving.steeringWheel#node") --FS17 to FS19 |
71 | |
72 | spec.reversedCharacterTargets = {} |
73 | IKUtil.loadIKChainTargets(self.xmlFile, "vehicle.reverseDriving", self.components, spec.reversedCharacterTargets, self.i3dMappings) |
74 | |
75 | local node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.reverseDriving.steeringWheel#node"), self.i3dMappings) |
76 | if node ~= nil then |
77 | spec.steeringWheel = {} |
78 | spec.steeringWheel.node = node |
79 | local _,ry,_ = getRotation(spec.steeringWheel.node) |
80 | spec.steeringWheel.lastRotation = ry |
81 | spec.steeringWheel.indoorRotation = math.rad(Utils.getNoNil(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.reverseDriving.steeringWheel#indoorRotation"), getXMLFloat(self.xmlFile, "vehicle.drivable.steeringWheel#indoorRotation")), 0)) |
82 | spec.steeringWheel.outdoorRotation = math.rad(Utils.getNoNil(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.reverseDriving.steeringWheel#outdoorRotation"), getXMLFloat(self.xmlFile, "vehicle.drivable.steeringWheel#outdoorRotation")), 0)) |
83 | end |
84 | |
85 | spec.reverseDrivingAnimation = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.reverseDriving#animationName"), "reverseDriving") |
86 | |
87 | if not self:getAnimationExists(spec.reverseDrivingAnimation) then |
88 | g_logManager:xmlError(self.configFileName, "ReverseDriving requires a animation in 'vehicle.reverseDriving#animationName'!") |
89 | end |
90 | |
91 | spec.isChangingDirection = false |
92 | spec.isReverseDriving = false |
93 | spec.isSelectable = true |
94 | |
95 | spec.smoothReverserDirection = 1 |
96 | end |
101 | function ReverseDriving:onPostLoad(savegame) |
102 | local spec = self.spec_reverseDriving |
103 | |
104 | local character = self:getVehicleCharacter() |
105 | if character ~= nil then |
106 | spec.defaultCharacterTargets = character:getIKChainTargets() |
107 | end |
108 | |
109 | local isReverseDriving = false |
110 | if savegame ~= nil then |
111 | isReverseDriving = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key..".reverseDriving#isActive"), false) |
112 | end |
113 | |
114 | self:setIsReverseDriving(isReverseDriving, true) |
115 | spec.updateAnimationOnEnter = isReverseDriving |
116 | end |
330 | function ReverseDriving:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection) |
331 | if self.isClient then |
332 | local spec = self.spec_reverseDriving |
333 | self:clearActionEventsTable(spec.actionEvents) |
334 | if isActiveForInputIgnoreSelection then |
335 | local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.CHANGE_DRIVING_DIRECTION, self, ReverseDriving.actionEventToggleReverseDriving, false, true, false, true, nil) |
336 | g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL) |
337 | g_inputBinding:setActionEventText(actionEventId, g_i18n:getText("input_CHANGE_DRIVING_DIRECTION")) |
338 | end |
339 | end |
340 | end |
146 | function ReverseDriving:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
147 | local spec = self.spec_reverseDriving |
148 | |
149 | if spec.isChangingDirection then |
150 | local character = self:getVehicleCharacter() |
151 | if character ~= nil then |
152 | character:setCharacterVisibility(false) |
153 | end |
154 | |
155 | if not self:getIsEntered() then |
156 | if spec.updateAnimationOnEnter then |
157 | AnimatedVehicle.updateAnimations(self, 99999999) |
158 | spec.updateAnimationOnEnter = false |
159 | end |
160 | end |
161 | |
162 | if not self:getIsAnimationPlaying(spec.reverseDrivingAnimation) then |
163 | self:reverseDirectionChanged(spec.reverserDirection) |
164 | end |
165 | |
166 | local direction = (spec.isReverseDriving and 1) or -1 |
167 | spec.smoothReverserDirection = MathUtil.clamp(spec.smoothReverserDirection - 0.001 * dt * direction, -1, 1) |
168 | end |
169 | end |
314 | function ReverseDriving:onVehicleCharacterChanged(character) |
315 | local spec = self.spec_reverseDriving |
316 | if spec.updateAnimationOnEnter then |
317 | AnimatedVehicle.updateAnimations(self, 99999999) |
318 | spec.updateAnimationOnEnter = false |
319 | end |
320 | |
321 | if spec.isReverseDriving and next(spec.reversedCharacterTargets) ~= nil then |
322 | character:setIKChainTargets(spec.reversedCharacterTargets, true) |
323 | else |
324 | character:setIKChainTargets(spec.defaultCharacterTargets, true) |
325 | end |
326 | end |
53 | function ReverseDriving.registerEventListeners(vehicleType) |
54 | SpecializationUtil.registerEventListener(vehicleType, "onLoad", ReverseDriving) |
55 | SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", ReverseDriving) |
56 | SpecializationUtil.registerEventListener(vehicleType, "onReadStream", ReverseDriving) |
57 | SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", ReverseDriving) |
58 | SpecializationUtil.registerEventListener(vehicleType, "onUpdate", ReverseDriving) |
59 | SpecializationUtil.registerEventListener(vehicleType, "onVehicleCharacterChanged", ReverseDriving) |
60 | SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ReverseDriving) |
61 | end |
41 | function ReverseDriving.registerOverwrittenFunctions(vehicleType) |
42 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "updateSteeringWheel", ReverseDriving.updateSteeringWheel) |
43 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getSteeringDirection", ReverseDriving.getSteeringDirection) |
44 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAllowCharacterVisibilityUpdate", ReverseDriving.getAllowCharacterVisibilityUpdate) |
45 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanStartAIVehicle", ReverseDriving.getCanStartAIVehicle) |
46 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadDashboardGroupFromXML", ReverseDriving.loadDashboardGroupFromXML) |
47 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsDashboardGroupActive", ReverseDriving.getIsDashboardGroupActive) |
48 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", ReverseDriving.getCanBeSelected) |
49 | end |
174 | function ReverseDriving:reverseDirectionChanged(direction) |
175 | local spec = self.spec_reverseDriving |
176 | |
177 | spec.isChangingDirection = false |
178 | if spec.isReverseDriving then |
179 | self:setReverserDirection(-1) |
180 | spec.smoothReverserDirection = -1 |
181 | else |
182 | self:setReverserDirection(1) |
183 | spec.smoothReverserDirection = 1 |
184 | end |
185 | |
186 | local character = self:getVehicleCharacter() |
187 | if character ~= nil then |
188 | if spec.isReverseDriving and next(spec.reversedCharacterTargets) ~= nil then |
189 | character:setIKChainTargets(spec.reversedCharacterTargets) |
190 | else |
191 | character:setIKChainTargets(spec.defaultCharacterTargets) |
192 | end |
193 | |
194 | if character.meshThirdPerson ~= nil and not self:getIsEntered() then |
195 | character:updateVisibility() |
196 | end |
197 | |
198 | character:setAllowCharacterUpdate(true) |
199 | end |
200 | |
201 | if self.setLightsTypesMask ~= nil then |
202 | self:setLightsTypesMask(self.spec_lights.lightsTypesMask, true, true) |
203 | end |
204 | |
205 | SpecializationUtil.raiseEvent(self, "onReverseDirectionChanged", direction) |
206 | end |
212 | function ReverseDriving:setIsReverseDriving(isReverseDriving, noEventSend) |
213 | local spec = self.spec_reverseDriving |
214 | if isReverseDriving ~= spec.isReverseDriving then |
215 | spec.isChangingDirection = true |
216 | spec.isReverseDriving = isReverseDriving |
217 | |
218 | local dir = (isReverseDriving and 1) or -1 |
219 | self:playAnimation(spec.reverseDrivingAnimation, dir, self:getAnimationTime(spec.reverseDrivingAnimation), true) |
220 | |
221 | -- deactivate update of character ik chains to prevent strange states after changing |
222 | local character = self:getVehicleCharacter() |
223 | if character ~= nil then |
224 | character:setAllowCharacterUpdate(false) |
225 | end |
226 | |
227 | self:setReverserDirection(0) |
228 | SpecializationUtil.raiseEvent(self, "onStartReverseDirectionChange") |
229 | ReverseDrivingSetStateEvent.sendEvent(self, isReverseDriving, noEventSend) |
230 | end |
231 | end |