mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-14 06:41:55 +00:00
work on door opening possibility while the elevator is stopped
This commit is contained in:
@@ -35,11 +35,7 @@ type Impl_Static_Props = {
|
||||
ElevatorDoorStyle: Enum.EasingStyle,
|
||||
__DontLeakMemory: RBXScriptConnection?,
|
||||
|
||||
Attributes: {
|
||||
Relay: {
|
||||
Open: BoolValue
|
||||
}
|
||||
}
|
||||
Attributes: {} & RelayAttributes
|
||||
}
|
||||
type Constructor_Return_Props = {
|
||||
FloorDoorsTags: Tags.LandingTags,
|
||||
@@ -57,6 +53,12 @@ type FloorDoors = {
|
||||
[Floor]: {Vector3}?
|
||||
}
|
||||
|
||||
export type RelayAttributes = {
|
||||
Relay: {
|
||||
Open: BoolValue
|
||||
}
|
||||
}
|
||||
|
||||
export type DoorConstructor = ClassConstructor
|
||||
|
||||
local Doors = {} :: Impl_Constructor
|
||||
|
||||
@@ -15,10 +15,15 @@ local Leveling: {number} = {
|
||||
[10] = 239.245,
|
||||
}
|
||||
|
||||
--Mainly used for the lanterns
|
||||
local LevelingBetween: {number} = {}
|
||||
|
||||
--Calculate between leveling
|
||||
for n: number = 1, #Leveling, 2 do
|
||||
LevelingBetween[n] = (Leveling[n]+Leveling[n+1])/2
|
||||
for n: number = 1, #Leveling do
|
||||
local FloorAhead: number? = Leveling[n+1]
|
||||
if FloorAhead then
|
||||
LevelingBetween[n] = (Leveling[n]+FloorAhead)/2
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
--!strict
|
||||
|
||||
local RelayAlgorithmModule = require(script.Parent:WaitForChild("RelayAlgorithm"))
|
||||
local DoorsModule = require(script.Parent:WaitForChild("Doors"))
|
||||
|
||||
type Leveling = {
|
||||
Leveling: {number},
|
||||
@@ -34,10 +35,12 @@ type Constructor_Return_Props = {
|
||||
BoxAlignPosition: BoxAlignPosition,
|
||||
ElevatorBox: ElevatorBox,
|
||||
|
||||
__ReadyConnection: RBXScriptConnection?,
|
||||
__OpenConnection: RBXScriptConnection?,
|
||||
__GoalYLevelConnection: RBXScriptConnection?,
|
||||
__GoalConnection: RBXScriptConnection?,
|
||||
__Connections: {
|
||||
Ready: RBXScriptConnection?,
|
||||
Open: RBXScriptConnection?,
|
||||
GoalYLevel: RBXScriptConnection?,
|
||||
Goal: RBXScriptConnection?
|
||||
}
|
||||
}
|
||||
|
||||
type ElevatorAttributes = {
|
||||
@@ -50,11 +53,7 @@ type ElevatorAttributes = {
|
||||
Stopped: BoolValue
|
||||
}
|
||||
|
||||
type DoorAttributes = {
|
||||
Relay: {
|
||||
Open: BoolValue
|
||||
}
|
||||
}
|
||||
type DoorAttributes = DoorsModule.RelayAttributes
|
||||
|
||||
export type RelayConstructor = ClassConstructor
|
||||
|
||||
@@ -68,7 +67,9 @@ function Relay.constructor(RelayAlgorthm, ElevatorAttributes, DoorAttributes, Le
|
||||
DoorAttributes = DoorAttributes,
|
||||
LevelingModule = LevelingModule,
|
||||
BoxAlignPosition = BoxAlignPosition,
|
||||
ElevatorBox = ElevatorBox
|
||||
ElevatorBox = ElevatorBox,
|
||||
|
||||
__Connections = {}
|
||||
}, Relay)
|
||||
end
|
||||
|
||||
@@ -101,25 +102,25 @@ function Relay:__Goal()
|
||||
end
|
||||
|
||||
function Relay:BulkConnect()
|
||||
self.__ReadyConnection = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
self.__Connections.Ready = self.ElevatorAttributes.Relay.Ready:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Ready()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__OpenConnection = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
self.__Connections.Open = self.DoorAttributes.Relay.Open:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Open()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__GoalYLevelConnection = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
self.__Connections.GoalYLevel = self.ElevatorAttributes.Relay.GoalYLevel:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__GoalYLevel()
|
||||
end
|
||||
end)
|
||||
|
||||
self.__GoalConnection = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
self.__Connections.Goal = self.ElevatorAttributes.Relay.Goal:GetPropertyChangedSignal("Value"):Connect(function()
|
||||
if not self.ElevatorAttributes.Stopped.Value then
|
||||
self:__Goal()
|
||||
end
|
||||
@@ -127,17 +128,17 @@ function Relay:BulkConnect()
|
||||
end
|
||||
|
||||
function Relay:BulkDisconnect()
|
||||
if self.__ReadyConnection and self.__ReadyConnection.Connected then
|
||||
self.__ReadyConnection:Disconnect()
|
||||
if self.__Connections.Ready and self.__Connections.Ready.Connected then
|
||||
self.__Connections.Ready:Disconnect()
|
||||
end
|
||||
if self.__OpenConnection and self.__OpenConnection.Connected then
|
||||
self.__OpenConnection:Disconnect()
|
||||
if self.__Connections.Open and self.__Connections.Open.Connected then
|
||||
self.__Connections.Open:Disconnect()
|
||||
end
|
||||
if self.__GoalYLevelConnection and self.__GoalYLevelConnection.Connected then
|
||||
self.__GoalYLevelConnection:Disconnect()
|
||||
if self.__Connections.GoalYLevel and self.__Connections.GoalYLevel.Connected then
|
||||
self.__Connections.GoalYLevel:Disconnect()
|
||||
end
|
||||
if self.__GoalConnection and self.__GoalConnection.Connected then
|
||||
self.__GoalConnection:Disconnect()
|
||||
if self.__Connections.Goal and self.__Connections.Goal.Connected then
|
||||
self.__Connections.Goal:Disconnect()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -28,8 +28,10 @@ type ElevatorAttributes = {
|
||||
}
|
||||
|
||||
type DoorAttributes = {
|
||||
Relay: {
|
||||
Open: BoolValue
|
||||
}
|
||||
}
|
||||
|
||||
export type RelayAlgorithmConstructor = ClassConstructor
|
||||
|
||||
@@ -78,7 +80,7 @@ end
|
||||
function RelayAlgorithm:AddFloor(ElevatorGoingUp, RequestedLevel)
|
||||
self:RawInsert(ElevatorGoingUp, RequestedLevel)
|
||||
|
||||
return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Open.Value
|
||||
return not self.ElevatorAttributes.Moving.Value and self.DoorAttributes.Relay.Open.Value
|
||||
end
|
||||
|
||||
return RelayAlgorithm
|
||||
@@ -179,7 +179,18 @@ local Attributes = Elevator.Attributes
|
||||
|
||||
--My clever math function for determining if the elevator goal is to move upwards or not
|
||||
local function ElevatorGoingUpDirection(Floor: number, RequestedFloor: number): boolean
|
||||
return -(Floor-RequestedFloor)>0
|
||||
--(Floor-RequestedFloor)>0
|
||||
return -Floor+RequestedFloor>0 --Simplified equation for less computation
|
||||
end
|
||||
|
||||
local function OpenElevatorDoorsSafe(self: ClassConstructor)
|
||||
print("Relay.Open=",Doors.Attributes.Relay.Open.Value,"Relay.Ready=",Attributes.Relay.Ready.Value)
|
||||
|
||||
if not Doors.Attributes.Relay.Open.Value and not Attributes.Relay.Ready.Value then
|
||||
task.spawn(function()
|
||||
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
|
||||
@@ -193,6 +204,10 @@ local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number
|
||||
if Attributes.CurrentFloor.Value == ButtonFloor and Attributes.Relay.Goal.Value == ButtonFloor then
|
||||
FloorTracker:Disconnect()
|
||||
|
||||
--If the elevator is fully stopped at the floor and the hall button or the same cab button for the same floor is pressed,
|
||||
--then open the doors
|
||||
OpenElevatorDoorsSafe(self)
|
||||
|
||||
self.ButtonsConstructor:__DeactivateButton(ButtonTree.Inst :: BasePart, (ButtonTree.Inst :: BasePart):FindFirstChild("Glass") :: BasePart?)
|
||||
ButtonTree.Prompt.Enabled = true
|
||||
end
|
||||
@@ -262,6 +277,20 @@ local function IterateButtons(self: ClassConstructor, ButtonsTagsConstructor: Bu
|
||||
end
|
||||
end
|
||||
|
||||
local function ElevatorInit(self: ClassConstructor)
|
||||
task.spawn(function()
|
||||
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
|
||||
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
|
||||
|
||||
--Open the elevator doors on server start
|
||||
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
|
||||
|
||||
--Some hacks
|
||||
Doors.Attributes.Relay.Open.Value = true
|
||||
self:RequestLevel(1)
|
||||
end)
|
||||
end
|
||||
|
||||
function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, LandingDoors)
|
||||
local self = {} :: Constructor_Return_Props
|
||||
|
||||
@@ -302,29 +331,31 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
|
||||
|
||||
self.BoxAttachment,
|
||||
self.BoxAlignPosition,
|
||||
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity)
|
||||
self.BoxAlignOrientation = ElevatorMover(
|
||||
self.ElevatorBox_1960,
|
||||
Vector3.new(self.ElevatorBox_1960.Position.X, LevelingModule.Leveling[1], self.ElevatorBox_1960.Position.Z),
|
||||
Elevator.Responsiveness,
|
||||
Elevator.MaxVelocity
|
||||
)
|
||||
|
||||
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes.Relay)
|
||||
self.RelayConstructor = Relay.constructor(self.RelayAlgorithmConstructor, Attributes, Doors.Attributes, LevelingModule, self.BoxAlignPosition, self.ElevatorBox_1960)
|
||||
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes)
|
||||
self.RelayConstructor = Relay.constructor(
|
||||
self.RelayAlgorithmConstructor,
|
||||
Attributes,
|
||||
Doors.Attributes,
|
||||
LevelingModule,
|
||||
self.BoxAlignPosition,
|
||||
self.ElevatorBox_1960
|
||||
)
|
||||
|
||||
self.RelayConstructor:BulkConnect()
|
||||
|
||||
local ClassConstructor = setmetatable(self, Elevator)
|
||||
IterateButtons(ClassConstructor, ButtonsTagsConstructor)
|
||||
|
||||
--Open the elevator doors on server start
|
||||
task.spawn(function()
|
||||
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
|
||||
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
|
||||
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
|
||||
|
||||
--Some hacks
|
||||
Doors.Attributes.Relay.Open.Value = true
|
||||
end)
|
||||
|
||||
print(LevelingModule.LevelingBetween)
|
||||
|
||||
ElevatorInit(self)
|
||||
print(`🔝 {Elevator.Name} initialized and ready`)
|
||||
|
||||
return ClassConstructor
|
||||
end
|
||||
|
||||
@@ -332,7 +363,7 @@ function Elevator:Leveled(RequestedLevel)
|
||||
(self.__MovingConnection :: RBXScriptConnection):Disconnect()
|
||||
Attributes.Moving.Value = false
|
||||
Attributes.CurrentFloor.Value = RequestedLevel
|
||||
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
|
||||
self.BoxAlignPosition.MaxVelocity = 0
|
||||
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
|
||||
|
||||
task.wait(Elevator.QueueWaitTime)
|
||||
@@ -351,7 +382,9 @@ function Elevator:Leveling(RequestedLevel)
|
||||
end
|
||||
|
||||
function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
|
||||
if ElevatorPositionY>=LevelingModule.Leveling[Attributes.CurrentFloor.Value+1] then
|
||||
local NextLevelBetweenFloors: number? = LevelingModule.LevelingBetween[Attributes.CurrentFloor.Value+1]
|
||||
|
||||
if NextLevelBetweenFloors and ElevatorPositionY>=NextLevelBetweenFloors then
|
||||
Attributes.CurrentFloor.Value+=1
|
||||
|
||||
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
|
||||
@@ -360,7 +393,7 @@ function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
|
||||
end
|
||||
|
||||
function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
|
||||
if ElevatorPositionY<=LevelingModule.Leveling[Attributes.CurrentFloor.Value-1] then
|
||||
if ElevatorPositionY<=LevelingModule.LevelingBetween[Attributes.CurrentFloor.Value-1] then
|
||||
Attributes.CurrentFloor.Value-=1
|
||||
|
||||
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
|
||||
@@ -374,7 +407,9 @@ function Elevator:__MovingHeartbeat(GoingUp, GoalFloor_Y)
|
||||
if self.__MovingConnection and self.__MovingConnection.Connected then
|
||||
self.__MovingConnection:Disconnect()
|
||||
end
|
||||
|
||||
self.MOConstructor:UpdateCFrame()
|
||||
self.BoxAlignPosition.MaxVelocity = Elevator.MaxVelocity
|
||||
|
||||
local Delta = 0
|
||||
local DoorsOpeningEvent = false
|
||||
@@ -445,7 +480,7 @@ end
|
||||
function Elevator:RequestLevel(RequestedLevel)
|
||||
local GoalFloor_Y: number? = LevelingModule.Leveling[RequestedLevel]
|
||||
|
||||
if GoalFloor_Y and RequestedLevel ~= Attributes.CurrentFloor.Value then
|
||||
if GoalFloor_Y --[[and RequestedLevel ~= Attributes.CurrentFloor.Value]] then
|
||||
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
|
||||
local Proceeding = self.RelayAlgorithmConstructor:AddFloor(ElevatorGoingUp, RequestedLevel)
|
||||
|
||||
@@ -456,7 +491,6 @@ function Elevator:RequestLevel(RequestedLevel)
|
||||
warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
@@ -135,8 +135,8 @@ function Lights:Init()
|
||||
LightProperties.Prompt.ActionText = "Toggle On"
|
||||
end
|
||||
|
||||
local ActivatedMaterial = table.find(EnumMaterialsNames, LightProperties.ActivatedMaterial) :: number
|
||||
local DeactivatedMaterial = table.find(EnumMaterialsNames, LightProperties.DeactivatedMaterial) :: number
|
||||
local ActivatedMaterial = table.find(EnumMaterialsNames, LightProperties.ActivatedMaterial)
|
||||
local DeactivatedMaterial = table.find(EnumMaterialsNames, LightProperties.DeactivatedMaterial)
|
||||
|
||||
if not ActivatedMaterial then
|
||||
ActivatedMaterial = 1
|
||||
@@ -147,8 +147,8 @@ function Lights:Init()
|
||||
warn()
|
||||
end
|
||||
|
||||
LightProperties.ActivatedMaterial = EnumMaterialsNames[ActivatedMaterial]
|
||||
LightProperties.DeactivatedMaterial = EnumMaterialsNames[DeactivatedMaterial]
|
||||
LightProperties.ActivatedMaterial = EnumMaterialsNames[ActivatedMaterial :: number]
|
||||
LightProperties.DeactivatedMaterial = EnumMaterialsNames[DeactivatedMaterial :: number]
|
||||
|
||||
ToggleSwitch(EnabledState, LightProperties :: LightPropertiesSafe, false)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user