LUADOC - Farming Simulator 19

DogBall

Description
Class for DogBalls
Parent
PhysicsObject
Functions

createNode

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

delete

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

getTerrainHeightWithProps

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

groundRaycastCallback

Description
Definition
groundRaycastCallback()
Code
95function DogBall:groundRaycastCallback(hitObjectId, x, y, z, distance)
96 if hitObjectId ~= nil then
97 local objectType = getRigidBodyType(hitObjectId)
98 if objectType ~= "Dynamic" and objectType ~= "Kinematic" then
99 self.groundY = y
100 return false
101 end
102 end
103 return true
104end

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
146function DogBall:load(i3dFilename, x,y,z, rx,ry,rz)
147 self:createNode(i3dFilename)
148 setTranslation(self.nodeId, x, y, z)
149 setRotation(self.nodeId, rx, ry, rz)
150
151 if self.isServer then
152 self.spawnPos = {x,y,z}
153 self.throwPos = {x,y,z}
154 self.startRot = {rx,ry,rz}
155 end
156 return true
157end

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
23 return self
24end

readStream

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

reset

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

updateTick

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

writeStream

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