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
function Relay:__Ready()
if self.ElevatorAttributes.Relay.Ready.Value then
self.BoxAlignPosition.Position = Vector3.new(
self.ElevatorBox.Position.X,
self.ElevatorAttributes.Relay.GoalYLevel.Value,
self.ElevatorBox.Position.Z
)
if not self.ElevatorAttributes.Relay.Ready.Value then
self:__GoalYLevel()
end
end
@@ -88,11 +84,13 @@ function Relay:__Open()
end
function Relay:__GoalYLevel()
if not self.ElevatorAttributes.Relay.Ready.Value then
self.BoxAlignPosition.Position = Vector3.new(
self.ElevatorBox.Position.X,
self.ElevatorAttributes.Relay.GoalYLevel.Value,
self.ElevatorBox.Position.Z
)
end
end
function Relay:__Goal()
@@ -103,24 +101,28 @@ end
function Relay:BulkConnect()
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
self:__Ready()
end
end)
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
self:__Open()
end
end)
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
self:__GoalYLevel()
end
end)
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
self:__Goal()
end

View File

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

View File

@@ -59,8 +59,8 @@ type Impl_Constructor = {
FloorPassingUp: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
FloorPassingDown: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
RequestLevel: (self: ClassConstructor, RequestedLevel: number) -> boolean,
__MoveTo: (self: ClassConstructor, GoingUp: boolean, GoalFloor_Y: number) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean, GoalFloor_Y: number) -> (),
__MoveTo: (self: ClassConstructor, GoingUp: boolean, RequestedLevel: number) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean, RequestedLevel: number) -> (),
} & Impl_Static_Props
type Impl_Static_Props = {
@@ -125,7 +125,11 @@ type Constructor_Return_Props = {
ButtonsConstructor: Buttons.ButtonsConstructor,
RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor,
RelayConstructor: Relay.RelayConstructor,
__MovingConnection: RBXScriptConnection?,
__Connections: {
Moving: RBXScriptConnection?,
FloorSorted: RBXScriptConnection?
}
}
local Elevator = {} :: Impl_Constructor
@@ -136,7 +140,7 @@ Elevator.FloorLevelingDistance = 2.5
Elevator.DoorOpeningDistance = Elevator.FloorLevelingDistance/2.8
Elevator.LeveledDistance = 0.5
Elevator.Responsiveness = 10
Elevator.MaxVelocity = 10
Elevator.MaxVelocity = 8
Elevator.QueueWaitTime = 5
Elevator.Sounds = {
@@ -178,10 +182,10 @@ Elevator.Attributes.Relay.Ready.Value = false
local Attributes = Elevator.Attributes
--My clever math function for determining if the elevator goal is to move upwards or not
local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean
--(Floor-RequestedFloor)>0
return -Floor+RequestedFloor>0 --Simplified equation for less computation
--Math function for determining if the elevator goal is to move upwards or not
local function ElevatorGoingUpDirection(CurrentFloor: number, RequestedFloor: number): boolean
--(CurrentFloor-RequestedFloor)>0
return -CurrentFloor+RequestedFloor>0
end
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
(ButtonTree.Inst :: BasePart).Position+=Vector3.new(0,0,.05)
if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect()
if self.__Connections.Moving and self.__Connections.Moving.Connected then
self.__Connections.Moving:Disconnect()
end
self.BoxAlignPosition.MaxVelocity = 0
else
(ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05)
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
end
end)
@@ -295,6 +299,7 @@ end
function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
local self = {} :: Constructor_Return_Props
self.__Connections = {}
self.MachineRoom = {_CFrame = {}} :: MovingObjects.MachineRoom
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
self.MOConstructor = MovingObjects.constructor({
MachineRoom = self.MachineRoom
} :: MovingObjects.InstanceTree)
self.MOConstructor = MovingObjects.constructor({MachineRoom = self.MachineRoom} :: MovingObjects.InstanceTree)
--Start the hall displays
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)
--Init the ropes
self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling)
--Init the lanterns
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors)
local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags)
@@ -351,6 +357,12 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
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)
IterateButtons(ClassConstructor, ButtonsTagsConstructor)
@@ -361,10 +373,10 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
end
function Elevator:Leveled(RequestedLevel)
(self.__MovingConnection :: RBXScriptConnection):Disconnect()
(self.__Connections.Moving :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel
self.BoxAlignPosition.MaxVelocity = 0
--self.BoxAlignPosition.MaxVelocity = 0
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Elevator.QueueWaitTime)
@@ -373,7 +385,10 @@ function Elevator:Leveled(RequestedLevel)
if self.RelayAlgorithmConstructor:Check(ElevatorGoingUp) then
--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
@@ -404,25 +419,22 @@ function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
end
end
function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
function Elevator:__MovingHeartbeat(GoingUp, RequestedLevel)
Attributes.GoingUp.Value = GoingUp
Attributes.Relay.Goal.Value = RequestedLevel
Attributes.Moving.Value = true
if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect()
if self.__Connections.Moving and self.__Connections.Moving.Connected then
self.__Connections.Moving:Disconnect()
end
self.MOConstructor:UpdateCFrame()
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
local Delta = 0
local DoorsOpeningDebounce = false
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt)
Delta+=1
local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
self.MOConstructor:UpdateCFrame()
Attributes.Moving.Value = true
Attributes.Relay.Goal.Value = FloorGoal
self.__Connections.Moving = RS.Heartbeat:Connect(function(_dt)
Delta+=1
local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position
local ElevatorPositionY: number = ElevatorPosition.Y
@@ -434,40 +446,40 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
--Kill the connection
if Attributes.GoingUp.Value then
self:FloorPassingUp(ElevatorPositionY, FloorGoal)
self:FloorPassingUp(ElevatorPositionY, Attributes.Relay.Goal.Value)
if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then
self:Leveling(FloorGoal)
self:Leveling(Attributes.Relay.Goal.Value)
if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningDebounce = true
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.Relay.Goal.Value)
end
end
if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then
self:Leveled(FloorGoal)
self:Leveled(Attributes.Relay.Goal.Value)
end
else
self:FloorPassingDown(ElevatorPositionY, FloorGoal)
self:FloorPassingDown(ElevatorPositionY, Attributes.Relay.Goal.Value)
if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then
self:Leveling(FloorGoal)
self:Leveling(Attributes.Relay.Goal.Value)
if not DoorsOpeningDebounce and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningDebounce = true
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.Relay.Goal.Value)
end
end
if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then
self:Leveled(FloorGoal)
self:Leveled(Attributes.Relay.Goal.Value)
end
end
end)
end
function Elevator:__MoveTo(GoingUp, GoalFloor_Y)
function Elevator:__MoveTo(GoingUp, RequestedLevel)
if Doors.Attributes.Relay.Open.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end
@@ -477,18 +489,18 @@ function Elevator:__MoveTo(GoingUp, GoalFloor_Y)
self.LanternsConstructor:DirectionDown(true)
end
self:__MovingHeartbeat(GoingUp, GoalFloor_Y)
self:__MovingHeartbeat(GoingUp, RequestedLevel)
end
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 Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel)
if Proceeding then
self:__MoveTo(ElevatorGoingUp, GoalFloor_Y)
self:__MoveTo(ElevatorGoingUp, RequestedLevel)
end
return true
else

View File

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