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
|
--!native
|
||||||
--!strict
|
--!strict
|
||||||
|
|
||||||
|
local Elevators = script.Parent
|
||||||
|
|
||||||
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
|
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 Tags = TagsModule.ExportedTags
|
||||||
|
|
||||||
type GetButtons = {
|
type GetButtons = {[string]: Instance}
|
||||||
[string]: Instance
|
|
||||||
}
|
|
||||||
type ButtonTree = {
|
type ButtonTree = {
|
||||||
Inst: Instance,
|
Inst: Instance,
|
||||||
Prompt: ProximityPrompt
|
Prompt: ProximityPrompt
|
||||||
@@ -35,7 +36,7 @@ type Impl_Constructor = {
|
|||||||
} & Impl_Static_Props
|
} & Impl_Static_Props
|
||||||
|
|
||||||
type Impl_Static_Props = {
|
type Impl_Static_Props = {
|
||||||
ButtonEnum: ButtonsEnum
|
ButtonEnum: any
|
||||||
}
|
}
|
||||||
|
|
||||||
type Constructor_Fun = (Tags: Tags, Model: string) -> ClassConstructor
|
type Constructor_Fun = (Tags: Tags, Model: string) -> ClassConstructor
|
||||||
@@ -54,12 +55,7 @@ export type ButtonsEnum = {
|
|||||||
local ButtonsModule = {} :: Impl_Constructor
|
local ButtonsModule = {} :: Impl_Constructor
|
||||||
ButtonsModule.__index = ButtonsModule
|
ButtonsModule.__index = ButtonsModule
|
||||||
|
|
||||||
ButtonsModule.ButtonEnum = {
|
local ButtonsEnum = Enums.ButtonsEnum
|
||||||
Car = "CarButton",
|
|
||||||
Landing = "LandingButton",
|
|
||||||
Special = "SpecialButton",
|
|
||||||
Unknown = "UnknownButton"
|
|
||||||
}
|
|
||||||
|
|
||||||
function ButtonsModule.constructor(Tags, Model)
|
function ButtonsModule.constructor(Tags, Model)
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
@@ -102,46 +98,52 @@ function ButtonsModule:CreatePromptButtons()
|
|||||||
Prompt.MaxActivationDistance = 3
|
Prompt.MaxActivationDistance = 3
|
||||||
Prompt.Parent = Attachment
|
Prompt.Parent = Attachment
|
||||||
|
|
||||||
local Split = TagName:split('_')
|
|
||||||
--CarButton = Model_ElevatorButton_1
|
--CarButton = Model_ElevatorButton_1
|
||||||
--LandingButton = Model_ElevatorButton_Floor_1_Up
|
--LandingButton = Model_ElevatorButton_Floor_1_Up
|
||||||
--SpecialButton = Model_ElevatorButton_Open
|
--SpecialButton = Model_ElevatorButton_Open
|
||||||
local ButtonType = if tonumber(Split[3]) then
|
local Split = TagName:split('_')
|
||||||
ButtonsModule.ButtonEnum.Car
|
local ButtonType = tonumber(Split[3]) and
|
||||||
elseif Split[3] == "Floor" and Split[4]:match('%d') then
|
ButtonsEnum.Car
|
||||||
ButtonsModule.ButtonEnum.Landing
|
or Split[3] == "Floor" and Split[4]:match('%d') and
|
||||||
elseif Split[2] == "ElevatorButton" then
|
ButtonsEnum.Landing
|
||||||
ButtonsModule.ButtonEnum.Special
|
or Split[2] == "ElevatorButton" and
|
||||||
else
|
ButtonsEnum.Special
|
||||||
ButtonsModule.ButtonEnum.Unknown
|
|
||||||
|
|
||||||
if ButtonType == ButtonsModule.ButtonEnum.Car then
|
ButtonsEnum:Match(ButtonType, {
|
||||||
--ElevatorButton_1
|
[ButtonsEnum.Car] = function()
|
||||||
Buttons.Car[`{Split[2]}_{Split[3]}`] = {
|
--ElevatorButton_1
|
||||||
Inst = Inst,
|
Buttons.Car[`{Split[2]}_{Split[3]}`] = {
|
||||||
Prompt = Prompt
|
Inst = Inst,
|
||||||
}
|
Prompt = Prompt
|
||||||
Prompt.ActionText = tostring(Split[3])
|
}
|
||||||
Prompt.ObjectText = "Floor"
|
Prompt.ActionText = tostring(Split[3])
|
||||||
elseif ButtonType == ButtonsModule.ButtonEnum.Landing then
|
Prompt.ObjectText = "Floor"
|
||||||
--ElevatorButton_Floor_1_Up
|
end,
|
||||||
Buttons.Landing[`{Split[2]}_{Split[3]}_{Split[4]}_{Split[5]}`] = {
|
|
||||||
Inst = Inst,
|
[ButtonsEnum.Landing] = function()
|
||||||
Prompt = Prompt
|
--ElevatorButton_Floor_1_Up
|
||||||
}
|
Buttons.Landing[`{Split[2]}_{Split[3]}_{Split[4]}_{Split[5]}`] = {
|
||||||
Prompt.ActionText = tostring(Split[5])
|
Inst = Inst,
|
||||||
Prompt.ObjectText = `Floor {tostring(Split[4])}`
|
Prompt = Prompt
|
||||||
elseif ButtonType == ButtonsModule.ButtonEnum.Special then
|
}
|
||||||
--ElevatorButton_Open
|
Prompt.ActionText = tostring(Split[5])
|
||||||
Buttons.Special[`{Split[2]}_{Split[3]}`] = {
|
Prompt.ObjectText = `Floor {tostring(Split[4])}`
|
||||||
Inst = Inst,
|
end,
|
||||||
Prompt = Prompt
|
|
||||||
}
|
[ButtonsEnum.Special] = function()
|
||||||
Prompt.ActionText = tostring(Split[3])
|
--ElevatorButton_Open
|
||||||
Prompt.ObjectText = "Floor"
|
Buttons.Special[`{Split[2]}_{Split[3]}`] = {
|
||||||
elseif ButtonType == ButtonsModule.ButtonEnum.Unknown then
|
Inst = Inst,
|
||||||
warn(`{self.Model}: Door tag was present but couldnt specify its type for use "{TagName}"`)
|
Prompt = Prompt
|
||||||
end
|
}
|
||||||
|
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()}"`)
|
print(`{self.Model}: created a ProximityPrompt @ "{Inst:GetFullName()}"`)
|
||||||
end
|
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()
|
function Tags:Nuke()
|
||||||
local Exports = self.Exports
|
local Exports = self.Exports
|
||||||
|
|
||||||
for i,v in Exports do
|
for i: string, v: Instance | {Instance} in Exports do
|
||||||
if type(v) == "table" then
|
if type(v) == "table" then
|
||||||
for n: number = 1, #v do
|
for n: number = 1, #v do
|
||||||
CS:RemoveTag(v[n], i)
|
CS:RemoveTag(v[n], i)
|
||||||
|
|||||||
Reference in New Issue
Block a user