Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
2 | |
3 | MountableObject = {}; |
4 | MountableObject_mt = Class(MountableObject, PhysicsObject); |
5 | |
6 | InitStaticObjectClass(MountableObject, "MountableObject", ObjectIds.OBJECT_MOUNTABLE_OBJECT); |
7 | |
8 | function MountableObject:new(isServer, isClient, customMt) |
9 | local mt = customMt; |
10 | if mt == nil then |
11 | mt = MountableObject_mt; |
12 | end; |
13 | |
14 | local self = PhysicsObject:new(isServer, isClient, mt); |
15 | self.dynamicMountSingleAxisFreeX = false; |
16 | self.dynamicMountSingleAxisFreeY = false; |
17 | |
18 | self.lastMoveTime = -100000; |
19 | return self; |
20 | end; |
21 | |
22 | function MountableObject:delete() |
23 | if self.dynamicMountTriggerId ~= nil then |
24 | removeTrigger(self.dynamicMountTriggerId); |
25 | end |
26 | if self.dynamicMountJointIndex ~= nil then |
27 | removeJointBreakReport(self.dynamicMountJointIndex); |
28 | removeJoint(self.dynamicMountJointIndex); |
29 | end |
30 | if self.dynamicMountObject ~= nil then |
31 | self.dynamicMountObject:removeDynamicMountedObject(self, true); |
32 | end |
33 | if self.mountObject ~= nil and self.mountObject.removeMountedObject ~= nil then |
34 | self.mountObject:removeMountedObject(self, true); |
35 | end |
36 | MountableObject:superClass().delete(self); |
37 | end; |
38 | |
39 | function MountableObject:getAllowsAutoDelete() |
40 | return self.mountObject == nil and MountableObject:superClass().getAllowsAutoDelete(self); |
41 | end |
42 | |
43 | --[[function MountableObject:readStream(streamId, connection) |
44 | MountableObject:superClass().readStream(self, streamId, connection); |
45 | end;]] |
46 | |
47 | --[[function MountableObject:writeStream(streamId, connection) |
48 | MountableObject:superClass().writeStream(self, streamId, connection); |
49 | end;]] |
50 | |
51 | function MountableObject:testScope(x,y,z, coeff) |
52 | if self.mountObject ~= nil then |
53 | return self.mountObject:testScope(x,y,z, coeff); |
54 | else |
55 | return MountableObject:superClass().testScope(self, x,y,z, coeff); |
56 | end; |
57 | end; |
58 | |
59 | --[[function MountableObject:update(dt) |
60 | if self.isServer then |
61 | if self.dynamicMountObjectActorId ~= nil then |
62 | local vx,vy,vz = |
63 | end |
64 | MountableObject:superClass().update(self, dt); |
65 | end]] |
66 | |
67 | function MountableObject:updateTick(dt) |
68 | if self.isServer then |
69 | if self:updateMove() then |
70 | self.lastMoveTime = g_currentMission.time; |
71 | end; |
72 | end; |
73 | end; |
74 | |
75 | function MountableObject:mount(object, node, x,y,z, rx,ry,rz) |
76 | -- set isDelete = true to remove mountableObject from pendingDynamicMountObjects (no delete leave callback) |
77 | self:unmountDynamic(true); |
78 | |
79 | if self.mountObject == nil then |
80 | removeFromPhysics(self.nodeId); |
81 | end; |
82 | link(node, self.nodeId); |
83 | local rx,ry,rz,rw = mathEulerToQuaternion(rx,ry,rz); |
84 | self:setPose(x,y,z, rx,ry,rz,rw); |
85 | self.mountObject = object; |
86 | end; |
87 | |
88 | function MountableObject:unmount() |
89 | if self.mountObject ~= nil then |
90 | self.mountObject = nil; |
91 | local x,y,z = getWorldTranslation(self.nodeId); |
92 | local rx,ry,rz,rw = getWorldQuaternion(self.nodeId); |
93 | |
94 | link(getRootNode(), self.nodeId); |
95 | self:setPose(x,y,z, rx,ry,rz,rw); |
96 | addToPhysics(self.nodeId); |
97 | return true; |
98 | end |
99 | return false; |
100 | end; |
101 | |
102 | function MountableObject:mountDynamic(object, objectActorId, jointNode, mountType, forceAcceleration) |
103 | if not self:getSupportsMountDynamic() or self.mountObject ~= nil then |
104 | return false; |
105 | end |
106 | return DynamicMountUtil.mountDynamic(self, self.nodeId, object, objectActorId, jointNode, mountType, forceAcceleration*self.dynamicMountForceLimitScale); |
107 | end |
108 | |
109 | function MountableObject:unmountDynamic(isDelete) |
110 | DynamicMountUtil.unmountDynamic(self, isDelete); |
111 | end |
112 | |
113 | function MountableObject:getSupportsMountDynamic() |
114 | return self.dynamicMountForceLimitScale ~= nil; |
115 | end |
116 | |
117 | function MountableObject:setNodeId(nodeId) |
118 | MountableObject:superClass().setNodeId(self, nodeId); |
119 | |
120 | if self.isServer then |
121 | self.dynamicMountTriggerId = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "dynamicMountTriggerIndex")); |
122 | if self.dynamicMountTriggerId ~= nil then |
123 | addTrigger(self.dynamicMountTriggerId, "dynamicMountTriggerCallback", self); |
124 | end |
125 | self.dynamicMountTriggerForceAcceleration = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "dynamicMountTriggerForceAcceleration")), 4); |
126 | self.dynamicMountForceLimitScale = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "dynamicMountForceLimitScale")), 1); |
127 | self.dynamicMountSingleAxisFreeY = getUserAttribute(nodeId, "dynamicMountSingleAxisFreeY") == true; |
128 | self.dynamicMountSingleAxisFreeX = getUserAttribute(nodeId, "dynamicMountSingleAxisFreeX") == true; |
129 | end |
130 | end; |
131 | |
132 | function MountableObject:dynamicMountTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId) |
133 | if onEnter then |
134 | if self.mountObject == nil then |
135 | local vehicle = g_currentMission.nodeToVehicle[otherActorId]; |
136 | if vehicle ~= nil and vehicle.dynamicMountAttacherNode ~= nil then |
137 | if self.dynamicMountObjectActorId == nil then |
138 | self:mountDynamic(vehicle, otherActorId, vehicle.dynamicMountAttacherNode, DynamicMountUtil.TYPE_FORK, self.dynamicMountTriggerForceAcceleration*vehicle.dynamicMountAttacherForceLimitScale); |
139 | self.dynamicMountObjectTriggerCount = 1; |
140 | elseif otherActorId ~= self.dynamicMountObjectActorId and self.dynamicMountObjectTriggerCount == nil then |
141 | -- we are already attached to another actor, but not from our mount trigger (e.g. a bale trailer) |
142 | self:unmountDynamic(); |
143 | self:mountDynamic(vehicle, otherActorId, vehicle.dynamicMountAttacherNode, DynamicMountUtil.TYPE_FORK, self.dynamicMountTriggerForceAcceleration*vehicle.dynamicMountAttacherForceLimitScale); |
144 | self.dynamicMountObjectTriggerCount = 1; |
145 | elseif otherActorId == self.dynamicMountObjectActorId then |
146 | if self.dynamicMountObjectTriggerCount ~= nil then |
147 | self.dynamicMountObjectTriggerCount = self.dynamicMountObjectTriggerCount+1; |
148 | end |
149 | end |
150 | end |
151 | end |
152 | elseif onLeave then |
153 | if otherActorId == self.dynamicMountObjectActorId and self.dynamicMountObjectTriggerCount ~= nil then |
154 | self.dynamicMountObjectTriggerCount = self.dynamicMountObjectTriggerCount-1; |
155 | if self.dynamicMountObjectTriggerCount == 0 then |
156 | self:unmountDynamic(); |
157 | self.dynamicMountObjectTriggerCount = nil; |
158 | end |
159 | end |
160 | end |
161 | end; |
162 | |
163 | function MountableObject:onDynamicMountJointBreak(jointIndex, breakingImpulse) |
164 | if jointIndex == self.dynamicMountJointIndex then |
165 | self:unmountDynamic(); |
166 | end |
167 | -- Do not delete the joint internally, we already deleted it with unmountDynamic |
168 | return false; |
169 | end
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de