work on door opening possibility while the elevator is stopped

This commit is contained in:
2024-05-25 01:06:48 -04:00
parent 61153b53e1
commit c9f0074217
7 changed files with 119 additions and 75 deletions

View File

@@ -35,11 +35,7 @@ type Impl_Static_Props = {
ElevatorDoorStyle: Enum.EasingStyle, ElevatorDoorStyle: Enum.EasingStyle,
__DontLeakMemory: RBXScriptConnection?, __DontLeakMemory: RBXScriptConnection?,
Attributes: { Attributes: {} & RelayAttributes
Relay: {
Open: BoolValue
}
}
} }
type Constructor_Return_Props = { type Constructor_Return_Props = {
FloorDoorsTags: Tags.LandingTags, FloorDoorsTags: Tags.LandingTags,
@@ -57,6 +53,12 @@ type FloorDoors = {
[Floor]: {Vector3}? [Floor]: {Vector3}?
} }
export type RelayAttributes = {
Relay: {
Open: BoolValue
}
}
export type DoorConstructor = ClassConstructor export type DoorConstructor = ClassConstructor
local Doors = {} :: Impl_Constructor local Doors = {} :: Impl_Constructor

View File

@@ -15,10 +15,15 @@ local Leveling: {number} = {
[10] = 239.245, [10] = 239.245,
} }
--Mainly used for the lanterns
local LevelingBetween: {number} = {} local LevelingBetween: {number} = {}
--Calculate between leveling --Calculate between leveling
for n: number = 1, #Leveling, 2 do for n: number = 1, #Leveling do
LevelingBetween[n] = (Leveling[n]+Leveling[n+1])/2 local FloorAhead: number? = Leveling[n+1]
if FloorAhead then
LevelingBetween[n] = (Leveling[n]+FloorAhead)/2
end
end end
return { return {

View File

@@ -3,6 +3,7 @@
--!strict --!strict
local RelayAlgorithmModule = require(script.Parent:WaitForChild("RelayAlgorithm")) local RelayAlgorithmModule = require(script.Parent:WaitForChild("RelayAlgorithm"))
local DoorsModule = require(script.Parent:WaitForChild("Doors"))
type Leveling = { type Leveling = {
Leveling: {number}, Leveling: {number},
@@ -34,10 +35,12 @@ type Constructor_Return_Props = {
BoxAlignPosition: BoxAlignPosition, BoxAlignPosition: BoxAlignPosition,
ElevatorBox: ElevatorBox, ElevatorBox: ElevatorBox,
__ReadyConnection: RBXScriptConnection?, __Connections: {
__OpenConnection: RBXScriptConnection?, Ready: RBXScriptConnection?,
__GoalYLevelConnection: RBXScriptConnection?, Open: RBXScriptConnection?,
__GoalConnection: RBXScriptConnection?, GoalYLevel: RBXScriptConnection?,
Goal: RBXScriptConnection?
}
} }
type ElevatorAttributes = { type ElevatorAttributes = {
@@ -50,11 +53,7 @@ type ElevatorAttributes = {
Stopped: BoolValue Stopped: BoolValue
} }
type DoorAttributes = { type DoorAttributes = DoorsModule.RelayAttributes
Relay: {
Open: BoolValue
}
}
export type RelayConstructor = ClassConstructor export type RelayConstructor = ClassConstructor
@@ -68,7 +67,9 @@ function Relay.constructor(RelayAlgorthm, ElevatorAttributes, DoorAttributes, Le
DoorAttributes = DoorAttributes, DoorAttributes = DoorAttributes,
LevelingModule = LevelingModule, LevelingModule = LevelingModule,
BoxAlignPosition = BoxAlignPosition, BoxAlignPosition = BoxAlignPosition,
ElevatorBox = ElevatorBox ElevatorBox = ElevatorBox,
__Connections = {}
}, Relay) }, Relay)
end end
@@ -101,25 +102,25 @@ function Relay:__Goal()
end end
function Relay:BulkConnect() function Relay:BulkConnect()
self.__ReadyConnection = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Ready = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Ready() self:__Ready()
end end
end) end)
self.__OpenConnection = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Open = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Open() self:__Open()
end end
end) end)
self.__GoalYLevelConnection = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.GoalYLevel = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__GoalYLevel() self:__GoalYLevel()
end end
end) end)
self.__GoalConnection = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function() self.__Connections.Goal = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
if not self.ElevatorAttributes.Stopped.Value then if not self.ElevatorAttributes.Stopped.Value then
self:__Goal() self:__Goal()
end end
@@ -127,17 +128,17 @@ function Relay:BulkConnect()
end end
function Relay:BulkDisconnect() function Relay:BulkDisconnect()
if self.__ReadyConnection and self.__ReadyConnection.Connected then if self.__Connections.Ready and self.__Connections.Ready.Connected then
self.__ReadyConnection:Disconnect() self.__Connections.Ready:Disconnect()
end end
if self.__OpenConnection and self.__OpenConnection.Connected then if self.__Connections.Open and self.__Connections.Open.Connected then
self.__OpenConnection:Disconnect() self.__Connections.Open:Disconnect()
end end
if self.__GoalYLevelConnection and self.__GoalYLevelConnection.Connected then if self.__Connections.GoalYLevel and self.__Connections.GoalYLevel.Connected then
self.__GoalYLevelConnection:Disconnect() self.__Connections.GoalYLevel:Disconnect()
end end
if self.__GoalConnection and self.__GoalConnection.Connected then if self.__Connections.Goal and self.__Connections.Goal.Connected then
self.__GoalConnection:Disconnect() self.__Connections.Goal:Disconnect()
end end
end end

