Physical Relays

This commit is contained in:
2024-06-03 01:42:05 -04:00
parent b58febf7ec
commit 9fe7fa1621
6 changed files with 75 additions and 40 deletions

View File

@@ -0,0 +1,3 @@
--!optimize 2
--!native
--!strict

View File

@@ -35,10 +35,9 @@ type Impl_Static_Props = {
AestheticDeactivateTime: number AestheticDeactivateTime: number
} }
type Constructor_Fun = (ElevatorAttributes: ElevatorAttributes, ElevatorEvents: ElevatorEvents, ElevatorButtonColors: ElevatorButtonColors) -> ClassConstructor type Constructor_Fun = (ElevatorAttributes: ElevatorAttributes, ElevatorButtonColors: ElevatorButtonColors) -> ClassConstructor
type Constructor_Return_Props = { type Constructor_Return_Props = {
ElevatorAttributes: ElevatorAttributes, ElevatorAttributes: ElevatorAttributes,
ElevatorEvents: ElevatorEvents,
ElevatorButtonColors: ElevatorButtonColors ElevatorButtonColors: ElevatorButtonColors
} }
@@ -69,10 +68,9 @@ ButtonFunctions.ButtonHoldDuration = .30
ButtonFunctions.AestheticDeactivateTime = .30 ButtonFunctions.AestheticDeactivateTime = .30
--ButtonTags.ButtonsConstructor --ButtonTags.ButtonsConstructor
function ButtonFunctions.constructor(ElevatorAttributes, ElevatorEvents, ElevatorButtonColors) function ButtonFunctions.constructor(ElevatorAttributes, ElevatorButtonColors)
return setmetatable({ return setmetatable({
ElevatorAttributes = ElevatorAttributes, ElevatorAttributes = ElevatorAttributes,
ElevatorEvents = ElevatorEvents,
ElevatorButtonColors = ElevatorButtonColors ElevatorButtonColors = ElevatorButtonColors
}, ButtonFunctions) }, ButtonFunctions)
end end
@@ -84,7 +82,6 @@ function ButtonFunctions:CarButton(ButtonID, ButtonTree, Callback, Fallback)
local Prompt = PromptModule.constructor(ButtonTree.Prompt :: ProximityPrompt, ButtonTree.Inst :: Instance) local Prompt = PromptModule.constructor(ButtonTree.Prompt :: ProximityPrompt, ButtonTree.Inst :: Instance)
Prompt:Triggered(function(Player: Player, _, __) Prompt:Triggered(function(Player: Player, _, __)
self.ElevatorEvents.ButtonActivated:Fire(Enums.Button.Car, ButtonID, self, ButtonTree)
self:AestheticActivateButton(ButtonTree.Inst :: BasePart) self:AestheticActivateButton(ButtonTree.Inst :: BasePart)
if DecodedCarFloorTag == self.ElevatorAttributes.CurrentFloor.Value then if DecodedCarFloorTag == self.ElevatorAttributes.CurrentFloor.Value then
@@ -107,7 +104,6 @@ function ButtonFunctions:LandingButton(ButtonID, ButtonTree, Callback, Fallback)
local Prompt = PromptModule.constructor(ButtonTree.Prompt :: ProximityPrompt, ButtonTree.Inst :: Instance) local Prompt = PromptModule.constructor(ButtonTree.Prompt :: ProximityPrompt, ButtonTree.Inst :: Instance)
Prompt:Triggered(function(Player: Player, _, __) Prompt:Triggered(function(Player: Player, _, __)
self.ElevatorEvents.ButtonActivated:Fire(Enums.Button.Landing, ButtonID, self, ButtonTree)
self:AestheticActivateButton(ButtonTree.Inst :: BasePart) self:AestheticActivateButton(ButtonTree.Inst :: BasePart)
if DecodedHallFloorTag == self.ElevatorAttributes.CurrentFloor.Value then if DecodedHallFloorTag == self.ElevatorAttributes.CurrentFloor.Value then
@@ -130,13 +126,12 @@ function ButtonFunctions:SpecialButton(ButtonID, ButtonName, ButtonTree, Callbac
local Toggled = false local Toggled = false
Prompt:Triggered(function(Player: Player, _, __) Prompt:Triggered(function(Player: Player, _, __)
self.ElevatorEvents.ButtonActivated:Fire(Enums.SpecialButton.Stop, ButtonID, self, ButtonTree)
Toggled = not Toggled Toggled = not Toggled
Callback(Toggled) Callback(Toggled)
end) end)
else else
warn() warn(``)
end end
end end

View File

@@ -15,18 +15,18 @@ local SoundEnums = require(Main:WaitForChild("Enums"):WaitForChild("Sounds"))
type DoorSensors = { type DoorSensors = {
[string]: BasePart [string]: BasePart
} }
type CustomTween = Tween.TweenClass type CustomTween<T,U> = Tween.TweenClass<T,U>
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) type ClassConstructor<T,U> = typeof(setmetatable({} :: Constructor_Return_Props<T,U>, {} :: Impl_Constructor))
type Impl_Constructor = { type Impl_Constructor = {
__index: Impl_Constructor, __index: Impl_Constructor,
constructor: Constructor_Fun, constructor: Constructor_Fun,
--Class functions --Class functions
__DetectSensorHit: (self: ClassConstructor, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection, __DetectSensorHit: <T,U>(self: ClassConstructor<T,U>, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection,
ToggleElevatorDoorsAsync: (self: ClassConstructor, opening: boolean, floor: number) -> () ToggleElevatorDoorsAsync: <T,U>(self: ClassConstructor<T,U>, opening: boolean, floor: number) -> ()
} & Impl_Static_Props } & Impl_Static_Props
type Constructor_Fun = (FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor type Constructor_Fun = <T,U>(FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor<T,U>
type Impl_Static_Props = { type Impl_Static_Props = {
Sensors: boolean, Sensors: boolean,
Door1Stopped_X: Vector3, Door1Stopped_X: Vector3,
@@ -37,10 +37,11 @@ type Impl_Static_Props = {
Attributes: {} & RelayAttributes Attributes: {} & RelayAttributes
} }
type Constructor_Return_Props = {
type Constructor_Return_Props<T,U> = {
FloorDoorsTags: Tags.LandingTags, FloorDoorsTags: Tags.LandingTags,
DoorTween1: CustomTween, DoorTween1: CustomTween<T,U>,
DoorTween2: CustomTween, DoorTween2: CustomTween<T,U>,
DoorSensor: DoorSensors, DoorSensor: DoorSensors,
ElevatorBox: BasePart, ElevatorBox: BasePart,
ElevatorDoor1: BasePart, ElevatorDoor1: BasePart,
@@ -59,7 +60,7 @@ export type RelayAttributes = {
} }
} }
export type DoorConstructor = ClassConstructor export type DoorConstructor<T,U> = ClassConstructor<T,U>
local Doors = {} :: Impl_Constructor local Doors = {} :: Impl_Constructor
Doors.__index = Doors Doors.__index = Doors
@@ -172,7 +173,7 @@ local function DoorsAnimationFloor(FloorDoors: {Instance?}, Floor: number, openi
return Door1Tween_Floor, Door2Tween_Floor return Door1Tween_Floor, Door2Tween_Floor
end end
local function ElevatorDoorsAnimationAsync(self: ClassConstructor, opening: boolean?, activated_via_censor: boolean?): (Tween?, Tween?) local function ElevatorDoorsAnimationAsync<T,U>(self: ClassConstructor<T,U>, 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
@@ -254,7 +255,7 @@ for n: number = 1, #workspace_items do
end end
end end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2) function Doors:__DetectSensorHit<T,U>(DoorTween1, DoorTween2)
local Step = nil local Step = nil
if Doors.Sensors and Attributes.Relay.Open.Value then if Doors.Sensors and Attributes.Relay.Open.Value then
@@ -276,7 +277,7 @@ function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
return Step return Step
end end
function Doors:ToggleElevatorDoorsAsync(opening, floor) function Doors:ToggleElevatorDoorsAsync<T,U>(opening, floor)
--short circuiting central --short circuiting central
if opening then if opening then
if Attributes.Relay.Open.Value then if Attributes.Relay.Open.Value then

View File

@@ -10,6 +10,7 @@ local LoadDir = MainDir:WaitForChild("Load")
local SoundEnums = require(EnumsDir:WaitForChild("Sounds")) local SoundEnums = require(EnumsDir:WaitForChild("Sounds"))
local Storage = game:GetService("ReplicatedStorage") local Storage = game:GetService("ReplicatedStorage")
local SS = game:GetService("SoundService")
local Tween = require(Storage:WaitForChild("Tween")) local Tween = require(Storage:WaitForChild("Tween"))
local Enums = require(Storage:WaitForChild("Enums")) local Enums = require(Storage:WaitForChild("Enums"))
@@ -29,12 +30,43 @@ type Constructor_Return_Props = {
RelayTagList: Tags.RelayDictionary RelayTagList: Tags.RelayDictionary
} }
type SoundEffects = {
SoundGroup: SoundGroup,
}
type OtimSoundEffects = SoundEffects & {
EqualizerSoundEffect: EqualizerSoundEffect
}
export type PhysicalRelayConstructor = ClassConstructor export type PhysicalRelayConstructor = ClassConstructor
local PhysicalRelay = {} :: Impl_Constructor local PhysicalRelay = {} :: Impl_Constructor
PhysicalRelay.__index = PhysicalRelay PhysicalRelay.__index = PhysicalRelay
local function OtimLowPassSoundEffect(Model: Enums.ElevatorValues): OtimSoundEffects
local SoundGroup = Instance.new("SoundGroup") :: SoundGroup
SoundGroup.Volume = .5
local EqualizerSoundEffect = Instance.new("EqualizerSoundEffect") :: EqualizerSoundEffect
EqualizerSoundEffect.HighGain = -20
EqualizerSoundEffect.LowGain = 10
EqualizerSoundEffect.MidGain = -10
EqualizerSoundEffect.Parent = SoundGroup
SoundGroup.Parent = SS
return {
SoundGroup = SoundGroup,
EqualizerSoundEffect = EqualizerSoundEffect
}
end
function PhysicalRelay.constructor(ElevatorModel, RelayTagList) function PhysicalRelay.constructor(ElevatorModel, RelayTagList)
local SoundEffects: OtimSoundEffects?
if ElevatorModel == Enums.Elevator.Otis1960 then
SoundEffects = OtimLowPassSoundEffect(Elevators)
end
--Idk how to construct an ideal way to identify these, --Idk how to construct an ideal way to identify these,
--Hard coding it is --Hard coding it is
for RelayName, RelayProperties in RelayTagList do for RelayName, RelayProperties in RelayTagList do
@@ -43,10 +75,12 @@ function PhysicalRelay.constructor(ElevatorModel, RelayTagList)
local RelayAudioActivated = Instance.new("Sound") :: Sound local RelayAudioActivated = Instance.new("Sound") :: Sound
local RelayAudioDeActivated = Instance.new("Sound") :: Sound local RelayAudioDeActivated = Instance.new("Sound") :: Sound
RelayAudioActivated.Volume = .5
RelayAudioDeActivated.Volume = .5
if ElevatorModel == Enums.Elevator.Otis1960 then if ElevatorModel == Enums.Elevator.Otis1960 then
RelayAudioActivated.SoundGroup = (SoundEffects :: OtimSoundEffects).SoundGroup
RelayAudioDeActivated.SoundGroup = (SoundEffects :: OtimSoundEffects).SoundGroup
if RelayProperties.Name == "240 V" or if RelayProperties.Name == "240 V" or
RelayProperties.Name == "440 V" or RelayProperties.Name == "440 V" or
RelayProperties.Name == "UP" or RelayProperties.Name == "UP" or
@@ -55,6 +89,8 @@ function PhysicalRelay.constructor(ElevatorModel, RelayTagList)
--Bigger relays --Bigger relays
RelayAudioActivated.SoundId = SoundEnums.Otis1960.BigRelayActivated RelayAudioActivated.SoundId = SoundEnums.Otis1960.BigRelayActivated
RelayAudioDeActivated.SoundId = SoundEnums.Otis1960.BigRelayDeActivated RelayAudioDeActivated.SoundId = SoundEnums.Otis1960.BigRelayDeActivated
RelayAudioActivated.Volume = .5
RelayAudioDeActivated.Volume = .5
RelayProperties.Unique = true RelayProperties.Unique = true
else else
--High placed relays for better spatial sound --High placed relays for better spatial sound
@@ -71,6 +107,10 @@ function PhysicalRelay.constructor(ElevatorModel, RelayTagList)
then then
RelayAudioActivated.SoundId = SoundEnums.Otis1960.RelayHighActivated RelayAudioActivated.SoundId = SoundEnums.Otis1960.RelayHighActivated
RelayAudioDeActivated.SoundId = SoundEnums.Otis1960.RelayDeActivated RelayAudioDeActivated.SoundId = SoundEnums.Otis1960.RelayDeActivated
if RelayProperties.Prompt then
RelayProperties.Prompt.MaxActivationDistance = 4
end
else else
--Low placed relays --Low placed relays
RelayAudioActivated.SoundId = SoundEnums.Otis1960.RelayLowActivated RelayAudioActivated.SoundId = SoundEnums.Otis1960.RelayLowActivated
@@ -78,6 +118,8 @@ function PhysicalRelay.constructor(ElevatorModel, RelayTagList)
end end
end end
RelayAudioActivated.RollOffMaxDistance = 130
RelayAudioActivated.RollOffMinDistance = 5
RelayAudioActivated.Parent = RelayProperties.Inst RelayAudioActivated.Parent = RelayProperties.Inst
RelayAudioDeActivated.Parent = RelayProperties.Inst RelayAudioDeActivated.Parent = RelayProperties.Inst
end end
@@ -98,8 +140,6 @@ end
local OtimAnimation = Tween.constructor(TweenInfo.new(.02, Enum.EasingStyle.Linear)) local OtimAnimation = Tween.constructor(TweenInfo.new(.02, Enum.EasingStyle.Linear))
local function OtimRelayAnimation(Relay: Tags.RelayProperties, State: boolean) local function OtimRelayAnimation(Relay: Tags.RelayProperties, State: boolean)
Relay.ActiveState = State
local RelayAnimation = OtimAnimation:Start(Relay.Inst :: BasePart, { local RelayAnimation = OtimAnimation:Start(Relay.Inst :: BasePart, {
CFrame = ( CFrame = (
Relay.Unique and CFrame.new((Relay.Inst :: BasePart).Position) or (Relay.Inst :: BasePart).CFrame Relay.Unique and CFrame.new((Relay.Inst :: BasePart).Position) or (Relay.Inst :: BasePart).CFrame
@@ -125,8 +165,10 @@ function PhysicalRelay:SetState(RelayName, State, ActivateImmediately)
if Relay then if Relay then
if Relay.Inst then if Relay.Inst then
if self.ElevatorModel == Enums.Elevator.Otis1960 then if State ~= Relay.ActiveState then
if State ~= Relay.ActiveState then if self.ElevatorModel == Enums.Elevator.Otis1960 then
Relay.ActiveState = State
if ActivateImmediately then if ActivateImmediately then
OtimRelayAnimation(Relay, State) OtimRelayAnimation(Relay, State)
else else

View File

@@ -91,9 +91,6 @@ type Impl_Static_Props = {
GoalYLevel: NumberValue, GoalYLevel: NumberValue,
ReadyForMoving: BoolValue, ReadyForMoving: BoolValue,
} }
},
Events: {
ButtonActivated: BindableEvent
} }
} }
@@ -173,10 +170,6 @@ Elevator.Attributes = {
}, },
} }
Elevator.Events = {
ButtonActivated = Instance.new("BindableEvent") :: BindableEvent
}
Elevator.Attributes.CurrentFloor.Value = 1 Elevator.Attributes.CurrentFloor.Value = 1
Elevator.Attributes.Moving.Value = false Elevator.Attributes.Moving.Value = false
Elevator.Attributes.GoingUp.Value = false Elevator.Attributes.GoingUp.Value = false
@@ -343,7 +336,7 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags, Elevator.Name) local ButtonsTagsConstructor = ButtonTags.constructor(TagsConstructor, ButtonsTags, Elevator.Name)
local Otis1960_Buttons = ButtonsTagsConstructor:CreatePromptButtons() local Otis1960_Buttons = ButtonsTagsConstructor:CreatePromptButtons()
self.ButtonsConstructor = Buttons.constructor(Elevator.Attributes, Elevator.Events, Elevator.Colors) self.ButtonsConstructor = Buttons.constructor(Elevator.Attributes, Elevator.Colors)
self.HallDisplaysConstructor:BindHallDisplays() self.HallDisplaysConstructor:BindHallDisplays()
@@ -393,8 +386,8 @@ local function Leveled(self: ClassConstructor, RequestedLevel: number)
--self.BoxAlignPosition.MaxVelocity = 0 --self.BoxAlignPosition.MaxVelocity = 0
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
self.PhysicalRelays:SetState("UP", false, false) self.PhysicalRelays:SetState("UP", false, false)
self.PhysicalRelays:SetState("DOWN", false, false) self.PhysicalRelays:SetState("DOWN", false, false)
self.PhysicalRelays:SetState("440 V", false, false) self.PhysicalRelays:SetState("440 V", false, false)
self.PhysicalRelays:SetState("240 V", false, false) self.PhysicalRelays:SetState("240 V", false, false)
@@ -413,6 +406,7 @@ end
local function Leveling(self: ClassConstructor, RequestedLevel: number) local function Leveling(self: ClassConstructor, RequestedLevel: number)
self.PhysicalRelays:SetState("440 V", false, false) self.PhysicalRelays:SetState("440 V", false, false)
self.BoxAlignPosition.MaxVelocity = 1 self.BoxAlignPosition.MaxVelocity = 1
self.LanternsConstructor:Toggle(true, RequestedLevel) self.LanternsConstructor:Toggle(true, RequestedLevel)
end end
@@ -451,7 +445,7 @@ function Elevator:__MovingHeartbeat(GoingUp, RequestedLevel)
Attributes.Relay.Goal.Value = RequestedLevel Attributes.Relay.Goal.Value = RequestedLevel
Attributes.Moving.Value = true Attributes.Moving.Value = true
self.PhysicalRelays:SetState(GoingUp and "UP" or "DOWN", true, false) --Maybe i should move GoingUp to a Relay attribute self.PhysicalRelays:SetState(GoingUp and "UP" or "DOWN", true, false)
self.PhysicalRelays:SetState("240 V", true, false) self.PhysicalRelays:SetState("240 V", true, false)
self.PhysicalRelays:SetState("440 V", true, false) self.PhysicalRelays:SetState("440 V", true, false)
self.MOConstructor:UpdateCFrame() self.MOConstructor:UpdateCFrame()

View File

@@ -55,7 +55,7 @@ Enums.Elevator = {
Enums.InteractType = { Enums.InteractType = {
LightSwitch = "LightSwitch" :: "LightSwitch", LightSwitch = "LightSwitch" :: "LightSwitch",
Light = "Light" :: "Light", Light = "Light" :: "Light",
LightSource = "LightSource" :: "LightSource" LightSource = "LightSource" :: "LightSource"
} }