Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local replicatedStorage = game:GetService("ReplicatedStorage")
- local httpService = game:GetService("HttpService")
- local data = require(replicatedStorage.ItemData)
- local cases = {
- maxAmount = function(self: table, integer: number)
- if integer == self:getData().maxAmount then
- self.maxAmount = self:getData().maxAmount
- end
- end,
- amount = function(self: table, integer: number)
- integer = math.clamp(integer, 0, self.maxAmount)
- self.amount = integer
- end,
- class = function(self: table, str: string)
- if data[str] then
- self.class = str
- if not data[self.class][self.name] then
- self.name = data[self.class].main
- end
- self:update()
- end
- end,
- name = function(self: table, str: string)
- if not data[self.class][str] then str = data[self.class].main end
- self.name = str
- end,
- }
- local item = {}
- item.__index = item
- function item.new(amount: number)
- local self = setmetatable({}, item)
- self.class = "null"
- self.name = "null"
- local data = self:getData()
- self.stackeable = data.stackeable
- self.maxAmount = data.maxAmount
- self.amount = amount or self.maxAmount
- self._guid = httpService:GenerateGUID()
- local behaviour = {
- __index = self,
- __newindex = function(t, k, v)
- if cases[k] then
- cases[k](self, v)
- else
- rawset(self, k, v)
- end
- end,
- __tostring = function(t)
- return self.name
- end,
- }
- return setmetatable({}, behaviour)
- end
- function item:update()
- local data = self:getData()
- self.stackeable = data.stackeable
- self.maxAmount = data.maxAmount
- self.amount = math.clamp(self.amount, 0, self.maxAmount)
- end
- function item:getData()
- return data[self.class][self.name]
- end
- return item
- -----------------------------------------------------------------------------------------------------------------
- local superclass = require(script.Parent.Item)
- local resource = {}
- resource.__index = resource
- setmetatable(resource, superclass)
- function resource.new(amount)
- local item = superclass.new(amount)
- local interface = {
- __index = function(t, k)
- return item[k] or resource[k]
- end,
- __newindex = function(t, k, v)
- item[k] = v
- end,
- __tostring = function(t)
- return tostring(item)
- end,
- }
- local self = setmetatable({}, interface)
- self.class = "resource"
- self.name = "wood"
- self.amount = amount or self.maxAmount
- return self
- end
- return resource
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement