Relay logic

This commit is contained in:
2024-05-24 01:39:47 -04:00
parent 7387e759c8
commit 61153b53e1
5 changed files with 186 additions and 59 deletions

File diff suppressed because one or more lines are too long

View File

@@ -36,9 +36,11 @@ type Impl_Static_Props = {
__DontLeakMemory: RBXScriptConnection?,
Attributes: {
Relay: {
Open: BoolValue
}
}
}
type Constructor_Return_Props = {
FloorDoorsTags: Tags.LandingTags,
DoorTween1: CustomTween,
@@ -67,10 +69,12 @@ Doors.ElevatorDoorTime = 4
Doors.ElevatorDoorStyle = Enum.EasingStyle.Quad
Doors.Attributes = {
Relay = {
Open = Instance.new("BoolValue") :: BoolValue
}
}
Doors.Attributes.Open.Value = false
Doors.Attributes.Relay.Open.Value = false
local Attributes = Doors.Attributes
@@ -246,7 +250,7 @@ end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
local Step = nil
if Doors.Sensors and Attributes.Open.Value then
if Doors.Sensors and Attributes.Relay.Open.Value then
raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)}
Step = RS.Heartbeat:Connect(function(_dt)
@@ -268,12 +272,12 @@ end
function Doors:ToggleElevatorDoorsAsync(opening, floor)
--short circuiting central
if opening then
if Attributes.Open.Value then
if Attributes.Relay.Open.Value then
warn("Doors are already closed, doing nothing")
return
end
else
if not Attributes.Open.Value then
if not Attributes.Relay.Open.Value then
warn("Doors are already open, doing nothing")
return
end
@@ -288,10 +292,10 @@ function Doors:ToggleElevatorDoorsAsync(opening, floor)
if Door2Tween then
Door2Tween.Completed:Wait()
Attributes.Open.Value = opening ~= nil and opening or false
Attributes.Relay.Open.Value = opening ~= nil and opening or false
elseif Door1Tween then
Door1Tween.Completed:Wait()
Attributes.Open.Value = opening ~= nil and opening or false
Attributes.Relay.Open.Value = opening ~= nil and opening or false
end
self.ElevatorDoor1.CanCollide = true

View File

@@ -15,4 +15,13 @@ local Leveling: {number} = {
[10] = 239.245,
}
return Leveling
local LevelingBetween: {number} = {}
--Calculate between leveling
for n: number = 1, #Leveling, 2 do
LevelingBetween[n] = (Leveling[n]+Leveling[n+1])/2
end
return {
Leveling = Leveling,
LevelingBetween = LevelingBetween
}

View File

@@ -0,0 +1,144 @@
--!optimize 2
--!native
--!strict
local RelayAlgorithmModule = require(script.Parent:WaitForChild("RelayAlgorithm"))
type Leveling = {
Leveling: {number},
LevelingBetween: {number}
}
type ElevatorBox = BasePart
type BoxAlignPosition = AlignPosition
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
--Class functions
BulkConnect: (self: ClassConstructor) -> (),
BulkDisconnect: (self: ClassConstructor) -> (),
__Ready: (self: ClassConstructor) -> (),
__Open: (self: ClassConstructor) -> (),
__Goal: (self: ClassConstructor) -> (),
__GoalYLevel: (self: ClassConstructor) -> (),
}
type Constructor_Fun = (RelayAlgorthm: RelayAlgorithmModule.RelayAlgorithmConstructor, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes, Leveling: Leveling, BoxAlignPosition: BoxAlignPosition, ElevatorBox: ElevatorBox) -> ClassConstructor
type Constructor_Return_Props = {
RelayAlgorthm: RelayAlgorithmModule.RelayAlgorithmConstructor,
ElevatorAttributes: ElevatorAttributes,
DoorAttributes: DoorAttributes,
LevelingModule: Leveling,
BoxAlignPosition: BoxAlignPosition,
ElevatorBox: ElevatorBox,
__ReadyConnection: RBXScriptConnection?,
__OpenConnection: RBXScriptConnection?,
__GoalYLevelConnection: RBXScriptConnection?,
__GoalConnection: RBXScriptConnection?,
}
type ElevatorAttributes = {
Relay: {
Ready: BoolValue,
GoalYLevel: NumberValue,
Goal: IntValue
},
Stopped: BoolValue
}
type DoorAttributes = {
Relay: {
Open: BoolValue
}
}
export type RelayConstructor = ClassConstructor
local Relay = {} :: Impl_Constructor
Relay.__index = Relay
function Relay.constructor(RelayAlgorthm, ElevatorAttributes, DoorAttributes, LevelingModule, BoxAlignPosition, ElevatorBox)
return setmetatable({
RelayAlgorthm = RelayAlgorthm,
ElevatorAttributes = ElevatorAttributes,
DoorAttributes = DoorAttributes,
LevelingModule = LevelingModule,
BoxAlignPosition = BoxAlignPosition,
ElevatorBox = ElevatorBox
}, Relay)
end
function Relay:__Ready()
if self.ElevatorAttributes.Relay.Ready.Value then
self.BoxAlignPosition.Position = Vector3.new(
self.ElevatorBox.Position.X,
self.LevelingModule.Leveling[self.RelayAlgorthm.__FloorQueue[1]],
self.ElevatorBox.Position.Z
)
end
end
function Relay:__Open()
self.ElevatorAttributes.Relay.Ready.Value = self.DoorAttributes.Relay.Open.Value
end
function Relay:__GoalYLevel()
self.BoxAlignPosition.Position = Vector3.new(
self.ElevatorBox.Position.X,
self.ElevatorAttributes.Relay.GoalYLevel.Value,
self.ElevatorBox.Position.Z
)
end
function Relay:__Goal()
local Level: number? = self.LevelingModule.Leveling[self.ElevatorAttributes.Relay.Goal.Value]
self.ElevatorAttributes.Relay.GoalYLevel.Value = Level or self.LevelingModule.Leveling[1] :: number
end
function Relay:BulkConnect()
self.__ReadyConnection = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then
self:__Ready()
end
end)
self.__OpenConnection = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then
self:__Open()
end
end)
self.__GoalYLevelConnection = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then
self:__GoalYLevel()
end
end)
self.__GoalConnection = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then
self:__Goal()
end
end)
end
function Relay:BulkDisconnect()
if self.__ReadyConnection and self.__ReadyConnection.Connected then
self.__ReadyConnection:Disconnect()
end
if self.__OpenConnection and self.__OpenConnection.Connected then
self.__OpenConnection:Disconnect()
end
if self.__GoalYLevelConnection and self.__GoalYLevelConnection.Connected then
self.__GoalYLevelConnection:Disconnect()
end
if self.__GoalConnection and self.__GoalConnection.Connected then
self.__GoalConnection:Disconnect()
end
end
return Relay

