32 | function Crawler:loadCrawlers(xmlFile) |
33 | local wheelId = Utils.getNoNil(self.configurations["wheel"], 1); |
34 | local baseKey = string.format("vehicle.wheelConfigurations.wheelConfiguration(%d)", wheelId-1); |
35 | if not hasXMLProperty(xmlFile, baseKey) then |
36 | baseKey = "vehicle"; |
37 | end; |
38 | |
39 | local i = 0; |
40 | while true do |
41 | local key = string.format(baseKey..".crawlers.crawler(%d)", i); |
42 | if not hasXMLProperty(xmlFile, key) then |
43 | break; |
44 | end; |
45 | |
46 | local indexBase = self.components; |
47 | local crawler = {}; |
48 | |
49 | local filename = getXMLString(xmlFile, key .. "#filename"); |
50 | if filename ~= nil then |
51 | crawler.filename = filename; |
52 | crawlerIndex = Utils.getNoNil(getXMLString(xmlFile, key .. "#crawlerIndex"), "0|0"); |
53 | linkIndex = Utils.getNoNil(getXMLString(xmlFile, key .. "#linkIndex"), "0>"); |
54 | offset = Utils.getVectorNFromString(Utils.getNoNil(getXMLString(xmlFile, key .. "#offset"), "0 0 0"), 3); |
55 | local i3dNode = Utils.loadSharedI3DFile(filename, self.baseDirectory, false, false, false); |
56 | if i3dNode ~= 0 then |
57 | local loadedCrawler = Utils.indexToObject(i3dNode, crawlerIndex); |
58 | local linkNode = Utils.indexToObject(self.components, linkIndex); |
59 | link(linkNode, loadedCrawler); |
60 | setTranslation(loadedCrawler, offset[1], offset[2], offset[3]); |
61 | indexBase = loadedCrawler; |
62 | delete(i3dNode); |
63 | end; |
64 | end; |
65 | |
66 | crawler.node = Utils.indexToObject(indexBase, getXMLString(xmlFile, key.."#index")); |
67 | crawler.speedRefNode = Utils.getNoNil(Utils.indexToObject(indexBase, getXMLString(xmlFile, key.."#speedRefNode")), node); |
68 | |
69 | if crawler.node ~= nil then |
70 | local speedRefWheel = getXMLInt(xmlFile, key.."#speedRefWheel"); |
71 | if speedRefWheel ~= nil and self.wheels[speedRefWheel] ~= nil then |
72 | crawler.speedRefWheel = self.wheels[speedRefWheel]; |
73 | end; |
74 | crawler.scrollSpeed = Utils.getNoNil(getXMLFloat(xmlFile, key.."#scrollSpeed"), 1); |
75 | crawler.shaderParameterName = Utils.getNoNil(getXMLString(xmlFile, key.."#shaderParameterName"), "scrollPosition"); |
76 | crawler.shaderParameterComponent = Utils.getNoNil(getXMLInt(xmlFile, key.."#shaderParameterComponent"), 1); |
77 | crawler.scrollPosition = 0; |
78 | crawler.scrollLength = Utils.getNoNil(getXMLFloat(xmlFile, key.."#scrollLength"), 1); |
79 | |
80 | crawler.rotatingParts = Utils.getNoNil(crawler.rotatingParts, {}); |
81 | local j = 0; |
82 | while true do |
83 | local keyRotation = key..string.format(".rotatingPart(%d)", j); |
84 | if not hasXMLProperty(xmlFile, keyRotation) then |
85 | break; |
86 | end; |
87 | local rotationPart = {}; |
88 | rotationPart.node = Utils.indexToObject(indexBase, getXMLString(xmlFile, keyRotation.."#index")); |
89 | rotationPart.speedScale = getXMLFloat(xmlFile, keyRotation.."#speedScale"); |
90 | if rotationPart.speedScale == nil then |
91 | rotationPart.speedScale = 1.0 / Utils.getNoNil(getXMLFloat(xmlFile, keyRotation.."#radius"), 1); |
92 | end; |
93 | if rotationPart.node ~= nil then |
94 | table.insert(crawler.rotatingParts, rotationPart); |
95 | end; |
96 | j = j + 1; |
97 | end; |
98 | |
99 | table.insert(self.crawlers, crawler); |
100 | end; |
101 | i = i + 1; |
102 | end; |
103 | end; |
124 | function Crawler:update(dt) |
125 | if self:getIsActive() then |
126 | for i, crawler in pairs(self.crawlers) do |
127 | local movedDistance = 0; |
128 | |
129 | if crawler.speedRefWheel ~= nil then |
130 | |
131 | local newX, _, _ = getRotation(crawler.speedRefWheel.driveNode); |
132 | if crawler.lastRotation == nil then |
133 | crawler.lastRotation = newX; |
134 | end; |
135 | |
136 | if newX - crawler.lastRotation < -math.pi then |
137 | crawler.lastRotation = crawler.lastRotation - 2*math.pi; |
138 | elseif newX - crawler.lastRotation > math.pi then |
139 | crawler.lastRotation = crawler.lastRotation + 2*math.pi; |
140 | end; |
141 | |
142 | movedDistance = crawler.speedRefWheel.radius * (newX-crawler.lastRotation); |
143 | |
144 | crawler.lastRotation = newX; |
145 | else |
146 | local newX, newY, newZ = getWorldTranslation(crawler.speedRefNode); |
147 | if crawler.lastPosition == nil then |
148 | crawler.lastPosition = {newX, newY, newZ}; |
149 | end; |
150 | local dx, dy, dz = worldDirectionToLocal(crawler.speedRefNode, newX-crawler.lastPosition[1], newY-crawler.lastPosition[2], newZ-crawler.lastPosition[3]); |
151 | local movingDirection = 0; |
152 | if dz > 0.0001 then |
153 | movingDirection = 1; |
154 | elseif dz < -0.0001 then |
155 | movingDirection = -1; |
156 | end; |
157 | movedDistance = Utils.vector3Length(dx, dy, dz) * movingDirection; |
158 | crawler.lastPosition[1] = newX; |
159 | crawler.lastPosition[2] = newY; |
160 | crawler.lastPosition[3] = newZ; |
161 | end; |
162 | |
163 | crawler.scrollPosition = (crawler.scrollPosition + movedDistance*crawler.scrollSpeed) % crawler.scrollLength; |
164 | if crawler.shaderParameterComponent == 1 then |
165 | setShaderParameter(crawler.node, crawler.shaderParameterName, crawler.scrollPosition,0,0,0, false); |
166 | else |
167 | setShaderParameter(crawler.node, crawler.shaderParameterName, 0,crawler.scrollPosition,0,0, false); |
168 | end; |
169 | |
170 | for _, rotatingPart in pairs(crawler.rotatingParts) do |
171 | rotate(rotatingPart.node, rotatingPart.speedScale * movedDistance, 0, 0); |
172 | end; |
173 | end; |
174 | end; |
175 | end; |