Built with pure steam coming out of my brain, have to heal brain power after a long break of programming

This commit is contained in:
2024-07-20 16:10:05 -04:00
parent a56e5c7052
commit 96db25fcce
3 changed files with 65 additions and 85 deletions

View File

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

View File

@@ -81,11 +81,13 @@ type Impl_Static_Props = {
LanternDisplayOff: Color3, LanternDisplayOff: Color3,
}, },
Attributes: { Attributes: {
CurrentFloor: IntValue, PreviousFloor: IntValue,
Moving: BoolValue, CurrentFloor: IntValue,
GoingUp: BoolValue, NextFloor: IntValue,
Stopped: BoolValue, Moving: BoolValue,
Goal: IntValue GoingUp: BoolValue,
Stopped: BoolValue,
Goal: IntValue
}, },
Events: { Events: {
CabProgression: BindableEvent, CabProgression: BindableEvent,
@@ -97,9 +99,9 @@ type Impl_Static_Props = {
} }
type Constructor_Fun = ( type Constructor_Fun = (
TagsConstructor: TagsConstructor, TagsConstructor: TagsConstructor,
ButtonsTags: Tags.ElevatorButtons, ButtonsTags: Tags.ElevatorButtons,
LanternsTags: Tags.Lanterns, LanternsTags: Tags.Lanterns,
LandingDoors: Tags.LandingTags LandingDoors: Tags.LandingTags
) -> ClassConstructor ) -> ClassConstructor
@@ -142,7 +144,7 @@ Elevator.Name = Enums.Elevator.Otis1960
Elevator.FloorLevelingDistance = 4 Elevator.FloorLevelingDistance = 4
Elevator.FloorLeveling3PhaseDistance = 1.5 Elevator.FloorLeveling3PhaseDistance = 1.5
Elevator.ParkedDistance = 0.2 Elevator.ParkedDistance = 0.2
Elevator.Responsiveness = 10 Elevator.Responsiveness = 20
Elevator.MaxVelocity = 7 Elevator.MaxVelocity = 7
Elevator.LevelingVelocity = 2 Elevator.LevelingVelocity = 2
Elevator.Phase3LevelingVelocity = .5 Elevator.Phase3LevelingVelocity = .5
@@ -168,14 +170,17 @@ Elevator.Events = {
} }
Elevator.Attributes = { Elevator.Attributes = {
CurrentFloor = Instance.new("IntValue") :: IntValue, PreviousFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue, CurrentFloor = Instance.new("IntValue") :: IntValue,
GoingUp = Instance.new("BoolValue") :: BoolValue, NextFloor = Instance.new("IntValue") :: IntValue,
Stopped = Instance.new("BoolValue") :: BoolValue, Moving = Instance.new("BoolValue") :: BoolValue,
Goal = Instance.new("IntValue") :: IntValue, GoingUp = Instance.new("BoolValue") :: BoolValue,
Stopped = Instance.new("BoolValue") :: BoolValue,
Goal = Instance.new("IntValue") :: IntValue,
} }
Elevator.Attributes.CurrentFloor.Value = 1 Elevator.Attributes.CurrentFloor.Value = 1
Elevator.Attributes.NextFloor.Value = Elevator.Attributes.CurrentFloor.Value+1
Elevator.Attributes.Moving.Value = false Elevator.Attributes.Moving.Value = false
Elevator.Attributes.GoingUp.Value = false Elevator.Attributes.GoingUp.Value = false
Elevator.Attributes.Goal.Value = 1 Elevator.Attributes.Goal.Value = 1
@@ -189,14 +194,6 @@ local function ElevatorGoingUpDirection(CurrentFloor: number, RequestedFloor: nu
return CurrentFloor<RequestedFloor return CurrentFloor<RequestedFloor
end end
local function elevatorwarn<T...>(...: T...)
warn("🛗 ["..Elevator.Name.."]:", ...)
end
local function elevatorprint<T...>(...: T...)
print("🛗 ["..Elevator.Name.."]:", ...)
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
@@ -217,20 +214,20 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.MachineRoom.PiePlatePulley = TagsConstructor:Request("Otis1960_PieplatePulley") :: UnionOperation self.MachineRoom.PiePlatePulley = TagsConstructor:Request("Otis1960_PieplatePulley") :: UnionOperation
self.MachineRoom.PiePlatePlates = TagsConstructor:Request("Otis1960_PiePlatePlates") :: UnionOperation self.MachineRoom.PiePlatePlates = TagsConstructor:Request("Otis1960_PiePlatePlates") :: UnionOperation
self.MachineRoom.PiePlateSelector = TagsConstructor:Request("Otis1960_PiePlateSelector") :: UnionOperation self.MachineRoom.PiePlateSelector = TagsConstructor:Request("Otis1960_PiePlateSelector") :: UnionOperation
local LanternDisplay = TagsConstructor:Request("Otis1960_LanternDisplayMain") :: UnionOperation local LanternDisplay = TagsConstructor:Request("Otis1960_LanternDisplayMain") :: UnionOperation
self.MOConstructor = MovingObjects.constructor({MachineRoom = self.MachineRoom} :: MovingObjects.InstanceTree) self.MOConstructor = MovingObjects.constructor({MachineRoom = self.MachineRoom} :: MovingObjects.InstanceTree)
--Start the hall displays --Start the hall displays
self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays) self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays)
--Init the doors --Init the doors
self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor) self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor)
--Init the ropes --Init the ropes
self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling) self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, LevelingModule.Leveling)
--Init the lanterns --Init the lanterns
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors) self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Elevator.Sounds, Elevator.Colors)
@@ -240,11 +237,11 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.ButtonsConstructor = Buttons.constructor(Elevator.Attributes, Elevator.Colors) self.ButtonsConstructor = Buttons.constructor(Elevator.Attributes, Elevator.Colors)
self.HallDisplaysConstructor:BindHallDisplays() self.HallDisplaysConstructor:BindHallDisplays()
self.BoxAttachment, self.BoxAttachment,
self.BoxAlignPosition, self.BoxAlignPosition,
self.BoxAlignOrientation = ElevatorMover( self.BoxAlignOrientation = ElevatorMover(
self.ElevatorBox_1960, self.ElevatorBox_1960,
Vector3.new(self.ElevatorBox_1960.Position.X, LevelingModule.Leveling[Attributes.Goal.Value] or LevelingModule.Leveling[1], self.ElevatorBox_1960.Position.Z), Vector3.new(self.ElevatorBox_1960.Position.X, LevelingModule.Leveling[Attributes.Goal.Value] or LevelingModule.Leveling[1], self.ElevatorBox_1960.Position.Z),
Elevator.Responsiveness, Elevator.Responsiveness,
Elevator.MaxVelocity Elevator.MaxVelocity
@@ -266,49 +263,50 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
--self.RelayConstructor:BulkConnect() --self.RelayConstructor:BulkConnect()
local ClassConstructor = setmetatable(self, Elevator) local ClassConstructor = setmetatable(self, Elevator)
elevatorprint("Initialized and ready") print(`🛗 [{Elevator.Name}]: Initialized and ready`)
return ClassConstructor return ClassConstructor
end end
local function CheckFloorQueue(self: ClassConstructor) local function CheckFloorQueue(self: ClassConstructor)
if self.RelayAlgorithmConstructor.__FloorQueue[1] ~= Attributes.CurrentFloor.Value then if self.RelayAlgorithmConstructor.__FloorQueue[1] ~= Attributes.CurrentFloor.Value then
elevatorwarn("The floor queue first index did not match the elevator's current floor, CurrentFloor=", Attributes.CurrentFloor.Value, "FloorQueue[1]=", self.RelayAlgorithmConstructor.__FloorQueue[1]) warn("The floor queue first index did not match the elevator's current floor, CurrentFloor=", Attributes.CurrentFloor.Value, "FloorQueue[1]=", self.RelayAlgorithmConstructor.__FloorQueue[1])
end end
table.remove(self.RelayAlgorithmConstructor.__FloorQueue, 1) table.remove(self.RelayAlgorithmConstructor.__FloorQueue, 1)
local NextFloor = self.RelayAlgorithmConstructor.__FloorQueue[1] local NextFloorInQueue = self.RelayAlgorithmConstructor.__FloorQueue[1]
if NextFloor then if NextFloorInQueue then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, NextFloor) local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, NextFloorInQueue)
local LevelVec3 = LevelingModule.Leveling[NextFloor] local LevelVec3 = LevelingModule.Leveling[NextFloorInQueue]
self:__TravelToFloor(NextFloor, LevelVec3, ElevatorGoingUp) self:__TravelToFloor(NextFloorInQueue, LevelVec3, ElevatorGoingUp)
end end
end end
local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3: number, ElevatorGoingUp: boolean) local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt: number, LevelVec3: number, ElevatorGoingUp: boolean)
local Elevator_Position = self.ElevatorBox_1960.Position local ElevatorPosition = self.ElevatorBox_1960.Position
local Between_Levels = LevelingModule.LevelingBetween[Attributes.CurrentFloor.Value] local AtFloorY = LevelingModule.Leveling[ElevatorGoingUp and Attributes.CurrentFloor.Value+1 or Attributes.CurrentFloor.Value-1]
if ElevatorGoingUp then if ElevatorGoingUp then
--Detecting between the floors --Detecting between the floors
if Elevator_Position.Y>=Between_Levels then if ElevatorPosition.Y>=AtFloorY-Elevator.FloorLevelingDistance then
local PreviousFloor = Attributes.CurrentFloor.Value Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value
Attributes.CurrentFloor.Value+=1 Attributes.CurrentFloor.Value+=1
Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value+1, #LevelingModule.Leveling)
Events.CabProgression:Fire(PreviousFloor, Attributes.CurrentFloor.Value) Events.CabProgression:Fire(Attributes.PreviousFloor.Value, Attributes.CurrentFloor.Value, Attributes.NextFloor.Value)
end end
--Elevator is riding upwards towards the destination --Elevator is riding upwards towards the destination
if Elevator_Position.Y>=LevelVec3-Elevator.FloorLevelingDistance then if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLevelingDistance then
Events.Leveling:Fire() Events.Leveling:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity
if Elevator_Position.Y>=LevelVec3-Elevator.FloorLeveling3PhaseDistance then if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLeveling3PhaseDistance then
Events.Leveling3Phase:Fire() Events.Leveling3Phase:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity
if Elevator_Position.Y>=LevelVec3-Elevator.ParkedDistance then if ElevatorPosition.Y>=LevelVec3-Elevator.ParkedDistance then
Events.Parked:Fire() Events.Parked:Fire()
CheckFloorQueue(self); CheckFloorQueue(self);
@@ -317,35 +315,34 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3
end end
end end
else else
--Detecting between the floors if ElevatorPosition.Y<=AtFloorY+Elevator.FloorLevelingDistance then
--leaving floor ElevatorPosition Y=62, Between_levels=75 Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value
if Elevator_Position.Y<=Between_Levels then
local PreviousFloor = Attributes.CurrentFloor.Value
Attributes.CurrentFloor.Value-=1 Attributes.CurrentFloor.Value-=1
Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value-1, #LevelingModule.Leveling)
Events.CabProgression:Fire(PreviousFloor, Attributes.CurrentFloor.Value) Events.CabProgression:Fire(Attributes.PreviousFloor.Value, Attributes.CurrentFloor.Value, Attributes.NextFloor.Value)
end end
--Elevator is riding upwards towards the destination --Elevator is riding upwards towards the destination
if Elevator_Position.Y<=LevelVec3-Elevator.FloorLevelingDistance then if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLevelingDistance then
Events.Leveling:Fire() Events.Leveling:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity
if Elevator_Position.Y<=LevelVec3-Elevator.FloorLeveling3PhaseDistance then if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLeveling3PhaseDistance then
Events.Leveling3Phase:Fire() Events.Leveling3Phase:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity
if Elevator_Position.Y<=LevelVec3-Elevator.ParkedDistance then if ElevatorPosition.Y<=LevelVec3+Elevator.ParkedDistance then
Events.Parked:Fire() Events.Parked:Fire()
CheckFloorQueue(self); CheckFloorQueue(self);
(self.__Connections.Moving :: RBXScriptConnection):Disconnect() (self.__Connections.Moving :: RBXScriptConnection):Disconnect()
end end
end end
end end
end end
Events.CabTraveling:Fire(Elevator_Position, Attributes.CurrentFloor.Value) Events.CabTraveling:Fire(deltaTime, ElevatorPosition)
end end
function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp) function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp)
@@ -357,7 +354,7 @@ function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp)
Attributes.GoingUp.Value = ElevatorGoingUp Attributes.GoingUp.Value = ElevatorGoingUp
self.__Connections.Moving = RS.Heartbeat:Connect(function(deltaTime) self.__Connections.Moving = RS.Heartbeat:Connect(function(deltaTime)
CabTraveling(self, deltaTime, LevelVec3, ElevatorGoingUp) CabTraveling(self, deltaTime, LevelInt, LevelVec3, ElevatorGoingUp)
end) end)
--Set the elevator's AlignPosition to the floor Y vector --Set the elevator's AlignPosition to the floor Y vector
@@ -372,7 +369,7 @@ function Elevator:RequestLevel(RequestedLevel)
if LevelVec3 then if LevelVec3 then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel) self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel)
if #self.RelayAlgorithmConstructor.__FloorQueue == 1 then if #self.RelayAlgorithmConstructor.__FloorQueue == 1 then
self:__TravelToFloor(RequestedLevel, LevelVec3, ElevatorGoingUp) self:__TravelToFloor(RequestedLevel, LevelVec3, ElevatorGoingUp)
end end
@@ -380,8 +377,8 @@ function Elevator:RequestLevel(RequestedLevel)
return true return true
end end
elevatorwarn(`Requested floor: "{RequestedLevel}" does not exist for this elevator`) warn(`Requested floor: "{RequestedLevel}" does not exist for this elevator`)
return false return false
end end
return Elevator return Elevator