View File

@@ -32,10 +32,11 @@ local ButtonTags = require(TagsDir:WaitForChild("Buttons"))
local Enums = require(Storage:WaitForChild("Enums"))
local SoundEnums = require(EnumsDir:WaitForChild("Sounds"))
local Leveling = require(script:WaitForChild("Leveling"))
local LevelingModule = require(script:WaitForChild("Leveling"))
local Doors = require(script:WaitForChild("Doors"))
local MovingObjects = require(script:WaitForChild("MovingObjects"))
local RelayAlgorithm = require(script:WaitForChild("RelayAlgorithm"))
local Relay = require(script:WaitForChild("Relay"))
local HallDisplays = require(Elevators:WaitForChild("HallDisplays"))
local ElevatorMover = require(Elevators:WaitForChild("Mover"))
@@ -123,6 +124,7 @@ type Constructor_Return_Props = {
HallDisplays: {Instance},
ButtonsConstructor: Buttons.ButtonsConstructor,
RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor,
RelayConstructor: Relay.RelayConstructor,
__MovingConnection: RBXScriptConnection?,
}
@@ -233,7 +235,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.Relay.Goal.Value), Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]])
self:__MoveTo(ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, Attributes.Relay.Goal.Value), LevelingModule.Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]])
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
end
end)
@@ -251,50 +253,15 @@ end
local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: ButtonTags.ButtonsTagsConstructor)
for ButtonNameType, ButtonList in ButtonsTagsConstructor.Buttons do
for ButtonID, ButtonTree in ButtonList do
if ButtonTree.Prompt then
if ButtonTree.Inst then
if ButtonTree.Attachment then
if ButtonTree.Prompt and ButtonTree.Inst and ButtonTree.Attachment then
HookButtons(self, ButtonNameType :: Enums.ButtonTreeValues, ButtonID, ButtonTree :: Tags.ButtonPropertiesSafe)
else
warn(`{ButtonTree} is missing the field "Attachment"`)
end
else
warn(`{ButtonTree} is missing the field "Inst"`)
end
else
warn(`{ButtonTree} is missing the field "Prompt"`)
warn(`{ButtonTree} is missing a field, {ButtonTree}`)
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)
local self = {} :: Constructor_Return_Props
@@ -322,7 +289,7 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
} :: MovingObjects.InstanceTree)
self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays)
self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor)
self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, Leveling)
self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling)
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors)
@@ -337,7 +304,10 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.BoxAlignPosition,
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity)
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes)
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes.Relay)
self.RelayConstructor = Relay.constructor(self.RelayAlgorithmConstructor, Attributes, Doors.Attributes, LevelingModule, self.BoxAlignPosition, self.ElevatorBox_1960)
self.RelayConstructor:BulkConnect()
local ClassConstructor = setmetatable(self, Elevator)
IterateButtons(ClassConstructor, ButtonsTagsConstructor)
@@ -349,10 +319,10 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
--Some hacks
Doors.Attributes.Open.Value = true
Doors.Attributes.Relay.Open.Value = true
end)
ElevatorReactionEvents(self)
print(LevelingModule.LevelingBetween)
print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor
@@ -371,7 +341,7 @@ function Elevator:Leveled(RequestedLevel)
if self.RelayAlgorithmConstructor:Check(ElevatorGoingUp) then
--More floors in the queue
self:__MoveTo(ElevatorGoingUp, Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]])
self:__MoveTo(ElevatorGoingUp, LevelingModule.Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]])
end
end
@@ -381,7 +351,7 @@ function Elevator:Leveling(RequestedLevel)
end
function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY>=Leveling[Attributes.CurrentFloor.Value+1] then
if ElevatorPositionY>=LevelingModule.Leveling[Attributes.CurrentFloor.Value+1] then
Attributes.CurrentFloor.Value+=1
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -390,7 +360,7 @@ function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
end
function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY<=Leveling[Attributes.CurrentFloor.Value-1] then
if ElevatorPositionY<=LevelingModule.Leveling[Attributes.CurrentFloor.Value-1] then
Attributes.CurrentFloor.Value-=1
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -460,7 +430,7 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
end
function Elevator:__MoveTo(GoingUp, GoalFloor_Y)
if Doors.Attributes.Open.Value then
if Doors.Attributes.Relay.Open.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end
if GoingUp then
@@ -473,7 +443,7 @@ function Elevator:__MoveTo(GoingUp, GoalFloor_Y)
end
function Elevator:RequestLevel(RequestedLevel)
local GoalFloor_Y: number? = Leveling[RequestedLevel]
local GoalFloor_Y: number? = LevelingModule.Leveling[RequestedLevel]
if GoalFloor_Y and RequestedLevel ~= Attributes.CurrentFloor.Value then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)