Name for Tag properties and work on special buttons

This commit is contained in:
2024-05-14 02:04:07 -04:00
parent 5018a8ef3b
commit 1e905fdee4
8 changed files with 253 additions and 163 deletions

View File

@@ -40,7 +40,7 @@ local HallDisplays = require(Elevators:WaitForChild("HallDisplays"))
local ElevatorMover = require(Elevators:WaitForChild("Mover"))
local TractionRopes = require(Elevators:WaitForChild("TractionRopes"))
local Lanterns = require(Elevators:WaitForChild("Lanterns"))
local Buttons = require(Elevators:WaitForChild("Buttons"))
local Buttons = require(Elevators:WaitForChild("ButtonsManager"))
local Tags = require(LoadDir:WaitForChild("Tags"))
@@ -79,7 +79,8 @@ type Impl_Static_Props = {
CurrentFloor: IntValue,
GoalFloor: IntValue,
Moving: BoolValue,
GoingUp: BoolValue
GoingUp: BoolValue,
Stopped: BoolValue
},
Events: {
ButtonActivated: BindableEvent
@@ -110,59 +111,59 @@ type Constructor_Return_Props = {
MachineRoom: MovingObjects.MachineRoom,
HallDisplays: {Instance},
ButtonsConstructor: Buttons.ButtonsConstructor,
FloorQueue: {number},
__MovingConnection: RBXScriptConnection?,
}
export type Otis1960Constructor = ClassConstructor
local Elevator = {} :: Impl_Constructor
Elevator.__index = Elevator
--TODO: Rename Otis1960 to Elevator
local Otis1960 = {} :: Impl_Constructor
Otis1960.__index = Otis1960
Elevator.Name = Enums.Elevator.Otis1960
Elevator.FloorLevelingDistance = 2.5
Elevator.DoorOpeningDistance = Elevator.FloorLevelingDistance/2.8
Elevator.LeveledDistance = 0.5
Elevator.Responsiveness = 50
Elevator.MaxVelocity = 10
Elevator.QueueWaitTime = 5
Otis1960.Name = Enums.Elevator.Otis1960
Otis1960.FloorLevelingDistance = 2.5
Otis1960.DoorOpeningDistance = Otis1960.FloorLevelingDistance/2.8
Otis1960.LeveledDistance = 0.5
Otis1960.Responsiveness = 50
Otis1960.MaxVelocity = 10
Otis1960.QueueWaitTime = 5
Otis1960.Sounds = {
Elevator.Sounds = {
LanternChimeDirection = SoundEnums.Otis1960.LanternChimeDirection,
LanternChimeLanding = SoundEnums.Otis1960.LanternChimeLanding
}
Otis1960.Colors = {
Elevator.Colors = {
ButtonActivated = Color3.fromRGB(180,0,0),
ButtonDeactivated = Color3.fromRGB(139,139,139),
LanternDisplayOn = Color3.fromRGB(255,114,71),
LanternDisplayOff = Color3.fromRGB(55,55,55),
}
Otis1960.Attributes = {
Elevator.Attributes = {
CurrentFloor = Instance.new("IntValue") :: IntValue,
GoalFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue,
GoingUp = Instance.new("BoolValue") :: BoolValue
GoingUp = Instance.new("BoolValue") :: BoolValue,
Stopped = Instance.new("BoolValue") :: BoolValue
}
Otis1960.Events = {
Elevator.Events = {
ButtonActivated = Instance.new("BindableEvent") :: BindableEvent
}
Otis1960.Attributes.CurrentFloor.Value = 1
Otis1960.Attributes.GoalFloor.Value = 1
Otis1960.Attributes.Moving.Value = false
Otis1960.Attributes.GoingUp.Value = false
Elevator.Attributes.CurrentFloor.Value = 1
Elevator.Attributes.GoalFloor.Value = 1
Elevator.Attributes.Moving.Value = false
Elevator.Attributes.GoingUp.Value = false
local Attributes = Otis1960.Attributes
local Attributes = Elevator.Attributes
--My clever math function for determining if the elevator goal is to move upwards or not
local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean
return -(Floor-RequestedFloor)>0
end
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonInst: BasePart)
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
ButtonTree.Prompt.Enabled = false
local Some = self:RequestLevelAsync(ButtonFloor)
if Some then
@@ -172,41 +173,64 @@ local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number
if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.GoalFloor.Value == ButtonFloor then
FloorTracker:Disconnect()
self.ButtonsConstructor:__DeactivateButton(ButtonInst, ButtonInst:FindFirstChild("Glass") :: BasePart?)
self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?)
ButtonTree.Prompt.Enabled = true
end
end)
else
warn(`Failed to call floor: {ButtonFloor}`)
end
end
local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonTreeValues, ButtonName: string, ButtonTree: Tags.ButtonProperties)
--Special cases inbound
local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonTreeValues, ButtonID: string, ButtonTree: Tags.ButtonPropertiesSafe)
if ButtonNameType == Enums.ButtonTree.Car then
self.ButtonsConstructor:CarButton(ButtonName, ButtonTree, function(ButtonFloor: number)
_ActivatedFloorButton(self, ButtonFloor, ButtonTree.Inst :: BasePart)
self.ButtonsConstructor:CarButton(ButtonID, ButtonTree, function(ButtonFloor: number)
_ActivatedFloorButton(self, ButtonFloor, ButtonTree)
end)
elseif ButtonNameType == Enums.ButtonTree.Landing then
self.ButtonsConstructor:LandingButton(ButtonName, ButtonTree, function(ButtonFloor: number)
_ActivatedFloorButton(self, ButtonFloor, ButtonTree.Inst :: BasePart)
self.ButtonsConstructor:LandingButton(ButtonID, ButtonTree, function(ButtonFloor: number)
_ActivatedFloorButton(self, ButtonFloor, ButtonTree)
end)
elseif ButtonNameType == Enums.ButtonTree.Special then
if Elevator.Name == Enums.Elevator.Otis1960 then
if ButtonTree.Name == Enums.SpecialButton.Stop then
ButtonTree.Attachment.Position-=Vector3.xAxis/10
ButtonTree.Prompt.HoldDuration = 1
end
end
self.ButtonsConstructor:SpecialButton(ButtonTree.Name :: Enums.SpecialButtonValues, ButtonID, ButtonTree, function<boolean>(Toggled: boolean)
Attributes.Stopped.Value = Toggled
if Toggled then
(ButtonTree.Inst :: BasePart).Position+=Vector3.new(0,0,.05)
else
(ButtonTree.Inst :: BasePart).Position-=Vector3.new(0,0,.05)
end
end)
elseif ButtonNameType == Enums.ButtonTree.Relays then
--Special case-y
if Otis1960.Name == Enums.Elevator.Otis1960 then
if Elevator.Name == Enums.Elevator.Otis1960 then
ButtonTree.Attachment.Position-=Vector3.zAxis/6
end
elseif ButtonNameType == Enums.ButtonTree.Unknown then
else
warn(`[{Otis1960.Name}]: Could not iterate a button, ButtonNameType={ButtonNameType}`)
warn(`[{Elevator.Name}]: Could not iterate a button, ButtonNameType={ButtonNameType}`)
end
end
local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: ButtonTags.ButtonsTagsConstructor)
for ButtonNameType, ButtonList in ButtonsTagsConstructor.Buttons do
for ButtonName, ButtonTree in ButtonList do
for ButtonID, ButtonTree in ButtonList do
if ButtonTree.Prompt then
if ButtonTree.Inst then
HookButtons(self, ButtonNameType :: Enums.ButtonTreeValues, ButtonName, ButtonTree)
if 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
@@ -217,7 +241,7 @@ local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: Bu
end
end
function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
local self = {} :: Constructor_Return_Props
self.MachineRoom = {_CFrame = {}} :: MovingObjects.MachineRoom
@@ -246,22 +270,24 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
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.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Otis1960.Sounds, Otis1960.Colors)
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors)
local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags)
local Otis1960_Buttons = ButtonsTagsConstructor:CreatePromptButtons()
local Otis1960_Buttons = ButtonsTagsConstructor:CreatePromptButtons()
self.ButtonsConstructor = Buttons.constructor(Otis1960.Attributes, Otis1960.Events, Otis1960.Colors)
self.ButtonsConstructor = Buttons.constructor(Elevator.Attributes, Elevator.Events, Elevator.Colors)
self.HallDisplaysConstructor:BindHallDisplays()
self.BoxAttachment,
self.BoxAlignPosition,
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Otis1960.Responsiveness, Otis1960.MaxVelocity)
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity)
local ClassConstructor = setmetatable(self, Otis1960)
local ClassConstructor = setmetatable(self, Elevator)
IterateButtons(ClassConstructor, ButtonsTagsConstructor)
self.FloorQueue = {}
--Open the elevator doors on server start
task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -269,19 +295,67 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, Attributes.CurrentFloor.Value)
end)
print(`🔝 {Otis1960.Name} initialized and ready`)
print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor
end
local FloorQueue: {number} = {}
--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
}
]]
local function SortQueue(self: ClassConstructor, ElevatorGoingUp: boolean)
table.sort(self.FloorQueue, function(a: number, b: number): boolean
if ElevatorGoingUp then
return a<b
else
return a>b
end
end)
print(table.unpack(self.FloorQueue))
end
local function CheckQueue(self: ClassConstructor)
table.remove(FloorQueue, 1)
if self.FloorQueue[1] == Attributes.CurrentFloor.Value then
table.remove(self.FloorQueue, 1)
end
if #self.FloorQueue ~= 0 then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, self.FloorQueue[1])
Attributes.GoingUp.Value = ElevatorGoingUp
if #FloorQueue ~= 0 then
--table.sort
Attributes.GoingUp.Value = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, FloorQueue[1])
self:__GoToFloor(Leveling[FloorQueue[1]], Attributes.GoingUp.Value)
SortQueue(self, ElevatorGoingUp)
self:__GoToFloor(Leveling[self.FloorQueue[1]], ElevatorGoingUp)
end
end
local function InsertFloorQueue(self: ClassConstructor, FirstIndex: boolean, RequestedLevel: number, ElevatorGoingUp: boolean)
table.insert(self.FloorQueue, FirstIndex and 1 or #self.FloorQueue+1, RequestedLevel)
SortQueue(self, ElevatorGoingUp)
end
local function ToFloorQueue(self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number, GoalLevelVEC: number)
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
if ElevatorGoingUp then
InsertFloorQueue(self, true, RequestedLevel, ElevatorGoingUp)
if not Doors.Attributes.DoorsOpen.Value then
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end
else
InsertFloorQueue(self, false, RequestedLevel, ElevatorGoingUp)
end
end
@@ -289,11 +363,11 @@ local function FloorLeveled(self: ClassConstructor, RequestedLevel: number)
(self.__MovingConnection :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel
self.BoxAlignPosition.MaxVelocity = Otis1960.MaxVelocity
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Otis1960.QueueWaitTime)
task.wait(Elevator.QueueWaitTime)
CheckQueue(self)
end
@@ -320,30 +394,17 @@ local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: numbe
end
end
function Otis1960:__GoToFloor(GoalLevelVEC, GoingUp)
if Doors.Attributes.DoorsOpen.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value)
end
local function ElevatorHeartbeat(self: ClassConstructor, GoingUp: boolean)
local Delta = 0
local DoorsOpeningEvent = false
if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect()
end
if GoingUp then
self.LanternsConstructor:DirectionUp(true)
else
self.LanternsConstructor:DirectionDown(true)
end
self.MOConstructor:UpdateCFrame()
local Delta = 0
local DoorsOpeningEvent = false
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
--Otis1960_ShaftGovernor
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt)
Delta+=1
local FloorGoal: number = FloorQueue[1]
local FloorGoal: number = self.FloorQueue[1]
Attributes.Moving.Value = true
Attributes.GoalFloor.Value = FloorGoal
@@ -360,80 +421,81 @@ function Otis1960:__GoToFloor(GoalLevelVEC, GoingUp)
if GoingUp then
FloorPassingUp(self, ElevatorPositionY, FloorGoal)
if ElevatorPositionY>=BoxAlignY-Otis1960.FloorLevelingDistance then
if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal)
end
end
if ElevatorPositionY>=BoxAlignY-Otis1960.LeveledDistance then
if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal)
end
else
FloorPassingDown(self, ElevatorPositionY, FloorGoal)
if ElevatorPositionY<=BoxAlignY+Otis1960.FloorLevelingDistance then
if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal)
end
end
if ElevatorPositionY<=BoxAlignY+Otis1960.LeveledDistance then
if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal)
end
end
end)
end
function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
if Doors.Attributes.DoorsOpen.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value)
end
if GoingUp then
self.LanternsConstructor:DirectionUp(true)
else
self.LanternsConstructor:DirectionDown(true)
end
self.MOConstructor:UpdateCFrame()
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
ElevatorHeartbeat(self, GoingUp)
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end
--This is a little wonky i can improve later
local function ToFloorQueue(self: ClassConstructor, InHeadingPath: boolean, RequestedLevel: number, GoalLevelVEC: number)
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
if InHeadingPath then
table.insert(FloorQueue, 1, RequestedLevel)
if not Doors.Attributes.DoorsOpen.Value then
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end
else
table.insert(FloorQueue, RequestedLevel)
end
end
function Otis1960:RequestLevelAsync(RequestedLevel)
function Elevator:RequestLevelAsync(RequestedLevel)
local GoalLevelVEC: number? = Leveling[RequestedLevel]
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then
local InHeadingPath = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
if Attributes.Moving.Value then
ToFloorQueue(self, InHeadingPath, RequestedLevel, GoalLevelVEC)
ToFloorQueue(self, ElevatorGoingUp, RequestedLevel, GoalLevelVEC)
else
if Doors.Attributes.DoorsOpen and #FloorQueue ~= 0 then
--Activated a call while the doors are closing? we need to wait until that is completed to move
repeat
Doors.Attributes.DoorsOpen:GetPropertyChangedSignal("Value"):Wait()
until not Doors.Attributes.DoorsOpen
end
Attributes.GoingUp.Value = InHeadingPath
Attributes.GoingUp.Value = ElevatorGoingUp
table.insert(FloorQueue, 1, RequestedLevel)
self:__GoToFloor(GoalLevelVEC, InHeadingPath)
if Doors.Attributes.DoorsOpen and #self.FloorQueue == 0 then
InsertFloorQueue(self, true, RequestedLevel, ElevatorGoingUp)
self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp)
else
InsertFloorQueue(self, false, RequestedLevel, ElevatorGoingUp)
end
end
else
warn(`[{Otis1960.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
end
return true
end
return Otis1960
return Elevator