24 | function TreeSaw:load(savegame) |
25 | self.treeSaw = {} |
26 | |
27 | if self.isClient then |
28 | self.treeSaw.treeSawTurnedOnRotationNodes = Utils.loadRotationNodes(self.xmlFile, {}, "vehicle.turnedOnRotationNodes.turnedOnRotationNode", "treeSaw", self.components) |
29 | |
30 | self.treeSaw.cutParticleSystems = {} |
31 | |
32 | local i = 0 |
33 | while true do |
34 | local keyPS = string.format("vehicle.treeSaw.cutParticleSystems.emitterShape(%d)", i) |
35 | if not hasXMLProperty(self.xmlFile, keyPS) then |
36 | break |
37 | end |
38 | local emitterShape = Utils.indexToObject(self.components, getXMLString(self.xmlFile, keyPS.."#node")) |
39 | local particleType = getXMLString(self.xmlFile, keyPS.."#particleType") |
40 | if emitterShape ~= nil then |
41 | local fillType = FillUtil.FILLTYPE_WOODCHIPS |
42 | local particleSystem = MaterialUtil.getParticleSystem(fillType, particleType) |
43 | if particleSystem ~= nil then |
44 | table.insert(self.treeSaw.cutParticleSystems, ParticleUtil.copyParticleSystem(self.xmlFile, keyPS, particleSystem, emitterShape)) |
45 | end |
46 | end |
47 | i = i + 1 |
48 | end |
49 | |
50 | if #self.treeSaw.cutParticleSystems == 0 then |
51 | self.treeSaw.cutParticleSystems = nil |
52 | end |
53 | |
54 | self.treeSaw.sampleCut = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.treeSaw.cutSound", nil, self.baseDirectory) |
55 | self.treeSaw.sampleSaw = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.treeSaw.sawSound", nil, self.baseDirectory) |
56 | end |
57 | |
58 | self.treeSaw.cutNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.treeSaw.cutNode#node")) |
59 | self.treeSaw.cutSizeY = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#sizeY"), 1) |
60 | self.treeSaw.cutSizeZ = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#sizeZ"), 1) |
61 | self.treeSaw.lengthAboveThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#lengthAboveThreshold"), 0.3) |
62 | self.treeSaw.lengthBelowThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#lengthBelowThreshold"), 0.3) |
63 | self.treeSaw.cutTimerDuration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#timer"), 1)*1000 |
64 | self.treeSaw.curSplitShape = nil |
65 | self.treeSaw.cutTimer = -1 |
66 | self.treeSaw.isCutting = false |
67 | end |
122 | function TreeSaw:update(dt) |
123 | if self:getIsActive() then |
124 | |
125 | -- Verify that the split shapes still exist (possible that someone has cut them) |
126 | if self.isServer then |
127 | if self.treeSaw.curSplitShape ~= nil then |
128 | if not entityExists(self.treeSaw.curSplitShape) then |
129 | self.treeSaw.curSplitShape = nil |
130 | if g_server ~= nil then |
131 | self.treeSaw.cutTimer = -1 |
132 | end |
133 | end |
134 | end |
135 | end |
136 | |
137 | if self.isServer then |
138 | self.treeSaw.isCutting = self.treeSaw.curSplitShape ~= nil |
139 | if self.treeSaw.curSplitShape ~= nil then |
140 | if self.treeSaw.cutTimer > 0 then |
141 | self.treeSaw.cutTimer = math.max(self.treeSaw.cutTimer - dt, 0) |
142 | end |
143 | |
144 | -- cut |
145 | if self.treeSaw.cutTimer == 0 then |
146 | self.treeSaw.cutTimer = -1 |
147 | |
148 | local x,y,z = getWorldTranslation(self.treeSaw.cutNode) |
149 | local nx,ny,nz = localDirectionToWorld(self.treeSaw.cutNode, 1,0,0) |
150 | local yx,yy,yz = localDirectionToWorld(self.treeSaw.cutNode, 0,1,0) |
151 | |
152 | ChainsawUtil.cutSplitShape(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ) |
153 | self.treeSaw.curSplitShape = nil |
154 | end |
155 | end |
156 | end |
157 | end |
158 | |
159 | -- PS and sound for cut |
160 | if self.isClient then |
161 | if self:getIsActiveForSound() then |
162 | if self:getIsTurnedOn() then |
163 | SoundUtil.playSample(self.treeSaw.sampleSaw, 0, 0, nil) |
164 | end |
165 | if self.treeSaw.cutTimer > 0 then |
166 | SoundUtil.playSample(self.treeSaw.sampleCut, 0, 0, nil) |
167 | else |
168 | SoundUtil.stopSample(self.treeSaw.sampleCut) |
169 | end |
170 | else |
171 | SoundUtil.stopSample(self.treeSaw.sampleCut) |
172 | end |
173 | |
174 | if self.treeSaw.cutParticleSystems ~= nil then |
175 | if self.treeSaw.cutTimer > 0 and not self.treeSaw.cutParticleSystemsActive then |
176 | self.treeSaw.cutParticleSystemsActive = true |
177 | for _,ps in pairs(self.treeSaw.cutParticleSystems) do |
178 | ParticleUtil.setEmittingState(ps, true) |
179 | end |
180 | elseif self.treeSaw.cutTimer <= 0 and self.treeSaw.cutParticleSystemsActive then |
181 | self.treeSaw.cutParticleSystemsActive = false |
182 | for _,ps in pairs(self.treeSaw.cutParticleSystems) do |
183 | ParticleUtil.setEmittingState(ps, false) |
184 | end |
185 | end |
186 | end |
187 | end |
188 | |
189 | if self.isClient then |
190 | Utils.updateRotationNodes(self, self.treeSaw.treeSawTurnedOnRotationNodes, dt, self:getIsActive() and self:getIsTurnedOn()) |
191 | end |
192 | end |
197 | function TreeSaw:updateTick(dt) |
198 | if self:getIsActive() then |
199 | |
200 | if self:getIsTurnedOn() then |
201 | if self.treeSaw.cutNode ~= nil then |
202 | local x,y,z = getWorldTranslation(self.treeSaw.cutNode) |
203 | local nx,ny,nz = localDirectionToWorld(self.treeSaw.cutNode, 1,0,0) |
204 | local yx,yy,yz = localDirectionToWorld(self.treeSaw.cutNode, 0,1,0) |
205 | |
206 | if self.treeSaw.curSplitShape == nil then |
207 | local shape, _, _, _, _ = findSplitShape(x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ) |
208 | if shape ~= 0 then |
209 | self.treeSaw.curSplitShape = shape |
210 | self.treeSaw.cutTimer = self.treeSaw.cutTimerDuration |
211 | end |
212 | end |
213 | |
214 | if self.treeSaw.curSplitShape ~= nil then |
215 | local minY,maxY, minZ,maxZ = testSplitShape(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ) |
216 | if minY == nil then |
217 | self.treeSaw.curSplitShape = nil |
218 | else |
219 | -- check if cut would be below y=0 (tree CoSy) |
220 | local cutTooLow = false |
221 | local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,minY,minZ) |
222 | cutTooLow = cutTooLow or y < 0.01 |
223 | local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,minY,maxZ) |
224 | cutTooLow = cutTooLow or y < 0.01 |
225 | local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,maxY,minZ) |
226 | cutTooLow = cutTooLow or y < 0.01 |
227 | local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,maxY,maxZ) |
228 | cutTooLow = cutTooLow or y < 0.01 |
229 | if cutTooLow then |
230 | self.treeSaw.curSplitShape = nil |
231 | end |
232 | end |
233 | end |
234 | |
235 | if self.treeSaw.curSplitShape ~= nil then |
236 | local lenBelow, lenAbove = getSplitShapePlaneExtents(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz) |
237 | if lenAbove < self.treeSaw.lengthAboveThreshold or lenBelow < self.treeSaw.lengthBelowThreshold then |
238 | self.treeSaw.curSplitShape = nil |
239 | end |
240 | end |
241 | |
242 | if self.treeSaw.curSplitShape == nil and self.treeSaw.cutTimer > -1 then |
243 | self.treeSaw.cutTimer = -1 |
244 | end |
245 | end |
246 | end |
247 | end |
248 | end |