Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Civilian : MonoBehaviour {
public TaskList task;
public RessourceManager RM;
private ActionList AL;
GameObject targetNode;
public NodeManager.ResourceTypes heldResourceType;
public bool isGathering = false;
private NavMeshAgent agent;
public int heldResource;
public int maxHeldResource;
public GameObject[] drops;
// Use this for initialization
void Start () {
StartCoroutine(GatherTick());
agent = GetComponent<NavMeshAgent>();
AL = FindObjectOfType<ActionList>();
}
// Update is called once per frame
void Update () {
if (targetNode == null)
{
if (heldResource != 0)
{
drops = GameObject.FindGameObjectsWithTag("Drops");
agent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}
else
{
task = TaskList.Idle;
}
}
if (heldResource >= maxHeldResource)
{
//Drop off point here
drops = GameObject.FindGameObjectsWithTag("Drops");
agent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}
if (Input.GetMouseButtonDown(1) && GetComponent<ObjectInfo>().isSelected)
{
RightClick();
}
}
GameObject GetClosestDropOff(GameObject[] dropOffs)
{
GameObject closestDrop = null;
float closestDistance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject targetDrop in dropOffs)
{
Vector3 direction = targetDrop.transform.position - position;
float distance = direction.sqrMagnitude;
if (distance < closestDistance)
{
closestDistance = distance;
closestDrop = targetDrop;
}
}
return closestDrop;
}
public void RightClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.tag == "Ground")
{
AL.Move(agent, hit, task);
}
else if (hit.collider.tag == "Ressource")
{
AL.Harvest(agent, hit, task, targetNode);
}
}
}
public void OnTriggerEnter(Collider other)
{
GameObject hitObject = other.gameObject;
if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
{
isGathering = true;
hitObject.GetComponent<NodeManager>().gatherers++;
heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
}
else if (hitObject.tag == "Drops" && task == TaskList.Delivering)
{
if (RM.stone >= RM.maxStone)
{
task = TaskList.Idle;
}
else
{
RM.stone += heldResource;
heldResource = 0;
task = TaskList.Gathering;
agent.destination = targetNode.transform.position;
}
}
}
public void OnTriggerExit(Collider other)
{
GameObject hitObject = other.gameObject;
if (hitObject.tag == "Ressource")
{
hitObject.GetComponent<NodeManager>().gatherers--;
isGathering = false;
}
}
IEnumerator GatherTick()
{
while (true)
{
yield return new WaitForSeconds(1);
if (isGathering)
{
heldResource++;
}
}
}
}