-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVehicleAttachEvent.lua
More file actions
78 lines (60 loc) · 2.54 KB
/
VehicleAttachEvent.lua
File metadata and controls
78 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---Event for attaching
local VehicleAttachEvent_mt = Class(VehicleAttachEvent, Event)
---Create instance of Event class
-- @return table self instance of class event
function VehicleAttachEvent.emptyNew()
local self = Event.new(VehicleAttachEvent_mt)
return self
end
---Create new instance of event
-- @param table vehicle vehicle
-- @param table implement implement
-- @param integer inputJointIndex index of input attacher joint
-- @param integer jointIndex index of attacher joint
-- @param boolean startLowered start in lowered state
-- @return table instance instance of event
function VehicleAttachEvent.new(vehicle, implement, inputJointIndex, jointIndex, startLowered)
local self = VehicleAttachEvent.emptyNew()
self.jointIndex = jointIndex
self.inputJointIndex = inputJointIndex
self.vehicle = vehicle
self.implement = implement
self.startLowered = startLowered
assert(self.jointIndex >= 0 and self.jointIndex < 127)
return self
end
---Called on client side on join
-- @param integer streamId streamId
-- @param integer connection connection
function VehicleAttachEvent:readStream(streamId, connection)
self.vehicle = NetworkUtil.readNodeObject(streamId)
self.implement = NetworkUtil.readNodeObject(streamId)
self.jointIndex = streamReadUIntN(streamId, 7)
self.inputJointIndex = streamReadUIntN(streamId, 7)
self.startLowered = streamReadBool(streamId)
self:run(connection)
end
---Called on server side on join
-- @param integer streamId streamId
-- @param integer connection connection
function VehicleAttachEvent:writeStream(streamId, connection)
NetworkUtil.writeNodeObject(streamId, self.vehicle)
NetworkUtil.writeNodeObject(streamId, self.implement)
streamWriteUIntN(streamId, self.jointIndex, 7)
streamWriteUIntN(streamId, self.inputJointIndex, 7)
streamWriteBool(streamId, self.startLowered)
end
---Run action on receiving side
-- @param integer connection connection
function VehicleAttachEvent:run(connection)
if self.vehicle ~= nil and self.vehicle:getIsSynchronized() then
if self.implement == nil then
Logging.error("Failed to attach unknown implement to vehicle '%s' between joints '%d' and '%d'", self.vehicle.configFileName, self.jointIndex, self.inputJointIndex)
return
end
self.vehicle:attachImplement(self.implement, self.inputJointIndex, self.jointIndex, true, nil, self.startLowered)
end
if not connection:getIsServer() then
g_server:broadcastEvent(self, nil, connection, self.object)
end
end