hi so i got resources like wood stone food,each can be produced by multiple sources. each source will have different amount and rate of harvest. I will be harvesting with mouse click. I am confused how to go about and set things up.
First i have a resource script like this
// class to define resources
// attached to every object which can produce resource
public class Resource: MonoBehaviour
{
public enum ResourceTypes
{
Wood,
Stone,
Food,
};
public ResourceTypes resourceType;
public float gatherableAmount;
public float gatherPerSec;
public GameObject resourceImage;
public AudioClip ResourceGatheringSfx;
}
and add this as a component to each of the sources and select the resource type then with my input class that handles raycast i will have to compare hit.resourceType with a string of the resource type which to me feels bad cuz of alot of if statements.
public class ResourceInput : MonoBehaviour
{
Resource res;
void Update ()
{
if (Input.GetMouseButton (0))
{
Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10f);
Ray ray = Camera.main.ScreenPointToRay (mousePos);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100f))
{
if (hit.collider.tag == hit.transform.GetComponent<Resource> ().resourceType.ToString ())
{
res = hit.transform.GetComponent<Resource> ();
if (res.gatherableAmount > 0)
{
res.gatherableAmount -= res.gatherPerSec * Time.deltaTime;
ResourceManager.Instance.AddResource (res.resourceType, res.gatherPerSec * Time.deltaTime);
}
else
{
hit.transform.gameObject.SetActive (false);
}
}
}
}
}
}
the problem arises in this script with all the if statements especially when i add more resources
// class to manage resources collected
public class ResourceManager : Singleton<ResourceManager>
{
public float woodCollected;
public float stoneCollected;
public float foodCollected;
public void AddResource (Resource.ResourceTypes resType, float amount)
{
if (resType.ToString () == "Wood") {
woodCollected += amount;
}
if (resType.ToString () == "Stone") {
stoneCollected += amount;
}
if (resType.ToString () == "Food") {
foodCollected += amount;
}
}
}
can someone suggest me a good structure in which i do not have to do these if statements?