Finish production line logic

This commit is contained in:
2024-08-18 17:03:09 +02:00
parent 67c6597c21
commit a7fd4a2fb1
21 changed files with 2558 additions and 736 deletions

View File

@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssignableWorker : MonoBehaviour
{
private int workersAssigned = 0;
private GameManager gameManager;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
public int GetAssignedWorkers()
{
return workersAssigned;
}
public void AddWorker()
{
if(gameManager.GetResource(Resource.WORKER) > 0)
{
workersAssigned++;
gameManager.RemoveResource(Resource.WORKER, 1);
}
}
public bool RemoveWorker()
{
if (workersAssigned == 0) return false;
workersAssigned--;
gameManager.AddResource(Resource.WORKER, 1);
return true;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bde21470ef329a94caf3c31d0d3ff253

View File

@@ -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);
}
}

View File

@@ -12,11 +12,11 @@ public enum Resource
TILE,
GLASS,
HOUSE,
WORKER
WORKER,
TOTAL_WORKER
}
public class GameManager : MonoBehaviour
{
private Dictionary<Resource, int> resources = new Dictionary<Resource, int>()
{
{ Resource.ROCK, 0 },
@@ -25,8 +25,9 @@ public class GameManager : MonoBehaviour
{ Resource.BRICK, 0 },
{ Resource.TILE, 0 },
{ Resource.GLASS, 0 },
{ Resource.HOUSE, 0 },
{ Resource.WORKER, 0 }
{ Resource.HOUSE, 1 },
{ Resource.WORKER, 0 },
{ Resource.TOTAL_WORKER, 0 },
};

View File

@@ -4,6 +4,7 @@ using UnityEngine;
public class HouseManager : MonoBehaviour
{
[SerializeField]
private float ttnw = 30; //Time to next worker
private float currentTtnw = 0;
private int workerPerHouse = 3;
@@ -23,9 +24,12 @@ public class HouseManager : MonoBehaviour
} else
{
currentTtnw = ttnw;
if (gameManager.GetResource(Resource.WORKER) < gameManager.GetResource(Resource.HOUSE) * workerPerHouse)
if (gameManager.GetResource(Resource.TOTAL_WORKER) < gameManager.GetResource(Resource.HOUSE) * workerPerHouse)
{
gameManager.AddResource(Resource.WORKER, 1);
int freeWorkerSlots = (gameManager.GetResource(Resource.HOUSE) * workerPerHouse) - gameManager.GetResource(Resource.TOTAL_WORKER);
int newWorkers = Mathf.CeilToInt((float)freeWorkerSlots / workerPerHouse);
gameManager.AddResource(Resource.WORKER, newWorkers);
gameManager.AddResource(Resource.TOTAL_WORKER, newWorkers);
}
}
}

View File

@@ -6,17 +6,32 @@ using UnityEngine;
public class Mine : MonoBehaviour
{
public Resource resource;
private GameManager gameManager;
private GameManager gameManager;
private float nextTick = 0f;
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()
{
nextTick -= Time.deltaTime;
if (nextTick <= 0f)
{
Tick();
nextTick = 1f;
}
}
private void Tick()
{
gameManager.AddResource(resource, 1 * assignableWorker.GetAssignedWorkers());
}
private void OnMouseDown()

8
Assets/Scripts/UI.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1ea22b389354695468bd9cbdf72350da
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Spinbox : MonoBehaviour
{
public AssignableWorker target;
[SerializeField]
private TMP_Text numAssignedLabel;
private void Start()
{
target = transform.parent.GetComponent<AssignableWorker>();
}
private void Update()
{
numAssignedLabel.text = target.GetAssignedWorkers().ToString();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e135e4c4d24079a499b77564d448e882

View File

@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum SpinnerBtnType
{
UP,
DOWN
}
public class SpinboxBtn : MonoBehaviour
{
[SerializeField]
private SpinnerBtnType type;
private void OnMouseUp()
{
if (type == SpinnerBtnType.UP)
{
transform.parent.gameObject.GetComponent<Spinbox>().target.AddWorker();
}
if (type == SpinnerBtnType.DOWN)
{
transform.parent.gameObject.GetComponent<Spinbox>().target.RemoveWorker();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b8370f1edd4d23141ada2da575eee2ac