Add Client_controller and mug interaction

This commit is contained in:
Antoine H 2020-12-04 13:12:01 +01:00
parent 70024c7b6a
commit 80704e1ba3
6 changed files with 205 additions and 24 deletions

View file

@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Collider2D))]
public class Client_controller : MonoBehaviour
{
public float consumeTime = 3.0f; //Time to consume currentMug
public float waitingTime = 10.0f; //Patience after ordering
GameObject currentMug;
public bool use(GameObject new_mug)
{
if(new_mug != null && new_mug.tag=="Mug")
{
Debug.Log(gameObject.name+" take "+new_mug.name);
currentMug = new_mug;
return true; //Object taken from tavernkeeper
}
else
{
Debug.Log("Display order (or something else) of "+gameObject.name);
return false; //Object not taken from tavernkeeper
}
}
// Start is called before the first frame update
void Start()
{
//Needs to be on Interactions layer and have the Client tag to work properly
gameObject.tag = "Client"; //Force gameobject tag
}
// Update is called once per frame
void Update()
{
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2dfbc8fb0a162ab4da4ff5cb76650e03
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -2,15 +2,18 @@
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Collider2D))]
public class Mug : MonoBehaviour, IGrabable
{
public int size = 1; //Size (1 or 2 hands) of the object
bool dirty = false;
int content = 1;
public void use()
{
//Do nothing
}
public void take()
public void take() //Object taken
{
gameObject.SetActive(false);
}
@ -20,10 +23,20 @@ public class Mug : MonoBehaviour, IGrabable
gameObject.transform.position = position;
}
public void fill(int new_content)
{
content = new_content;
}
public void consume()
{
content=0;
}
// Start is called before the first frame update
void Start()
{
//Needs to be on Interactions layer and have the Mug tag to work properly
gameObject.tag = "Mug"; //Force gameobject tag
}
// Update is called once per frame

View file

@ -2,6 +2,8 @@
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))]
public class Tavernkeeper_controller : MonoBehaviour
{
public float mvt_speed = 5.0f; //Movement speed
@ -36,10 +38,10 @@ public class Tavernkeeper_controller : MonoBehaviour
//Read inputs
horizontal = Input.GetAxis("Horizontal"); //See Edit/Project setting / Input Manager
vertical = Input.GetAxis("Vertical");
// Debug.Log(horizontal);
hands = Input.GetAxis("Hand");
//Movement action
Vector2 move = new Vector2(horizontal, vertical);
//Update animation direction
if(!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)) //Movement requested ?
{
@ -50,9 +52,7 @@ public class Tavernkeeper_controller : MonoBehaviour
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
hands = Input.GetAxis("Hand");
// Debug.Log(hands);
//Hands actions
if(!Mathf.Approximately(hands, 0.0f))
{
if(hands>0)
@ -79,6 +79,7 @@ public class Tavernkeeper_controller : MonoBehaviour
rigidbody2d.MovePosition(position); //Movement processed by the phyisc engine for Collision, etc.
}
//Handle action with hands
void handAction(string hand)
{
// Test collision of ray from tavernkeeper center (A verifier) at action_dist unit distance on Interactions layer
@ -86,32 +87,37 @@ public class Tavernkeeper_controller : MonoBehaviour
if (hit.collider != null)
{
GameObject hit_object = hit.collider.gameObject;
// Debug.Log("Raycast has hit the object " + hit_object+ hit_object.tag);
// Debug.Log("Raycast has hit the object " + hit_object.name+ hit_object.tag);
if (hit_object != null)
{
//Empty hand : try grab grabable object
if(hand_container[hand] is null)
if(hit_object.tag == "Mug")
{
if(hit_object.tag == "Grabable") //by tag, layer-mask or with parent-name ?
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);
//Doit traiter différement chaque Grabable ...
Mug obj = hit_object.GetComponent<Mug>();
// if (obj.size==1 || (obj.size==2 && ));
if(obj!=null)
if(obj!=null && obj.size == 1) //Only take obj of size 1 hand
{
obj.take();
hand_container[hand]=hit_object;
}
}
}
//Full hand : try give to client
else if(hit_object.tag == "Client") //by tag or with parent-name ?
else if(hit_object.tag == "Client")
{
Debug.Log("Give "+ hand_container[hand]+" to "+hit_object);
Destroy(hand_container[hand]);
// 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
// Destroy(hand_container[hand]);
hand_container[hand]=null;
}
}
}
}
}
@ -121,12 +127,18 @@ public class Tavernkeeper_controller : MonoBehaviour
// Debug.Log("Hand full with "+ hand_container[hand]);
// hand_container[hand].transform.SetParent(null);
//Doit traiter différement chaque Grabable ...
Mug obj = hand_container[hand].GetComponent<Mug>();
if(obj !=null)
if (hand_container[hand].tag == "Mug")
{
obj.drop(rigidbody2d.position);
hand_container[hand]=null;
Mug obj = hand_container[hand].GetComponent<Mug>();
if(obj !=null)
{
obj.drop(rigidbody2d.position);
hand_container[hand]=null;
}
}
else
{
Debug.Log(gameObject+" doesn't handle Hand full with "+ hand_container[hand]);
}
}
}