Adapt the module more to the new system

This commit is contained in:
2024-07-20 17:07:38 -04:00
parent 82a58c9a20
commit 083f002ace

View File

@@ -72,6 +72,8 @@ type Impl_Static_Props = {}
type Constructor_Fun = (ElevatorConfigurationTable: ElevatorConfigurationTable) -> ClassConstructor type Constructor_Fun = (ElevatorConfigurationTable: ElevatorConfigurationTable) -> ClassConstructor
type Constructor_Return_Props = { type Constructor_Return_Props = {
ElevatorConfiguration: ElevatorConfigurationTable,
Attributes: { Attributes: {
PreviousFloor: IntValue, PreviousFloor: IntValue,
CurrentFloor: IntValue, CurrentFloor: IntValue,
@@ -104,58 +106,71 @@ local function ElevatorGoingUpDirection(CurrentFloor: number, RequestedFloor: nu
end end
function Elevator.constructor(ElevatorConfigurationTable: ElevatorConfigurationTable) function Elevator.constructor(ElevatorConfigurationTable: ElevatorConfigurationTable)
local self = {} :: Constructor_Return_Props print(`🛗 [{ElevatorConfigurationTable.Name}]: Initialized and ready`)
return setmetatable({
ElevatorConfiguration = ElevatorConfigurationTable,
Attributes = {
--self.RelayConstructor:BulkConnect() PreviousFloor = Instance.new("IntValue") :: IntValue,
CurrentFloor = Instance.new("IntValue") :: IntValue,
local ClassConstructor = setmetatable(self, Elevator) NextFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue,
print(`🛗 [{Elevator.Name}]: Initialized and ready`) GoingUp = Instance.new("BoolValue") :: BoolValue,
return ClassConstructor 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 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] ~= self.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]) 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 end
table.remove(self.RelayAlgorithmConstructor.__FloorQueue, 1) table.remove(self.RelayAlgorithmConstructor.__FloorQueue, 1)
local NextFloorInQueue = self.RelayAlgorithmConstructor.__FloorQueue[1] local NextFloorInQueue = self.RelayAlgorithmConstructor.__FloorQueue[1]
if NextFloorInQueue then if NextFloorInQueue then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, NextFloorInQueue) local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, NextFloorInQueue)
local LevelVec3 = LevelingModule.Leveling[NextFloorInQueue] local LevelVec3 = LevelingModule.Leveling[NextFloorInQueue]
self:__TravelToFloor(NextFloorInQueue, LevelVec3, ElevatorGoingUp) self:__TravelToFloor(NextFloorInQueue, LevelVec3, ElevatorGoingUp)
end end
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 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 if ElevatorGoingUp then
--Detecting between the floors --Detecting between the floors
if ElevatorPosition.Y>=AtFloorY-Elevator.FloorLevelingDistance then if ElevatorPosition.Y>=AtFloorY-self.ElevatorConfiguration.FloorLevelingDistance then
Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value
Attributes.CurrentFloor.Value+=1 self.Attributes.CurrentFloor.Value+=1
Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value+1, #LevelingModule.Leveling) 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 end
--Elevator is riding upwards towards the destination --Elevator is riding upwards towards the destination
if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLevelingDistance then if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.FloorLevelingDistance then
Events.Leveling:Fire() self.Events.Leveling:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.LevelingVelocity
if ElevatorPosition.Y>=LevelVec3-Elevator.FloorLeveling3PhaseDistance then if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.FloorLeveling3PhaseDistance then
Events.Leveling3Phase:Fire() self.Events.Leveling3Phase:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.Phase3LevelingVelocity
if ElevatorPosition.Y>=LevelVec3-Elevator.ParkedDistance then if ElevatorPosition.Y>=LevelVec3-self.ElevatorConfiguration.ParkedDistance then
Events.Parked:Fire() self.Events.Parked:Fire()
CheckFloorQueue(self); CheckFloorQueue(self);
(self.__Connections.Moving :: RBXScriptConnection):Disconnect() (self.__Connections.Moving :: RBXScriptConnection):Disconnect()
@@ -163,25 +178,25 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt:
end end
end end
else else
if ElevatorPosition.Y<=AtFloorY+Elevator.FloorLevelingDistance then if ElevatorPosition.Y<=AtFloorY+self.ElevatorConfiguration.FloorLevelingDistance then
Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value
Attributes.CurrentFloor.Value-=1 self.Attributes.CurrentFloor.Value-=1
Attributes.NextFloor.Value = math.clamp(1, Attributes.CurrentFloor.Value-1, #LevelingModule.Leveling) 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 end
--Elevator is riding upwards towards the destination --Elevator is riding upwards towards the destination
if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLevelingDistance then if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.FloorLevelingDistance then
Events.Leveling:Fire() self.Events.Leveling:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.LevelingVelocity self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.LevelingVelocity
if ElevatorPosition.Y<=LevelVec3+Elevator.FloorLeveling3PhaseDistance then if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.FloorLeveling3PhaseDistance then
Events.Leveling3Phase:Fire() self.Events.Leveling3Phase:Fire()
self.BoxAlignPosition.MaxVelocity = Elevator.Phase3LevelingVelocity self.BoxAlignPosition.MaxVelocity = self.ElevatorConfiguration.Phase3LevelingVelocity
if ElevatorPosition.Y<=LevelVec3+Elevator.ParkedDistance then if ElevatorPosition.Y<=LevelVec3+self.ElevatorConfiguration.ParkedDistance then
Events.Parked:Fire() self.Events.Parked:Fire()
CheckFloorQueue(self); CheckFloorQueue(self);
(self.__Connections.Moving :: RBXScriptConnection):Disconnect() (self.__Connections.Moving :: RBXScriptConnection):Disconnect()
@@ -190,7 +205,7 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelInt:
end end
end end
Events.CabTraveling:Fire(deltaTime, ElevatorPosition) self.Events.CabTraveling:Fire(deltaTime, ElevatorPosition)
end end
function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp) function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp)
@@ -198,24 +213,24 @@ function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp)
self.__Connections.Moving:Disconnect() self.__Connections.Moving:Disconnect()
end end
Attributes.Goal.Value = LevelInt self.Attributes.Goal.Value = LevelInt
Attributes.GoingUp.Value = ElevatorGoingUp self.Attributes.GoingUp.Value = ElevatorGoingUp
self.__Connections.Moving = RS.Heartbeat:Connect(function(deltaTime) self.__Connections.Moving = RunService.Heartbeat:Connect(function(deltaTime)
CabTraveling(self, deltaTime, LevelInt, LevelVec3, ElevatorGoingUp) CabTraveling(self, deltaTime, LevelVec3, ElevatorGoingUp)
end) end)
--Set the elevator's AlignPosition to the floor Y vector --Set the elevator's AlignPosition to the floor Y vector
self.BoxAlignPosition.Position = Vector3.new(self.BoxAlignPosition.Position.X, LevelVec3, self.BoxAlignPosition.Position.Z) 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 --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 end
function Elevator:RequestLevel(RequestedLevel) function Elevator:RequestLevel(RequestedLevel)
local LevelVec3 = LevelingModule.Leveling[RequestedLevel] local LevelVec3 = LevelingModule.Leveling[RequestedLevel]
if LevelVec3 then if LevelVec3 then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) local ElevatorGoingUp = ElevatorGoingUpDirection(self.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