View File

@@ -55,19 +55,14 @@ local Otis1960Buttons = Buttons[Enums.Elevator.Otis1960]
local Otis1960LandingDoors = LandingDoors[Enums.Elevator.Otis1960] local Otis1960LandingDoors = LandingDoors[Enums.Elevator.Otis1960]
local Otis1960 = Otis1960_Module.constructor(TagsConstructor, Otis1960Buttons, Otis1960Lanterns, Otis1960LandingDoors) local Otis1960 = Otis1960_Module.constructor(TagsConstructor, Otis1960Buttons, Otis1960Lanterns, Otis1960LandingDoors)
task.wait(5)
Otis1960:RequestLevel(3)
Otis1960_Module.Events.Parked.Event:Connect(function() Otis1960_Module.Events.Parked.Event:Connect(function()
print("Cab parked")
task.wait(3)
Otis1960:RequestLevel(1)
end) end)
Otis1960_Module.Events.CabProgression.Event:Connect(function(previousFloor: number?, nextFloor: number) Otis1960_Module.Events.CabProgression.Event:Connect(function(previousFloor: number, currentFloor: number, nextFloor: number)
--print("previousFloor=",previousFloor, "nextFloor=",nextFloor) print("previousFloor=",previousFloor, "currentFloor=",currentFloor, "nextFloor=",nextFloor)
end) end)
Otis1960_Module.Events.CabTraveling.Event:Connect(function(Elevator_Position: Vector3, CurrentFloor: number) Otis1960_Module.Events.CabTraveling.Event:Connect(function(deltaTime: number, Elevator_Position: Vector3)
print("CurrentFloor=",CurrentFloor)
end) end)