Advertisement
CluelessDev

Roles Component

Oct 4th, 2022 (edited)
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.08 KB | None | 0 0
  1. -- !== ================================================================================||>
  2. -- !==                                       Services
  3. -- !== ================================================================================||>
  4. local CollectionService = game:GetService("CollectionService")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local RunService = game:GetService("RunService")
  7.  
  8.  
  9.  
  10. --!== ================================================================================||>
  11. --!==                                       Dependencies
  12. --!== ================================================================================||>
  13. local Data = ReplicatedStorage:FindFirstChild("Data")
  14. local Structs = require(Data:FindFirstChild("Structs"))
  15. local Lists   = require(Data:FindFirstChild("Lists"))
  16.  
  17.  
  18. local Packages = ReplicatedStorage:FindFirstChild("Packages")
  19. local Net    = require(Packages:FindFirstChild("Net"))
  20. local Signal = require(Packages.Signal)
  21.  
  22. local Assets = ReplicatedStorage:FindFirstChild("Assets")
  23. local Tools = Assets:FindFirstChild("Tools")
  24.  
  25.  
  26. local Utilities = ReplicatedStorage:FindFirstChild("Utilities")
  27. local ConverToAssetId = require(Utilities:FindFirstChild("ConverToAssetId"))
  28.  
  29.  
  30. --!== ================================================================================||>
  31. --!==                                    ROLES DATA
  32. --!== ================================================================================||>
  33. local module = {}
  34.  
  35. module.Comms = {
  36.     Events    = {},
  37.     Functions = {},
  38.     Signals   = {},
  39. }
  40.  
  41. module.Comms.Events.RoleSelected         = "RoleSelected"
  42. module.Comms.Events.EnteredRoleSelection = "EnteredRoleSelection"
  43. module.Comms.Events.ExitedRoleSelection  = "ExitedRoleSelection"
  44. module.Comms.Events.RequestSetRole       = "RequestSetRole"
  45. module.Comms.Events.SetRole              = "SetRole"
  46. module.Comms.Events.RoleSet              = "RoleSet"
  47.  
  48. module.Comms.Signals.RoleSet = Signal.new()
  49. module.Comms.Signals.EnteredRoleSelection = Signal.new()
  50.  
  51.  
  52. for _, eventName: string in module.Comms.Events do
  53.     Net:RemoteEvent(eventName)
  54. end
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. -- =============================== Data Upvalues ===============================||>
  62. local RolesList = Lists.Roles
  63. local ToolsList =  Lists.Tools
  64.  
  65. local Entities: Structs.LUT<Structs.Entity, Structs.RoleComponent> = {}
  66.  
  67.  
  68. local RolesData = {
  69.     OrderedList = { --> useful for tags and intance names and in menus were role order matters
  70.         "Civilian",
  71.         "Nurse",
  72.         "Doctor",
  73.         "PoliceOfficer",
  74.         "Firefighter",
  75.     },
  76.  
  77.  
  78.     Names = {  --> useful for gui's and gameplay
  79.         [RolesList.Civilian]      = "Civilian",
  80.         [RolesList.Nurse]         = "Nurse",
  81.         [RolesList.Doctor]        = "Doctor",
  82.         [RolesList.PoliceOfficer] = "Police Officer",
  83.         [RolesList.Firefighter]   = "Firefighter",
  84.     },
  85.  
  86.  
  87.     RoleThumbnails = {
  88.         [RolesList.Nurse]         = "rbxassetid://11158922704",
  89.         [RolesList.Doctor]        = "rbxassetid://11158923050",
  90.         [RolesList.PoliceOfficer] = "rbxassetid://11158922530",
  91.         [RolesList.Firefighter]   = "rbxassetid://11158922861",
  92.  
  93.     },
  94.    
  95.  
  96.     --## Roles are not really being added to this list during gameplay!
  97.     --## And if that was the case then that's up to the Roles system to handle
  98.  
  99.     Spawns = {
  100.         [RolesList.Civilian]      = CollectionService:GetTagged("CivilianSpawn"),
  101.         [RolesList.Nurse]         = CollectionService:GetTagged("NurseSpawn"),
  102.         [RolesList.Doctor]        = CollectionService:GetTagged("DoctorSpawn"),
  103.         [RolesList.PoliceOfficer] = CollectionService:GetTagged("PoliceOfficerSpawn"),
  104.         [RolesList.Firefighter]   = CollectionService:GetTagged("FirefighterSpawn"),
  105.     },
  106.  
  107.  
  108.     RoleStatingItems = {
  109.         [RolesList.Civilian]      = {},
  110.  
  111.         [RolesList.Nurse]         = {
  112.             [ToolsList.Bandages]    = Tools:FindFirstChild("Bandages"),
  113.             [ToolsList.Thermometer] = Tools:FindFirstChild("Thermometer"),
  114.             [ToolsList.Clipboard]   = Tools:FindFirstChild("Clipboard"),
  115.             [ToolsList.Scissors]    = Tools:FindFirstChild("Scissors"),
  116.         },
  117.  
  118.         [RolesList.Doctor]        = {
  119.             [ToolsList.SurgicalKnife] = Tools:FindFirstChild("SurgicalKnife"),
  120.             [ToolsList.Clipboard]     = Tools:FindFirstChild("Clipboard"),
  121.             [ToolsList.Thermometer] = Tools:FindFirstChild("Thermometer"),
  122.             [ToolsList.Scissors]    = Tools:FindFirstChild("Scissors"),
  123.             [ToolsList.Bandages]    = Tools:FindFirstChild("Bandages"),
  124.         },
  125.  
  126.         [RolesList.PoliceOfficer] = {
  127.             [ToolsList.Taser] = Tools:FindFirstChild("Taser"),
  128.             [ToolsList.Baton] = Tools:FindFirstChild("Baton"),
  129.         },
  130.  
  131.         [RolesList.Firefighter]   = {
  132.             [ToolsList.Bandages]    = Tools:FindFirstChild("Bandages"),
  133.             [ToolsList.Thermometer] = Tools:FindFirstChild("Thermometer"),
  134.         }
  135.     },
  136.  
  137.  
  138.  
  139.     LimitedRoles = {
  140.         [RolesList.Nurse]         = 15,
  141.         [RolesList.Doctor]        = 10,
  142.         [RolesList.PoliceOfficer] = 8,
  143.         [RolesList.Firefighter]   = 5,
  144.     },
  145.  
  146.     --## Since these roles can only be unlocked through
  147.     --## Leveling, I can do this cheeky thing.
  148.  
  149.     LockedRoles = {
  150.         [RolesList.Nurse]         = "Nurse",
  151.         [RolesList.Doctor]        = "Doctor",
  152.         [RolesList.PoliceOfficer] = "PoliceOfficer",
  153.         [RolesList.Firefighter]   = "FireFighter",
  154.  
  155.     },
  156.  
  157.     RolesUnlockedByLeveling = {
  158.         [RolesList.Nurse]         = 3,
  159.         [RolesList.Doctor]        = 7,
  160.         [RolesList.PoliceOfficer] = 10,
  161.         [RolesList.Firefighter]   = 14,
  162.     },
  163.  
  164.     RoleCloths = {
  165.         [RolesList.Nurse] = {
  166.             Top    = 10831844528,
  167.             Bottom = 10831848965,
  168.         },
  169.  
  170.         [RolesList.Doctor] = {
  171.             Top    = 10831842029,
  172.             Bottom = 10831846781,
  173.         },
  174.  
  175.         [RolesList.PoliceOfficer] = {
  176.             Top    = 10831845888,
  177.             Bottom = 10831849869,
  178.         },
  179.  
  180.         [RolesList.Firefighter] =  {
  181.             Top    = 10831843159,
  182.             Bottom = 10831847796,
  183.         },
  184.  
  185.  
  186.  
  187.     }
  188.  
  189. }
  190.  
  191. -- =============================== Contstructor ===============================||>
  192. module.addEntity = function(entity: Structs.Entity, parent: Instance?): Structs.RoleComponent
  193.     local RolesComponentFolder: Structs.RoleComponent & Folder = Instance.new("Folder")
  194.     RolesComponentFolder.Name = script.Name
  195.  
  196.     Entities[entity] = RolesComponentFolder
  197.    
  198.  
  199.     local CurrentRole: StringValue = Instance.new("StringValue")
  200.     CurrentRole.Name   = "CurrentRole"
  201.     CurrentRole.Parent = RolesComponentFolder
  202.  
  203.     local OwnedRoles: Folder & Structs.Dictionary<string, BoolValue> = Instance.new("Folder")
  204.     OwnedRoles.Name   = "OwnedRoles"
  205.     OwnedRoles.Parent = RolesComponentFolder
  206.    
  207.  
  208.     for  _, roleName: string in module.GetOrderedRolesList() do
  209.         local hasRoleValue: BoolValue = Instance.new("BoolValue")
  210.         hasRoleValue.Name   = roleName
  211.         hasRoleValue.Parent = OwnedRoles
  212.     end
  213.    
  214.  
  215.     RolesComponentFolder.Parent = parent or entity
  216.    
  217.     CollectionService:AddTag(entity, "RolesComponent")
  218.     return Entities[entity]
  219. end
  220.  
  221.  
  222. --=============================== Getters ===============================||>
  223.  
  224. ---* Roles Data Getters ||>
  225. module.GetOrderedRolesList = function(): Structs.Array<string>
  226.     return RolesData.OrderedList
  227. end
  228.  
  229. module.GetRolesNames = function(): Structs.Array<string>
  230.     return RolesData.Names
  231. end
  232.  
  233. module.GetRolesThumbnails = function(): Structs.Dictionary<string, string>
  234.     return RolesData.RoleThumbnails
  235. end
  236.  
  237.  
  238. module.GetRoleThumbnail = function(roleName: string): string
  239.     if not module.GetRolesThumbnails()[roleName] then
  240.         warn(roleName, "Has no registered thumbnail, returning default")
  241.         return module.GetRolesThumbnails()[RolesList.Nurse]
  242.     end
  243.  
  244.     return module.GetRolesThumbnails()[roleName]
  245. end
  246.  
  247.  
  248. module.GetRoleSpawns = function(): Structs.Array<Structs.Array<BasePart>>
  249.     return RolesData.Spawns
  250. end
  251.  
  252.  
  253. module.GetLimitedRoles = function(): Structs.Array<number>
  254.     return RolesData.LimitedRoles
  255. end
  256.  
  257. module.GetRolePlayersLimit = function(roleName: number): number
  258.     if RolesData.LimitedRoles[roleName] then
  259.         return RolesData.LimitedRoles[roleName]
  260.     end
  261.  
  262.     warn("Given roleKey was not found OR the role has no actual limit")
  263.     return 1E4
  264. end
  265.  
  266.  
  267. module.GetLockedRoles = function(): Structs.Dictionary<string, string>
  268.     return RolesData.LockedRoles
  269. end
  270.  
  271. module.GetRolesUnlockedByLeveling = function():Structs.Array<number>
  272.     return RolesData.RolesUnlockedByLeveling
  273. end
  274.  
  275.  
  276. module.GetRolesStartingItems = function()
  277.     return RolesData.RoleStatingItems
  278. end
  279.  
  280.  
  281. module.GetRoleStartingItems = function(roleIndexKey)
  282.     if not RolesData.RoleStatingItems[roleIndexKey] then return end
  283.     return RolesData.RoleStatingItems[roleIndexKey]
  284. end
  285.  
  286.  
  287.  
  288. module.GetRoleClothing = function(roleName: string): {Top: string, Bottom: string}
  289.     if not RolesData.RoleCloths[roleName] then
  290.         return RolesData.RoleCloths[RolesList.Nurse]
  291.     end
  292.  
  293.  
  294.     return RolesData.RoleCloths[roleName]
  295. end
  296.  
  297. ---* Entity Roles Getters ||>
  298. module.GetEntity = function(entity: Structs.Entity): Structs.RoleComponent
  299.     if not Entities[entity] then warn("Entity does not has the role component")
  300.         return
  301.     end
  302.  
  303.     return Entities[entity]
  304. end
  305.  
  306.  
  307.  
  308. module.GetEntities = function(): Structs.EntitiesList<Structs.Entity>
  309.     return Entities
  310. end
  311.  
  312. module.GetOwnedRoles = function(entity: Structs.Entity): Structs.Array<BoolValue> & Folder
  313.     if not Entities[entity] then
  314.         warn("Entity does not has the role component")
  315.         return
  316.     end
  317.  
  318.     return module.GetEntity(entity).OwnedRoles
  319. end
  320.  
  321. module.GetCurrentRole = function(entity: Structs.Entity): StringValue
  322.     if not Entities[entity] then
  323.         warn("Entity does not has the role component")
  324.         return
  325.     end
  326.  
  327.     return module.GetEntity(entity).CurrentRole
  328. end
  329.  
  330.  
  331. return module
  332.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement