ProjetPatate/Assets/Scripts/Client_controller.cs

102 lines
3.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-12-30 20:09:20 +01:00
using UnityEngine.AI;
2020-12-05 15:22:00 +01:00
//Define the behavior of a client
[RequireComponent(typeof(Collider2D))]
2020-12-30 20:09:20 +01:00
[RequireComponent(typeof(NavMeshAgent))]
public class Client_controller : MonoBehaviour, IUsable
{
public float consumeTime = 3.0f; //Time to consume currentMug
public float waitingTime = 10.0f; //Patience after ordering
2020-12-07 15:40:34 +01:00
float consumeTimer;
2020-12-07 16:04:11 +01:00
GameObject currentMug = null; //Mug currently held by the client
2020-12-30 20:09:20 +01:00
Transform target;
Vector2 destination;
NavMeshAgent agent;
2020-12-05 15:22:00 +01:00
//Handle objects interactions w/ Workshop
//Return wether the object is taken from tavernkeeper
2020-12-04 21:49:58 +01:00
public bool use(GameObject object_used)
{
2020-12-05 13:38:13 +01:00
if(currentMug is null) //No mug in hand
{
//TODO : Gérer Grabale qui ne sont pas des Mugs ?
if(object_used != null && object_used.tag=="Grabable")
2020-12-04 21:49:58 +01:00
{
2020-12-05 13:38:13 +01:00
Mug mug = object_used.GetComponent<Mug>();
if (mug!= null && mug.content != null)
{
Debug.Log(gameObject.name+" take "+object_used.name+ " of "+mug.content.Type);
currentMug = object_used;
2020-12-07 15:40:34 +01:00
consumeTimer=consumeTime;
2020-12-05 13:38:13 +01:00
return true;
}
else
{
Debug.Log("Display order (or something else) of "+gameObject.name);
return false;
}
2020-12-04 21:49:58 +01:00
}
else
{
Debug.Log("Display order (or something else) of "+gameObject.name);
return false;
}
}
else
{
2020-12-05 13:38:13 +01:00
Debug.Log(gameObject.name+" already consumming "+currentMug.name);
return false;
}
}
// Start is called before the first frame update
void Start()
{
2020-12-07 17:22:30 +01:00
if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
2020-12-10 11:57:23 +01:00
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
2020-12-30 20:09:20 +01:00
// 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()
{
2020-12-30 20:09:20 +01:00
//Navigation
// if (Vector2.Distance(destination, target.position) > 1.0f)
// {
// destination = target.position;
// agent.destination = destination;
// }
2020-12-07 15:40:34 +01:00
//Timer
if (currentMug!= null) //Consuming mug if there's one
{
consumeTimer -= Time.deltaTime;
if (consumeTimer < 0) //Finished consuming mug ?
{
Mug obj = currentMug.GetComponent<Mug>();
if(obj !=null)
{
obj.consume();
//Drop mug
obj.drop(gameObject.transform.position+ (Vector3)Vector2.down * 0.2f);
currentMug=null;
}
}
}
}
}