Implement basic production chain logic
This commit is contained in:
51
Assets/Scripts/Facility.cs
Normal file
51
Assets/Scripts/Facility.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
private bool processing;
|
||||
private float currentProcess = 0f;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!processing) return;
|
||||
|
||||
if(currentProcess > 0)
|
||||
{
|
||||
currentProcess -= Time.deltaTime;
|
||||
}
|
||||
|
||||
if (currentProcess <= 0){
|
||||
currentProcess = processingTime;
|
||||
processing = false;
|
||||
gameManager.AddResource(outputResource, 1 + worker);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMouseUp()
|
||||
{
|
||||
if (processing) return; //Already processing
|
||||
|
||||
if (gameManager.GetResource(inputResource) < materialConsumption) return; //Not enough material
|
||||
|
||||
gameManager.RemoveResource(inputResource, materialConsumption);
|
||||
currentProcess = processingTime;
|
||||
processing = true;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Facility.cs.meta
Normal file
2
Assets/Scripts/Facility.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e163bdac6933dc49ad182a7fdcad213
|
||||
65
Assets/Scripts/GameManager.cs
Normal file
65
Assets/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public enum Resource
|
||||
{
|
||||
ROCK,
|
||||
SAND,
|
||||
CLAY,
|
||||
BRICK,
|
||||
TILE,
|
||||
GLASS,
|
||||
HOUSE,
|
||||
WORKER
|
||||
}
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
|
||||
private Dictionary<Resource, int> resources = new Dictionary<Resource, int>()
|
||||
{
|
||||
{ Resource.ROCK, 0 },
|
||||
{ Resource.SAND, 0 },
|
||||
{ Resource.CLAY, 0 },
|
||||
{ Resource.BRICK, 0 },
|
||||
{ Resource.TILE, 0 },
|
||||
{ Resource.GLASS, 0 },
|
||||
{ Resource.HOUSE, 0 },
|
||||
{ Resource.WORKER, 0 }
|
||||
};
|
||||
|
||||
|
||||
public TMPro.TMP_Text debugText;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
debugText.text = "";
|
||||
foreach (var item in resources)
|
||||
{
|
||||
debugText.text += item.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void AddResource(Resource res, int amt)
|
||||
{
|
||||
resources[res] += amt;
|
||||
}
|
||||
|
||||
public void RemoveResource(Resource res, int amt)
|
||||
{
|
||||
resources[res] -= amt;
|
||||
}
|
||||
|
||||
public int GetResource(Resource res)
|
||||
{
|
||||
return resources[res];
|
||||
}
|
||||
|
||||
}
|
||||
2
Assets/Scripts/GameManager.cs.meta
Normal file
2
Assets/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94fc5ec1bbc06a34b8ea2ada97112e3b
|
||||
48
Assets/Scripts/HouseManager.cs
Normal file
48
Assets/Scripts/HouseManager.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HouseManager : MonoBehaviour
|
||||
{
|
||||
private float ttnw = 30; //Time to next worker
|
||||
private float currentTtnw = 0;
|
||||
private int workerPerHouse = 3;
|
||||
private GameManager gameManager;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (currentTtnw > 0)
|
||||
{
|
||||
currentTtnw -= Time.deltaTime;
|
||||
} else
|
||||
{
|
||||
currentTtnw = ttnw;
|
||||
if (gameManager.GetResource(Resource.WORKER) < gameManager.GetResource(Resource.HOUSE) * workerPerHouse)
|
||||
{
|
||||
gameManager.AddResource(Resource.WORKER, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void BuildHouse()
|
||||
{
|
||||
if (gameManager.GetResource(Resource.BRICK) < 10 ||
|
||||
gameManager.GetResource(Resource.TILE) < 5 ||
|
||||
gameManager.GetResource(Resource.GLASS) < 2)
|
||||
{
|
||||
Debug.Log("Not enough resources to build house!");
|
||||
return;
|
||||
}
|
||||
|
||||
gameManager.RemoveResource(Resource.BRICK, 10);
|
||||
gameManager.RemoveResource(Resource.TILE, 5);
|
||||
gameManager.RemoveResource(Resource.GLASS, 2);
|
||||
gameManager.AddResource(Resource.HOUSE, 1);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/HouseManager.cs.meta
Normal file
2
Assets/Scripts/HouseManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc980ba9f26d56d428418e476e18f318
|
||||
26
Assets/Scripts/Mine.cs
Normal file
26
Assets/Scripts/Mine.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class Mine : MonoBehaviour
|
||||
{
|
||||
public Resource resource;
|
||||
private GameManager gameManager;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void OnMouseDown()
|
||||
{
|
||||
gameManager.AddResource(resource, 1);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Mine.cs.meta
Normal file
2
Assets/Scripts/Mine.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51a62f1c9fec82147853c4aa578e47b8
|
||||
Reference in New Issue
Block a user