From eacf4f2711d63c145ab0a68493bdd1af06eca274 Mon Sep 17 00:00:00 2001 From: Antoine H Date: Wed, 9 Dec 2020 15:41:56 +0100 Subject: [PATCH] Factorisation des interactions IGrabable --- Assets/Prefabs/Characters/Tavernkeeper.prefab | 2 +- Assets/Scenes/Tests/SampleScene.unity | 19 ++++--- Assets/Scripts/IGrabable.cs | 4 +- Assets/Scripts/Mug.cs | 7 +-- Assets/Scripts/Tavernkeeper_controller.cs | 54 +++++++++++++------ Assets/Scripts/Workshop.cs | 32 +++++++---- ProjectSettings/TagManager.asset | 2 +- 7 files changed, 76 insertions(+), 44 deletions(-) diff --git a/Assets/Prefabs/Characters/Tavernkeeper.prefab b/Assets/Prefabs/Characters/Tavernkeeper.prefab index 2730758..cc625e1 100644 --- a/Assets/Prefabs/Characters/Tavernkeeper.prefab +++ b/Assets/Prefabs/Characters/Tavernkeeper.prefab @@ -16,7 +16,7 @@ GameObject: - component: {fileID: 5841415790350674207} m_Layer: 0 m_Name: Tavernkeeper - m_TagString: Untagged + m_TagString: Player m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 diff --git a/Assets/Scenes/Tests/SampleScene.unity b/Assets/Scenes/Tests/SampleScene.unity index c80d345..ac31c6a 100644 --- a/Assets/Scenes/Tests/SampleScene.unity +++ b/Assets/Scenes/Tests/SampleScene.unity @@ -6942,7 +6942,7 @@ GameObject: - component: {fileID: 1163880822} m_Layer: 8 m_Name: Mug - m_TagString: Mug + m_TagString: Grabable m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -6959,7 +6959,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 06f2e18c7d91f7f4d99cd5e5c7d12b45, type: 3} m_Name: m_EditorClassIdentifier: - size: 1 dirty: 0 --- !u!212 &1163880823 SpriteRenderer: @@ -32485,7 +32484,7 @@ Tilemap: second: serializedVersion: 2 m_TileIndex: 17 - m_TileSpriteIndex: 17 + m_TileSpriteIndex: 20 m_TileMatrixIndex: 0 m_TileColorIndex: 0 m_TileObjectToInstantiateIndex: 65535 @@ -32975,7 +32974,7 @@ Tilemap: second: serializedVersion: 2 m_TileIndex: 8 - m_TileSpriteIndex: 8 + m_TileSpriteIndex: 17 m_TileMatrixIndex: 0 m_TileColorIndex: 0 m_TileObjectToInstantiateIndex: 65535 @@ -33182,9 +33181,8 @@ Tilemap: - m_RefCount: 5 m_Data: {fileID: 3856148304754805834, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} - - m_RefCount: 1 - m_Data: {fileID: 3819976832533034578, guid: 1621d03d32d320d49b9c24ebdfc85290, - type: 3} + - m_RefCount: 0 + m_Data: {fileID: 0} - m_RefCount: 1 m_Data: {fileID: -1736721297704704416, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} @@ -33210,7 +33208,7 @@ Tilemap: m_Data: {fileID: -2113876696273562854, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} - m_RefCount: 1 - m_Data: {fileID: -3853352564985148678, guid: 1621d03d32d320d49b9c24ebdfc85290, + m_Data: {fileID: 3819976832533034578, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} - m_RefCount: 4 m_Data: {fileID: -7767860433515643062, guid: 1621d03d32d320d49b9c24ebdfc85290, @@ -33218,8 +33216,9 @@ Tilemap: - m_RefCount: 1 m_Data: {fileID: -3195008476270375236, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} - - m_RefCount: 0 - m_Data: {fileID: 0} + - m_RefCount: 1 + m_Data: {fileID: -3853352564985148678, guid: 1621d03d32d320d49b9c24ebdfc85290, + type: 3} - m_RefCount: 2 m_Data: {fileID: 2633558140701125291, guid: 1621d03d32d320d49b9c24ebdfc85290, type: 3} diff --git a/Assets/Scripts/IGrabable.cs b/Assets/Scripts/IGrabable.cs index 5ff2cfb..7664c8d 100644 --- a/Assets/Scripts/IGrabable.cs +++ b/Assets/Scripts/IGrabable.cs @@ -4,8 +4,8 @@ using UnityEngine; public interface IGrabable { - //Unity inspector doesn't handle interface... - // int size {get; set;} //Size (1 or 2 hands) of the object + //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 ccf7684..91a65e6 100644 --- a/Assets/Scripts/Mug.cs +++ b/Assets/Scripts/Mug.cs @@ -6,7 +6,8 @@ using UnityEngine; [RequireComponent(typeof(Collider2D))] public class Mug : MonoBehaviour, IGrabable { - public int size = 1; //Size (1 or 2 hands) of the object + //Redfine attributes of IGrabable to allow display in Unity Inspector + public int size{get; set;} = 1; //Size (1 or 2 hands) of the object public bool dirty = false; public Consumable content{get; protected set;} = null; //new Consumable("beer",1,null); @@ -46,8 +47,8 @@ public class Mug : MonoBehaviour, IGrabable { if(gameObject.layer != LayerMask.NameToLayer("Interactions")) Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly"); - if(gameObject.tag != "Mug") - Debug.LogWarning(gameObject.name+" tag should be set to 'Mug' to work properly"); + if(gameObject.tag != "Grabable") + Debug.LogWarning(gameObject.name+" tag should be set to 'Grabable' to work properly"); } // Update is called once per frame diff --git a/Assets/Scripts/Tavernkeeper_controller.cs b/Assets/Scripts/Tavernkeeper_controller.cs index b946641..81f5ae2 100644 --- a/Assets/Scripts/Tavernkeeper_controller.cs +++ b/Assets/Scripts/Tavernkeeper_controller.cs @@ -21,9 +21,35 @@ public class Tavernkeeper_controller : MonoBehaviour Rigidbody2D rigidbody2d; Animator animator; + public void grab(GameObject obj, string hand) + { + //Test + if(!hand_container.ContainsKey(hand)) + { + throw new Exception("Invalid key for hands :"+hand); + } + + IGrabable grabable_obj = obj.GetComponent(); + if(grabable_obj!=null && hand_container[hand] is null && grabable_obj.size==1) //Empty hand + { + // hit_object.transform.SetParent(transform); + // hit_object.transform.localPosition = new Vector2(-0.2f,0.2f); + + grabable_obj.take(); + hand_container[hand]=obj; + } + else + { + Debug.Log(gameObject.name+" cannot grab (hand full): " + obj); + } + } + // Start is called before the first frame update void Start() { + if(gameObject.tag != "Player") + Debug.LogWarning(gameObject.name+" tag should be set to 'Player' to work properly"); + rigidbody2d = GetComponent(); animator = GetComponent(); @@ -98,21 +124,11 @@ public class Tavernkeeper_controller : MonoBehaviour if (hit_object != null) { //Handle objects interactions by tags - //TODO : Factoriser actions des IGrabable et actions des Clients/Workshop - if(hit_object.tag == "Mug") + //TODO : Factoriser actions des Clients/Workshop + //TODO : Gérer cas Grabable & Workshop + if(hit_object.tag == "Grabable") { - if(hand_container[hand] is null) //Empty hand : try grab mug - { - // hit_object.transform.SetParent(transform); - // hit_object.transform.localPosition = new Vector2(-0.2f,0.2f); - - Mug obj = hit_object.GetComponent(); - if(obj!=null && obj.size == 1) //Only take obj of size 1 hand - { - obj.take(); - hand_container[hand]=hit_object; - } - } + grab(hit_object, hand); } else if(hit_object.tag == "Client") { @@ -133,7 +149,11 @@ public class Tavernkeeper_controller : MonoBehaviour Workshop workshop = hit_object.GetComponent(); if(workshop!=null) { - if(workshop.use(hand_container[hand])) //Interactions w/ object in hands + if(hand_container[hand] is null) //No object in hands + { + workshop.use(gameObject); //Tavernkeeper interacting directly w/ workshop + } + else if(workshop.use(hand_container[hand])) //Interactions w/ object in hands { //Object taken by workshop hand_container[hand]=null; @@ -148,9 +168,9 @@ public class Tavernkeeper_controller : MonoBehaviour } else if (hand_container[hand] != null) //Hand full and no hits { - if (hand_container[hand].tag == "Mug") //Drop mug on player position + if (hand_container[hand].tag == "Grabable") //Drop obj carried on player position { - Mug obj = hand_container[hand].GetComponent(); + IGrabable obj = hand_container[hand].GetComponent(); if(obj !=null) { obj.drop(rigidbody2d.position); diff --git a/Assets/Scripts/Workshop.cs b/Assets/Scripts/Workshop.cs index 9b7e8af..7e98202 100644 --- a/Assets/Scripts/Workshop.cs +++ b/Assets/Scripts/Workshop.cs @@ -11,31 +11,43 @@ public class Workshop : MonoBehaviour public Sprite product_sprite; public float prepTime = 2.0f; //Time for preparation of product public int stock = 5; //Stock of product - // GameObject currentMug = null; //Mug currently stocked in workshop + GameObject currentMug = null; //Mug currently stocked in workshop //Handle objects interactions w/ Workshop //Return wether the object is taken from tavernkeeper - public bool use(GameObject object_used) + public bool use(GameObject userObject) { - if(object_used != null && object_used.tag=="Mug") + if(userObject != null) { - Mug mug = object_used.GetComponent(); - if (mug!= null && mug.content is null && !mug.dirty && stock>0) //Mug clean & empty + remaining stock in workshop + if(userObject.tag=="Mug") { - Debug.Log(gameObject.name+" fill "+object_used.name+ " with "+product_name); - mug.fill(new Consumable(product_name,product_value,product_sprite)); - stock--; + Mug mug = userObject.GetComponent(); + if (mug!= null && mug.content is null && !mug.dirty && stock>0) //Mug clean & empty + remaining stock in workshop + { + Debug.Log(gameObject.name+" fill "+userObject.name+ " with "+product_name); + mug.fill(new Consumable(product_name,product_value,product_sprite)); + stock--; + return false; + } + else + { + Debug.Log(userObject.name+" cannot be filled with "+product_name+ " -stock:"+stock); + return false; + } + } + else if(userObject.tag=="Player" && currentMug != null) //Give tavernkeeper currentMug + { + Tavernkeeper_controller player = userObject.GetComponent(); return false; } else { - Debug.Log(object_used.name+" cannot be filled with "+product_name+ " -stock:"+stock); return false; } } else { - Debug.Log(gameObject.name+" doesn't handle : "+object_used); + Debug.Log(gameObject.name+" doesn't handle : "+userObject); return false; } } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 04f6c45..bdf3ada 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -5,9 +5,9 @@ TagManager: serializedVersion: 2 tags: - Client - - Mug - Workshop - Cleaning_workshop + - Grabable layers: - Default - TransparentFX