-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParkVehicleSystem.lua
95 lines (77 loc) · 2.94 KB
/
ParkVehicleSystem.lua
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
--- Global instance of park vehicle.
---@class ParkVehicleSystem
---@field modName string
---@field modDir string
---@field inputManager InputBinding
---@field debug boolean
---@field instances table<integer, ParkVehicle>
---@field counter integer Increased if an instance is registered, used as key within the table
ParkVehicleSystem = {}
local ParkVehicleSystem_mt = Class(ParkVehicleSystem)
---
---@param modName string
---@param modDir string
---@param inputManager InputBinding
---@param debug boolean
---@return ParkVehicleSystem
function ParkVehicleSystem:new(modName, modDir, inputManager, debug)
local self = {}
setmetatable(self, ParkVehicleSystem_mt)
self.modName = modName
self.modDir = modDir
self.debug = debug
self.inputManager = inputManager
self.instances = {}
self.counter = 0
return self
end
---@param typeManager TypeManager
---@param specManager SpecializationManager
function ParkVehicleSystem:installSpecialization(typeManager, specManager)
-- register spec
specManager:addSpecialization("parkVehicle", "ParkVehicle", Utils.getFilename("ParkVehicle.lua", self.modDir), nil)
-- add spec to vehicle types
local totalCount = 0
local modified = 0
for typeName, typeEntry in pairs(typeManager:getTypes()) do
totalCount = totalCount + 1
if SpecializationUtil.hasSpecialization(Enterable, typeEntry.specializations) and
not SpecializationUtil.hasSpecialization(Rideable, typeEntry.specializations) and
not SpecializationUtil.hasSpecialization(ParkVehicle, typeEntry.specializations) then
typeManager:addSpecialization(typeName, self.modName .. ".parkVehicle")
modified = modified + 1
if (self.debug) then
print("Adding park vehicle to " .. typeName)
end
else
if (self.debug) then
print("Not adding park vehicle to " .. typeName)
end
end
end
print(string.format("Inserted Park Vehicle into %i of %i vehicle types", modified, totalCount))
end
function ParkVehicleSystem:registerActionEvents()
local _, eventId = self.inputManager:registerActionEvent(InputAction.PARKVEHICLE_UNPARK_ALL, self, self.unparkAll, false, true, false, true)
self.inputManager:setActionEventTextPriority(eventId, GS_PRIO_VERY_LOW)
end
function ParkVehicleSystem:unregisterActionEvents()
self.inputManager:removeActionEventsByTarget(self)
end
---@param instance ParkVehicle
---@return integer the key to unregister this instance
function ParkVehicleSystem:registerInstance(instance)
local key = self.counter
self.instances[key] = instance
self.counter = self.counter + 1
return key
end
---@param key integer
function ParkVehicleSystem:unregisterInstance(key)
self.instances[key] = nil
end
function ParkVehicleSystem:unparkAll()
for _, value in pairs(self.instances) do
value:setParkVehicleState(false)
end
end