From 4aa2aef787ca294117d3e9bbd6863c4c9654950c Mon Sep 17 00:00:00 2001 From: Antoine H Date: Thu, 10 Dec 2020 11:57:23 +0100 Subject: [PATCH] Factorisation des Usable --- Assets/Scenes/Tests/SampleScene.unity | 4 +- Assets/Scripts/Client_controller.cs | 4 +- Assets/Scripts/IGrabable.cs | 3 +- Assets/Scripts/Mug.cs | 4 +- Assets/Scripts/Tavernkeeper_controller.cs | 45 ++++++++++++----------- Assets/Scripts/Workshop.cs | 4 +- ProjectSettings/TagManager.asset | 1 + 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Assets/Scenes/Tests/SampleScene.unity b/Assets/Scenes/Tests/SampleScene.unity index ac31c6a..89656ef 100644 --- a/Assets/Scenes/Tests/SampleScene.unity +++ b/Assets/Scenes/Tests/SampleScene.unity @@ -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 diff --git a/Assets/Scripts/Client_controller.cs b/Assets/Scripts/Client_controller.cs index 2cdd29d..8012d15 100644 --- a/Assets/Scripts/Client_controller.cs +++ b/Assets/Scripts/Client_controller.cs @@ -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 diff --git a/Assets/Scripts/IGrabable.cs b/Assets/Scripts/IGrabable.cs index 0f0d768..3bd512b 100644 --- a/Assets/Scripts/IGrabable.cs +++ b/Assets/Scripts/IGrabable.cs @@ -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 } diff --git a/Assets/Scripts/Mug.cs b/Assets/Scripts/Mug.cs index 91a65e6..7212fa8 100644 --- a/Assets/Scripts/Mug.cs +++ b/Assets/Scripts/Mug.cs @@ -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 { diff --git a/Assets/Scripts/Tavernkeeper_controller.cs b/Assets/Scripts/Tavernkeeper_controller.cs index 89717cf..a954489 100644 --- a/Assets/Scripts/Tavernkeeper_controller.cs +++ b/Assets/Scripts/Tavernkeeper_controller.cs @@ -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 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(); - 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(); - if(workshop!=null) + IUsable usable = hit_object.GetComponent(); + 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); } } } diff --git a/Assets/Scripts/Workshop.cs b/Assets/Scripts/Workshop.cs index 905dca5..f5d0f8c 100644 --- a/Assets/Scripts/Workshop.cs +++ b/Assets/Scripts/Workshop.cs @@ -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 diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index bdf3ada..2ebdbbf 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -8,6 +8,7 @@ TagManager: - Workshop - Cleaning_workshop - Grabable + - Usable layers: - Default - TransparentFX