diff --git a/src/server/main/Elevators/Otis1960/init.luau b/src/server/main/Elevators/Otis1960/init.luau index efa2c4d..53c9f06 100644 --- a/src/server/main/Elevators/Otis1960/init.luau +++ b/src/server/main/Elevators/Otis1960/init.luau @@ -72,6 +72,8 @@ type Impl_Static_Props = {} type Constructor_Fun = (ElevatorConfigurationTable: ElevatorConfigurationTable) -> ClassConstructor type Constructor_Return_Props = { + ElevatorConfiguration: ElevatorConfigurationTable, + Attributes: { PreviousFloor: IntValue, CurrentFloor: IntValue, @@ -104,58 +106,71 @@ local function ElevatorGoingUpDirection(CurrentFloor: number, RequestedFloor: nu end function Elevator.constructor(ElevatorConfigurationTable: ElevatorConfigurationTable) - local self = {} :: Constructor_Return_Props + print(`🛗 [{ElevatorConfigurationTable.Name}]: Initialized and ready`) + return setmetatable({ + ElevatorConfiguration = ElevatorConfigurationTable, - - --self.RelayConstructor:BulkConnect() - - local ClassConstructor = setmetatable(self, Elevator) - - print(`🛗 [{Elevator.Name}]: Initialized and ready`) - return ClassConstructor + Attributes = { + PreviousFloor = Instance.new("IntValue") :: IntValue, + CurrentFloor = Instance.new("IntValue") :: IntValue, + NextFloor = Instance.new("IntValue") :: IntValue, + Moving = Instance.new("BoolValue") :: BoolValue, + GoingUp = Instance.new("BoolValue") :: BoolValue, + Stopped = Instance.new("BoolValue") :: BoolValue, + Goal = Instance.new("IntValue") :: IntValue + }, + Events = { + CabProgression = Instance.new("BindableEvent") :: BindableEvent, + CabTraveling = Instance.new("BindableEvent") :: BindableEvent, + Parked = Instance.new("BindableEvent") :: BindableEvent, + Leveling = Instance.new("BindableEvent") :: BindableEvent, + Leveling3Phase = Instance.new("BindableEvent") :: BindableEvent, + }, + __Connections = {} + }, Elevator) end local function CheckFloorQueue(self: ClassConstructor) - if self.RelayAlgorithmConstructor.__FloorQueue[1] ~= Attributes.CurrentFloor.Value then - warn("The floor queue first index did not match the elevator's current floor, CurrentFloor=", Attributes.CurrentFloor.Value, "FloorQueue[1]=", self.RelayAlgorithmConstructor.__FloorQueue[1]) + if self.RelayAlgorithmConstructor.__FloorQueue[1] ~= self.Attributes.CurrentFloor.Value then + warn("The floor queue first index did not match the elevator's current floor, CurrentFloor=", self.Attributes.CurrentFloor.Value, "FloorQueue[1]=", self.RelayAlgorithmConstructor.__FloorQueue[1]) end table.remove(self.RelayAlgorithmConstructor.__FloorQueue, 1) local NextFloorInQueue = self.RelayAlgorithmConstructor.__FloorQueue[1] if NextFloorInQueue then - local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, NextFloorInQueue) + local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, NextFloorInQueue) local LevelVec3 = LevelingModule.Leveling[NextFloorInQueue] self:__TravelToFloor(NextFloorInQueue, LevelVec3, ElevatorGoingUp) end end -local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt: number, LevelVec3: number, ElevatorGoingUp: boolean) +local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3: number, ElevatorGoingUp: boolean) local ElevatorPosition = self.ElevatorBox_1960.Position - local AtFloorY = LevelingModule.Leveling[ElevatorGoingUp and Attributes.CurrentFloor.Value+1 or Attributes.CurrentFloor.Value-1] + local AtFloorY = LevelingModule.Leveling[ElevatorGoingUp and self.Attributes.CurrentFloor.Value+1 or self.Attributes.CurrentFloor.Value-1] if ElevatorGoingUp then --Detecting between the floors - if ElevatorPosition.Y>=AtFloorY-Elevator.FloorLevelingDistance then - Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value - Attributes.CurrentFloor.Value+=1 - Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value+1, #LevelingModule.Leveling) + if ElevatorPosition.Y>=AtFloorY-self.ElevatorConfiguration.FloorLevelingDistance then + self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value + self.Attributes.CurrentFloor.Value+=1 + self.Attributes.NextFloor.Value = math.clamp(1, self.Attributes.CurrentFloor.Value+1, #LevelingModule.Leveling) - Events.CabProgression:Fire(Attributes.PreviousFloor.Value, Attributes.CurrentFloor.Value, Attributes.NextFloor.Value) + self.Events.CabProgression:Fire(self.Attributes.PreviousFloor.Value, self.Attributes.CurrentFloor.Value, self.Attributes.NextFloor.Value) end --Elevator is riding upwards towards the destination - if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLevelingDistance then - Events.Leveling:Fire() - self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity + if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.FloorLevelingDistance then + self.Events.Leveling:Fire() + self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.LevelingVelocity - if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLeveling3PhaseDistance then - Events.Leveling3Phase:Fire() - self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity + if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.FloorLeveling3PhaseDistance then + self.Events.Leveling3Phase:Fire() + self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.Phase3LevelingVelocity - if ElevatorPosition.Y>=LevelVec3-Elevator.ParkedDistance then - Events.Parked:Fire() + if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.ParkedDistance then + self.Events.Parked:Fire() CheckFloorQueue(self); (self.__Connections.Moving :: RBXScriptConnection):Disconnect() @@ -163,25 +178,25 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt: end end else - if ElevatorPosition.Y<=AtFloorY+Elevator.FloorLevelingDistance then - Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value - Attributes.CurrentFloor.Value-=1 - Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value-1, #LevelingModule.Leveling) + if ElevatorPosition.Y<=AtFloorY+self.ElevatorConfiguration.FloorLevelingDistance then + self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value + self.Attributes.CurrentFloor.Value-=1 + self.Attributes.NextFloor.Value = math.clamp(1, self.Attributes.CurrentFloor.Value-1, #LevelingModule.Leveling) - Events.CabProgression:Fire(Attributes.PreviousFloor.Value, Attributes.CurrentFloor.Value, Attributes.NextFloor.Value) + self.Events.CabProgression:Fire(self.Attributes.PreviousFloor.Value, self.Attributes.CurrentFloor.Value, self.Attributes.NextFloor.Value) end --Elevator is riding upwards towards the destination - if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLevelingDistance then - Events.Leveling:Fire() - self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity + if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.FloorLevelingDistance then + self.Events.Leveling:Fire() + self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.LevelingVelocity - if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLeveling3PhaseDistance then - Events.Leveling3Phase:Fire() - self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity + if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.FloorLeveling3PhaseDistance then + self.Events.Leveling3Phase:Fire() + self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.Phase3LevelingVelocity - if ElevatorPosition.Y<=LevelVec3+Elevator.ParkedDistance then - Events.Parked:Fire() + if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.ParkedDistance then + self.Events.Parked:Fire() CheckFloorQueue(self); (self.__Connections.Moving :: RBXScriptConnection):Disconnect() @@ -190,7 +205,7 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt: end end - Events.CabTraveling:Fire(deltaTime, ElevatorPosition) + self.Events.CabTraveling:Fire(deltaTime, ElevatorPosition) end function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp) @@ -198,24 +213,24 @@ function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp) self.__Connections.Moving:Disconnect() end - Attributes.Goal.Value = LevelInt - Attributes.GoingUp.Value = ElevatorGoingUp + self.Attributes.Goal.Value = LevelInt + self.Attributes.GoingUp.Value = ElevatorGoingUp - self.__Connections.Moving = RS.Heartbeat:Connect(function(deltaTime) - CabTraveling(self, deltaTime, LevelInt, LevelVec3, ElevatorGoingUp) + self.__Connections.Moving = RunService.Heartbeat:Connect(function(deltaTime) + CabTraveling(self, deltaTime, LevelVec3, ElevatorGoingUp) end) --Set the elevator's AlignPosition to the floor Y vector self.BoxAlignPosition.Position = Vector3.new(self.BoxAlignPosition.Position.X, LevelVec3, self.BoxAlignPosition.Position.Z) --Set the elevator's max velocity to its fastest speed when moving starts - self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity + self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.MaxVelocity end function Elevator:RequestLevel(RequestedLevel) local LevelVec3 = LevelingModule.Leveling[RequestedLevel] if LevelVec3 then - local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) + local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, RequestedLevel) self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel) if #self.RelayAlgorithmConstructor.__FloorQueue == 1 then