This commit is contained in:
2024-07-21 23:29:33 -04:00
parent aa2b5a9c2c
commit d6b1da04e1
7 changed files with 157 additions and 83 deletions

View File

@@ -4,17 +4,12 @@
local Elevators = script.Parent
local MainDir = Elevators.Parent
local EnumsDir = MainDir:WaitForChild("Enums")
local LoadDir = MainDir:WaitForChild("Load")
local StorageService = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Enums = require(StorageService:WaitForChild("Enums"))
local SoundEnums = require(EnumsDir:WaitForChild("Sounds"))
local Tags = require(LoadDir:WaitForChild("Tags"))
local RunService = game:GetService("RunService")
local ElevatorTypes = require(MainDir:WaitForChild("Types"):WaitForChild("Elevator"))
local Tags = require(LoadDir:WaitForChild("Tags"))
local RelayAlgorithm = require(script:WaitForChild("RelayAlgorithm"))
type Tags = Tags.ExportedTags
@@ -29,33 +24,8 @@ type Impl_Constructor = {
__TravelToFloor: (self: ClassConstructor, LevelInt: number, LevelVec3: number, ElevatorGoingUp: boolean) -> (),
}
type ElevatorConfigurationTable = {
Name: Enums.ElevatorValues,
Responsiveness: number,
MaxVelocity: number,
FloorLevelingDistance: number,
FloorLeveling3PhaseDistance: number,
ParkedDistance: number,
LevelingVelocity: number,
Phase3LevelingVelocity: number,
Sounds: {
LanternChimeDirection: SoundEnums.ElevatorSoundValues,
LanternChimeLanding: SoundEnums.ElevatorSoundValues,
},
Colors: {
ButtonActivated: Color3,
ButtonDeactivated: Color3,
LanternDisplayOn: Color3,
LanternDisplayOff: Color3,
},
}
type FloorLevelingPositions = {number}
type Constructor_Fun = (
ElevatorBoxModel: UnionOperation,
ElevatorConfigurationTable: ElevatorConfigurationTable,
FloorLevelingPositions: FloorLevelingPositions
) -> ClassConstructor
type Constructor_Fun = (ElevatorBoxModel: UnionOperation, ElevatorConfigurationTable: ElevatorTypes.ElevatorConfigurationTable, FloorLevelingPositions: FloorLevelingPositions) -> ClassConstructor
type Constructor_Return_Props = {
RelayAlgorithm: RelayAlgorithm.RelayAlgorithmConstructor,
@@ -64,7 +34,7 @@ type Constructor_Return_Props = {
Elevator: {
BoxModel: UnionOperation,
AlignPosition: AlignPosition,
Configuration: ElevatorConfigurationTable
Configuration: ElevatorTypes.ElevatorConfigurationTable
},
Attributes: {
PreviousFloor: IntValue,
@@ -75,11 +45,18 @@ type Constructor_Return_Props = {
Stopped: BoolValue
},
Events: {
CabProgression: BindableEvent,
CabTraveling: BindableEvent,
Parked: BindableEvent,
Leveling: BindableEvent,
Leveling3Phase: BindableEvent,
CabProgression: RBXScriptSignal<number, number, number>,
CabTraveling: RBXScriptSignal<number, Vector3>,
Parked: RBXScriptSignal,
Leveling: RBXScriptSignal,
Leveling3Phase: RBXScriptSignal,
__eventInstances__: {
CabProgression: BindableEvent,
CabTraveling: BindableEvent,
Parked: BindableEvent,
Leveling: BindableEvent,
Leveling3Phase: BindableEvent,
}
},
__Connections: {
@@ -123,19 +100,19 @@ local function Mover(ElevatorBoxModel: UnionOperation, Responsiveness: number, M
end
function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, FloorLevelingPositions)
assert(#FloorLevelingPositions>1, `[{ElevatorConfigurationTable.Name}] requires more floors to operate. Floors={FloorLevelingPositions}, #Floors={#FloorLevelingPositions}.`)
local _BoxAttachment,
BoxAlignPosition,
_BoxAlignOrientation = Mover(ElevatorBoxModel, ElevatorConfigurationTable.Responsiveness, ElevatorConfigurationTable.MaxVelocity)
local RelayAlgorithmConstructor = RelayAlgorithm.constructor(BoxAlignPosition)
local 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,
}
local CabProgression = Instance.new("BindableEvent") :: BindableEvent
local CabTraveling = Instance.new("BindableEvent") :: BindableEvent
local Parked = Instance.new("BindableEvent") :: BindableEvent
local Leveling = Instance.new("BindableEvent") :: BindableEvent
local Leveling3Phase = Instance.new("BindableEvent") :: BindableEvent
local Attributes = {
PreviousFloor = Instance.new("IntValue") :: IntValue,
@@ -145,10 +122,11 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
GoingUp = Instance.new("BoolValue") :: BoolValue,
Stopped = Instance.new("BoolValue") :: BoolValue
}
Attributes.CurrentFloor.Value = 1
Attributes.NextFloor.Value = Attributes.CurrentFloor.Value+1
Attributes.GoingUp.Value = false
Attributes.Goal.Value = 1
Attributes.CurrentFloor.Value = 1
Attributes.PreviousFloor.Value = Attributes.CurrentFloor.Value
Attributes.NextFloor.Value = Attributes.CurrentFloor.Value+1
Attributes.GoingUp.Value = false
Attributes.Goal.Value = 1
print(`🛗 [{ElevatorConfigurationTable.Name}]: Initialized and ready`)
@@ -160,8 +138,21 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
AlignPosition = BoxAlignPosition,
Configuration = ElevatorConfigurationTable,
},
Events = {
CabProgression = CabProgression.Event,
CabTraveling = CabTraveling.Event,
Parked = Parked.Event,
Leveling = Leveling.Event,
Leveling3Phase = Leveling3Phase.Event,
__eventInstances__ = {
CabProgression = CabProgression,
CabTraveling = CabTraveling,
Parked = Parked,
Leveling = Leveling,
Leveling3Phase = Leveling3Phase,
}
},
Attributes = Attributes,
Events = Events,
__Connections = {}
}, Elevator)
end
@@ -173,6 +164,7 @@ local function CheckFloorQueue(self: ClassConstructor)
table.remove(self.RelayAlgorithm.__FloorQueue, 1)
local NextFloorInQueue = self.RelayAlgorithm.__FloorQueue[1]
print(NextFloorInQueue)
if NextFloorInQueue then
local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, NextFloorInQueue)
local LevelVec3 = self.FloorLevelingPositions[NextFloorInQueue]
@@ -192,20 +184,20 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3
self.Attributes.CurrentFloor.Value+=1
self.Attributes.NextFloor.Value = math.clamp(1, self.Attributes.CurrentFloor.Value+1, #self.FloorLevelingPositions)
self.Events.CabProgression:Fire(self.Attributes.PreviousFloor.Value, self.Attributes.CurrentFloor.Value, self.Attributes.NextFloor.Value)
self.Events.__eventInstances__.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-self.Elevator.Configuration.FloorLevelingDistance then
self.Events.Leveling:Fire()
self.Events.__eventInstances__.Leveling:Fire()
self.Elevator.AlignPosition.MaxVelocity = self.Elevator.Configuration.LevelingVelocity
if ElevatorPosition.Y>=LevelVec3-self.Elevator.Configuration.FloorLeveling3PhaseDistance then
self.Events.Leveling3Phase:Fire()
self.Events.__eventInstances__.Leveling3Phase:Fire()
self.Elevator.AlignPosition.MaxVelocity = self.Elevator.Configuration.Phase3LevelingVelocity
if ElevatorPosition.Y>=LevelVec3-self.Elevator.Configuration.ParkedDistance then
self.Events.Parked:Fire()
self.Events.__eventInstances__.Parked:Fire()
CheckFloorQueue(self);
(self.__Connections.Moving :: RBXScriptConnection):Disconnect()
@@ -218,20 +210,20 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3
self.Attributes.CurrentFloor.Value-=1
self.Attributes.NextFloor.Value = math.clamp(1, self.Attributes.CurrentFloor.Value-1, #self.FloorLevelingPositions)
self.Events.CabProgression:Fire(self.Attributes.PreviousFloor.Value, self.Attributes.CurrentFloor.Value, self.Attributes.NextFloor.Value)
self.Events.__eventInstances__.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+self.Elevator.Configuration.FloorLevelingDistance then
self.Events.Leveling:Fire()
self.Events.__eventInstances__.Leveling:Fire()
self.Elevator.AlignPosition.MaxVelocity = self.Elevator.Configuration.LevelingVelocity
if ElevatorPosition.Y<=LevelVec3+self.Elevator.Configuration.FloorLeveling3PhaseDistance then
self.Events.Leveling3Phase:Fire()
self.Events.__eventInstances__.Leveling3Phase:Fire()
self.Elevator.AlignPosition.MaxVelocity = self.Elevator.Configuration.Phase3LevelingVelocity
if ElevatorPosition.Y<=LevelVec3+self.Elevator.Configuration.ParkedDistance then
self.Events.Parked:Fire()
self.Events.__eventInstances__.Parked:Fire()
CheckFloorQueue(self);
(self.__Connections.Moving :: RBXScriptConnection):Disconnect()
@@ -240,7 +232,7 @@ local function CabTraveling(self: ClassConstructor, deltaTime: number, LevelVec3
end
end
self.Events.CabTraveling:Fire(deltaTime, ElevatorPosition)
self.Events.__eventInstances__.CabTraveling:Fire(deltaTime, ElevatorPosition)
end
function Elevator:__TravelToFloor(LevelInt, LevelVec3, ElevatorGoingUp)
@@ -265,17 +257,21 @@ function Elevator:RequestLevel(RequestedLevel)
local LevelVec3 = self.FloorLevelingPositions[RequestedLevel]
if LevelVec3 then
local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, RequestedLevel)
self.RelayAlgorithm:AddFloor(ElevatorGoingUp, self.Attributes.GoingUp, RequestedLevel)
if RequestedLevel ~= self.Attributes.CurrentFloor.Value then
local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, RequestedLevel)
self.RelayAlgorithm:AddFloor(ElevatorGoingUp, self.Attributes.GoingUp, RequestedLevel)
if #self.RelayAlgorithm.__FloorQueue == 1 then
self:__TravelToFloor(RequestedLevel, LevelVec3, ElevatorGoingUp)
if #self.RelayAlgorithm.__FloorQueue == 1 then
self:__TravelToFloor(RequestedLevel, LevelVec3, ElevatorGoingUp)
end
else
warn(`[{self.Elevator.Configuration.Name}]: The elevator is already at the requested floor. RequestLevel={RequestedLevel}, CurrentLevel={self.Attributes.CurrentFloor.Value}`)
return false
end
return true
end
warn(`Requested floor: "{RequestedLevel}" does not exist for this elevator`)
warn(`[{self.Elevator.Configuration.Name}]: Requested floor: "{RequestedLevel}" does not exist for this elevator`)
return false
end