mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-14 06:41:55 +00:00
new switch statement for Enums like Rust
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -2,14 +2,15 @@
|
||||
--!native
|
||||
--!strict
|
||||
|
||||
local Elevators = script.Parent
|
||||
|
||||
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local TagsModule = require(RS:WaitForChild("Tags"))
|
||||
local TagsModule = require(RS:WaitForChild("Tags"))
|
||||
local Enums = require(Elevators:WaitForChild("Enums"))
|
||||
|
||||
type Tags = TagsModule.ExportedTags
|
||||
|
||||
type GetButtons = {
|
||||
[string]: Instance
|
||||
}
|
||||
type GetButtons = {[string]: Instance}
|
||||
type ButtonTree = {
|
||||
Inst: Instance,
|
||||
Prompt: ProximityPrompt
|
||||
@@ -35,7 +36,7 @@ type Impl_Constructor = {
|
||||
} & Impl_Static_Props
|
||||
|
||||
type Impl_Static_Props = {
|
||||
ButtonEnum: ButtonsEnum
|
||||
ButtonEnum: any
|
||||
}
|
||||
|
||||
type Constructor_Fun = (Tags: Tags, Model: string) -> ClassConstructor
|
||||
@@ -54,12 +55,7 @@ export type ButtonsEnum = {
|
||||
local ButtonsModule = {} :: Impl_Constructor
|
||||
ButtonsModule.__index = ButtonsModule
|
||||
|
||||
ButtonsModule.ButtonEnum = {
|
||||
Car = "CarButton",
|
||||
Landing = "LandingButton",
|
||||
Special = "SpecialButton",
|
||||
Unknown = "UnknownButton"
|
||||
}
|
||||
local ButtonsEnum = Enums.ButtonsEnum
|
||||
|
||||
function ButtonsModule.constructor(Tags, Model)
|
||||
return setmetatable({
|
||||
@@ -102,46 +98,52 @@ function ButtonsModule:CreatePromptButtons()
|
||||
Prompt.MaxActivationDistance = 3
|
||||
Prompt.Parent = Attachment
|
||||
|
||||
local Split = TagName:split('_')
|
||||
--CarButton = Model_ElevatorButton_1
|
||||
--LandingButton = Model_ElevatorButton_Floor_1_Up
|
||||
--SpecialButton = Model_ElevatorButton_Open
|
||||
local ButtonType = if tonumber(Split[3]) then
|
||||
ButtonsModule.ButtonEnum.Car
|
||||
elseif Split[3] == "Floor" and Split[4]:match('%d') then
|
||||
ButtonsModule.ButtonEnum.Landing
|
||||
elseif Split[2] == "ElevatorButton" then
|
||||
ButtonsModule.ButtonEnum.Special
|
||||
else
|
||||
ButtonsModule.ButtonEnum.Unknown
|
||||
local Split = TagName:split('_')
|
||||
local ButtonType = tonumber(Split[3]) and
|
||||
ButtonsEnum.Car
|
||||
or Split[3] == "Floor" and Split[4]:match('%d') and
|
||||
ButtonsEnum.Landing
|
||||
or Split[2] == "ElevatorButton" and
|
||||
ButtonsEnum.Special
|
||||
|
||||
if ButtonType == ButtonsModule.ButtonEnum.Car then
|
||||
--ElevatorButton_1
|
||||
Buttons.Car[`{Split[2]}_{Split[3]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[3])
|
||||
Prompt.ObjectText = "Floor"
|
||||
elseif ButtonType == ButtonsModule.ButtonEnum.Landing then
|
||||
--ElevatorButton_Floor_1_Up
|
||||
Buttons.Landing[`{Split[2]}_{Split[3]}_{Split[4]}_{Split[5]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[5])
|
||||
Prompt.ObjectText = `Floor {tostring(Split[4])}`
|
||||
elseif ButtonType == ButtonsModule.ButtonEnum.Special then
|
||||
--ElevatorButton_Open
|
||||
Buttons.Special[`{Split[2]}_{Split[3]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[3])
|
||||
Prompt.ObjectText = "Floor"
|
||||
elseif ButtonType == ButtonsModule.ButtonEnum.Unknown then
|
||||
warn(`{self.Model}: Door tag was present but couldnt specify its type for use "{TagName}"`)
|
||||
end
|
||||
ButtonsEnum:Match(ButtonType, {
|
||||
[ButtonsEnum.Car] = function()
|
||||
--ElevatorButton_1
|
||||
Buttons.Car[`{Split[2]}_{Split[3]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[3])
|
||||
Prompt.ObjectText = "Floor"
|
||||
end,
|
||||
|
||||
[ButtonsEnum.Landing] = function()
|
||||
--ElevatorButton_Floor_1_Up
|
||||
Buttons.Landing[`{Split[2]}_{Split[3]}_{Split[4]}_{Split[5]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[5])
|
||||
Prompt.ObjectText = `Floor {tostring(Split[4])}`
|
||||
end,
|
||||
|
||||
[ButtonsEnum.Special] = function()
|
||||
--ElevatorButton_Open
|
||||
Buttons.Special[`{Split[2]}_{Split[3]}`] = {
|
||||
Inst = Inst,
|
||||
Prompt = Prompt
|
||||
}
|
||||
Prompt.ActionText = tostring(Split[3])
|
||||
Prompt.ObjectText = "Floor"
|
||||
end,
|
||||
|
||||
["_"] = function()
|
||||
warn(`{self.Model}: Door tag was present but couldnt specify its type for use "{TagName}"`)
|
||||
end
|
||||
})
|
||||
|
||||
print(`{self.Model}: created a ProximityPrompt @ "{Inst:GetFullName()}"`)
|
||||
end
|
||||
|
||||
12
src/server/main/Elevators/Enums.lua
Normal file
12
src/server/main/Elevators/Enums.lua
Normal file
@@ -0,0 +1,12 @@
|
||||
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local Enum = require(RS:WaitForChild("Enum"))
|
||||
|
||||
local Enums = {}
|
||||
|
||||
Enums.ButtonsEnum = Enum.Create("Buttons", {
|
||||
Car = "CarButton",
|
||||
Landing = "LandingButton",
|
||||
Special = "SpecialButton",
|
||||
})
|
||||
|
||||
return Enums
|
||||
88
src/shared/Enum.lua
Normal file
88
src/shared/Enum.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
--!optimize 2
|
||||
--!native
|
||||
--!strict
|
||||
|
||||
type EnumValue = any
|
||||
type EnumName = string
|
||||
type Enums = {[EnumName]: EnumValue}
|
||||
type EnumsMetadata = {
|
||||
__index: (self: EnumsMetadata, i: EnumName) -> EnumValue?,
|
||||
__newindex: (self: EnumsMetadata, i: EnumName, v: EnumValue) -> ()
|
||||
}
|
||||
|
||||
export type CustomEnums = {
|
||||
Enums: typeof(setmetatable({} :: Enums, {} :: EnumsMetadata))
|
||||
} & CustonEnumsFunctions
|
||||
|
||||
type CustonEnumsFunctions = {
|
||||
Create: <T>(Name: EnumName, EnumValue: T) -> T,
|
||||
Remove: (Name: EnumName) -> ()
|
||||
}
|
||||
|
||||
--type MatchList = {
|
||||
-- [EnumValue]
|
||||
--}
|
||||
|
||||
local CustomEnum = {} :: CustomEnums
|
||||
local EnumMeta = {} :: EnumsMetadata
|
||||
|
||||
function EnumMeta.__index(self, i)
|
||||
local get = rawget(self, i)
|
||||
if type(i) == "string" then
|
||||
return get
|
||||
end
|
||||
warn(`Enum: attempt to retrieve an unknown Enum "{i}".`, debug.traceback())
|
||||
return nil
|
||||
end
|
||||
|
||||
function EnumMeta.__newindex(self, i, v)
|
||||
local PossibleEnum = rawget(self, i)
|
||||
if not PossibleEnum then
|
||||
if type(i) == "string" then
|
||||
rawset(self, i, v)
|
||||
else
|
||||
error(`Enum: attempt to set an Enum but the requested name was not a string "{i}", type="{type(i)}".`, 2)
|
||||
end
|
||||
else
|
||||
error(`Enum: attempt to set an Enum but the name "{i}" is already registered.`, 2)
|
||||
end
|
||||
end
|
||||
|
||||
CustomEnum.Enums = setmetatable({}, EnumMeta)
|
||||
|
||||
local function EnumMethods(Enum)
|
||||
function Enum:Match(Result: string, MatchList) --Branch this out later
|
||||
local Return: any? = nil
|
||||
local b: boolean = false
|
||||
|
||||
for MatchEnumName, EnumFunc in MatchList do
|
||||
if MatchEnumName == Result then
|
||||
Return = EnumFunc()
|
||||
break
|
||||
end
|
||||
end
|
||||
if not Return and MatchList["_"] then
|
||||
Return = MatchList["_"]()
|
||||
end
|
||||
return Return
|
||||
end
|
||||
end
|
||||
|
||||
function CustomEnum.Create(Name, EnumValue)
|
||||
CustomEnum.Enums[Name] = EnumValue
|
||||
|
||||
local Enum = CustomEnum.Enums[Name]
|
||||
EnumMethods(Enum)
|
||||
|
||||
return Enum
|
||||
end
|
||||
|
||||
function CustomEnum.Remove(Name)
|
||||
if CustomEnum.Enums[Name] then
|
||||
CustomEnum.Enums[Name] = nil
|
||||
else
|
||||
warn(`Enum: attempt to remove an Enum that does not exist "{Name}".`, debug.traceback())
|
||||
end
|
||||
end
|
||||
|
||||
return CustomEnum
|
||||
@@ -42,7 +42,7 @@ end
|
||||
function Tags:Nuke()
|
||||
local Exports = self.Exports
|
||||
|
||||
for i,v in Exports do
|
||||
for i: string, v: Instance | {Instance} in Exports do
|
||||
if type(v) == "table" then
|
||||
for n: number = 1, #v do
|
||||
CS:RemoveTag(v[n], i)
|
||||
|
||||
Reference in New Issue
Block a user