Factorisation des Usable

This commit is contained in:
Antoine H 2020-12-10 11:57:23 +01:00
parent 5b787c7da6
commit 4aa2aef787
7 changed files with 34 additions and 31 deletions

View file

@ -139,7 +139,7 @@ GameObject:
- component: {fileID: 107140170}
m_Layer: 8
m_Name: Oldman
m_TagString: Client
m_TagString: Usable
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -7065,7 +7065,7 @@ GameObject:
- component: {fileID: 1165291008}
m_Layer: 8
m_Name: Barrel
m_TagString: Workshop
m_TagString: Usable
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0

View file

@ -53,8 +53,8 @@ public class Client_controller : MonoBehaviour, IUsable
{
if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
if(gameObject.tag != "Client")
Debug.LogWarning(gameObject.name+" tag should be set to 'Client' to work properly");
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
}
// Update is called once per frame

View file

@ -3,11 +3,10 @@ using System.Collections.Generic;
using UnityEngine;
//Represent object that can be grabed.
public interface IGrabable
public interface IGrabable: IUsable
{
//Unity inspector doesn't handle well interface...
int size {get; set;} //Size (1 or 2 hands) of the object
void use();
void take();
void drop(Vector2 position); //Drop to position
}

View file

@ -11,9 +11,11 @@ public class Mug : MonoBehaviour, IGrabable
public bool dirty = false;
public Consumable content{get; protected set;} = null; //new Consumable("beer",1,null);
public void use()
//TODO: Gérer objets tavernier (drop) et autres
public bool use(GameObject userObject)
{
//Do nothing
return false; //Return wether the object is taken from tavernkeeper
}
public void take() //Object taken
{

View file

@ -9,8 +9,12 @@ public class Tavernkeeper_controller : MonoBehaviour
{
public float mvt_speed = 5.0f; //Movement speed
public float action_dist = 1.5f; //Action distance
public float action_cd = 0.5f; //Action cooldown
IDictionary<string, GameObject> hand_container; //Objects in hand
float actionTimer;
bool isInteracting;
// Last user inputs
float horizontal;
@ -80,9 +84,19 @@ public class Tavernkeeper_controller : MonoBehaviour
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
//Hands actions
if(!Mathf.Approximately(hands, 0.0f))
//Actions delay
actionTimer -= Time.deltaTime;
if(isInteracting)
{
actionTimer -= Time.deltaTime;
if (actionTimer < 0)
isInteracting = false;
}
//Hands actions
if(!isInteracting && !Mathf.Approximately(hands, 0.0f))
{
actionTimer= action_cd;
isInteracting=true;
if(hands>0)
{
// Debug.Log("Left hand");
@ -131,34 +145,21 @@ public class Tavernkeeper_controller : MonoBehaviour
{
grab(hit_object, hand);
}
else if(hit_object.tag == "Client")
else if(hit_object.tag == "Usable")
{
// Debug.Log("Give "+ hand_container[hand].name+" to "+hit_object.name);
Client_controller client = hit_object.GetComponent<Client_controller>();
if(client!=null)
{
if(client.use(hand_container[hand])) //Interactions w/ object in hands
{
//Object taken by client
hand_container[hand]=null;
}
}
}
else if(hit_object.tag == "Workshop")
{
IUsable workshop = hit_object.GetComponent<IUsable>();
if(workshop!=null)
IUsable usable = hit_object.GetComponent<IUsable>();
if(usable!=null)
{
if(hand_container[hand] is null) //No object in hands
{
workshop.use(gameObject); //Tavernkeeper interacting directly w/ workshop
usable.use(gameObject); //Tavernkeeper interacting directly w/ usable
Debug.Log(gameObject.name+" use "+hit_object.name);
}
else if(workshop.use(hand_container[hand])) //Interactions w/ object in hands
else if(usable.use(hand_container[hand])) //Interactions w/ object in hands
{
//Object taken by workshop
// Debug.Log("Give "+ hand_container[hand].name+" to "+hit_object.name);
//Object taken by usable
hand_container[hand]=null;
Debug.Log("Give "+ hand_container[hand].name+" to "+hit_object.name);
}
}
}

View file

@ -59,8 +59,8 @@ public class Workshop : MonoBehaviour, IUsable
{
if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
if(gameObject.tag != "Workshop")
Debug.LogWarning(gameObject.name+" tag should be set to 'Workshop' to work properly");
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
}
// Update is called once per frame

View file

@ -8,6 +8,7 @@ TagManager:
- Workshop
- Cleaning_workshop
- Grabable
- Usable
layers:
- Default
- TransparentFX