23 | function HeadlandAnimation.initSpecialization() |
24 | local schema = Vehicle.xmlSchema |
25 | schema:setXMLSpecializationType("HeadlandAnimation") |
26 | |
27 | schema:register(XMLValueType.TIME, "vehicle.headlandAnimation#activationDelay", "Headland is activated after this time above activationAngle", 0.5) |
28 | schema:register(XMLValueType.TIME, "vehicle.headlandAnimation#deactivationDelay", "Headland is deactivated after this time below deactivationAngle", 4) |
29 | schema:register(XMLValueType.FLOAT, "vehicle.headlandAnimation#activationAngle", "Headland is activated above this steering percentage [0-1]", 0.2) |
30 | schema:register(XMLValueType.FLOAT, "vehicle.headlandAnimation#deactivationAngle", "Headland is deactivated below this steering percentage [0-1]", 0.13) |
31 | schema:register(XMLValueType.STRING, "vehicle.headlandAnimation#requiredGroundTypes", "Headland is only activated one of these ground types is below vehicle") |
32 | |
33 | schema:register(XMLValueType.STRING, "vehicle.headlandAnimation.animation(?)#name", "Animation name") |
34 | schema:register(XMLValueType.FLOAT, "vehicle.headlandAnimation.animation(?)#speed", "Animation speed") |
35 | |
36 | schema:setXMLSpecializationType() |
37 | end |
59 | function HeadlandAnimation:onLoad(savegame) |
60 | local spec = self.spec_headlandAnimation |
61 | |
62 | spec.isAvailable = self.xmlFile:hasProperty("vehicle.headlandAnimation") |
63 | if spec.isAvailable then |
64 | spec.headlandActivationDelay = self.xmlFile:getValue("vehicle.headlandAnimation#activationDelay", 0.5) |
65 | spec.headlandDeactivationDelay = self.xmlFile:getValue("vehicle.headlandAnimation#deactivationDelay", 4) |
66 | spec.headlandActivationAngle = self.xmlFile:getValue("vehicle.headlandAnimation#activationAngle", 0.2) |
67 | spec.headlandDeactivationAngle = self.xmlFile:getValue("vehicle.headlandAnimation#deactivationAngle", 0.13) |
68 | |
69 | local requiredGroundTypesStr = self.xmlFile:getValue("vehicle.headlandAnimation#requiredGroundTypes") |
70 | requiredGroundTypesStr = requiredGroundTypesStr:split(" ") |
71 | spec.requiredGroundTypes = {} |
72 | for i=1, #requiredGroundTypesStr do |
73 | local name = requiredGroundTypesStr[i] |
74 | if name ~= nil and name ~= "" then |
75 | local groundType = FieldGroundType[name] |
76 | if groundType ~= nil then |
77 | spec.requiredGroundTypes[groundType] = true |
78 | else |
79 | Logging.xmlWarning(self.xmlFile, "Unknown ground type '%s' defined for headland animation", name) |
80 | end |
81 | end |
82 | end |
83 | |
84 | spec.headlandDeactivationTime = 0 |
85 | spec.headlandState = false |
86 | spec.lastHeadlandState = false |
87 | |
88 | spec.animations = {} |
89 | |
90 | local i = 0 |
91 | while true do |
92 | local baseKey = string.format("vehicle.headlandAnimation.animation(%d)", i) |
93 | if not self.xmlFile:hasProperty(baseKey) then |
94 | break |
95 | end |
96 | |
97 | local animation = {} |
98 | animation.name = self.xmlFile:getValue(baseKey .. "#name") |
99 | animation.speed = self.xmlFile:getValue(baseKey .. "#speed") |
100 | if animation.name ~= nil then |
101 | table.insert(spec.animations, animation) |
102 | end |
103 | |
104 | i = i + 1 |
105 | end |
106 | else |
107 | SpecializationUtil.removeEventListener(self, "onUpdate", HeadlandAnimation) |
108 | end |
109 | end |
117 | function HeadlandAnimation:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
118 | local spec = self.spec_headlandAnimation |
119 | |
120 | local validGround = true |
121 | if spec.headlandRequiredDensityBits ~= 0 then |
122 | local x, y, z = getWorldTranslation(self.components[1].node) |
123 | local _, _, groundType = FSDensityMapUtil.getFieldDataAtWorldPosition(x, y, z) |
124 | |
125 | if spec.requiredGroundTypes[groundType] ~= true then |
126 | validGround = false |
127 | end |
128 | end |
129 | |
130 | if validGround and math.abs(self.rotatedTime) > spec.headlandActivationAngle then |
131 | spec.headlandDeactivationTime = spec.headlandDeactivationTime + dt |
132 | if spec.headlandDeactivationTime > spec.headlandActivationDelay then |
133 | spec.headlandState = true |
134 | spec.headlandDeactivationTime = spec.headlandDeactivationDelay |
135 | end |
136 | elseif math.abs(self.rotatedTime) < spec.headlandDeactivationAngle then |
137 | spec.headlandDeactivationTime = math.max(spec.headlandDeactivationTime - dt, 0) |
138 | end |
139 | |
140 | if spec.headlandDeactivationTime == 0 then |
141 | spec.headlandState = false |
142 | end |
143 | |
144 | if spec.headlandState ~= spec.lastHeadlandState then |
145 | local direction = spec.headlandState and 1 or -1 |
146 | |
147 | for i=1, #spec.animations do |
148 | local animation = spec.animations[i] |
149 | self:playAnimation(animation.name, animation.speed * direction, self:getAnimationTime(animation.name)) |
150 | end |
151 | |
152 | spec.lastHeadlandState = spec.headlandState |
153 | end |
154 | end |