View File

@@ -28,8 +28,10 @@ type ElevatorAttributes = {
} }
type DoorAttributes = { type DoorAttributes = {
Relay: {
Open: BoolValue Open: BoolValue
} }
}
export type RelayAlgorithmConstructor = ClassConstructor export type RelayAlgorithmConstructor = ClassConstructor
@@ -78,7 +80,7 @@ end
function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel) function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel)
self:RawInsert(ElevatorGoingUp, RequestedLevel) self:RawInsert(ElevatorGoingUp, RequestedLevel)
return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Open.Value return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Relay.Open.Value
end end
return RelayAlgorithm return RelayAlgorithm

View File

@@ -179,7 +179,18 @@ 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
local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean
return -(Floor-RequestedFloor)>0 --(Floor-RequestedFloor)>0
return -Floor+RequestedFloor>0 --Simplified equation for less computation
end
local function OpenElevatorDoorsSafe(self: ClassConstructor)
print("Relay.Open=",Doors.Attributes.Relay.Open.Value,"Relay.Ready=",Attributes.Relay.Ready.Value)
if not Doors.Attributes.Relay.Open.Value and not Attributes.Relay.Ready.Value then
task.spawn(function()
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
end)
end
end end
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe) local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
@@ -193,6 +204,10 @@ local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number
if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.Relay.Goal.Value == ButtonFloor then if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.Relay.Goal.Value == ButtonFloor then
FloorTracker:Disconnect() FloorTracker:Disconnect()
--If the elevator is fully stopped at the floor and the hall button or the same cab button for the same floor is pressed,
--then open the doors
OpenElevatorDoorsSafe(self)
self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?) self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?)
ButtonTree.Prompt.Enabled = true ButtonTree.Prompt.Enabled = true
end end
@@ -262,6 +277,20 @@ local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: Bu
end end
end end
local function ElevatorInit(self: ClassConstructor)
task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
--Open the elevator doors on server start
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
--Some hacks
Doors.Attributes.Relay.Open.Value = true
self:RequestLevel(1)
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
@@ -302,29 +331,31 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.BoxAttachment, self.BoxAttachment,
self.BoxAlignPosition, self.BoxAlignPosition,
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity) self.BoxAlignOrientation = ElevatorMover(
self.ElevatorBox_1960,
Vector3.new(self.ElevatorBox_1960.Position.X, LevelingModule.Leveling[1], self.ElevatorBox_1960.Position.Z),
Elevator.Responsiveness,
Elevator.MaxVelocity
)
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes.Relay) self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes)
self.RelayConstructor = Relay.constructor(self.RelayAlgorithmConstructor, Attributes, Doors.Attributes, LevelingModule, self.BoxAlignPosition, self.ElevatorBox_1960) self.RelayConstructor = Relay.constructor(
self.RelayAlgorithmConstructor,
Attributes,
Doors.Attributes,
LevelingModule,
self.BoxAlignPosition,
self.ElevatorBox_1960
)
self.RelayConstructor:BulkConnect() self.RelayConstructor:BulkConnect()
local ClassConstructor = setmetatable(self, Elevator) local ClassConstructor = setmetatable(self, Elevator)
IterateButtons(ClassConstructor, ButtonsTagsConstructor) IterateButtons(ClassConstructor, ButtonsTagsConstructor)
--Open the elevator doors on server start ElevatorInit(self)
task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
--Some hacks
Doors.Attributes.Relay.Open.Value = true
end)
print(LevelingModule.LevelingBetween)
print(`🔝 {Elevator.Name} initialized and ready`) print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor return ClassConstructor
end end
@@ -332,7 +363,7 @@ function Elevator:Leveled(RequestedLevel)
(self.__MovingConnection :: RBXScriptConnection):Disconnect() (self.__MovingConnection :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel Attributes.CurrentFloor.Value = RequestedLevel
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity self.BoxAlignPosition.MaxVelocity = 0
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Elevator.QueueWaitTime) task.wait(Elevator.QueueWaitTime)
@@ -351,7 +382,9 @@ function Elevator:Leveling(RequestedLevel)
end end
function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel) function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY>=LevelingModule.Leveling[Attributes.CurrentFloor.Value+1] then local NextLevelBetweenFloors: number? = LevelingModule.LevelingBetween[Attributes.CurrentFloor.Value+1]
if NextLevelBetweenFloors and ElevatorPositionY>=NextLevelBetweenFloors then
Attributes.CurrentFloor.Value+=1 Attributes.CurrentFloor.Value+=1
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -360,7 +393,7 @@ function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
end end
function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel) function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY<=LevelingModule.Leveling[Attributes.CurrentFloor.Value-1] then if ElevatorPositionY<=LevelingModule.LevelingBetween[Attributes.CurrentFloor.Value-1] then
Attributes.CurrentFloor.Value-=1 Attributes.CurrentFloor.Value-=1
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -374,7 +407,9 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
if self.__MovingConnection and self.__MovingConnection.Connected then if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect() self.__MovingConnection:Disconnect()
end end
self.MOConstructor:UpdateCFrame() self.MOConstructor:UpdateCFrame()
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
local Delta = 0 local Delta = 0
local DoorsOpeningEvent = false local DoorsOpeningEvent = false
@@ -445,7 +480,7 @@ end
function Elevator:RequestLevel(RequestedLevel) function Elevator:RequestLevel(RequestedLevel)
local GoalFloor_Y: number? = LevelingModule.Leveling[RequestedLevel] local GoalFloor_Y: number? = LevelingModule.Leveling[RequestedLevel]
if GoalFloor_Y and RequestedLevel ~= Attributes.CurrentFloor.Value then if GoalFloor_Y --[[and RequestedLevel ~= 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) local Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel)
@@ -456,7 +491,6 @@ function Elevator:RequestLevel(RequestedLevel)
warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`) warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)
return false return false
end end
return true return true
end end

View File

@@ -135,8 +135,8 @@ function Lights:Init()
LightProperties.Prompt.ActionText = "Toggle On" LightProperties.Prompt.ActionText = "Toggle On"
end end
local ActivatedMaterial = table.find(EnumMaterialsNames, LightProperties.ActivatedMaterial) :: number local ActivatedMaterial = table.find(EnumMaterialsNames, LightProperties.ActivatedMaterial)
local DeactivatedMaterial = table.find(EnumMaterialsNames, LightProperties.DeactivatedMaterial) :: number local DeactivatedMaterial = table.find(EnumMaterialsNames, LightProperties.DeactivatedMaterial)
if not ActivatedMaterial then if not ActivatedMaterial then
ActivatedMaterial = 1 ActivatedMaterial = 1
@@ -147,8 +147,8 @@ function Lights:Init()
warn() warn()
end end
LightProperties.ActivatedMaterial = EnumMaterialsNames[ActivatedMaterial] LightProperties.ActivatedMaterial = EnumMaterialsNames[ActivatedMaterial :: number]
LightProperties.DeactivatedMaterial = EnumMaterialsNames[DeactivatedMaterial] LightProperties.DeactivatedMaterial = EnumMaterialsNames[DeactivatedMaterial :: number]
ToggleSwitch(EnabledState, LightProperties :: LightPropertiesSafe, false) ToggleSwitch(EnabledState, LightProperties :: LightPropertiesSafe, false)