relay system

This commit is contained in:
2024-05-22 22:18:28 -04:00
parent b5f214f3b8
commit 7387e759c8
2 changed files with 59 additions and 40 deletions

View File

@@ -23,9 +23,8 @@ type Constructor_Return_Props = {
type ElevatorAttributes = { type ElevatorAttributes = {
CurrentFloor: IntValue, CurrentFloor: IntValue,
Goal: IntValue,
GoingUp: BoolValue, GoingUp: BoolValue,
Moving: BoolValue Moving: BoolValue,
} }
type DoorAttributes = { type DoorAttributes = {
@@ -77,17 +76,9 @@ function RelayAlgorithm:RawInsert(ElevatorGoingUp, RequestedLevel)
end end
function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel) function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel)
if self.ElevatorAttributes.Moving.Value then
self:RawInsert(ElevatorGoingUp, RequestedLevel) self:RawInsert(ElevatorGoingUp, RequestedLevel)
else
if self.DoorAttributes.Open.Value then return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Open.Value
self:RawInsert(ElevatorGoingUp, RequestedLevel)
return true
else
self:RawInsert(ElevatorGoingUp, RequestedLevel)
end
end
return false
end end
return RelayAlgorithm return RelayAlgorithm

View File

@@ -83,12 +83,15 @@ type Impl_Static_Props = {
}, },
Attributes: { Attributes: {
CurrentFloor: IntValue, CurrentFloor: IntValue,
Moving: BoolValue,
GoingUp: BoolValue,
Stopped: BoolValue,
Relay: {
Goal: IntValue, Goal: IntValue,
GoalYLevel: NumberValue, GoalYLevel: NumberValue,
Ready: BoolValue, Ready: BoolValue,
Moving: BoolValue, }
GoingUp: BoolValue,
Stopped: BoolValue
}, },
Events: { Events: {
ButtonActivated: BindableEvent ButtonActivated: BindableEvent
@@ -148,12 +151,15 @@ Elevator.Colors = {
Elevator.Attributes = { Elevator.Attributes = {
CurrentFloor = Instance.new("IntValue") :: IntValue, 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, Moving = Instance.new("BoolValue") :: BoolValue,
GoingUp = Instance.new("BoolValue") :: BoolValue, GoingUp = Instance.new("BoolValue") :: BoolValue,
Stopped = 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 = { Elevator.Events = {
@@ -161,11 +167,12 @@ Elevator.Events = {
} }
Elevator.Attributes.CurrentFloor.Value = 1 Elevator.Attributes.CurrentFloor.Value = 1
Elevator.Attributes.Goal.Value = 1
Elevator.Attributes.Ready.Value = false
Elevator.Attributes.Moving.Value = false Elevator.Attributes.Moving.Value = false
Elevator.Attributes.GoingUp.Value = false Elevator.Attributes.GoingUp.Value = false
Elevator.Attributes.Relay.Goal.Value = 1
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 --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 local FloorTracker: RBXScriptConnection
FloorTracker = Attributes.CurrentFloor:GetPropertyChangedSignal("Value"):Connect(function() 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() FloorTracker:Disconnect()
self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?) 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) (ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05)
self.LanternsConstructor:Reset() 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 self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
end end
end) end)
@@ -261,6 +268,33 @@ local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: Bu
end end
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) function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
local self = {} :: Constructor_Return_Props local self = {} :: Constructor_Return_Props
@@ -313,18 +347,12 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value) self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
--Some hacks
Doors.Attributes.Open.Value = true
end) end)
--This is how the elevator logic works with the elevator itself ElevatorReactionEvents(self)
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)
print(`🔝 {Elevator.Name} initialized and ready`) print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor return ClassConstructor
@@ -386,7 +414,7 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1] local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
Attributes.Moving.Value = true Attributes.Moving.Value = true
Attributes.Goal.Value = FloorGoal Attributes.Relay.Goal.Value = FloorGoal
local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position local ElevatorPosition: Vector3 = self.ElevatorBox_1960.Position
local ElevatorPositionY: number = ElevatorPosition.Y local ElevatorPositionY: number = ElevatorPosition.Y
@@ -451,7 +479,7 @@ function Elevator:RequestLevel(RequestedLevel)
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 Doors.Attributes.Open.Value then if Proceeding then
self:__MoveTo(ElevatorGoingUp, GoalFloor_Y) self:__MoveTo(ElevatorGoingUp, GoalFloor_Y)
end end
else else