mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-15 21:51:55 +00:00
Nice and reinvented
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user