elevator lanterns

This commit is contained in:
2024-04-07 12:07:16 -04:00
parent 05e3b9406f
commit 42db037fc5
12 changed files with 399 additions and 189 deletions

View File

@@ -0,0 +1,174 @@
--!optimize 2
--!native
--!strict
local Storage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tween = require(Storage:WaitForChild("Tween"))
local Tags = require(Storage:WaitForChild("Tags"))
local Enums = require(script.Parent:WaitForChild("Enums"))
type rbxassetid = string
type TagProduct = Tags.TagProduct
type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
Get: (Tags: Tags.TagsConstructor, Model: Enums.ElevatorValues) -> Lanterns,
--Class functions
__Activate: (self: ClassConstructor, EnabledState: boolean, IsDirectionLantern: boolean, Lantern: Lantern) -> (),
DirectionUp: (self: ClassConstructor, Enabled: boolean) -> (),
DirectionDown: (self: ClassConstructor, Enabled: boolean) -> (),
Toggle: (self: ClassConstructor, Enabled: boolean, Floor: number) -> (),
Reset: (self: ClassConstructor) -> ()
} & Impl_Static_Props
type Impl_Static_Props = {
Volume: number
}
type Constructor_Fun = (MainDisplay: UnionOperation, ChimeDirectionID: rbxassetid, ChimeLandingID: rbxassetid, Lanterns: Lanterns, Colors: Colors) -> ClassConstructor
type Constructor_Return_Props = {
LanternsMap: Lanterns,
AudioChimeDirection: Sound,
AudioChimeLanding: Sound,
Colors: Colors
}
type Lantern = {
Inst: BasePart,
Light: BasePart?,
Played: boolean
}
export type Lanterns = {
[number]: Lantern,
Up: Lantern,
Down: Lantern
}
export type Colors = {
Active: Color3,
Off: Color3
}
export type LanternsConstructor = ClassConstructor
local Lanterns = {} :: Impl_Constructor
Lanterns.__index = Lanterns
Lanterns.Volume = .8
function Lanterns.constructor(MainDisplay, ChimeDirectionID, ChimeLandingID, LanternsMap, Colors)
local AudioChimeDirection = Instance.new("Sound") :: Sound
AudioChimeDirection.SoundId = ChimeDirectionID
AudioChimeDirection.Volume = Lanterns.Volume
local AudioChimeLanding = Instance.new("Sound") :: Sound
AudioChimeLanding.SoundId = ChimeLandingID
AudioChimeLanding.Volume = Lanterns.Volume
AudioChimeDirection.Parent = MainDisplay
AudioChimeLanding.Parent = MainDisplay
return setmetatable({
LanternsMap = LanternsMap,
AudioChimeDirection = AudioChimeDirection,
AudioChimeLanding = AudioChimeLanding,
Colors = Colors
}, Lanterns)
end
function Lanterns.Get(Tags, Model)
local Lanterns = {} :: Lanterns
for TagName: string, Inst: TagProduct in Tags.__export do
local Split = TagName:split('_')
if Split[1] == Model and Split[2] == "DirectionIndicator" then
local Floor = tonumber(Split[3])
if Floor then
if not Lanterns[Floor] then
Lanterns[Floor] = {
Inst = Inst :: BasePart,
Light = (Inst :: BasePart).Parent:FindFirstChild("Light") :: BasePart?,
Played = false
}
else
warn(`Lanterns: Floor "{tostring(Floor)}" was already wrote while parsing`)
end
else
if Split[3] == "Up" then
Lanterns.Up = {
Inst = Inst :: BasePart,
Light = (Inst :: BasePart).Parent:FindFirstChild("Light") :: BasePart?,
Played = false
}
elseif Split[3] == "Down" then
Lanterns.Down = {
Inst = Inst :: BasePart,
Light = (Inst :: BasePart).Parent:FindFirstChild("Light") :: BasePart?,
Played = false
}
else
warn(`Lanterns: Unknown type paired with "DirectionIndicator", {Split[3]}`)
end
end
end
end
return Lanterns
end
local LanternLight = Tween.constructor(TweenInfo.new(1))
function Lanterns:__Activate(EnabledState, IsDirectionLantern, Lantern)
local Tween = LanternLight:Start(Lantern.Light, {
Color = EnabledState and self.Colors.Active or self.Colors.Off
})
if EnabledState then
if IsDirectionLantern then
self.AudioChimeDirection:Play()
else
Tween.Completed:Connect(function()
self.AudioChimeLanding:Play()
end)
end
end
end
function Lanterns:Reset()
self.LanternsMap.Up.Played = false
self.LanternsMap.Down.Played = false
self:__Activate(false, true, self.LanternsMap.Up)
self:__Activate(false, true, self.LanternsMap.Down)
for n: number = 1, #self.LanternsMap do
self:__Activate(false, false, self.LanternsMap[n])
self.LanternsMap[n].Played = false
end
end
function Lanterns:DirectionUp(Enabled)
if not self.LanternsMap.Up.Played then
self.LanternsMap.Up.Played = true
self:__Activate(Enabled, true, self.LanternsMap.Up)
end
end
function Lanterns:DirectionDown(Enabled)
if not self.LanternsMap.Down.Played then
self.LanternsMap.Down.Played = true
self:__Activate(Enabled, true, self.LanternsMap.Down)
end
end
function Lanterns:Toggle(Enabled, Floor)
local f = self.LanternsMap[Floor]
if not f.Played then
f.Played = true
self:__Activate(Enabled, false, f)
end
end
return Lanterns