Relays are broken

This commit is contained in:
2024-05-27 02:36:28 -04:00
parent 54d6358751
commit 1d8f684393
4 changed files with 86 additions and 65 deletions

View File

@@ -74,12 +74,8 @@ function Relay.constructor(RelayAlgorthm, ElevatorAttributes, DoorAttributes, Le
end end
function Relay:__Ready() function Relay:__Ready()
if self.ElevatorAttributes.Relay.Ready.Value then if not self.ElevatorAttributes.Relay.Ready.Value then
self.BoxAlignPosition.Position = Vector3.new( self:__GoalYLevel()
self.ElevatorBox.Position.X,
self.ElevatorAttributes.Relay.GoalYLevel.Value,
self.ElevatorBox.Position.Z
)
end end
end end
@@ -88,12 +84,14 @@ function Relay:__Open()
end end
function Relay:__GoalYLevel() function Relay:__GoalYLevel()
if not self.ElevatorAttributes.Relay.Ready.Value then
self.BoxAlignPosition.Position = Vector3.new( self.BoxAlignPosition.Position = Vector3.new(
self.ElevatorBox.Position.X, self.ElevatorBox.Position.X,
self.ElevatorAttributes.Relay.GoalYLevel.Value, self.ElevatorAttributes.Relay.GoalYLevel.Value,
self.ElevatorBox.Position.Z self.ElevatorBox.Position.Z
) )
end end
end
function Relay:__Goal() function Relay:__Goal()
local Level: number? = self.LevelingModule.Leveling[self.ElevatorAttributes.Relay.Goal.Value] local Level: number? = self.LevelingModule.Leveling[self.ElevatorAttributes.Relay.Goal.Value]
@@ -103,24 +101,28 @@ end
function Relay:BulkConnect() function Relay:BulkConnect()
self.__Connections.Ready = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Ready = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
print("Relay=Ready","State=", self.ElevatorAttributes.Relay.Ready.Value)
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Ready() self:__Ready()
end end
end) end)
self.__Connections.Open = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Open = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
print("Relay=Open","State=", self.DoorAttributes.Relay.Open.Value)
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Open() self:__Open()
end end
end) end)
self.__Connections.GoalYLevel = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.GoalYLevel = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
print("Relay=GoalYLevel","State=", self.ElevatorAttributes.Relay.GoalYLevel.Value)
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__GoalYLevel() self:__GoalYLevel()
end end
end) end)
self.__Connections.Goal = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Goal = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
print("Relay=Goal","State=", self.ElevatorAttributes.Relay.Goal.Value)
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Goal() self:__Goal()
end end

View File

@@ -18,7 +18,11 @@ type Constructor_Return_Props = {
BoxAlignPosition: AlignPosition, BoxAlignPosition: AlignPosition,
ElevatorAttributes: ElevatorAttributes, ElevatorAttributes: ElevatorAttributes,
DoorAttributes: DoorAttributes, DoorAttributes: DoorAttributes,
__FloorQueue: {number} __FloorQueue: FloorQueue,
Events: {
Sorted: BindableEvent
}
} }
type ElevatorAttributes = { type ElevatorAttributes = {
@@ -33,6 +37,8 @@ type DoorAttributes = {
} }
} }
export type FloorQueue = {number?}
export type RelayAlgorithmConstructor = ClassConstructor export type RelayAlgorithmConstructor = ClassConstructor
local RelayAlgorithm = {} :: Impl_Constructor local RelayAlgorithm = {} :: Impl_Constructor
@@ -40,10 +46,13 @@ RelayAlgorithm.__index = RelayAlgorithm
function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes) function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes)
return setmetatable({ return setmetatable({
Events = {
Sorted = Instance.new("BindableEvent")
},
BoxAlignPosition = BoxAlignPosition, BoxAlignPosition = BoxAlignPosition,
ElevatorAttributes = ElevatorAttributes, ElevatorAttributes = ElevatorAttributes,
DoorAttributes = DoorAttributes, DoorAttributes = DoorAttributes,
__FloorQueue = {} __FloorQueue = {},
}, RelayAlgorithm) }, RelayAlgorithm)
end end
@@ -51,14 +60,14 @@ end
--https://youtu.be/BCN9mQOT3RQ --https://youtu.be/BCN9mQOT3RQ
function RelayAlgorithm:Sort(ElevatorGoingUp) function RelayAlgorithm:Sort(ElevatorGoingUp)
table.sort(self.__FloorQueue, function(a: number, b: number): boolean table.sort(self.__FloorQueue, function(a, b): boolean
if ElevatorGoingUp then if ElevatorGoingUp then
return a<b return a<b
else else
return a>b return a>b
end end
end) end)
print(table.unpack(self.__FloorQueue)) self.Events.Sorted:Fire(self.__FloorQueue)
end end
function RelayAlgorithm:Check(ElevatorGoingUp) function RelayAlgorithm:Check(ElevatorGoingUp)

