mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-13 22:31:55 +00:00
Just about a work state, Bug with different direction and the loop not working
This commit is contained in:
@@ -34,6 +34,9 @@ type Constructor_Fun = (ElevatorBoxModel: UnionOperation, ElevatorConfigurationT
|
||||
type Constructor_Return_Props = {
|
||||
RelayAlgorithm: RelayAlgorithm.RelayAlgorithmConstructor,
|
||||
FloorLevelingPositions: FloorLevelingPositions,
|
||||
eprint: <T...>(T...) -> (),
|
||||
ewarn: <T...>(T...) -> (),
|
||||
eprintStudio: <T...>(T...) -> (),
|
||||
|
||||
Elevator: {
|
||||
TravelingDirection: Enums.ElevatorCallDirectionValues,
|
||||
@@ -107,6 +110,15 @@ end
|
||||
function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, FloorLevelingPositions)
|
||||
assert(#FloorLevelingPositions>1, `"{ElevatorConfigurationTable.Name}" requires more floors to operate. Floors={FloorLevelingPositions}, #Floors={#FloorLevelingPositions}.`)
|
||||
|
||||
local function eprint<T...>(...: T...)
|
||||
print(`[{ElevatorConfigurationTable.Name}]:`, ...)
|
||||
end
|
||||
local function ewarn<T...>(...: T...)
|
||||
warn(`[{ElevatorConfigurationTable.Name}]:`, ...)
|
||||
end
|
||||
local function eprintStudio<T...>(...: T...)
|
||||
Out.printStudio(`[{ElevatorConfigurationTable.Name}]:`, ...)
|
||||
end
|
||||
local _BoxAttachment,
|
||||
BoxAlignPosition,
|
||||
_BoxAlignOrientation = Mover(ElevatorBoxModel, ElevatorConfigurationTable.Responsiveness, ElevatorConfigurationTable.MaxVelocity)
|
||||
@@ -118,7 +130,6 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
||||
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,
|
||||
CurrentFloor = Instance.new("IntValue") :: IntValue,
|
||||
@@ -136,6 +147,10 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
||||
local ElevatorClass = setmetatable({
|
||||
RelayAlgorithm = RelayAlgorithmConstructor,
|
||||
FloorLevelingPositions = FloorLevelingPositions,
|
||||
eprint = eprint,
|
||||
ewarn = ewarn,
|
||||
eprintStudio = eprintStudio,
|
||||
|
||||
Elevator = {
|
||||
TravelingDirection = Enums.ElevatorCallDirection.Up,
|
||||
BoxModel = ElevatorBoxModel,
|
||||
@@ -167,7 +182,7 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
||||
local Level = FloorLevelingPositions[NextFloorAsTraveling]
|
||||
ElevatorClass:__TravelToFloor(Level, Vector3.new(0, Level, 0), AddedFloorDirection)
|
||||
end
|
||||
Out.printStudio(`[{ElevatorConfigurationTable.Name}]: Floors sorted in proceeding direction. direction={AddedFloorDirection}, FloorDirectionQueue={FloorDirectionQueue}`)
|
||||
ElevatorClass.eprintStudio(`Floors sorted in proceeding direction. direction={AddedFloorDirection}, FloorDirectionQueue={FloorDirectionQueue}`)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -175,38 +190,64 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
||||
return ElevatorClass
|
||||
end
|
||||
|
||||
local function ProceedToNextLevel(self: ClassConstructor, Level: number, Direction: Enums.ElevatorCallDirectionValues): Vector3?
|
||||
local VEC3_Y_WRAP = self:GetLevel(Level, Direction)
|
||||
if VEC3_Y_WRAP then
|
||||
self:__TravelToFloor(Level, VEC3_Y_WRAP, Direction)
|
||||
else
|
||||
self.ewarn(`Failed to get the requested level's Y position. VEC3_Y_WRAP={VEC3_Y_WRAP} Level={Level} Direction={Direction}`)
|
||||
end
|
||||
return VEC3_Y_WRAP
|
||||
end
|
||||
|
||||
local function CheckFloorQueue(self: ClassConstructor, Direction: Enums.ElevatorCallDirectionValues)
|
||||
local DirectionFloorQueue = Direction == Enums.ElevatorCallDirection.Up and self.RelayAlgorithm.FloorQueue.Up or self.RelayAlgorithm.FloorQueue.Down
|
||||
|
||||
if DirectionFloorQueue[1] ~= self.Attributes.CurrentFloor.Value then
|
||||
warn(`[{self.Elevator.Configuration.Name}]: The floor queue first index did not match the elevator's current floor, CurrentFloor=`, self.Attributes.CurrentFloor.Value, "FloorQueue[1]=", DirectionFloorQueue[1])
|
||||
self.ewarn("The floor queue first index did not match the elevator's current floor, CurrentFloor=", self.Attributes.CurrentFloor.Value, "FloorQueue[1]=", DirectionFloorQueue[1])
|
||||
end
|
||||
table.remove(DirectionFloorQueue, 1)
|
||||
Out.printStudio(`[{self.Elevator.Configuration.Name}]: Checking more floors in direction queue. DirectionFloorQueue=`, DirectionFloorQueue)
|
||||
self.RelayAlgorithm:Sort(Direction)
|
||||
self.eprintStudio("Checking more floors in direction queue. Direction=", Direction, "DirectionFloorQueue=", DirectionFloorQueue)
|
||||
|
||||
local NextFloorInQueue = DirectionFloorQueue[1]
|
||||
if NextFloorInQueue then
|
||||
|
||||
local ProceedingToTheNextLevel = ProceedToNextLevel(self, NextFloorInQueue, Direction)
|
||||
if not ProceedingToTheNextLevel then
|
||||
self.RelayAlgorithm:Sort(Enums.ElevatorCallDirection.Down)
|
||||
ProceedToNextLevel(self, 1, Enums.ElevatorCallDirection.Down)
|
||||
end
|
||||
else
|
||||
--No more floors in this direction?
|
||||
--No more floors in the current direction?
|
||||
--Check the opposite
|
||||
self.eprintStudio(`No more floors in the direction "{Direction}" checking the opposite direction floors`)
|
||||
if Direction == Enums.ElevatorCallDirection.Up then
|
||||
if #self.RelayAlgorithm.FloorQueue.Down ~= 0 then
|
||||
|
||||
self.RelayAlgorithm:Sort(Enums.ElevatorCallDirection.Down)
|
||||
local NextLevelDown = self.RelayAlgorithm.FloorQueue.Down[1]
|
||||
if NextLevelDown then
|
||||
self.eprint("Floors found in the opposite direction. Direction=", Enums.ElevatorCallDirection.Down, "NextLevel=", NextLevelDown)
|
||||
ProceedToNextLevel(self, NextLevelDown, Enums.ElevatorCallDirection.Down)
|
||||
end
|
||||
end
|
||||
else
|
||||
if #self.RelayAlgorithm.FloorQueue.Up ~= 0 then
|
||||
|
||||
self.RelayAlgorithm:Sort(Enums.ElevatorCallDirection.Up)
|
||||
local NextLevelUp = self.RelayAlgorithm.FloorQueue.Up[1]
|
||||
if NextLevelUp then
|
||||
self.eprint("Floors found in the opposite direction. Direction=", Enums.ElevatorCallDirection.Up, "NextLevel=", NextLevelUp)
|
||||
ProceedToNextLevel(self, NextLevelUp, Enums.ElevatorCallDirection.Up)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function CabTraveling(self: ClassConstructor, deltaTime: number, VEC3_Y_WRAP: Vector3, Direction: Enums.ElevatorCallDirectionValues)
|
||||
local function CabTraveling(self: ClassConstructor, deltaTime: number, VEC3_Y_WRAP: Vector3, Direction: Enums.ElevatorCallDirectionValues, ElevatorTravelingUpwards: boolean)
|
||||
local ElevatorPosition = self.Elevator.BoxModel.Position
|
||||
local AtFloorY = self.FloorLevelingPositions[Direction == Enums.ElevatorCallDirection.Up and self.Attributes.CurrentFloor.Value+1 or self.Attributes.CurrentFloor.Value-1]
|
||||
local AtFloorY = self.FloorLevelingPositions[ElevatorTravelingUpwards and self.Attributes.CurrentFloor.Value+1 or self.Attributes.CurrentFloor.Value-1]
|
||||
|
||||
if Direction == Enums.ElevatorCallDirection.Up then
|
||||
if ElevatorTravelingUpwards then
|
||||
--Detecting between the floors
|
||||
if ElevatorPosition.Y>=AtFloorY-self.Elevator.Configuration.FloorLevelingDistance then
|
||||
self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value
|
||||
@@ -278,11 +319,12 @@ function Elevator:__TravelToFloor(LevelInt, VEC3_Y_WRAP, Direction)
|
||||
self.__Connections.Moving:Disconnect()
|
||||
end
|
||||
|
||||
local ElevatorTravelingUpwards = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, LevelInt)
|
||||
self.Attributes.Goal.Value = LevelInt
|
||||
self.Attributes.TravelingUpwards.Value = Direction == Enums.ElevatorCallDirection.Up
|
||||
self.Attributes.TravelingUpwards.Value = ElevatorTravelingUpwards
|
||||
|
||||
self.__Connections.Moving = RunService.Heartbeat:Connect(function(deltaTime: number)
|
||||
CabTraveling(self, deltaTime, VEC3_Y_WRAP, Direction :: Enums.ElevatorCallDirectionValues)
|
||||
CabTraveling(self, deltaTime, VEC3_Y_WRAP, Direction :: Enums.ElevatorCallDirectionValues, ElevatorTravelingUpwards)
|
||||
end)
|
||||
|
||||
--Set the elevator's AlignPosition to the floor Y vector
|
||||
@@ -297,7 +339,7 @@ function Elevator:RequestLevel(RequestedLevel, Direction)
|
||||
if Level then
|
||||
if RequestedLevel ~= self.Attributes.CurrentFloor.Value then
|
||||
if (RequestedLevel == 1 and Direction == Enums.ElevatorCallDirection.Down) or (RequestedLevel == #self.FloorLevelingPositions and Direction == Enums.ElevatorCallDirection.Up) then
|
||||
warn(`[{self.Elevator.Configuration.Name}]: Impossible direction requested, Direction={Direction}, RequestedLevel={Level}`)
|
||||
self.ewarn(`Impossible direction requested, Direction={Direction}, RequestedLevel={Level}`)
|
||||
return false
|
||||
else
|
||||
local DirectionQueue = Direction == Enums.ElevatorCallDirection.Up and self.RelayAlgorithm.FloorQueue.Up or self.RelayAlgorithm.FloorQueue.Down
|
||||
@@ -308,13 +350,13 @@ function Elevator:RequestLevel(RequestedLevel, Direction)
|
||||
end
|
||||
end
|
||||
else
|
||||
warn(`[{self.Elevator.Configuration.Name}]: The elevator is already at the requested floor. RequestLevel={RequestedLevel}, CurrentLevel={self.Attributes.CurrentFloor.Value}`)
|
||||
self.ewarn(`The elevator is already at the requested floor. RequestLevel={RequestedLevel}, CurrentLevel={self.Attributes.CurrentFloor.Value}`)
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
warn(`[{self.Elevator.Configuration.Name}]: Requested floor: "{RequestedLevel}" does not exist for this elevator`)
|
||||
self.ewarn(`Requested floor: "{RequestedLevel}" does not exist for this elevator`)
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
@@ -51,7 +51,9 @@ return function(TagsConstructor: TagsModule.TagsConstructor, ButtonTags: TagsMod
|
||||
end)
|
||||
|
||||
task.wait(1)
|
||||
Elevator:RequestLevel(3, Enums.ElevatorCallDirection.Up)
|
||||
Elevator:RequestLevel(5, Enums.ElevatorCallDirection.Up)
|
||||
Elevator:RequestLevel(2, Enums.ElevatorCallDirection.Down)
|
||||
Elevator:RequestLevel(2, Enums.ElevatorCallDirection.Up)
|
||||
Elevator:RequestLevel(8, Enums.ElevatorCallDirection.Up)
|
||||
|
||||
Elevator:RequestLevel(10, Enums.ElevatorCallDirection.Down)
|
||||
Elevator:RequestLevel(6, Enums.ElevatorCallDirection.Down)
|
||||
end
|
||||
|
||||
@@ -9,13 +9,11 @@ local function printStudio<T...>(...: T...)
|
||||
print(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function printServer<T...>(...: T...)
|
||||
if RunService:IsServer() then
|
||||
print(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function printClient<T...>(...: T...)
|
||||
if RunService:IsClient() then
|
||||
print(...)
|
||||
@@ -27,13 +25,11 @@ local function warnStudio<T...>(...: T...)
|
||||
warn(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function warnServer<T...>(...: T...)
|
||||
if RunService:IsServer() then
|
||||
warn(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function warnClient<T...>(...: T...)
|
||||
if RunService:IsClient() then
|
||||
warn(...)
|
||||
|
||||
Reference in New Issue
Block a user