Advertisement
Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. --[[
  2.     Author: LifeInDevelopment
  3.     Timestamp: 19/April/2018
  4.    
  5.     Class Diagram:
  6.    
  7.     Attributes:
  8.         tipBudget
  9.         character
  10.         plot
  11.         order
  12.         type
  13.         owner
  14.     Member Functions:
  15.         Spawn
  16.         Move
  17.         Order
  18. --]]
  19.  
  20. --[[ Services ]]--
  21. local serverStorage = game:GetService("ServerStorage")
  22. local pathfindingService = game:GetService("PathfindingService")
  23. local physicsService = game:GetService("PhysicsService")
  24.  
  25. --[[ Components ]]--
  26. local components = serverStorage:WaitForChild("Components")
  27.  
  28. --// Classes //--
  29. local Order = require(components:WaitForChild("Order"))
  30.  
  31. --[[ Data ]]--
  32. local data = serverStorage:WaitForChild("Data")
  33.  
  34. local CustomerData = require(data:WaitForChild("CustomerData"))
  35.  
  36. --[[ Assets ]]--
  37. local assets = serverStorage:WaitForChild("Assets")
  38.  
  39. --// Rigs //--
  40. local rigs = assets:WaitForChild("Rigs")
  41.  
  42. --[[ Collision Groups ]]--
  43.  
  44. --// Npc Collision Group //--
  45. local NpcCollisionGroup = physicsService:CreateCollisionGroup("NpcCollisionGroup")
  46. physicsService:CollisionGroupSetCollidable("NpcCollisionGroup", "NpcCollisionGroup")
  47.  
  48. --[[ Private Functions ]]--
  49. local function FindItemsOfType(character, itemFolder, itemType)
  50.     local itemArray = {}
  51.    
  52.     -- Append valid items to itemArray
  53.     for _, v in pairs(itemFolder:GetChildren()) do
  54.         if v.ItemData.ItemType == itemType then
  55.             local itemDistance = (character.PrimaryPart.Position - v.ItemModel.PrimaryPart.Position).magnitude
  56.             local element = {item = v, distance = itemDistance}
  57.         end
  58.     end
  59.    
  60.     -- Sort itemArray
  61.     table.sort(itemArray, function(elementA, elementB)
  62.         return elementA.distance < elementB.distance
  63.     end)
  64.    
  65.     return itemArray
  66. end
  67.  
  68. local function AddModelToCollisionGroup(model, collisionGroup)
  69.     for _, part in pairs(model:GetChildren()) do
  70.         if part:IsA("BasePart") then
  71.             physicsService:SetPartCollisionGroup(part, collisionGroup)
  72.         end
  73.     end
  74. end
  75.  
  76. --// Class //--
  77. local Customer = {}
  78. Customer.__index = Customer
  79.  
  80. --// Constructor //--
  81. function Customer.new(owner, plot, customerType, rig)
  82.     --Variable Initialization.
  83.     local self = setmetatable({}, Customer)
  84.     local customerTemplate = CustomerData.CustomerTemplates[customerType]
  85.    
  86.     CustomerData.CustomerCount = CustomerData.CustomerCount
  87.    
  88.     -- Attribute declaration.
  89.     self.type = customerTemplate.Type
  90.     self.tipBudget = customerTemplate.MaxTipBudget
  91.     self.plot = plot
  92.     self.owner = owner
  93.     self.objective = nil
  94.     self.currentState = nil
  95.     self.customerId = CustomerData.CustomerCount
  96.    
  97.     -- Rig selection.
  98.     self.character = rig and rig:Clone() or (function() local rigs = rigs:GetChildren() return rigs[math.random(#rigs)]:Clone() end)()
  99.    
  100.     -- Add character to collision group.
  101.     AddModelToCollisionGroup(self.character, "NpcCollisionGroup")
  102.    
  103.     return self
  104. end
  105.  
  106. --// Methods //--
  107. function Customer:Spawn()
  108.     -- Move character
  109.     self.character.Parent = self.plot.Customers
  110.     self.character:SetPrimaryPartCFrame(CFrame.new(self.plot.Spawn.Position + Vector3.new(0, 3, 0)))
  111. end
  112.  
  113. function Customer:Move(position) -- Use A* in the future.
  114.     local path = pathfindingService:FindPathAsync(self.character.PrimaryPart.Position, position)
  115.    
  116.     if path.Status == Enum.PathStatus.NoPath then
  117.         spawn(function()
  118.             repeat
  119.                 path = pathfindingService:FindPathAsync(self.character.PrimaryPart.Position, position)
  120.                 wait(0.5)
  121.             until path.Status == Enum.PathStatus.Success
  122.         end)
  123.     end
  124.    
  125.     spawn(function()
  126.         local waypoints = path:GetWaypoints()
  127.         for _, pathWaypoint in pairs(waypoints) do
  128.             self.character.Humanoid:MoveTo(pathWaypoint.Position)
  129.            
  130.             if pathWaypoint.Action == Enum.PathWaypointAction.Jump then self.charactr.Humanoid.Jump = true end
  131.            
  132.             self.character.Humanoid.MoveToFinished:Wait()
  133.         end
  134.     end)
  135. end
  136.  
  137. function Customer:SetObjective(objective)
  138.     if not objective then warn("Objective wasn't passed") return end
  139.    
  140.     self.objective = objective
  141.    
  142.     if objective == "Order" then
  143.         -- Find the nearest counters.
  144.         local counters = FindItemsOfType(self.character, self.plot.Items, "Counter")   
  145.        
  146.         -- Assign counter to the nearest, free counter, and add the character to the queue.
  147.         local counter = counters[1]
  148.         do
  149.             local index = 1
  150.            
  151.             while true do
  152.                 local counterQueue = counter.Queue:GetChildren()
  153.                
  154.                 if #counterQueue < counter.ItemData.MaxQueue.Value then
  155.                     -- Insert character to queue.
  156.                     local characterQueueValue = Instance.new("ObjectValue")
  157.                     characterQueueValue.Name = #counterQueue + 1
  158.                     characterQueueValue.Value = self.character
  159.                     characterQueueValue.Parent = counter.Queue         
  160.                     break
  161.                 end
  162.                
  163.                 counter = counters[index + 1]
  164.                
  165.                 wait(0.1)
  166.             end
  167.         end
  168.        
  169.         -- Move to queue
  170.         do
  171.             -- Set customer state.
  172.             self.currentState = "Queueing"
  173.        
  174.             -- Move to the end of the queue.
  175.             local queueTargetPosition = CFrame.new(counter.ItemModel.CustomerInteractionPart.CFrame * CFrame.new(Vector3.new(0, 0, -3) * #counter.Queue:GetChildren())).p
  176.             self:Move(queueTargetPosition, true)
  177.            
  178.             -- Stay & Move up in the queue until the character is first.
  179.             local childRemovedSignal
  180.             childRemovedSignal = counter.Queue.ChildRemoved:Connect(function(child)
  181.                 if child.Value == self.character then
  182.                     childRemovedSignal:Disconnect()
  183.                     return
  184.                 end
  185.                
  186.                 -- Increment own character's value obj name.
  187.                 local queueValue
  188.                
  189.                 for i, v in pairs(counter.Queue:GetChildren()) do
  190.                     if v.Value == self.character then
  191.                         queueValue = v
  192.                     end
  193.                 end
  194.             end)
  195.            
  196.             repeat wait(0.1) until not (function()
  197.                 for i, v in pairs(counter.Queue:GetChildren()) do
  198.                     if v.Value == self.character then
  199.                         return true
  200.                     end
  201.                 end
  202.             end)()
  203.         end
  204.        
  205.         -- Move to the interaction part.
  206.        
  207.        
  208.         -- Construct order object.
  209.         self.order = Order.new(self)
  210.     elseif objective == "Sit" then
  211.     elseif objective == "Leave" then
  212.     end
  213. end
  214.  
  215. return Customer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement