Advertisement
TaylorsRus

Farm System

May 31st, 2023
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. -- [[
  2.     Class
  3.         --]]
  4.  
  5. local FarmClass = {}
  6. FarmClass.__index = FarmClass
  7.  
  8. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  9. local RunService = game:GetService("RunService")
  10.  
  11. local CropData = require(ReplicatedStorage.CropData)
  12.  
  13. local CROP_PLANT_TIME = 5
  14. local CROP_GROW_TIME = 5
  15. local CROP_HARVEST_TIME = 5
  16.  
  17. function FarmClass.new(Plot)
  18.     local self = setmetatable({}, FarmClass)
  19.     self.Name = Plot.Name
  20.     self.Plot = Plot
  21.    
  22.     self.Prompt = Plot:FindFirstChildOfClass("ProximityPrompt")
  23.    
  24.     self.Progress = 0
  25.    
  26.     self.Ripe = false
  27.     self.Planting = false
  28.     self.Growing = false
  29.    
  30.     return self
  31. end
  32.  
  33. function FarmClass:Plant(Crop, Player) 
  34.     if self.Planting then return end
  35.    
  36.     local Character = Player.Character
  37.     local Humanoid = Character:FindFirstChild("Humanoid")
  38.     local CachedSpeed = Humanoid.WalkSpeed
  39.    
  40.     self.Planting = true
  41.     self.Owner = Player
  42.  
  43.     self.Plot.Transparency = 1
  44.     self.Prompt.Enabled = false
  45.    
  46.     Humanoid.WalkSpeed, Humanoid.AutoRotate = 0, false
  47.     print("Planting at:",self.Name.."...")     
  48.     task.wait(CROP_PLANT_TIME)
  49.    
  50.     self.Crop = Crop
  51.    
  52.     self.Planting = false
  53.     self.Plot:SetAttribute("Occupied", true)
  54.    
  55.     Humanoid.WalkSpeed, Humanoid.AutoRotate = CachedSpeed, true
  56.     print("Crop planted!")
  57.     self:Grow()
  58. end
  59.  
  60. function FarmClass:Grow()
  61.     local StartTime = os.clock()
  62.    
  63.     if self.Growing then return end
  64.     self.Growing = true
  65.    
  66.     repeat
  67.         local DeltaTime = os.clock() - StartTime
  68.         local Percentage = (DeltaTime / CROP_GROW_TIME) * 100
  69.         Percentage = math.floor(Percentage >= 100 and 100 or Percentage)
  70.  
  71.         self.Progress = Percentage
  72.         self.Plot.Transparency = (100 - Percentage) / 100
  73.         print("Percentage done:",Percentage.."%")
  74.         task.wait(.1)
  75.     until DeltaTime >= CROP_GROW_TIME  
  76.    
  77.     self.Ripe = true
  78.     self.Growing = false
  79.     self.Prompt.Enabled = true
  80.     print("Crop fully grown.")
  81. end
  82.  
  83. function FarmClass:Harvest()
  84.     if not self.Ripe then warn("Crop not fully grown.") return end
  85.    
  86.     self.Plot.Transparency = 1 
  87.     self.Plot:SetAttribute("Occupied", false)
  88.     print("Crop harvested.")
  89. end
  90.  
  91. return FarmClass
  92.  
  93. --[[
  94.     Handler
  95.         --]]
  96.  
  97. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  98.  
  99. local FarmClass = require(ReplicatedStorage.FarmClass)
  100.  
  101. local Plots = workspace:FindFirstChild("Plots")
  102.  
  103. for _,Plot in ipairs(Plots:GetChildren()) do   
  104.     local Land = FarmClass.new(Plot)
  105.     local Prompt = Plot:FindFirstChildOfClass("ProximityPrompt")
  106.     Prompt.Triggered:Connect(function(Player)
  107.         local Action = (Plot:GetAttribute("Occupied") and "Harvest") or "Plant"
  108.        
  109.         print(Action)
  110.         Land[Action](Land, "Wheat", Player)
  111.     end)
  112. end
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement