Big elevator refactoring

This commit is contained in:
2024-05-11 00:03:21 -04:00
parent 17193ac908
commit 73045d5bf6
9 changed files with 272 additions and 142 deletions

View File

@@ -4,27 +4,30 @@
local Elevators = script.Parent
local MainDir = Elevators.Parent
local EnumsDir = MainDir:WaitForChild("Enums")
local LoadDir = MainDir:WaitForChild("Load")
local TagsDir = LoadDir:WaitForChild("Tags")
local Storage = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local Enums = require(Storage:WaitForChild("Enums"))
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 Doors = require(script:WaitForChild("Doors"))
local MovingObjects = require(script:WaitForChild("MovingObjects"))
local HallDisplays = require(script:WaitForChild("HallDisplays"))
local HallDisplays = require(Elevators:WaitForChild("HallDisplays"))
local ElevatorMover = require(Elevators:WaitForChild("Mover"))
local ButtonTags = require(Elevators:WaitForChild("Buttons"))
local TractionRopes = require(Elevators:WaitForChild("TractionRopes"))
local Lanterns = require(Elevators:WaitForChild("Lanterns"))
local Buttons = require(Elevators:WaitForChild("Buttons"))
local Tags = require(LoadDir:WaitForChild("Tags"))
type rbxassetid = string
type Tags = Tags.ExportedTags
type TagsConstructor = Tags.TagsConstructor
@@ -34,27 +37,31 @@ type Impl_Constructor = {
constructor: Constructor_Fun,
--Class functions
__MoveFloors: (self: ClassConstructor, Level: number, RequestedLevel: number, GoingUp: boolean) -> (),
GoToLevel: (self: ClassConstructor, RequestedLevel: number) -> ()
RequestLevel: (self: ClassConstructor, RequestedLevel: number) -> ()
} & Impl_Static_Props
type Impl_Static_Props = {
Name: Enums.ElevatorValues,
Responsiveness: number,
MaxVelocity: number,
ButtonActivatedColor: Color3,
ButtonDeactivatedColor: Color3,
LanternDisplayColorOn: Color3,
LanternDisplayColorOff: Color3,
LanternChimeDirection: rbxassetid,
LanternChimeLanding: rbxassetid,
FloorLevelingDistance: number,
DoorOpeningDistance: number,
LeveledDistance: number,
Name: Enums.ElevatorValues,
Responsiveness: number,
MaxVelocity: number,
FloorLevelingDistance: number,
DoorOpeningDistance: number,
LeveledDistance: number,
Sounds: {
LanternChimeDirection: SoundEnums.Otis1960LanternChimeDirection,
LanternChimeLanding: SoundEnums.Otis1960LanternChimeLanding,
},
Colors: {
ButtonActivated: Color3,
ButtonDeactivated: Color3,
LanternDisplayOn: Color3,
LanternDisplayOff: Color3,
},
Attributes: {
PassingFloor: IntValue,
Moving: BoolValue,
CurrentFloor: IntValue
CurrentFloor: IntValue,
Moving: BoolValue
},
Events: {
ButtonActivated: BindableEvent
@@ -92,23 +99,29 @@ export type Otis1960Constructor = ClassConstructor
local Otis1960 = {} :: Impl_Constructor
Otis1960.__index = Otis1960
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.ButtonActivatedColor = Color3.fromRGB(180,0,0)
Otis1960.ButtonDeactivatedColor = Color3.fromRGB(139,139,139)
Otis1960.LanternDisplayColorOn = Color3.fromRGB(255,114,71)
Otis1960.LanternDisplayColorOff = Color3.fromRGB(55,55,55)
Otis1960.LanternChimeDirection = "rbxassetid://16990287228"
Otis1960.LanternChimeLanding = "rbxassetid://16990290265"
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.Sounds = {
LanternChimeDirection = SoundEnums.Otis1960.LanternChimeDirection,
LanternChimeLanding = SoundEnums.Otis1960.LanternChimeLanding
}
Otis1960.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 = {
PassingFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue,
CurrentFloor = Instance.new("IntValue") :: IntValue
PassingFloor = Instance.new("IntValue") :: IntValue,
CurrentFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue
}
Otis1960.Events = {
@@ -121,18 +134,34 @@ Otis1960.Attributes.CurrentFloor.Value = 1
local Attributes = Otis1960.Attributes
local function HookButtons(self: ClassConstructor, ButtonsConstructor: ButtonTags.ButtonsConstructor, ButtonType: Enums.EnumValue)
local ButtonFunctions = require(script:WaitForChild("ButtonFunctions"))
local ButtonsConstructor = Buttons.constructor(Otis1960.Attributes, Otis1960.Events, Otis1960.Colors)
local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonTreeValues, ButtonName: string, ButtonTree)
if ButtonNameType == Enums.ButtonTree.Car then
ButtonsConstructor:CarButton(ButtonName, ButtonTree, function(Floor: number)
self:RequestLevel(Floor)
end)
elseif ButtonNameType == Enums.ButtonTree.Landing then
ButtonsConstructor:LandingButton(ButtonName, ButtonTree, function(Floor: number)
self:RequestLevel(Floor)
end)
elseif ButtonNameType == Enums.ButtonTree.Special then
elseif ButtonNameType == Enums.ButtonTree.Relays then
elseif ButtonNameType == Enums.ButtonTree.Unknown then
else
warn(`[{Otis1960.Name}]: Could not iterate a button, ButtonNameType={ButtonNameType}`)
end
end
local function IterateButtons(self: ClassConstructor, ButtonsConstructor: ButtonTags.ButtonsConstructor)
for ButtonNameType, ButtonList in ButtonsConstructor.Buttons do
for ButtonName, ButtonTree in ButtonList do
if ButtonTree.Prompt then
if ButtonTree.Inst then
local Button = ButtonFunctions[ButtonNameType :: Enums.ButtonTreeValues]
if Button then
Button(self, ButtonName, ButtonsConstructor, ButtonTree)
end
HookButtons(self, ButtonNameType :: Enums.ButtonTreeValues, ButtonName, ButtonTree)
else
warn(`{ButtonTree} is missing the field "Inst"`)
end
@@ -172,10 +201,7 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.ElevatorDoors = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor)
self.TractionRopes = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, Leveling)
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, Otis1960.LanternChimeDirection, Otis1960.LanternChimeLanding, LanternsTags, {
Active = Otis1960.LanternDisplayColorOn,
Off = Otis1960.LanternDisplayColorOff
} :: Lanterns.Colors)
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Otis1960.Sounds, Otis1960.Colors)
local ButtonsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags)
local Otis1960_Buttons = ButtonsConstructor:CreatePromptButtons()
@@ -187,7 +213,7 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Otis1960.Responsiveness, Otis1960.MaxVelocity)
local ClassConstructor = setmetatable(self, Otis1960)
HookButtons(ClassConstructor, ButtonsConstructor, Enums.ButtonTree.Car)
IterateButtons(ClassConstructor, ButtonsConstructor)
--Open the elevator doors on server start
task.spawn(function()
@@ -301,13 +327,33 @@ function Otis1960:__MoveFloors(GoalLevelVEC, RequestedLevel, GoingUp)
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end
function Otis1960:GoToLevel(RequestedLevel)
local FloorAdvancer = {}
--My clever math function for determining if the elevator goal is to move upwards or not
local function ElevatorDirectionGoingUp(Floor: number, RequestedFloor: number): boolean
return -(Floor-RequestedFloor)>0
end
function Otis1960:RequestLevel(RequestedLevel)
local GoalLevelVEC: number = Leveling[RequestedLevel]
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then
local GoingUp: boolean = -(Attributes.CurrentFloor.Value-RequestedLevel)>0 --My clever math function for determining if the elevator goal is to move upwards or not
self:__MoveFloors(GoalLevelVEC, RequestedLevel, GoingUp)
--My clever math function for determining if the elevator goal is to move upwards or not
local GoingUp = ElevatorDirectionGoingUp(Attributes.CurrentFloor.Value, RequestedLevel)
if Attributes.Moving.Value then
if GoingUp then
if Attributes.CurrentFloor.Value<=RequestedLevel then
self:__MoveFloors(GoalLevelVEC, RequestedLevel, GoingUp)
end
else
if Attributes.CurrentFloor.Value>=RequestedLevel then
self:__MoveFloors(GoalLevelVEC, RequestedLevel, GoingUp)
end
end
else
self:__MoveFloors(GoalLevelVEC, RequestedLevel, GoingUp)
end
else
warn(`[{Otis1960.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)
end