Finish production line logic
This commit is contained in:
37
Assets/Scripts/AssignableWorker.cs
Normal file
37
Assets/Scripts/AssignableWorker.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AssignableWorker.cs.meta
Normal file
2
Assets/Scripts/AssignableWorker.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bde21470ef329a94caf3c31d0d3ff253
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 },
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
8
Assets/Scripts/UI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ea22b389354695468bd9cbdf72350da
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Scripts/UI/Spinbox.cs
Normal file
21
Assets/Scripts/UI/Spinbox.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/Spinbox.cs.meta
Normal file
2
Assets/Scripts/UI/Spinbox.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e135e4c4d24079a499b77564d448e882
|
||||
28
Assets/Scripts/UI/SpinboxBtn.cs
Normal file
28
Assets/Scripts/UI/SpinboxBtn.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/SpinboxBtn.cs.meta
Normal file
2
Assets/Scripts/UI/SpinboxBtn.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8370f1edd4d23141ada2da575eee2ac
|
||||
Reference in New Issue
Block a user