Factorisation des interactions IGrabable
This commit is contained in:
parent
c254d73103
commit
eacf4f2711
7 changed files with 76 additions and 44 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue