No work motiviation but hooked the relay algorithm to the elevator

This commit is contained in:
2024-05-20 23:04:58 -04:00
parent b67c70aad1
commit 70d58e9727
4 changed files with 62 additions and 75 deletions

View File

@@ -36,7 +36,7 @@ type Impl_Static_Props = {
__DontLeakMemory: RBXScriptConnection?, __DontLeakMemory: RBXScriptConnection?,
Attributes: { Attributes: {
DoorsOpen: BoolValue Open: BoolValue
} }
} }
type Constructor_Return_Props = { type Constructor_Return_Props = {
@@ -67,13 +67,13 @@ Doors.ElevatorDoorTime = 4
Doors.ElevatorDoorStyle = Enum.EasingStyle.Quad Doors.ElevatorDoorStyle = Enum.EasingStyle.Quad
Doors.Attributes = { Doors.Attributes = {
DoorsOpen = Instance.new("BoolValue") :: BoolValue Open = Instance.new("BoolValue") :: BoolValue
} }
Doors.Attributes.DoorsOpen.Value = false Doors.Attributes.Open.Value = false
Doors.Attributes.DoorsOpen:GetPropertyChangedSignal("Value"):Connect(function() Doors.Attributes.Open:GetPropertyChangedSignal("Value"):Connect(function()
print("DoorsOpen=",Doors.Attributes.DoorsOpen.Value) print("Open=",Doors.Attributes.Open.Value)
end) end)
local Attributes = Doors.Attributes local Attributes = Doors.Attributes
@@ -199,7 +199,7 @@ local function ElevatorDoorsAnimation(self: ClassConstructor, opening: boolean?,
end end
end end
Attributes.DoorsOpen.Value = true Attributes.Open.Value = true
else else
local Door1Tween = self.DoorTween1:Start(nil, { local Door1Tween = self.DoorTween1:Start(nil, {
Position = ElevatorDoor1_P+Doors.Door1Stopped_X Position = ElevatorDoor1_P+Doors.Door1Stopped_X
@@ -252,7 +252,7 @@ end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2) function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
local Step = nil local Step = nil
if Doors.Sensors and Attributes.DoorsOpen.Value then if Doors.Sensors and Attributes.Open.Value then
raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)} raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)}
Step = RS.Heartbeat:Connect(function(_dt) Step = RS.Heartbeat:Connect(function(_dt)
@@ -274,15 +274,15 @@ end
function Doors:ToggleElevatorDoorsAsync(opening, floor) function Doors:ToggleElevatorDoorsAsync(opening, floor)
--short circuiting central --short circuiting central
if opening then if opening then
if not Attributes.DoorsOpen.Value then if not Attributes.Open.Value then
Attributes.DoorsOpen.Value = not Attributes.DoorsOpen.Value Attributes.Open.Value = not Attributes.Open.Value
else else
warn("Doors are already closed, doing nothing") warn("Doors are already closed, doing nothing")
return return
end end
else else
if Attributes.DoorsOpen.Value then if Attributes.Open.Value then
Attributes.DoorsOpen.Value = not Attributes.DoorsOpen.Value Attributes.Open.Value = not Attributes.Open.Value
else else
warn("Doors are already open, doing nothing") warn("Doors are already open, doing nothing")
return return
@@ -298,10 +298,10 @@ function Doors:ToggleElevatorDoorsAsync(opening, floor)
if Door2Tween then if Door2Tween then
Door2Tween.Completed:Wait() Door2Tween.Completed:Wait()
Attributes.DoorsOpen.Value = opening ~= nil and opening or false Attributes.Open.Value = opening ~= nil and opening or false
elseif Door1Tween then elseif Door1Tween then
Door1Tween.Completed:Wait() Door1Tween.Completed:Wait()
Attributes.DoorsOpen.Value = opening ~= nil and opening or false Attributes.Open.Value = opening ~= nil and opening or false
end end
self.ElevatorDoor1.CanCollide = true self.ElevatorDoor1.CanCollide = true

View File

@@ -78,7 +78,7 @@ function MovingObjects:Frame_Pullies(Delta, ElevatorVelocity_Y)
local PullAngle = math.rad(RotAngle) local PullAngle = math.rad(RotAngle)
MR.Pulley.CFrame = MR_C.PulleyCFrame *CFrame.Angles(-PullAngle_2, 0, 0) MR.Pulley.CFrame = MR_C.PulleyCFrame *CFrame.Angles(-PullAngle_2, 0, 0)
MR.Pulley2.CFrame = MR_C.Pulley2CFrame *CFrame.Angles(PullAngle_2, 0, 0) MR.Pulley2.CFrame = MR_C.Pulley2CFrame *CFrame.Angles(PullAngle_2, 0, 0)
MR.Governor.CFrame = MR_C.GovernorCFrame *CFrame.Angles(0, PullAngle_2, 0) MR.Governor.CFrame = MR_C.GovernorCFrame *CFrame.Angles(0, PullAngle_2, 0)
MR.GovernorFlyballs.CFrame = MR_C.GovernorFlyballsCFrame*CFrame.Angles(PullAngle, 0, 0) MR.GovernorFlyballs.CFrame = MR_C.GovernorFlyballsCFrame*CFrame.Angles(PullAngle, 0, 0)
MR.PiePlatePulley.CFrame = MR_C.PieplatePulleyCFrame *CFrame.Angles(PullAngle_2, 0, 0) MR.PiePlatePulley.CFrame = MR_C.PieplatePulleyCFrame *CFrame.Angles(PullAngle_2, 0, 0)

View File

@@ -7,9 +7,10 @@ type Impl_Constructor = {
__index: Impl_Constructor, __index: Impl_Constructor,
constructor: Constructor_Fun, constructor: Constructor_Fun,
--Class functions --Class functions
Sort: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (), Sort: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (),
Check: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (), Check: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (),
Insert: (self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number) -> (), RawInsert: (self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number) -> (),
AddFloor: (self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number) -> boolean
} }
type Constructor_Fun = (BoxAlignPosition: AlignPosition, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes) -> ClassConstructor type Constructor_Fun = (BoxAlignPosition: AlignPosition, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes) -> ClassConstructor
@@ -17,19 +18,18 @@ type Constructor_Return_Props = {
BoxAlignPosition: AlignPosition, BoxAlignPosition: AlignPosition,
ElevatorAttributes: ElevatorAttributes, ElevatorAttributes: ElevatorAttributes,
DoorAttributes: DoorAttributes, DoorAttributes: DoorAttributes,
__FloorQueue: Floors __FloorQueue: {number}
} }
type Floors = {number}
type ElevatorAttributes = { type ElevatorAttributes = {
CurrentFloor: IntValue, CurrentFloor: IntValue,
Goal: IntValue,
GoingUp: BoolValue, GoingUp: BoolValue,
GoalFloor: IntValue Moving: BoolValue
} }
type DoorAttributes = { type DoorAttributes = {
DoorsOpen: BoolValue Open: BoolValue
} }
export type RelayAlgorithmConstructor = ClassConstructor export type RelayAlgorithmConstructor = ClassConstructor
@@ -49,23 +49,6 @@ end
--The Otis relay based call logic --The Otis relay based call logic
--https://youtu.be/BCN9mQOT3RQ --https://youtu.be/BCN9mQOT3RQ
--Sort the queue based on direction
--[[
Up: {
[1] = 5,
[2] = 6,
[3] = 7,
[4] = 8,
[5] = 9,
[6] = 10
}
Down: {
[1] = 5,
[2] = 3,
[3] = 2,
[4] = 1
}
]]
function RelayAlgorithm:Sort(ElevatorGoingUp) function RelayAlgorithm:Sort(ElevatorGoingUp)
table.sort(self.__FloorQueue, function(a: number, b: number): boolean table.sort(self.__FloorQueue, function(a: number, b: number): boolean
if ElevatorGoingUp then if ElevatorGoingUp then
@@ -85,13 +68,26 @@ function RelayAlgorithm:Check(ElevatorGoingUp)
self:Sort(ElevatorGoingUp) self:Sort(ElevatorGoingUp)
return true return true
end end
return false return false
end end
function RelayAlgorithm:Insert(ElevatorGoingUp, RequestedLevel) function RelayAlgorithm:RawInsert(ElevatorGoingUp, RequestedLevel)
table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel) table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel)
self:Sort(ElevatorGoingUp) self:Sort(ElevatorGoingUp)
end 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
end
return RelayAlgorithm return RelayAlgorithm

View File

@@ -58,7 +58,7 @@ 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,
__GoToFloor: (self: ClassConstructor, Level: number, GoingUp: boolean) -> (), __Move: (self: ClassConstructor, Level: number, GoingUp: boolean) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean) -> (), __MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean) -> (),
} & Impl_Static_Props } & Impl_Static_Props
@@ -83,7 +83,7 @@ type Impl_Static_Props = {
}, },
Attributes: { Attributes: {
CurrentFloor: IntValue, CurrentFloor: IntValue,
GoalFloor: IntValue, Goal: IntValue,
Moving: BoolValue, Moving: BoolValue,
GoingUp: BoolValue, GoingUp: BoolValue,
Stopped: BoolValue Stopped: BoolValue
@@ -146,7 +146,7 @@ Elevator.Colors = {
Elevator.Attributes = { Elevator.Attributes = {
CurrentFloor = Instance.new("IntValue") :: IntValue, CurrentFloor = Instance.new("IntValue") :: IntValue,
GoalFloor = Instance.new("IntValue") :: IntValue, Goal = Instance.new("IntValue") :: IntValue,
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
@@ -157,7 +157,7 @@ Elevator.Events = {
} }
Elevator.Attributes.CurrentFloor.Value = 1 Elevator.Attributes.CurrentFloor.Value = 1
Elevator.Attributes.GoalFloor.Value = 1 Elevator.Attributes.Goal.Value = 1
Elevator.Attributes.Moving.Value = false Elevator.Attributes.Moving.Value = false
Elevator.Attributes.GoingUp.Value = false Elevator.Attributes.GoingUp.Value = false
@@ -176,7 +176,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.GoalFloor.Value == ButtonFloor then if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.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?)
@@ -185,6 +185,7 @@ local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number
end) end)
else else
warn(`Failed to call floor: {ButtonFloor}`) warn(`Failed to call floor: {ButtonFloor}`)
ButtonTree.Prompt.Enabled = true
end end
end end
@@ -220,7 +221,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:__GoToFloor(Leveling[Attributes.CurrentFloor.Value], ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.GoalFloor.Value)) self:__GoToFloor(Leveling[Attributes.CurrentFloor.Value], ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Goal.Value))
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
end end
end) end)
@@ -318,7 +319,6 @@ function Elevator:Leveled(RequestedLevel)
Attributes.Moving.Value = false Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel Attributes.CurrentFloor.Value = RequestedLevel
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Elevator.QueueWaitTime) task.wait(Elevator.QueueWaitTime)
@@ -327,8 +327,7 @@ 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
Attributes.GoingUp.Value = ElevatorGoingUp self:__Move(Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], ElevatorGoingUp)
self:__GoToFloor(Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], ElevatorGoingUp)
end end
end end
@@ -356,22 +355,21 @@ function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
end end
function Elevator:__MovingHeartbeat(GoingUp) function Elevator:__MovingHeartbeat(GoingUp)
local Delta = 0 Attributes.GoingUp.Value = GoingUp
local DoorsOpeningEvent = false
if self.__MovingConnection and self.__MovingConnection.Connected then if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect() self.__MovingConnection:Disconnect()
end end
--Only used for the X and Z axis local Delta = 0
local ElevatorPositionInit: Vector3 = self.ElevatorBox_1960.Position local DoorsOpeningEvent = false
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt) self.__MovingConnection = RS.Heartbeat:Connect(function(_dt)
Delta+=1 Delta+=1
local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1] local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
Attributes.Moving.Value = true Attributes.Moving.Value = true
Attributes.GoalFloor.Value = FloorGoal Attributes.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
@@ -413,16 +411,13 @@ function Elevator:__MovingHeartbeat(GoingUp)
self:Leveled(FloorGoal) self:Leveled(FloorGoal)
end end
end end
self.BoxAlignPosition.Position = Vector3.new(ElevatorPositionInit.X, Leveling[FloorGoal], ElevatorPositionInit.Z)
end) end)
end end
function Elevator:__GoToFloor(GoalLevelVEC, GoingUp) function Elevator:__Move(GoalLevelVEC, GoingUp)
if Doors.Attributes.DoorsOpen.Value then if Doors.Attributes.Open.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end end
if GoingUp then if GoingUp then
self.LanternsConstructor:DirectionUp(true) self.LanternsConstructor:DirectionUp(true)
else else
@@ -431,9 +426,9 @@ function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
self.MOConstructor:UpdateCFrame() self.MOConstructor:UpdateCFrame()
self:__MovingHeartbeat(GoingUp) self:__MovingHeartbeat(GoingUp)
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position local ElevatorPositionInit = self.ElevatorBox_1960.Position
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z) self.BoxAlignPosition.Position = Vector3.new(ElevatorPositionInit.X, GoalLevelVEC, ElevatorPositionInit.Z)
end end
function Elevator:RequestLevel(RequestedLevel) function Elevator:RequestLevel(RequestedLevel)
@@ -441,19 +436,15 @@ function Elevator:RequestLevel(RequestedLevel)
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then if GoalLevelVEC and GoalLevelVEC ~= 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)
if Attributes.Moving.Value then if Proceeding then
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel) self:__Move(GoalLevelVEC, ElevatorGoingUp)
else else
if Doors.Attributes.DoorsOpen then if not Doors.Attributes.Open.Value then
Attributes.GoingUp.Value = ElevatorGoingUp local ElevatorPositionInit = self.ElevatorBox_1960.Position
self.BoxAlignPosition.Position = Vector3.new(ElevatorPositionInit.X, Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], ElevatorPositionInit.Z)
if Doors.Attributes.DoorsOpen and #self.RelayAlgorithmConstructor.__FloorQueue == 0 then print(Doors.Attributes.Open.Value)
self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp)
end
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
else
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
end end
end end
else else