Nice and reinvented

This commit is contained in:
2024-07-20 21:07:35 -04:00
parent 083f002ace
commit 1c026f4a52
4 changed files with 348 additions and 95 deletions

View File

@@ -1,94 +0,0 @@
--!optimize 2
--!native
--!strict
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
--Class functions
Sort: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (),
Check: (self: ClassConstructor, ElevatorGoingUp: boolean) -> number?,
AddFloor: (self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number) -> (),
}
type Constructor_Fun = (BoxAlignPosition: AlignPosition, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes) -> ClassConstructor
type Constructor_Return_Props = {
BoxAlignPosition: AlignPosition,
ElevatorAttributes: ElevatorAttributes,
DoorAttributes: DoorAttributes,
__FloorQueue: FloorQueue,
Events: {
Sorted: BindableEvent
},
}
type ElevatorAttributes = {
CurrentFloor: IntValue,
GoingUp: BoolValue,
Moving: BoolValue,
Relay: {
Goal: IntValue,
GoalYLevel: NumberValue,
ReadyForMoving: BoolValue,
}
}
type DoorAttributes = {
Relay: {
Open: BoolValue
}
}
export type FloorQueue = {number?}
export type RelayAlgorithmConstructor = ClassConstructor
local RelayAlgorithm = {} :: Impl_Constructor
RelayAlgorithm.__index = RelayAlgorithm
function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes)
return setmetatable({
BoxAlignPosition = BoxAlignPosition,
ElevatorAttributes = ElevatorAttributes,
DoorAttributes = DoorAttributes,
__FloorQueue = {},
Events = {
Sorted = Instance.new("BindableEvent") :: BindableEvent
},
}, RelayAlgorithm)
end
--The Otis relay based call logic
--https://youtu.be/BCN9mQOT3RQ
function RelayAlgorithm:Sort(ElevatorGoingUp)
table.sort(self.__FloorQueue, function(a, b): boolean
if ElevatorGoingUp then
return a<b
end
return a>b
end)
self.Events.Sorted:Fire()
end
function RelayAlgorithm:Check(ElevatorGoingUp)
if self.__FloorQueue[1] == self.ElevatorAttributes.CurrentFloor.Value then
table.remove(self.__FloorQueue, 1)
end
if #self.__FloorQueue ~= 0 then
self:Sort(ElevatorGoingUp)
return self.__FloorQueue[1]
end
return nil
end
function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel)
table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel)
self:Sort(ElevatorGoingUp)
end
return RelayAlgorithm