2020-12-01 17:08:40 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class Tavernkeeper_controller : MonoBehaviour
|
|
|
|
|
{
|
2020-12-02 10:40:27 +01:00
|
|
|
|
public float mvt_speed = 5.0f; //Movement speed
|
2020-12-02 17:57:44 +01:00
|
|
|
|
public float action_dist = 1.5f; //Action distance
|
2020-12-02 12:31:12 +01:00
|
|
|
|
|
|
|
|
|
IDictionary<string, GameObject> hand_container;
|
|
|
|
|
|
2020-12-01 17:08:40 +01:00
|
|
|
|
// Last user inputs
|
|
|
|
|
float horizontal;
|
|
|
|
|
float vertical;
|
2020-12-02 10:40:27 +01:00
|
|
|
|
float hands;
|
2020-12-01 17:08:40 +01:00
|
|
|
|
|
|
|
|
|
Vector2 lookDirection = new Vector2(1,0);
|
2020-12-02 12:31:12 +01:00
|
|
|
|
Rigidbody2D rigidbody2d;
|
|
|
|
|
Animator animator;
|
2020-12-01 17:08:40 +01:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
rigidbody2d = GetComponent<Rigidbody2D>();
|
|
|
|
|
animator = GetComponent<Animator>();
|
2020-12-02 12:31:12 +01:00
|
|
|
|
|
|
|
|
|
hand_container = new Dictionary<string, GameObject>(){
|
|
|
|
|
{"left", null},
|
|
|
|
|
{"right", null}
|
|
|
|
|
};
|
2020-12-01 17:08:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
//Read inputs
|
|
|
|
|
horizontal = Input.GetAxis("Horizontal"); //See Edit/Project setting / Input Manager
|
|
|
|
|
vertical = Input.GetAxis("Vertical");
|
|
|
|
|
// Debug.Log(horizontal);
|
|
|
|
|
|
|
|
|
|
Vector2 move = new Vector2(horizontal, vertical);
|
|
|
|
|
|
|
|
|
|
//Update animation direction
|
|
|
|
|
if(!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)) //Movement requested ?
|
|
|
|
|
{
|
|
|
|
|
lookDirection.Set(move.x, move.y); //== lookDirection=move
|
|
|
|
|
lookDirection.Normalize();
|
|
|
|
|
}
|
|
|
|
|
animator.SetFloat("Look X", lookDirection.x);
|
|
|
|
|
animator.SetFloat("Look Y", lookDirection.y);
|
|
|
|
|
animator.SetFloat("Speed", move.magnitude);
|
2020-12-02 10:40:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hands = Input.GetAxis("Hand");
|
|
|
|
|
// Debug.Log(hands);
|
|
|
|
|
if(!Mathf.Approximately(hands, 0.0f))
|
|
|
|
|
{
|
|
|
|
|
if(hands>0)
|
|
|
|
|
{
|
2020-12-02 12:31:12 +01:00
|
|
|
|
// Debug.Log("Left hand");
|
|
|
|
|
handAction("left");
|
2020-12-02 10:40:27 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-02 12:31:12 +01:00
|
|
|
|
// Debug.Log("Right hand");
|
|
|
|
|
handAction("right");
|
2020-12-02 10:40:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 17:08:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update used by the Physics engine
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
//Movement of a physic object
|
|
|
|
|
Vector2 position = rigidbody2d.position;
|
2020-12-02 10:40:27 +01:00
|
|
|
|
position.x = position.x + mvt_speed * horizontal * Time.deltaTime;
|
|
|
|
|
position.y = position.y + mvt_speed * vertical * Time.deltaTime;
|
2020-12-01 17:08:40 +01:00
|
|
|
|
|
|
|
|
|
rigidbody2d.MovePosition(position); //Movement processed by the phyisc engine for Collision, etc.
|
|
|
|
|
}
|
2020-12-02 12:31:12 +01:00
|
|
|
|
|
|
|
|
|
void handAction(string hand)
|
|
|
|
|
{
|
2020-12-02 17:57:44 +01:00
|
|
|
|
// Test collision of ray from tavernkeeper center (A verifier) at action_dist unit distance
|
|
|
|
|
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, action_dist);
|
|
|
|
|
if (hit.collider != null)
|
2020-12-02 12:31:12 +01:00
|
|
|
|
{
|
2020-12-02 17:57:44 +01:00
|
|
|
|
GameObject hit_object = hit.collider.gameObject;
|
|
|
|
|
// Debug.Log("Raycast has hit the object " + hit_object+ hit_object.tag);
|
|
|
|
|
if (hit_object != null)
|
2020-12-02 12:31:12 +01:00
|
|
|
|
{
|
2020-12-02 17:57:44 +01:00
|
|
|
|
//Empty hand : try grab grabable object
|
|
|
|
|
if(hand_container[hand] is null)
|
2020-12-02 12:31:12 +01:00
|
|
|
|
{
|
2020-12-02 17:57:44 +01:00
|
|
|
|
if(hit_object.tag == "Grabable") //by tag or with parent-name ?
|
|
|
|
|
{
|
|
|
|
|
// hit_object.transform.SetParent(transform);
|
|
|
|
|
// hit_object.transform.localPosition = new Vector2(-0.2f,0.2f);
|
|
|
|
|
hit_object.SetActive(false);
|
|
|
|
|
hand_container[hand]=hit_object;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Full hand : try give to client
|
|
|
|
|
else if(hit_object.tag == "Client") //by tag or with parent-name ?
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Give "+ hand_container[hand]+" to "+hit_object);
|
|
|
|
|
Destroy(hand_container[hand]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-02 12:31:12 +01:00
|
|
|
|
}
|
2020-12-02 17:57:44 +01:00
|
|
|
|
//Full hand : drop
|
|
|
|
|
else if (hand_container[hand] != null)
|
2020-12-02 12:31:12 +01:00
|
|
|
|
{
|
|
|
|
|
// Debug.Log("Hand full with "+ hand_container[hand]);
|
2020-12-02 17:14:17 +01:00
|
|
|
|
// hand_container[hand].transform.SetParent(null);
|
|
|
|
|
hand_container[hand].SetActive(true);
|
2020-12-02 12:31:12 +01:00
|
|
|
|
hand_container[hand].transform.position = rigidbody2d.position;
|
|
|
|
|
hand_container[hand]=null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 17:08:40 +01:00
|
|
|
|
}
|