Factorisation des interactions IGrabable

This commit is contained in:
Antoine H 2020-12-09 15:41:56 +01:00
parent c254d73103
commit eacf4f2711
7 changed files with 76 additions and 44 deletions

View file

@ -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

View file

@ -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}

View file

@ -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

View file

@ -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

View file

@ -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<IGrabable>();
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<Rigidbody2D>();
animator = GetComponent<Animator>();
@ -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<Mug>();
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<Workshop>();
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<Mug>();
IGrabable obj = hand_container[hand].GetComponent<IGrabable>();
if(obj !=null)
{
obj.drop(rigidbody2d.position);

View file

@ -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<Mug>();
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<Mug>();
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<Tavernkeeper_controller>();
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;
}
}

View file

@ -5,9 +5,9 @@ TagManager:
serializedVersion: 2
tags:
- Client
- Mug
- Workshop
- Cleaning_workshop
- Grabable
layers:
- Default
- TransparentFX