forked from stijnwop/shuttledrivedirection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuttleDriveDirection.lua
More file actions
275 lines (221 loc) · 11.6 KB
/
ShuttleDriveDirection.lua
File metadata and controls
275 lines (221 loc) · 11.6 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
---
-- ShuttleDriveDirection
--
-- Toggles the drive direction.
--
-- Copyright (c) Wopster, 2019
ShuttleDriveDirection = {}
ShuttleDriveDirection.MOD_DIR = g_shuttleDriveDirectionModDirectory
ShuttleDriveDirection.MOD_NAME = g_shuttleDriveDirectionModName
ShuttleDriveDirection.DIR_FORWARDS = 1
ShuttleDriveDirection.DIR_NEUTRAL = 0
ShuttleDriveDirection.DIR_BACKWARDS = -1
ShuttleDriveDirection.BLINK_INTERVAL = 500
ShuttleDriveDirection.COLOR_NEUTRAL = { 0.0781, 0.2233, 0.0478, 0.75 }
ShuttleDriveDirection.COLOR_ACTIVE = { 0.0953, 1, 0.0685, 0.75 }
function ShuttleDriveDirection.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Drivable, specializations)
end
function ShuttleDriveDirection.registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, "getShuttleDriveDirection", ShuttleDriveDirection.getShuttleDriveDirection)
SpecializationUtil.registerFunction(vehicleType, "getShuttleDriveActive", ShuttleDriveDirection.getShuttleDriveActive)
SpecializationUtil.registerFunction(vehicleType, "setOnNeutral", ShuttleDriveDirection.setOnNeutral)
SpecializationUtil.registerFunction(vehicleType, "isOnNeutral", ShuttleDriveDirection.isOnNeutral)
SpecializationUtil.registerFunction(vehicleType, "toggleShuttleDriveDirection", ShuttleDriveDirection.toggleShuttleDriveDirection)
SpecializationUtil.registerFunction(vehicleType, "setShuttleDriveDirection", ShuttleDriveDirection.setShuttleDriveDirection)
SpecializationUtil.registerFunction(vehicleType, "setIsHoldingBrake", ShuttleDriveDirection.setIsHoldingBrake)
SpecializationUtil.registerFunction(vehicleType, "isHoldingBrake", ShuttleDriveDirection.isHoldingBrake)
end
function ShuttleDriveDirection.registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onLoad", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onDelete", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onUpdate", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onDraw", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onLeaveVehicle", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onEnterVehicle", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ShuttleDriveDirection)
SpecializationUtil.registerEventListener(vehicleType, "onStopMotor", ShuttleDriveDirection)
end
function ShuttleDriveDirection:onLoad(savegame)
self.spec_shuttleDriveDirection = self[("spec_%s.shuttleDriveDirection"):format(ShuttleDriveDirection.MOD_NAME)]
local spec = self.spec_shuttleDriveDirection
spec.shuttleDirection = 0
spec.shuttleDirectionSent = 0
spec.isHoldingBrake = false
spec.isHoldingBrakeSent = false
spec.blinkTime = 0
spec.doBlink = false
spec.active = false
local speedMeter = g_currentMission.inGameMenu.hud.speedMeter
local baseX, baseY = speedMeter.gaugeBackgroundElement:getPosition()
baseX = baseX + speedMeter.gaugeBackgroundElement:getWidth() * 0.5
baseY = baseY + speedMeter.speedTextOffsetY
local imagePath = Utils.getFilename("direction_arrow.png", ShuttleDriveDirection.MOD_DIR)
spec.overlayForwards = ShuttleDriveDirection.createIcon(imagePath, speedMeter, baseX, baseY, { 10, -10.5 }, { 25, 25 }, false)
spec.overlayBackwards = ShuttleDriveDirection.createIcon(imagePath, speedMeter, baseX, baseY, { 10, 14.5 }, { 25, 25 }, true)
spec.dirtyFlag = self:getNextDirtyFlag()
end
function ShuttleDriveDirection.createIcon(imagePath, parent, baseX, baseY, position, size, isRotated)
local posX, posY = parent:scalePixelToScreenVector(position)
local width, height = parent:scalePixelToScreenVector(size)
baseX = baseX - width * 0.5 -- center
local iconOverlay = Overlay:new(imagePath, baseX - width - posX, baseY - posY, width, height)
local element = HUDElement:new(iconOverlay)
if isRotated then
iconOverlay:setRotation(math.rad(180), width * 0.5, height * 0.5)
end
parent:addChild(element)
element:setVisible(false)
return element
end
function ShuttleDriveDirection:onDelete()
local spec = self.spec_shuttleDriveDirection
spec.overlayForwards:delete()
spec.overlayBackwards:delete()
end
function ShuttleDriveDirection:onReadUpdateStream(streamId, timestamp, connection)
local spec = self.spec_shuttleDriveDirection
if streamReadBool(streamId) then
spec.shuttleDirection = streamReadUIntN(streamId, 10) / 1023 * 2 - 1
if math.abs(spec.shuttleDirection) < 0.00099 then
spec.shuttleDirection = 0 -- set to 0 to avoid noise caused by compression
end
spec.isHoldingBrake = streamReadBool(streamId)
end
end
function ShuttleDriveDirection:onWriteUpdateStream(streamId, connection, dirtyMask)
local spec = self.spec_shuttleDriveDirection
if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
local shuttleDirection = (spec.shuttleDirection + 1) / 2 * 1023
streamWriteUIntN(streamId, shuttleDirection, 10)
streamWriteBool(streamId, spec.isHoldingBrake)
end
end
function ShuttleDriveDirection:onUpdate(dt)
if self.isClient then
local spec = self.spec_shuttleDriveDirection
if self:getIsActive() and self.getIsEntered ~= nil and self:getIsEntered() then
if self:isOnNeutral() then
spec.blinkTime = spec.blinkTime + dt
if spec.blinkTime > ShuttleDriveDirection.BLINK_INTERVAL then
spec.doBlink = not spec.doBlink
spec.blinkTime = 0
end
end
end
if spec.active and self.setBrakeLightsVisibility ~= nil then
self:setBrakeLightsVisibility(spec.isHoldingBrake)
end
if g_ShuttleSettings.shuttleSettings.active.active ~= spec.active then
spec.active = g_ShuttleSettings.shuttleSettings.active.active;
if ShuttleDriveDirection.canRenderOnCurrentVehicle(self) then
spec.overlayForwards:setVisible(spec.active)
spec.overlayBackwards:setVisible(spec.active)
end
end
end
end
function ShuttleDriveDirection:onDraw()
if self.isClient and ShuttleDriveDirection.canRenderOnCurrentVehicle(self) then
if self:getIsActive() and self.getIsEntered ~= nil and self:getIsEntered() then
local spec = self.spec_shuttleDriveDirection
local forwardsColor = ShuttleDriveDirection.COLOR_NEUTRAL
local backwardsColor = ShuttleDriveDirection.COLOR_NEUTRAL
if spec.shuttleDirection == ShuttleDriveDirection.DIR_FORWARDS then
forwardsColor = ShuttleDriveDirection.COLOR_ACTIVE
elseif spec.shuttleDirection == ShuttleDriveDirection.DIR_BACKWARDS then
backwardsColor = ShuttleDriveDirection.COLOR_ACTIVE
elseif spec.shuttleDirection == ShuttleDriveDirection.DIR_NEUTRAL and spec.doBlink then
forwardsColor = ShuttleDriveDirection.COLOR_ACTIVE
backwardsColor = ShuttleDriveDirection.COLOR_ACTIVE
end
spec.overlayForwards:setColor(unpack(forwardsColor))
spec.overlayBackwards:setColor(unpack(backwardsColor))
end
end
end
function ShuttleDriveDirection:onStopMotor()
self:setOnNeutral()
end
function ShuttleDriveDirection:onEnterVehicle()
local spec = self.spec_shuttleDriveDirection
spec.active = g_ShuttleSettings.shuttleSettings.active.active;
if g_ShuttleSettings.shuttleSettings.active.active and ShuttleDriveDirection.canRenderOnCurrentVehicle(self) then
spec.overlayForwards:setVisible(true)
spec.overlayBackwards:setVisible(true)
end
end
function ShuttleDriveDirection:onLeaveVehicle()
local spec = self.spec_shuttleDriveDirection
if ShuttleDriveDirection.canRenderOnCurrentVehicle(self) then
spec.overlayForwards:setVisible(false)
spec.overlayBackwards:setVisible(false)
end
self:setOnNeutral()
end
function ShuttleDriveDirection.canRenderOnCurrentVehicle(vehicle)
return g_currentMission.controlledVehicle == vehicle
end
function ShuttleDriveDirection:getShuttleDriveDirection()
return self.spec_shuttleDriveDirection.shuttleDirection
end
function ShuttleDriveDirection:getShuttleDriveActive()
return self.spec_shuttleDriveDirection.active
end
function ShuttleDriveDirection:setOnNeutral()
self:setShuttleDriveDirection(ShuttleDriveDirection.DIR_NEUTRAL)
end
function ShuttleDriveDirection:isOnNeutral()
return self.spec_shuttleDriveDirection.shuttleDirection == ShuttleDriveDirection.DIR_NEUTRAL
end
function ShuttleDriveDirection:toggleShuttleDriveDirection()
local direction = self:getShuttleDriveDirection()
if self:isOnNeutral() then
direction = ShuttleDriveDirection.DIR_BACKWARDS
end
self:setShuttleDriveDirection(-direction)
end
function ShuttleDriveDirection:toggleShuttleDriveDirectionForward()
self:setShuttleDriveDirection(ShuttleDriveDirection.DIR_FORWARDS)
end
function ShuttleDriveDirection:toggleShuttleDriveDirectionBackward()
self:setShuttleDriveDirection(ShuttleDriveDirection.DIR_BACKWARDS)
end
function ShuttleDriveDirection:setShuttleDriveDirection(direction)
local spec = self.spec_shuttleDriveDirection
spec.shuttleDirection = direction
if spec.shuttleDirection ~= spec.shuttleDirectionSent then
spec.shuttleDirectionSent = spec.shuttleDirection
self:raiseDirtyFlags(spec.dirtyFlag)
end
end
function ShuttleDriveDirection:setIsHoldingBrake(isHoldingBrake)
local spec = self.spec_shuttleDriveDirection
spec.isHoldingBrake = isHoldingBrake
if spec.isHoldingBrake ~= spec.isHoldingBrakeSent then
spec.isHoldingBrakeSent = spec.isHoldingBrake
self:raiseDirtyFlags(spec.dirtyFlag)
end
end
function ShuttleDriveDirection:isHoldingBrake()
return self.spec_shuttleDriveDirection.isHoldingBrake
end
function ShuttleDriveDirection:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
if self.isClient then
local spec = self.spec_shuttleDriveDirection
self:clearActionEventsTable(spec.actionEvents)
if isActiveForInputIgnoreSelection then
local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.TOGGLE_DRIVE_DIRECTION, self, ShuttleDriveDirection.toggleShuttleDriveDirection, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_VERY_LOW)
g_inputBinding:setActionEventText(actionEventId, g_i18n:getText("action_toggle_drive_direction"))
local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.TOGGLE_DRIVE_FORWARD, self, ShuttleDriveDirection.toggleShuttleDriveDirectionForward, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_VERY_LOW)
g_inputBinding:setActionEventText(actionEventId, g_i18n:getText("action_toggle_drive_direction_forward"))
local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.TOGGLE_DRIVE_BACKWARD, self, ShuttleDriveDirection.toggleShuttleDriveDirectionBackward, false, true, false, true, nil)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_VERY_LOW)
g_inputBinding:setActionEventText(actionEventId, g_i18n:getText("action_toggle_drive_direction_backward"))
end
end
end