mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-14 06:41: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 = {
|
type Constructor_Return_Props = {
|
||||||
RelayAlgorithm: RelayAlgorithm.RelayAlgorithmConstructor,
|
RelayAlgorithm: RelayAlgorithm.RelayAlgorithmConstructor,
|
||||||
FloorLevelingPositions: FloorLevelingPositions,
|
FloorLevelingPositions: FloorLevelingPositions,
|
||||||
|
eprint: <T...>(T...) -> (),
|
||||||
|
ewarn: <T...>(T...) -> (),
|
||||||
|
eprintStudio: <T...>(T...) -> (),
|
||||||
|
|
||||||
Elevator: {
|
Elevator: {
|
||||||
TravelingDirection: Enums.ElevatorCallDirectionValues,
|
TravelingDirection: Enums.ElevatorCallDirectionValues,
|
||||||
@@ -107,6 +110,15 @@ end
|
|||||||
function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, FloorLevelingPositions)
|
function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, FloorLevelingPositions)
|
||||||
assert(#FloorLevelingPositions>1, `"{ElevatorConfigurationTable.Name}" requires more floors to operate. Floors={FloorLevelingPositions}, #Floors={#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,
|
local _BoxAttachment,
|
||||||
BoxAlignPosition,
|
BoxAlignPosition,
|
||||||
_BoxAlignOrientation = Mover(ElevatorBoxModel, ElevatorConfigurationTable.Responsiveness, ElevatorConfigurationTable.MaxVelocity)
|
_BoxAlignOrientation = Mover(ElevatorBoxModel, ElevatorConfigurationTable.Responsiveness, ElevatorConfigurationTable.MaxVelocity)
|
||||||
@@ -118,7 +130,6 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
|||||||
local Parked = Instance.new("BindableEvent") :: BindableEvent
|
local Parked = Instance.new("BindableEvent") :: BindableEvent
|
||||||
local Leveling = Instance.new("BindableEvent") :: BindableEvent
|
local Leveling = Instance.new("BindableEvent") :: BindableEvent
|
||||||
local Leveling3Phase = Instance.new("BindableEvent") :: BindableEvent
|
local Leveling3Phase = Instance.new("BindableEvent") :: BindableEvent
|
||||||
|
|
||||||
local Attributes = {
|
local Attributes = {
|
||||||
PreviousFloor = Instance.new("IntValue") :: IntValue,
|
PreviousFloor = Instance.new("IntValue") :: IntValue,
|
||||||
CurrentFloor = Instance.new("IntValue") :: IntValue,
|
CurrentFloor = Instance.new("IntValue") :: IntValue,
|
||||||
@@ -136,6 +147,10 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
|||||||
local ElevatorClass = setmetatable({
|
local ElevatorClass = setmetatable({
|
||||||
RelayAlgorithm = RelayAlgorithmConstructor,
|
RelayAlgorithm = RelayAlgorithmConstructor,
|
||||||
FloorLevelingPositions = FloorLevelingPositions,
|
FloorLevelingPositions = FloorLevelingPositions,
|
||||||
|
eprint = eprint,
|
||||||
|
ewarn = ewarn,
|
||||||
|
eprintStudio = eprintStudio,
|
||||||
|
|
||||||
Elevator = {
|
Elevator = {
|
||||||
TravelingDirection = Enums.ElevatorCallDirection.Up,
|
TravelingDirection = Enums.ElevatorCallDirection.Up,
|
||||||
BoxModel = ElevatorBoxModel,
|
BoxModel = ElevatorBoxModel,
|
||||||
@@ -167,7 +182,7 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
|||||||
local Level = FloorLevelingPositions[NextFloorAsTraveling]
|
local Level = FloorLevelingPositions[NextFloorAsTraveling]
|
||||||
ElevatorClass:__TravelToFloor(Level, Vector3.new(0, Level, 0), AddedFloorDirection)
|
ElevatorClass:__TravelToFloor(Level, Vector3.new(0, Level, 0), AddedFloorDirection)
|
||||||
end
|
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
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -175,38 +190,64 @@ function Elevator.constructor(ElevatorBoxModel, ElevatorConfigurationTable, Floo
|
|||||||
return ElevatorClass
|
return ElevatorClass
|
||||||
end
|
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 function CheckFloorQueue(self: ClassConstructor, Direction: Enums.ElevatorCallDirectionValues)
|
||||||
local DirectionFloorQueue = Direction == Enums.ElevatorCallDirection.Up and self.RelayAlgorithm.FloorQueue.Up or self.RelayAlgorithm.FloorQueue.Down
|
local DirectionFloorQueue = Direction == Enums.ElevatorCallDirection.Up and self.RelayAlgorithm.FloorQueue.Up or self.RelayAlgorithm.FloorQueue.Down
|
||||||
|
|
||||||
if DirectionFloorQueue[1] ~= self.Attributes.CurrentFloor.Value then
|
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
|
end
|
||||||
table.remove(DirectionFloorQueue, 1)
|
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]
|
local NextFloorInQueue = DirectionFloorQueue[1]
|
||||||
if NextFloorInQueue then
|
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
|
else
|
||||||
--No more floors in this direction?
|
--No more floors in the current direction?
|
||||||
--Check the opposite
|
--Check the opposite
|
||||||
|
self.eprintStudio(`No more floors in the direction "{Direction}" checking the opposite direction floors`)
|
||||||
if Direction == Enums.ElevatorCallDirection.Up then
|
if Direction == Enums.ElevatorCallDirection.Up then
|
||||||
if #self.RelayAlgorithm.FloorQueue.Down ~= 0 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
|
end
|
||||||
else
|
else
|
||||||
if #self.RelayAlgorithm.FloorQueue.Up ~= 0 then
|
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
|
||||||
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 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
|
--Detecting between the floors
|
||||||
if ElevatorPosition.Y>=AtFloorY-self.Elevator.Configuration.FloorLevelingDistance then
|
if ElevatorPosition.Y>=AtFloorY-self.Elevator.Configuration.FloorLevelingDistance then
|
||||||
self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value
|
self.Attributes.PreviousFloor.Value = self.Attributes.CurrentFloor.Value
|
||||||
@@ -278,11 +319,12 @@ function Elevator:__TravelToFloor(LevelInt, VEC3_Y_WRAP, Direction)
|
|||||||
self.__Connections.Moving:Disconnect()
|
self.__Connections.Moving:Disconnect()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local ElevatorTravelingUpwards = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, LevelInt)
|
||||||
self.Attributes.Goal.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)
|
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)
|
end)
|
||||||
|
|
||||||
--Set the elevator's AlignPosition to the floor Y vector
|
--Set the elevator's AlignPosition to the floor Y vector
|
||||||
@@ -297,7 +339,7 @@ function Elevator:RequestLevel(RequestedLevel, Direction)
|
|||||||
if Level then
|
if Level then
|
||||||
if RequestedLevel ~= self.Attributes.CurrentFloor.Value 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
|
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
|
return false
|
||||||
else
|
else
|
||||||
local DirectionQueue = Direction == Enums.ElevatorCallDirection.Up and self.RelayAlgorithm.FloorQueue.Up or self.RelayAlgorithm.FloorQueue.Down
|
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
|
||||||
end
|
end
|
||||||
else
|
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
|
return false
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
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
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ return function(TagsConstructor: TagsModule.TagsConstructor, ButtonTags: TagsMod
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
task.wait(1)
|
task.wait(1)
|
||||||
Elevator:RequestLevel(3, Enums.ElevatorCallDirection.Up)
|
Elevator:RequestLevel(2, Enums.ElevatorCallDirection.Up)
|
||||||
Elevator:RequestLevel(5, Enums.ElevatorCallDirection.Up)
|
Elevator:RequestLevel(8, Enums.ElevatorCallDirection.Up)
|
||||||
Elevator:RequestLevel(2, Enums.ElevatorCallDirection.Down)
|
|
||||||
|
Elevator:RequestLevel(10, Enums.ElevatorCallDirection.Down)
|
||||||
|
Elevator:RequestLevel(6, Enums.ElevatorCallDirection.Down)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,13 +9,11 @@ local function printStudio<T...>(...: T...)
|
|||||||
print(...)
|
print(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printServer<T...>(...: T...)
|
local function printServer<T...>(...: T...)
|
||||||
if RunService:IsServer() then
|
if RunService:IsServer() then
|
||||||
print(...)
|
print(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printClient<T...>(...: T...)
|
local function printClient<T...>(...: T...)
|
||||||
if RunService:IsClient() then
|
if RunService:IsClient() then
|
||||||
print(...)
|
print(...)
|
||||||
@@ -27,13 +25,11 @@ local function warnStudio<T...>(...: T...)
|
|||||||
warn(...)
|
warn(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function warnServer<T...>(...: T...)
|
local function warnServer<T...>(...: T...)
|
||||||
if RunService:IsServer() then
|
if RunService:IsServer() then
|
||||||
warn(...)
|
warn(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function warnClient<T...>(...: T...)
|
local function warnClient<T...>(...: T...)
|
||||||
if RunService:IsClient() then
|
if RunService:IsClient() then
|
||||||
warn(...)
|
warn(...)
|
||||||
|
|||||||
Reference in New Issue
Block a user