View File

@@ -59,8 +59,8 @@ type Impl_Constructor = {
FloorPassingUp: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (), FloorPassingUp: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
FloorPassingDown: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (), FloorPassingDown: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
RequestLevel: (self: ClassConstructor, RequestedLevel: number) -> boolean, RequestLevel: (self: ClassConstructor, RequestedLevel: number) -> boolean,
__MoveTo: (self: ClassConstructor, GoingUp: boolean, GoalFloor_Y: number) -> (), __MoveTo: (self: ClassConstructor, GoingUp: boolean, RequestedLevel: number) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean, GoalFloor_Y: number) -> (), __MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean, RequestedLevel: number) -> (),
} & Impl_Static_Props } & Impl_Static_Props
type Impl_Static_Props = { type Impl_Static_Props = {
@@ -125,7 +125,11 @@ type Constructor_Return_Props = {
ButtonsConstructor: Buttons.ButtonsConstructor, ButtonsConstructor: Buttons.ButtonsConstructor,
RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor, RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor,
RelayConstructor: Relay.RelayConstructor, RelayConstructor: Relay.RelayConstructor,
__MovingConnection: RBXScriptConnection?,
__Connections: {
Moving: RBXScriptConnection?,
FloorSorted: RBXScriptConnection?
}
} }
local Elevator = {} :: Impl_Constructor local Elevator = {} :: Impl_Constructor
@@ -136,7 +140,7 @@ Elevator.FloorLevelingDistance = 2.5
Elevator.DoorOpeningDistance = Elevator.FloorLevelingDistance/2.8 Elevator.DoorOpeningDistance = Elevator.FloorLevelingDistance/2.8
Elevator.LeveledDistance = 0.5 Elevator.LeveledDistance = 0.5
Elevator.Responsiveness = 10 Elevator.Responsiveness = 10
Elevator.MaxVelocity = 10 Elevator.MaxVelocity = 8
Elevator.QueueWaitTime = 5 Elevator.QueueWaitTime = 5
Elevator.Sounds = { Elevator.Sounds = {
@@ -178,10 +182,10 @@ Elevator.Attributes.Relay.Ready.Value = false
local Attributes = Elevator.Attributes local Attributes = Elevator.Attributes
--My clever math function for determining if the elevator goal is to move upwards or not --Math function for determining if the elevator goal is to move upwards or not
local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean local function ElevatorGoingUpDirection(CurrentFloor: number, RequestedFloor: number): boolean
--(Floor-RequestedFloor)>0 --(CurrentFloor-RequestedFloor)>0
return -Floor+RequestedFloor>0 --Simplified equation for less computation return -CurrentFloor+RequestedFloor>0
end end
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe) local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
@@ -245,15 +249,15 @@ local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonT
if Toggled then if Toggled then
(ButtonTree.Inst :: BasePart).Position+=Vector3.new(0,0,.05) (ButtonTree.Inst :: BasePart).Position+=Vector3.new(0,0,.05)
if self.__MovingConnection and self.__MovingConnection.Connected then if self.__Connections.Moving and self.__Connections.Moving.Connected then
self.__MovingConnection:Disconnect() self.__Connections.Moving:Disconnect()
end end
self.BoxAlignPosition.MaxVelocity = 0 self.BoxAlignPosition.MaxVelocity = 0
else else
(ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05) (ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05)
self.LanternsConstructor:Reset() self.LanternsConstructor:Reset()
self:__MoveTo(ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Relay.Goal.Value), Attributes.Relay.GoalYLevel.Value) self:__MoveTo(ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Relay.Goal.Value))
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
end end
end) end)
@@ -295,6 +299,7 @@ end
function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors) function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
local self = {} :: Constructor_Return_Props local self = {} :: Constructor_Return_Props
self.__Connections = {}
self.MachineRoom = {_CFrame = {}} :: MovingObjects.MachineRoom self.MachineRoom = {_CFrame = {}} :: MovingObjects.MachineRoom
self.ElevatorBox_1960 = TagsConstructor:Request("ElevatorMover_1960") :: UnionOperation self.ElevatorBox_1960 = TagsConstructor:Request("ElevatorMover_1960") :: UnionOperation
@@ -314,13 +319,14 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
local LanternDisplay = TagsConstructor:Request("Otis1960_LanternDisplayMain") :: UnionOperation local LanternDisplay = TagsConstructor:Request("Otis1960_LanternDisplayMain") :: UnionOperation
self.MOConstructor = MovingObjects.constructor({ self.MOConstructor = MovingObjects.constructor({MachineRoom = self.MachineRoom} :: MovingObjects.InstanceTree)
MachineRoom = self.MachineRoom --Start the hall displays
} :: MovingObjects.InstanceTree)
self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays) self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays)
--Init the doors
self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor) self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor)
--Init the ropes
self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling) self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling)
--Init the lanterns
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors) self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors)
local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags) local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags)
@@ -351,6 +357,12 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.RelayConstructor:BulkConnect() self.RelayConstructor:BulkConnect()
self.__Connections.FloorSorted = self.RelayAlgorithmConstructor.Events.Sorted.Event:Connect(function(FloorQueue: RelayAlgorithm.FloorQueue)
if Attributes.Moving.Value and FloorQueue[1] then
Attributes.Relay.Goal.Value = FloorQueue[1] :: number
end
end)
local ClassConstructor = setmetatable(self, Elevator) local ClassConstructor = setmetatable(self, Elevator)
IterateButtons(ClassConstructor, ButtonsTagsConstructor) IterateButtons(ClassConstructor, ButtonsTagsConstructor)
@@ -361,10 +373,10 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
end end
function Elevator:Leveled(RequestedLevel) function Elevator:Leveled(RequestedLevel)
(self.__MovingConnection :: RBXScriptConnection):Disconnect() (self.__Connections.Moving :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel Attributes.CurrentFloor.Value = RequestedLevel
self.BoxAlignPosition.MaxVelocity = 0 --self.BoxAlignPosition.MaxVelocity = 0
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Elevator.QueueWaitTime) task.wait(Elevator.QueueWaitTime)
@@ -373,7 +385,10 @@ function Elevator:Leveled(RequestedLevel)
if self.RelayAlgorithmConstructor:Check(ElevatorGoingUp) then if self.RelayAlgorithmConstructor:Check(ElevatorGoingUp) then
--More floors in the queue --More floors in the queue
self:__MoveTo(ElevatorGoingUp, LevelingModule.Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]]) self:__MoveTo(ElevatorGoingUp, RequestedLevel)
else
--The elevator is at a full park now
Attributes.Relay.Ready.Value = false
end end
end end
@@ -404,25 +419,22 @@ function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
end end
end end
function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y) function Elevator:__MovingHeartbeat(GoingUp, RequestedLevel)
Attributes.GoingUp.Value = GoingUp Attributes.GoingUp.Value = GoingUp
Attributes.Relay.Goal.Value = RequestedLevel
Attributes.Moving.Value = true
if self.__MovingConnection and self.__MovingConnection.Connected then if self.__Connections.Moving and self.__Connections.Moving.Connected then
self.__MovingConnection:Disconnect() self.__Connections.Moving:Disconnect()
end end
self.MOConstructor:UpdateCFrame()
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
local Delta = 0 local Delta = 0
local DoorsOpeningDebounce = false local DoorsOpeningDebounce = false
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt) self.MOConstructor:UpdateCFrame()
Delta+=1
local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
Attributes.Moving.Value = true self.__Connections.Moving = RS.Heartbeat:Connect(function(_dt)
Attributes.Relay.Goal.Value = FloorGoal Delta+=1
local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position
local ElevatorPositionY: number = ElevatorPosition.Y local ElevatorPositionY: number = ElevatorPosition.Y
@@ -434,40 +446,40 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
--Kill the connection --Kill the connection
if Attributes.GoingUp.Value then if Attributes.GoingUp.Value then
self:FloorPassingUp(ElevatorPositionY, FloorGoal) self:FloorPassingUp(ElevatorPositionY, Attributes.Relay.Goal.Value)
if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then
self:Leveling(FloorGoal) self:Leveling(Attributes.Relay.Goal.Value)
if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningDebounce = true DoorsOpeningDebounce = true
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.Relay.Goal.Value)
end end
end end
if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then
self:Leveled(FloorGoal) self:Leveled(Attributes.Relay.Goal.Value)
end end
else else
self:FloorPassingDown(ElevatorPositionY, FloorGoal) self:FloorPassingDown(ElevatorPositionY, Attributes.Relay.Goal.Value)
if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then
self:Leveling(FloorGoal) self:Leveling(Attributes.Relay.Goal.Value)
if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningDebounce = true DoorsOpeningDebounce = true
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.Relay.Goal.Value)
end end
end end
if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then
self:Leveled(FloorGoal) self:Leveled(Attributes.Relay.Goal.Value)
end end
end end
end) end)
end end
function Elevator:__MoveTo(GoingUp, GoalFloor_Y) function Elevator:__MoveTo(GoingUp, RequestedLevel)
if Doors.Attributes.Relay.Open.Value then if Doors.Attributes.Relay.Open.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end end
@@ -477,18 +489,18 @@ function Elevator:__MoveTo(GoingUp, GoalFloor_Y)
self.LanternsConstructor:DirectionDown(true) self.LanternsConstructor:DirectionDown(true)
end end
self:__MovingHeartbeat(GoingUp, GoalFloor_Y) self:__MovingHeartbeat(GoingUp, RequestedLevel)
end end
function Elevator:RequestLevel(RequestedLevel) function Elevator:RequestLevel(RequestedLevel)
local GoalFloor_Y: number? = LevelingModule.Leveling[RequestedLevel] local FloorExist: number? = LevelingModule.Leveling[RequestedLevel]
if GoalFloor_Y and RequestedLevel ~= Attributes.CurrentFloor.Value then if FloorExist and RequestedLevel ~= Attributes.CurrentFloor.Value then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
local Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel) local Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel)
if Proceeding then if Proceeding then
self:__MoveTo(ElevatorGoingUp, GoalFloor_Y) self:__MoveTo(ElevatorGoingUp, RequestedLevel)
end end
return true return true
else else

View File

@@ -13,8 +13,6 @@ local TweenModule = require(Storage:WaitForChild("Tween"))
local PromptsConstructor = require(MapDir:WaitForChild("Prompts")) local PromptsConstructor = require(MapDir:WaitForChild("Prompts"))
type LightCallback = (Player: Player) -> ()
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
type Impl_Constructor = { type Impl_Constructor = {
__index: Impl_Constructor, __index: Impl_Constructor,