Advertisement
remi_

Item Classes

Apr 7th, 2021 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. local replicatedStorage = game:GetService("ReplicatedStorage")
  2. local httpService = game:GetService("HttpService")
  3. local data = require(replicatedStorage.ItemData)
  4.  
  5. local cases = {
  6.     maxAmount = function(self: table, integer: number)
  7.         if integer == self:getData().maxAmount then
  8.             self.maxAmount = self:getData().maxAmount
  9.         end
  10.     end,
  11.     amount = function(self: table, integer: number)
  12.         integer = math.clamp(integer, 0, self.maxAmount)
  13.         self.amount = integer
  14.     end,
  15.     class = function(self: table, str: string)
  16.         if data[str] then
  17.             self.class = str
  18.             if not data[self.class][self.name] then
  19.                 self.name = data[self.class].main
  20.             end
  21.             self:update()
  22.         end
  23.     end,
  24.     name = function(self: table, str: string)
  25.         if not data[self.class][str] then str = data[self.class].main end
  26.         self.name = str
  27.     end,
  28. }
  29.  
  30. local item = {}
  31. item.__index = item
  32.  
  33. function item.new(amount: number)
  34.     local self = setmetatable({}, item)
  35.    
  36.     self.class = "null"
  37.     self.name = "null"
  38.    
  39.     local data = self:getData()
  40.  
  41.     self.stackeable = data.stackeable
  42.     self.maxAmount = data.maxAmount
  43.     self.amount = amount or self.maxAmount
  44.    
  45.     self._guid = httpService:GenerateGUID()
  46.    
  47.     local behaviour = {
  48.         __index = self,
  49.         __newindex = function(t, k, v)
  50.             if cases[k] then
  51.                 cases[k](self, v)
  52.             else
  53.                 rawset(self, k, v)
  54.             end
  55.         end,
  56.         __tostring = function(t)
  57.             return self.name
  58.         end,
  59.     }
  60.    
  61.     return setmetatable({}, behaviour)
  62. end
  63.  
  64. function item:update()
  65.     local data = self:getData()
  66.  
  67.     self.stackeable = data.stackeable
  68.     self.maxAmount = data.maxAmount
  69.     self.amount = math.clamp(self.amount, 0, self.maxAmount)
  70. end
  71.  
  72. function item:getData()
  73.     return data[self.class][self.name]
  74. end
  75.  
  76. return item
  77. -----------------------------------------------------------------------------------------------------------------
  78. local superclass = require(script.Parent.Item)
  79.  
  80. local resource = {}
  81. resource.__index = resource
  82. setmetatable(resource, superclass)
  83.  
  84. function resource.new(amount)
  85.     local item = superclass.new(amount)
  86.     local interface = {
  87.         __index = function(t, k)
  88.             return item[k] or resource[k]
  89.         end,
  90.         __newindex = function(t, k, v)
  91.             item[k] = v
  92.         end,
  93.         __tostring = function(t)
  94.             return tostring(item)
  95.         end,
  96.     }
  97.     local self = setmetatable({}, interface)
  98.    
  99.     self.class = "resource"
  100.     self.name = "wood"
  101.    
  102.     self.amount = amount or self.maxAmount
  103.    
  104.     return self
  105. end
  106.  
  107. return resource
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement