First client navigation

This commit is contained in:
Antoine H 2020-12-30 20:09:20 +01:00
parent 57088cfd12
commit 2db3e27a47
10 changed files with 493 additions and 12 deletions

View file

@ -1,9 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
//Define the behavior of a client
[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(NavMeshAgent))]
public class Client_controller : MonoBehaviour, IUsable
{
public float consumeTime = 3.0f; //Time to consume currentMug
@ -12,6 +14,10 @@ public class Client_controller : MonoBehaviour, IUsable
float consumeTimer;
GameObject currentMug = null; //Mug currently held by the client
Transform target;
Vector2 destination;
NavMeshAgent agent;
//Handle objects interactions w/ Workshop
//Return wether the object is taken from tavernkeeper
public bool use(GameObject object_used)
@ -55,11 +61,26 @@ public class Client_controller : MonoBehaviour, IUsable
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
// Navigation //
agent = GetComponent<NavMeshAgent>();
//Prevent rotation of the ground at movement
agent.updateRotation = false;
agent.updateUpAxis = false;
//Get target
agent.destination = ClientManager.Instance.assignTarget().position;
}
// Update is called once per frame
void Update()
{
//Navigation
// if (Vector2.Distance(destination, target.position) > 1.0f)
// {
// destination = target.position;
// agent.destination = destination;
// }
//Timer
if (currentMug!= null) //Consuming mug if there's one
{

View file

@ -9,18 +9,20 @@ public sealed class ClientManager : MonoBehaviour
int currentNbClient = 0;
int nbMaxClients = 1;
bool clientSpawnReady = false;
float clientSpawnTimer = 2.0f; //Intial time before first spawn (pseudo-random after that)
float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
float maxTimeNewClients = 10.0f;
string ClientRessourceFolder = "Clients";
Vector3 spawnPosition = new Vector3(0, 0, 0);
private Object[] clients;
Vector3 spawnPosition = new Vector3(0, 0, 0); //TODO : Use gameObject
Dictionary<Transform, bool> targets_dict; //Dict with target and wether they're taken by a client
//Request new client
//Return wether a new client was created
public bool clientRequest()
{
if(clientSpawnReady && currentNbClient<nbMaxClients)
if(clientSpawnReady && currentNbClient<nbMaxClients && targets_dict.ContainsValue(false))
{
GameObject newClient = (GameObject)clients[Random.Range(0, clients.Length)];
// Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition);
@ -34,9 +36,24 @@ public sealed class ClientManager : MonoBehaviour
return false; //No new client
}
//Assign a random available target
public Transform assignTarget()
{
List<Transform> avail_tgt = new List<Transform>();
foreach(KeyValuePair<Transform, bool> tgt in targets_dict)
if(tgt.Value is false)
avail_tgt.Add(tgt.Key);
Transform target = avail_tgt[Random.Range(0, avail_tgt.Count)];
targets_dict[target]=true;
return target;
}
// Start is called before the first frame update
void Start()
{
// Load clients prefabs //
// Find all assets labelled with 'usable' :
// string[] guids = AssetDatabase.FindAssets("", new string[] {"Assets/Prefabs/Characters/Clients"});
@ -52,6 +69,31 @@ public sealed class ClientManager : MonoBehaviour
{
Debug.Log(gameObject.name+" : "+c.name + " loaded");
}
// Load Client spawn point //
GameObject spawnObj = GameObject.Find("/GameSystem/ClientSpawn");
if (spawnObj is null)
throw new System.Exception("No ClientSpawn GameObject found under GameSystem");
spawnPosition = spawnObj.transform.position;
// Load Client targets //
targets_dict = new Dictionary<Transform, bool>();
GameObject targetsObj = GameObject.Find("/GameSystem/Targets");
if (targetsObj is null)
throw new System.Exception("No Targets GameObject found under GameSystem");
Component[] targets = targetsObj.GetComponentsInChildren<Transform>();
if(targets != null)
{
foreach(Transform target in targets)
{
if(target.gameObject.name != "Targets")
{
targets_dict.Add(target, false);
Debug.Log("Client target : "+ target.gameObject.name + target.position);
}
}
}
}
// Update is called once per frame