LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

DogBall

Description
Class for DogBalls
Parent
PhysicsObject
Functions

createNode

Description
Load node from i3d file
Definition
createNode(string i3dFilename)
Arguments
stringi3dFilenamei3d file name
Code
69function DogBall:createNode(i3dFilename)
70 self.i3dFilename = i3dFilename
71 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename)
72 local dogBallRoot, sharedLoadRequestId = g_i3DManager:loadSharedI3DFile(i3dFilename, false, false)
73 self.sharedLoadRequestId = sharedLoadRequestId
74
75 local dogBallId = getChildAt(dogBallRoot, 0)
76 link(getRootNode(), dogBallId)
77 delete(dogBallRoot)
78
79 self:setNodeId(dogBallId)
80end

delete

Description
Deleting DogBall object
Definition
delete()
Code
29function DogBall:delete()
30 self.isDeleted = true -- mark as deleted so we can track it in Doghouse
31 if self.sharedLoadRequestId ~= nil then
32 g_i3DManager:releaseSharedI3DFile(self.sharedLoadRequestId)
33 end
34 unregisterObjectClassName(self)
35 DogBall:superClass().delete(self)
36end

getTerrainHeightWithProps

Description
Definition
getTerrainHeightWithProps()
Code
84function DogBall:getTerrainHeightWithProps(x, z)
85 local terrainY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z)
86 local offset = 1.0
87 local distance = 20.0
88 local collisionMask = 63
89
90 self.groundY = -1.0
91 raycastClosest(x, terrainY + offset, z, 0.0, -1.0, 0.0, "groundRaycastCallback", 5.0, self, collisionMask)
92 return math.max(terrainY, self.groundY)
93end

groundRaycastCallback

Description
Definition
groundRaycastCallback()
Code
97function DogBall:groundRaycastCallback(hitObjectId, x, y, z, distance)
98 if hitObjectId ~= nil then
99 local objectType = getRigidBodyType(hitObjectId)
100 if objectType ~= RigidBodyType.DYNAMIC and objectType ~= RigidBodyType.KINEMATIC then
101 self.groundY = y
102 return false
103 end
104 end
105 return true
106end

load

Description
Load DogBall
Definition
load(string i3dFilename, float x, float y, float z, float rx, float ry, float rz)
Arguments
stringi3dFilenamei3d file name
floatxx world position
floatyz world position
floatzz world position
floatrxrx world rotation
floatryry world rotation
floatrzrz world rotation
Code
148function DogBall:load(i3dFilename, x,y,z, rx,ry,rz)
149 self:createNode(i3dFilename)
150 setTranslation(self.nodeId, x, y, z)
151 setRotation(self.nodeId, rx, ry, rz)
152
153 if self.isServer then
154 self.spawnPos = {x,y,z}
155 self.throwPos = {x,y,z}
156 self.startRot = {rx,ry,rz}
157 end
158 return true
159end

new

Description
Creating DogBall object
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustomMt
Return Values
tableinstanceInstance of object
Code
17function DogBall.new(isServer, isClient, customMt)
18 local self = PhysicsObject.new(isServer, isClient, customMt or DogBall_mt)
19
20 self.forcedClipDistance = 150
21 registerObjectClassName(self, "DogBall")
22 self.sharedLoadRequestId = nil
23
24 return self
25end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
42function DogBall:readStream(streamId, connection)
43 if connection:getIsServer() then
44 local i3dFilename = NetworkUtil.convertFromNetworkFilename(streamReadString(streamId))
45
46 local isNew = self.i3dFilename == nil
47 if isNew then
48 self:load(i3dFilename, 0,0,0, 0,0,0)
49 -- The pose will be set by PhysicsObject, and we don't care about spawnPos/startRot on clients
50 end
51 end
52 DogBall:superClass().readStream(self, streamId, connection)
53end

reset

Description
Definition
reset()
Code
163function DogBall:reset()
164 if self.isServer then
165 removeFromPhysics(self.nodeId)
166 setTranslation(self.nodeId, unpack(self.spawnPos))
167 setRotation(self.nodeId, unpack(self.startRot))
168 addToPhysics(self.nodeId)
169 end
170end

updateTick

Description
Load node from i3d file
Definition
updateTick(string i3dFilename)
Arguments
stringi3dFilenamei3d file name
Code
112function DogBall:updateTick(dt)
113 if self.isServer then
114
115 -- Reset when fallen through the terrain
116 local x,y,z = getWorldTranslation(self.nodeId)
117 if self:getTerrainHeightWithProps(x, z) > (y + 1.0) then
118 self:reset()
119 end
120
121 local parentNode = getParent(self.nodeId)
122 if parentNode ~= 0 and (parentNode == getRootNode() or parentNode == g_currentMission.terrainRootNode) then
123 local distSq = MathUtil.vector3LengthSq(x - self.spawnPos[1], y - self.spawnPos[2], z - self.spawnPos[3])
124 -- Reset when too far away from spawn pos
125 if distSq > (DogBall.RESET_DISTANCE * DogBall.RESET_DISTANCE) then
126 self:reset()
127 end
128 else
129 local distSq = MathUtil.vector3LengthSq(x - self.throwPos[1], y - self.throwPos[2], z - self.throwPos[3])
130 -- Reset when too far away from thrown pos
131 if distSq > (DogBall.RESET_DISTANCE * DogBall.RESET_DISTANCE) then
132 self:reset()
133 end
134 end
135 end
136 DogBall:superClass().updateTick(self, dt)
137end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
59function DogBall:writeStream(streamId, connection)
60 if not connection:getIsServer() then
61 streamWriteString(streamId, NetworkUtil.convertToNetworkFilename(self.i3dFilename))
62 end
63 DogBall:superClass().writeStream(self, streamId, connection)
64end