Finish production line logic
This commit is contained in:
@@ -1,40 +1,65 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class Facility : MonoBehaviour
|
||||
{
|
||||
public Resource inputResource;
|
||||
public Resource outputResource;
|
||||
private int worker = 0;
|
||||
private GameManager gameManager;
|
||||
|
||||
[SerializeField]
|
||||
private int materialConsumption = 5;
|
||||
[SerializeField]
|
||||
private float processingTime = 5.0f;
|
||||
[SerializeField]
|
||||
private int baseOutput = 2;
|
||||
[SerializeField]
|
||||
private TMP_Text currentProgressLabel;
|
||||
private bool processing;
|
||||
private float currentProcess = 0f;
|
||||
private int currentProductionBatch = 0;
|
||||
|
||||
private AssignableWorker assignableWorker;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
assignableWorker = gameObject.GetComponent<AssignableWorker>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!processing) return;
|
||||
if (!processing && assignableWorker.GetAssignedWorkers() <= 0) return; //No workers and no manual process running
|
||||
|
||||
if (!processing) //Not processing but workers assigned, start new job if material is available
|
||||
{
|
||||
if (gameManager.GetResource(inputResource) < materialConsumption) return; //Not enough material
|
||||
|
||||
//Consumtion 5, 2
|
||||
int workerCount = (assignableWorker.GetAssignedWorkers() == 0 ? 1 : assignableWorker.GetAssignedWorkers());
|
||||
int maxPossibleBatch = gameManager.GetResource(inputResource) / materialConsumption;
|
||||
currentProductionBatch = Mathf.Min(maxPossibleBatch, workerCount);
|
||||
|
||||
int requiredRessources = currentProductionBatch * materialConsumption;
|
||||
|
||||
gameManager.RemoveResource(inputResource,requiredRessources);
|
||||
currentProcess = processingTime;
|
||||
processing = true;
|
||||
}
|
||||
|
||||
if(currentProcess > 0)
|
||||
{
|
||||
currentProcess -= Time.deltaTime;
|
||||
currentProgressLabel.text = currentProcess.ToString();
|
||||
}
|
||||
|
||||
if (currentProcess <= 0){
|
||||
currentProcess = processingTime;
|
||||
processing = false;
|
||||
gameManager.AddResource(outputResource, 1 + worker);
|
||||
gameManager.AddResource(outputResource, currentProductionBatch * baseOutput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user