From 7387e759c8db8e36137f0fbe21802fb049c68d69 Mon Sep 17 00:00:00 2001 From: unittensor Date: Wed, 22 May 2024 22:18:28 -0400 Subject: [PATCH] relay system --- .../Elevators/Otis1960/RelayAlgorithm.lua | 17 +--- src/server/main/Elevators/Otis1960/init.lua | 82 +++++++++++++------ 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua b/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua index 77d729b..2ed9e35 100644 --- a/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua +++ b/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua @@ -23,9 +23,8 @@ type Constructor_Return_Props = { type ElevatorAttributes = { CurrentFloor: IntValue, - Goal: IntValue, GoingUp: BoolValue, - Moving: BoolValue + Moving: BoolValue, } type DoorAttributes = { @@ -77,17 +76,9 @@ function RelayAlgorithm:RawInsert(ElevatorGoingUp, RequestedLevel) end function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel) - if self.ElevatorAttributes.Moving.Value then - self:RawInsert(ElevatorGoingUp, RequestedLevel) - else - if self.DoorAttributes.Open.Value then - self:RawInsert(ElevatorGoingUp, RequestedLevel) - return true - else - self:RawInsert(ElevatorGoingUp, RequestedLevel) - end - end - return false + self:RawInsert(ElevatorGoingUp, RequestedLevel) + + return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Open.Value end return RelayAlgorithm \ No newline at end of file diff --git a/src/server/main/Elevators/Otis1960/init.lua b/src/server/main/Elevators/Otis1960/init.lua index d3c59ec..ff73520 100644 --- a/src/server/main/Elevators/Otis1960/init.lua +++ b/src/server/main/Elevators/Otis1960/init.lua @@ -83,12 +83,15 @@ type Impl_Static_Props = { }, Attributes: { CurrentFloor: IntValue, - Goal: IntValue, - GoalYLevel: NumberValue, - Ready: BoolValue, Moving: BoolValue, GoingUp: BoolValue, - Stopped: BoolValue + Stopped: BoolValue, + + Relay: { + Goal: IntValue, + GoalYLevel: NumberValue, + Ready: BoolValue, + } }, Events: { ButtonActivated: BindableEvent @@ -147,13 +150,16 @@ Elevator.Colors = { } Elevator.Attributes = { - CurrentFloor = Instance.new("IntValue") :: IntValue, - Goal = Instance.new("IntValue") :: IntValue, - GoalYLevel = Instance.new("NumberValue") :: NumberValue, - Ready = Instance.new("BoolValue") :: BoolValue, - Moving = Instance.new("BoolValue") :: BoolValue, - GoingUp = Instance.new("BoolValue") :: BoolValue, - Stopped = Instance.new("BoolValue") :: BoolValue + CurrentFloor = Instance.new("IntValue") :: IntValue, + Moving = Instance.new("BoolValue") :: BoolValue, + GoingUp = Instance.new("BoolValue") :: BoolValue, + Stopped = Instance.new("BoolValue") :: BoolValue, + + Relay = { + Ready = Instance.new("BoolValue") :: BoolValue, + Goal = Instance.new("IntValue") :: IntValue, + GoalYLevel = Instance.new("NumberValue") :: NumberValue, + }, } Elevator.Events = { @@ -161,11 +167,12 @@ Elevator.Events = { } Elevator.Attributes.CurrentFloor.Value = 1 -Elevator.Attributes.Goal.Value = 1 -Elevator.Attributes.Ready.Value = false Elevator.Attributes.Moving.Value = false Elevator.Attributes.GoingUp.Value = false +Elevator.Attributes.Relay.Goal.Value = 1 +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 @@ -181,7 +188,7 @@ local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number local FloorTracker: RBXScriptConnection FloorTracker = Attributes.CurrentFloor:GetPropertyChangedSignal("Value"):Connect(function() - if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.Goal.Value == ButtonFloor then + if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.Relay.Goal.Value == ButtonFloor then FloorTracker:Disconnect() self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?) @@ -226,7 +233,7 @@ local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonT (ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05) self.LanternsConstructor:Reset() - self:__MoveTo(ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Goal.Value), Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]]) + self:__MoveTo(ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Relay.Goal.Value), Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]]) self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity end end) @@ -261,6 +268,33 @@ local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: Bu end end +local function ElevatorReactionEvents(self: ClassConstructor) + --This is how the elevator logic works with the elevator itself + Attributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function() + if not Attributes.Stopped.Value and Attributes.Relay.Ready.Value then + self.BoxAlignPosition.Position = Vector3.new(self.ElevatorBox_1960.Position.X, Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], self.ElevatorBox_1960.Position.Z) + end + end) + Doors.Attributes.Open:GetPropertyChangedSignal("Value"):Connect(function() + if not Attributes.Stopped.Value then + Attributes.Relay.Ready.Value = Doors.Attributes.Open.Value + end + end) + + Attributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function() + if not Attributes.Stopped.Value then + self.BoxAlignPosition.Position = Vector3.new(self.ElevatorBox_1960.Position.X, Attributes.Relay.GoalYLevel.Value, self.ElevatorBox_1960.Position.Z) + end + end) + Attributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function() + if not Attributes.Stopped.Value then + local Level: number? = Leveling[Attributes.Relay.Goal.Value] + + Attributes.Relay.GoalYLevel.Value = Level or Leveling[1] + end + end) +end + function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors) local self = {} :: Constructor_Return_Props @@ -313,18 +347,12 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value) + + --Some hacks + Doors.Attributes.Open.Value = true end) - --This is how the elevator logic works with the elevator itself - Attributes.Ready:GetPropertyChangedSignal("Value"):Connect(function() - if Attributes.Ready.Value then - self.BoxAlignPosition.Position = Vector3.new(self.ElevatorBox_1960.Position.X, Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], self.ElevatorBox_1960.Position.Z) - end - end) - Doors.Attributes.Open:GetPropertyChangedSignal("Value"):Connect(function() - print(Doors.Attributes.Open.Value) - Attributes.Ready.Value = not Doors.Attributes.Open.Value - end) + ElevatorReactionEvents(self) print(`🔝 {Elevator.Name} initialized and ready`) return ClassConstructor @@ -386,7 +414,7 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y) local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1] Attributes.Moving.Value = true - Attributes.Goal.Value = FloorGoal + Attributes.Relay.Goal.Value = FloorGoal local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position local ElevatorPositionY: number = ElevatorPosition.Y @@ -451,7 +479,7 @@ function Elevator:RequestLevel(RequestedLevel) local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) local Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel) - if Doors.Attributes.Open.Value then + if Proceeding then self:__MoveTo(ElevatorGoingUp, GoalFloor_Y) end else