mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-15 21:41:53 +00:00
Relay logic
This commit is contained in:
144
src/server/main/Elevators/Otis1960/Relay.lua
Normal file
144
src/server/main/Elevators/Otis1960/Relay.lua
Normal file
@@ -0,0 +1,144 @@
|
||||
--!optimize 2
|
||||
--!native
|
||||
--!strict
|
||||
|
||||
local RelayAlgorithmModule = require(script.Parent:WaitForChild("RelayAlgorithm"))
|
||||
|
||||
type Leveling = {
|
||||
Leveling: {number},
|
||||
LevelingBetween: {number}
|
||||
}
|
||||
type ElevatorBox = BasePart
|
||||
type BoxAlignPosition = AlignPosition
|
||||
|
||||
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
|
||||
type Impl_Constructor = {
|
||||
__index: Impl_Constructor,
|
||||
constructor: Constructor_Fun,
|
||||
--Class functions
|
||||
BulkConnect: (self: ClassConstructor) -> (),
|
||||
BulkDisconnect: (self: ClassConstructor) -> (),
|
||||
__Ready: (self: ClassConstructor) -> (),
|
||||
__Open: (self: ClassConstructor) -> (),
|
||||
__Goal: (self: ClassConstructor) -> (),
|
||||
__GoalYLevel: (self: ClassConstructor) -> (),
|
||||
}
|
||||
|
||||
type Constructor_Fun = (RelayAlgorthm: RelayAlgorithmModule.RelayAlgorithmConstructor, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes, Leveling: Leveling, BoxAlignPosition: BoxAlignPosition, ElevatorBox: ElevatorBox) -> ClassConstructor
|
||||
|
||||
type Constructor_Return_Props = {
|
||||
RelayAlgorthm: RelayAlgorithmModule.RelayAlgorithmConstructor,
|
||||
ElevatorAttributes: ElevatorAttributes,
|
||||
DoorAttributes: DoorAttributes,
|
||||
LevelingModule: Leveling,
|
||||
BoxAlignPosition: BoxAlignPosition,
|
||||
ElevatorBox: ElevatorBox,
|
||||
|
||||
__ReadyConnection: RBXScriptConnection?,
|
||||
__OpenConnection: RBXScriptConnection?,
|
||||
__GoalYLevelConnection: RBXScriptConnection?,
|
||||
__GoalConnection: RBXScriptConnection?,
|
||||
}
|
||||
|
||||
type ElevatorAttributes = {
|
||||
Relay: {
|
||||
Ready: BoolValue,
|
||||
GoalYLevel: NumberValue,
|
||||
Goal: IntValue
|
||||
},
|
||||
|
||||
Stopped: BoolValue
|
||||
}
|
||||
|
||||
type DoorAttributes = {
|
||||
Relay: {
|
||||
Open: BoolValue
|
||||
}
|
||||
}
|
||||
|
||||
export type RelayConstructor = ClassConstructor
|
||||
|
||||
local Relay = {} :: Impl_Constructor
|
||||
Relay.__index = Relay
|
||||
|
||||
function Relay.constructor(RelayAlgorthm, ElevatorAttributes, DoorAttributes, LevelingModule, BoxAlignPosition, ElevatorBox)
|
||||
return setmetatable({
|
||||
RelayAlgorthm = RelayAlgorthm,
|
||||
ElevatorAttributes = ElevatorAttributes,
|
||||
DoorAttributes = DoorAttributes,
|
||||
LevelingModule = LevelingModule,
|
||||
BoxAlignPosition = BoxAlignPosition,
|
||||
ElevatorBox = ElevatorBox
|
||||
}, Relay)
|
||||
end
|
||||
|
||||
function Relay:__Ready()
|
||||
if self.ElevatorAttributes.Relay.Ready.Value then
|
||||
self.BoxAlignPosition.Position = Vector3.new(
|
||||
self.ElevatorBox.Position.X,
|
||||
self.LevelingModule.Leveling[self.RelayAlgorthm.__FloorQueue[1]],
|
||||
self.ElevatorBox.Position.Z
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function Relay:__Open()
|
||||
self.ElevatorAttributes.Relay.Ready.Value = self.DoorAttributes.Relay.Open.Value
|
||||
end
|
||||
|
||||
function Relay:__GoalYLevel()
|
||||
self.BoxAlignPosition.Position = Vector3.new(
|
||||
self.ElevatorBox.Position.X,
|
||||
self.ElevatorAttributes.Relay.GoalYLevel.Value,
|
||||
self.ElevatorBox.Position.Z
|
||||
)
|
||||
end
|
||||
|
||||
function Relay:__Goal()
|
||||
local Level: number? = self.LevelingModule.Leveling[self.ElevatorAttributes.Relay.Goal.Value]
|
||||
|
||||
self.ElevatorAttributes.Relay.GoalYLevel.Value = Level or self.LevelingModule.Leveling[1] :: number
|
||||
end
|
||||
|
||||
function Relay:BulkConnect()
|
||||
self.__ReadyConnection = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Ready()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__OpenConnection = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Open()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__GoalYLevelConnection = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__GoalYLevel()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__GoalConnection = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Goal()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Relay:BulkDisconnect()
|
||||
if self.__ReadyConnection and self.__ReadyConnection.Connected then
|
||||
self.__ReadyConnection:Disconnect()
|
||||
end
|
||||
if self.__OpenConnection and self.__OpenConnection.Connected then
|
||||
self.__OpenConnection:Disconnect()
|
||||
end
|
||||
if self.__GoalYLevelConnection and self.__GoalYLevelConnection.Connected then
|
||||
self.__GoalYLevelConnection:Disconnect()
|
||||
end
|
||||
if self.__GoalConnection and self.__GoalConnection.Connected then
|
||||
self.__GoalConnection:Disconnect()
|
||||
end
|
||||
end
|
||||
|
||||
return Relay
|
||||
Reference in New Issue
Block a user