work on doors and think of algorithm more

This commit is contained in:
2024-05-11 18:26:02 -04:00
parent 4043195793
commit 438abcca56
3 changed files with 94 additions and 58 deletions

View File

@@ -10,6 +10,7 @@ local Load = Main:WaitForChild("Load")
local Tween = require(Storage:WaitForChild("Tween")) local Tween = require(Storage:WaitForChild("Tween"))
local Tags = require(Load:WaitForChild("Tags")) local Tags = require(Load:WaitForChild("Tags"))
local SoundEnums = require(Main:WaitForChild("Enums"):WaitForChild("Sounds"))
type DoorSensors = { type DoorSensors = {
[string]: BasePart [string]: BasePart
@@ -27,13 +28,16 @@ type Impl_Constructor = {
type Constructor_Fun = (FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor type Constructor_Fun = (FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor
type Impl_Static_Props = { type Impl_Static_Props = {
Closed: boolean,
Sensors: boolean, Sensors: boolean,
Door1Stopped_X: Vector3, Door1Stopped_X: Vector3,
Door2Stopped_X: Vector3, Door2Stopped_X: Vector3,
ElevatorDoorTime: number, ElevatorDoorTime: number,
ElevatorDoorStyle: Enum.EasingStyle, ElevatorDoorStyle: Enum.EasingStyle,
__DontLeakMemory: RBXScriptConnection? __DontLeakMemory: RBXScriptConnection?,
Attributes: {
DoorsOpen: BoolValue
}
} }
type Constructor_Return_Props = { type Constructor_Return_Props = {
FloorDoorsTags: Tags.LandingTags, FloorDoorsTags: Tags.LandingTags,
@@ -56,13 +60,19 @@ export type DoorConstructor = ClassConstructor
local Doors = {} :: Impl_Constructor local Doors = {} :: Impl_Constructor
Doors.__index = Doors Doors.__index = Doors
Doors.Closed = true
Doors.Sensors = true Doors.Sensors = true
Doors.Door1Stopped_X = Vector3.xAxis*2.9 Doors.Door1Stopped_X = Vector3.xAxis*2.9
Doors.Door2Stopped_X = Vector3.xAxis*5.8 Doors.Door2Stopped_X = Vector3.xAxis*5.8
Doors.ElevatorDoorTime = 3 Doors.ElevatorDoorTime = 3
Doors.ElevatorDoorStyle = Enum.EasingStyle.Quad Doors.ElevatorDoorStyle = Enum.EasingStyle.Quad
Doors.__DontLeakMemory = nil
Doors.Attributes = {
DoorsOpen = Instance.new("BoolValue") :: BoolValue
}
Doors.Attributes.DoorsOpen.Value = false
local Attributes = Doors.Attributes
function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorDoor2, ElevatorDoorSensor) function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorDoor2, ElevatorDoorSensor)
local DoorTween1 = Tween.constructor(nil, ElevatorDoor1) local DoorTween1 = Tween.constructor(nil, ElevatorDoor1)
@@ -73,7 +83,7 @@ function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorD
} }
local DoorClosingClick = Instance.new("Sound") :: Sound local DoorClosingClick = Instance.new("Sound") :: Sound
DoorClosingClick.SoundId = "rbxassetid://16357740945" DoorClosingClick.SoundId = SoundEnums.Otis1960.DoorClosingClick
DoorClosingClick.Volume = .1 DoorClosingClick.Volume = .1
DoorClosingClick.Parent = ElevatorDoor2 DoorClosingClick.Parent = ElevatorDoor2
@@ -164,7 +174,7 @@ local function DoorsAnimationFloor(FloorDoors: {Instance?}, Floor: number, openi
return Door1Tween_Floor, Door2Tween_Floor return Door1Tween_Floor, Door2Tween_Floor
end end
local function ElevatorDoorsAnimation(self: ClassConstructor, opening: boolean?, activated_via_censor: boolean?): Tween? local function ElevatorDoorsAnimation(self: ClassConstructor, opening: boolean?, activated_via_censor: boolean?): (Tween?, Tween?)
--Roblox physics will freak out --Roblox physics will freak out
self.ElevatorDoor1.CanCollide = false self.ElevatorDoor1.CanCollide = false
self.ElevatorDoor2.CanCollide = false self.ElevatorDoor2.CanCollide = false
@@ -191,6 +201,8 @@ local function ElevatorDoorsAnimation(self: ClassConstructor, opening: boolean?,
break break
end end
end end
Attributes.DoorsOpen.Value = true
else else
local TweenTime = activated_via_censor and sensor_opening_speed or opening and opening_speed or Doors.ElevatorDoorTime local TweenTime = activated_via_censor and sensor_opening_speed or opening and opening_speed or Doors.ElevatorDoorTime
@@ -223,7 +235,7 @@ local function ElevatorDoorsAnimation(self: ClassConstructor, opening: boolean?,
end end
end) end)
return Door2Tween return Door1Tween, Door2Tween
end end
return nil return nil
@@ -245,10 +257,10 @@ end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2) function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
local Step = nil local Step = nil
if Doors.Sensors and Doors.Closed then if Doors.Sensors and not Attributes.DoorsOpen.Value then
raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)} raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)}
Step = RS.Heartbeat:ConnectParallel(function(_dt) Step = RS.Heartbeat:Connect(function(_dt)
local DoorSensor = workspace:Raycast(self.DoorSensor.Start.Position, self.DoorSensor.End.Position, raycastParams) local DoorSensor = workspace:Raycast(self.DoorSensor.Start.Position, self.DoorSensor.End.Position, raycastParams)
if DoorSensor and DoorSensor.Instance and DoorSensor.Instance:IsA("BasePart") then if DoorSensor and DoorSensor.Instance and DoorSensor.Instance:IsA("BasePart") then
@@ -270,14 +282,14 @@ function Doors:ToggleElevatorDoors(opening, floor)
if Doors.Closed then if Doors.Closed then
Doors.Closed = not Doors.Closed Doors.Closed = not Doors.Closed
else else
print("Doors are already closed, doing nothing") warn("Doors are already closed, doing nothing")
return return
end end
else else
if not Doors.Closed then if not Doors.Closed then
Doors.Closed = not Doors.Closed Doors.Closed = not Doors.Closed
else else
print("Doors are already open, doing nothing") warn("Doors are already open, doing nothing")
return return
end end
end end
@@ -286,11 +298,17 @@ function Doors:ToggleElevatorDoors(opening, floor)
if FloorDoorsObjects then if FloorDoorsObjects then
DoorsAnimationFloor(FloorDoorsObjects, floor, opening) DoorsAnimationFloor(FloorDoorsObjects, floor, opening)
end end
local Door2Tween = ElevatorDoorsAnimation(self, opening)
local Door2Tween, Door1Tween = ElevatorDoorsAnimation(self, opening)
if Door2Tween then if Door2Tween then
Door2Tween.Completed:Wait() Door2Tween.Completed:Wait()
Attributes.DoorsOpen.Value = opening ~= nil and opening or false
elseif Door1Tween then
Door1Tween.Completed:Wait()
Attributes.DoorsOpen.Value = opening ~= nil and opening or false
end end
self.ElevatorDoor1.CanCollide = true self.ElevatorDoor1.CanCollide = true
self.ElevatorDoor2.CanCollide = true self.ElevatorDoor2.CanCollide = true

View File

@@ -61,7 +61,8 @@ type Impl_Static_Props = {
Attributes: { Attributes: {
PassingFloor: IntValue, PassingFloor: IntValue,
CurrentFloor: IntValue, CurrentFloor: IntValue,
Moving: BoolValue Moving: BoolValue,
GoingUp: BoolValue
}, },
Events: { Events: {
ButtonActivated: BindableEvent ButtonActivated: BindableEvent
@@ -81,9 +82,9 @@ type Constructor_Return_Props = {
BoxAttachment: Attachment, BoxAttachment: Attachment,
BoxAlignPosition: AlignPosition, BoxAlignPosition: AlignPosition,
BoxAlignOrientation: AlignOrientation, BoxAlignOrientation: AlignOrientation,
ElevatorDoors: Doors.DoorConstructor, ElevatorDoorsConstructor: Doors.DoorConstructor,
Ropes: {Instance}, Ropes: {Instance},
TractionRopes: TractionRopes.TractionRopesConstructor, TractionRopesConstructor: TractionRopes.TractionRopesConstructor,
Pulley: UnionOperation, Pulley: UnionOperation,
Pulley2: UnionOperation, Pulley2: UnionOperation,
Governor: UnionOperation, Governor: UnionOperation,
@@ -121,7 +122,8 @@ Otis1960.Colors = {
Otis1960.Attributes = { Otis1960.Attributes = {
PassingFloor = Instance.new("IntValue") :: IntValue, PassingFloor = Instance.new("IntValue") :: IntValue,
CurrentFloor = Instance.new("IntValue") :: IntValue, CurrentFloor = Instance.new("IntValue") :: IntValue,
Moving = Instance.new("BoolValue") :: BoolValue Moving = Instance.new("BoolValue") :: BoolValue,
GoingUp = Instance.new("BoolValue") :: BoolValue
} }
Otis1960.Events = { Otis1960.Events = {
@@ -131,9 +133,15 @@ Otis1960.Events = {
Otis1960.Attributes.PassingFloor.Value = 1 Otis1960.Attributes.PassingFloor.Value = 1
Otis1960.Attributes.CurrentFloor.Value = 1 Otis1960.Attributes.CurrentFloor.Value = 1
Otis1960.Attributes.Moving.Value = false Otis1960.Attributes.Moving.Value = false
Otis1960.Attributes.GoingUp.Value = false
local Attributes = Otis1960.Attributes local Attributes = Otis1960.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
end
local ButtonsConstructor = Buttons.constructor(Otis1960.Attributes, Otis1960.Events, Otis1960.Colors) local ButtonsConstructor = Buttons.constructor(Otis1960.Attributes, Otis1960.Events, Otis1960.Colors)
local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonTreeValues, ButtonName: string, ButtonTree) local function HookButtons(self: ClassConstructor, ButtonNameType: Enums.ButtonTreeValues, ButtonName: string, ButtonTree)
@@ -198,8 +206,8 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
MachineRoom = self.MachineRoom MachineRoom = self.MachineRoom
} :: MovingObjects.InstanceTree) } :: MovingObjects.InstanceTree)
self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays) self.HallDisplaysConstructor = HallDisplays.constructor(Attributes.CurrentFloor, self.HallDisplays)
self.ElevatorDoors = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor) self.ElevatorDoorsConstructor = Doors.constructor(LandingDoors, self.ElevatorBox_1960, self.ElevatorDoor1, self.ElevatorDoor2, self.ElevatorDoorSensor)
self.TractionRopes = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, Leveling) self.TractionRopesConstructor = TractionRopes.constructor(self.Ropes, self.ElevatorBox_1960, Leveling)
self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Otis1960.Sounds, Otis1960.Colors) self.LanternsConstructor = Lanterns.constructor(LanternDisplay, LanternsTags, Otis1960.Sounds, Otis1960.Colors)
@@ -219,13 +227,19 @@ function Otis1960.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
task.spawn(function() task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value) self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
self.ElevatorDoors:ToggleElevatorDoors(true, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, Attributes.CurrentFloor.Value)
end) end)
print(`🔝 {Otis1960.Name} initialized and ready`) print(`🔝 {Otis1960.Name} initialized and ready`)
return ClassConstructor return ClassConstructor
end end
local FloorQueue = {}
local function CheckQueue()
end
local function FloorLeveling(self: ClassConstructor, RequestedLevel: number) local function FloorLeveling(self: ClassConstructor, RequestedLevel: number)
self.BoxAlignPosition.MaxVelocity = 1 self.BoxAlignPosition.MaxVelocity = 1
self.LanternsConstructor:Toggle(true, RequestedLevel) self.LanternsConstructor:Toggle(true, RequestedLevel)
@@ -239,10 +253,14 @@ local function FloorLeveled(self: ClassConstructor, RequestedLevel: number)
self.BoxAlignPosition.MaxVelocity = Otis1960.MaxVelocity self.BoxAlignPosition.MaxVelocity = Otis1960.MaxVelocity
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
end end
local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number)
if ElevatorPositionY>=Leveling[Attributes.CurrentFloor.Value+1] then local CF = Attributes.CurrentFloor.Value
if ElevatorPositionY>=Leveling[CF+1] then
Attributes.CurrentFloor.Value+=1 Attributes.CurrentFloor.Value+=1
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
@@ -259,10 +277,8 @@ local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: numbe
end end
end end
local FloorQueue = {}
function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp) function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
self.ElevatorDoors:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value)
if self.__MovingConnection and self.__MovingConnection.Connected then if self.__MovingConnection and self.__MovingConnection.Connected then
self.__MovingConnection:Disconnect() self.__MovingConnection:Disconnect()
@@ -289,7 +305,7 @@ function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
local BoxAlignY: number = self.BoxAlignPosition.Position.Y local BoxAlignY: number = self.BoxAlignPosition.Position.Y
local ElevatorVelocityY: number = self.ElevatorBox_1960:GetVelocityAtPosition(ElevatorPosition).Y local ElevatorVelocityY: number = self.ElevatorBox_1960:GetVelocityAtPosition(ElevatorPosition).Y
self.TractionRopes:Move(27, self.ElevatorBox_1960.Position) self.TractionRopesConstructor:Move(27, self.ElevatorBox_1960.Position)
self.MOConstructor:Frame_Pullies(Delta, ElevatorVelocityY) self.MOConstructor:Frame_Pullies(Delta, ElevatorVelocityY)
--Kill the connection --Kill the connection
@@ -301,7 +317,7 @@ function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then
DoorsOpeningEvent = true DoorsOpeningEvent = true
self.ElevatorDoors:ToggleElevatorDoors(true, RequestedLevel) self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, RequestedLevel)
end end
end end
@@ -316,7 +332,7 @@ function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Otis1960.DoorOpeningDistance then
DoorsOpeningEvent = true DoorsOpeningEvent = true
self.ElevatorDoors:ToggleElevatorDoors(true, RequestedLevel) self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, RequestedLevel)
end end
end end
@@ -329,19 +345,17 @@ function Otis1960:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z) self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end end
--My clever math function for determining if the elevator goal is to move upwards or not
local function ElevatorDirectionGoingUp(Floor: number, RequestedFloor: number): boolean
return -(Floor-RequestedFloor)>0
end
function Otis1960:RequestLevel(RequestedLevel) function Otis1960:RequestLevel(RequestedLevel)
local GoalLevelVEC: number = Leveling[RequestedLevel] local GoalLevelVEC: number = Leveling[RequestedLevel]
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then
local GoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
if Attributes.Moving.Value then if Attributes.Moving.Value then
else else
self:__RequestFloor(GoalLevelVEC, RequestedLevel, ElevatorDirectionGoingUp(Attributes.CurrentFloor.Value, RequestedLevel)) Attributes.GoingUp.Value = GoingUp
self:__RequestFloor(GoalLevelVEC, RequestedLevel, GoingUp)
end end
else else
warn(`[{Otis1960.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`) warn(`[{Otis1960.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)

View File

@@ -8,12 +8,16 @@ export type Otis1960Sounds = typeof(SoundEnums.Otis1960)
export type Otis1960LanternChimeDirection = "rbxassetid://16990287228" export type Otis1960LanternChimeDirection = "rbxassetid://16990287228"
export type Otis1960LanternChimeLanding = "rbxassetid://16990290265" export type Otis1960LanternChimeLanding = "rbxassetid://16990290265"
export type Otis1960DoorClosingClick = "rbxassetid://16357740945"
export type Otis1960SoundValues = Otis1960LanternChimeDirection | Otis1960LanternChimeLanding export type Otis1960SoundValues = Otis1960LanternChimeDirection |
Otis1960LanternChimeLanding |
Otis1960DoorClosingClick
SoundEnums.Otis1960 = { SoundEnums.Otis1960 = {
LanternChimeDirection = "rbxassetid://16990287228" :: Otis1960LanternChimeDirection, LanternChimeDirection = "rbxassetid://16990287228" :: Otis1960LanternChimeDirection,
LanternChimeLanding = "rbxassetid://16990290265" :: Otis1960LanternChimeLanding LanternChimeLanding = "rbxassetid://16990290265" :: Otis1960LanternChimeLanding,
DoorClosingClick = "rbxassetid://16357740945" :: Otis1960DoorClosingClick
} }
return SoundEnums return SoundEnums