ProjetPatate/Assets/Scripts/Tavernkeeper_controller.cs

219 lines
7.2 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
public class Tavernkeeper_controller : MonoBehaviour
{
public float mvt_speed = 5.0f; //Movement speed
public float action_dist = 1.5f; //Action distance
2020-12-10 11:57:23 +01:00
public float action_cd = 0.5f; //Action cooldown
2020-12-02 12:31:12 +01:00
2021-01-19 10:46:47 +01:00
Dictionary<string, GameObject> hand_container; //Objects (IGrabable) in hand
2020-12-10 11:57:23 +01:00
float actionTimer;
bool isInteracting;
2020-12-02 12:31:12 +01:00
// Last user inputs
float horizontal;
float vertical;
float hands;
2020-12-10 14:38:20 +01:00
string currentHand;
Vector2 lookDirection = new Vector2(1,0);
2020-12-02 12:31:12 +01:00
Rigidbody2D rigidbody2d;
Animator animator;
2021-01-17 17:56:17 +01:00
//Grab an Object (IGrabable) w/ a specific or w/ last hand used (hand=null).
2020-12-10 14:38:20 +01:00
public void grab(GameObject obj, string hand=null)
{
2020-12-10 14:38:20 +01:00
//Default hand
if(hand is null)
hand = currentHand;
//Test
if(!hand_container.ContainsKey(hand))
{
2021-01-12 16:55:43 +01:00
Debug.LogError("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);
2021-01-19 10:46:47 +01:00
Transform mugSlot = gameObject.transform.Find(hand+"MugSlot");
grabable_obj.take(mugSlot);
hand_container[hand]=obj;
}
else
{
Debug.Log(gameObject.name+" cannot grab (hand full): " + obj);
}
}
2021-01-17 17:56:17 +01:00
//Drop every objects in hands at tavernkeeper position.
public void emptyHands()
{
foreach(string hand in hand_container.Keys)
{
if(hand_container[hand]!=null)
{
IGrabable grabable_obj = hand_container[hand].GetComponent<IGrabable>();
2021-01-19 10:46:47 +01:00
grabable_obj.drop(transform);
2021-01-17 17:56:17 +01:00
}
}
hand_container["left"]=null;
hand_container["right"]=null;
}
// 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>();
2020-12-02 12:31:12 +01:00
hand_container = new Dictionary<string, GameObject>(){
{"left", null},
{"right", null}
};
// GameSystem.Instance.startService();
}
// Update is called once per frame
void Update()
{
//Read inputs
horizontal = Input.GetAxis("Horizontal"); //See Edit/Project setting / Input Manager
vertical = Input.GetAxis("Vertical");
hands = Input.GetAxis("Hand");
//Movement action
movement(new Vector2(horizontal, vertical));
2020-12-10 11:57:23 +01:00
//Actions delay
if(isInteracting)
{
actionTimer -= Time.deltaTime;
if (actionTimer < 0)
isInteracting = false;
}
//Hands actions
2020-12-10 11:57:23 +01:00
if(!isInteracting && !Mathf.Approximately(hands, 0.0f))
{
2020-12-10 11:57:23 +01:00
actionTimer= action_cd;
isInteracting=true;
if(hands>0)
2020-12-10 14:38:20 +01:00
currentHand="left";
else
2020-12-10 14:38:20 +01:00
currentHand="right";
handAction(currentHand);
}
}
// Update used by the Physics engine
void FixedUpdate()
{
//Movement of a physic object
Vector2 position = rigidbody2d.position;
position.x = position.x + mvt_speed * horizontal * Time.deltaTime;
position.y = position.y + mvt_speed * vertical * Time.deltaTime;
rigidbody2d.MovePosition(position); //Movement processed by the phyisc engine for Collision, etc.
}
2020-12-02 12:31:12 +01:00
//Movement
void movement(Vector2 move)
{
//Update animation direction
if(move != Vector2.zero)//Movement requested ? //!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)
{
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-05 15:22:00 +01:00
//Handle action with hands ("left" or "right")
2020-12-02 12:31:12 +01:00
void handAction(string hand)
{
2020-12-05 15:22:00 +01:00
//Test
if(!hand_container.ContainsKey(hand))
{
2021-01-12 16:55:43 +01:00
Debug.LogError("Invalid key for hands :"+hand);
2020-12-05 15:22:00 +01:00
}
// Test collision of ray from tavernkeeper center (A verifier) at action_dist unit distance on Interactions layer
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, action_dist, LayerMask.GetMask("Interactions"));
if (hit.collider != null)
2020-12-02 12:31:12 +01:00
{
GameObject hit_object = hit.collider.gameObject;
// Debug.Log("Raycast has hit the object " + hit_object.name+ hit_object.tag);
if (hit_object != null)
2020-12-02 12:31:12 +01:00
{
2020-12-05 15:22:00 +01:00
//Handle objects interactions by tags
//TODO : Factoriser actions des Clients/Workshop
//TODO : Gérer cas Grabable & Workshop
if(hit_object.tag == "Grabable")
2020-12-02 12:31:12 +01:00
{
grab(hit_object, hand);
}
2020-12-10 11:57:23 +01:00
else if(hit_object.tag == "Usable")
2020-12-05 13:38:13 +01:00
{
2020-12-10 11:57:23 +01:00
IUsable usable = hit_object.GetComponent<IUsable>();
if(usable!=null)
2020-12-05 13:38:13 +01:00
{
if(hand_container[hand] is null) //No object in hands
{
// Debug.Log(gameObject.name+" use "+hit_object.name);
2020-12-10 11:57:23 +01:00
usable.use(gameObject); //Tavernkeeper interacting directly w/ usable
}
2020-12-10 11:57:23 +01:00
else if(usable.use(hand_container[hand])) //Interactions w/ object in hands
2020-12-05 13:38:13 +01:00
{
2020-12-10 11:57:23 +01:00
//Object taken by usable
// Debug.Log("Give "+ hand_container[hand].name+" to "+hit_object.name);
2020-12-05 13:38:13 +01:00
hand_container[hand]=null;
}
}
}
2020-12-05 15:22:00 +01:00
else
{
Debug.Log(hit_object.tag+" tag not handled by "+gameObject.name);
}
}
2020-12-02 12:31:12 +01:00
}
else if (hand_container[hand] != null) //Hand full and no hits : use object
2020-12-02 12:31:12 +01:00
{
IGrabable obj = hand_container[hand].GetComponent<IGrabable>();
if(obj !=null)
{
if(obj.use(gameObject)) //Use object in hand
hand_container[hand]=null; //True = object dropped
2020-12-04 11:15:18 +01:00
}
2020-12-02 12:31:12 +01:00
}
}
2020-12-04 11:15:18 +01:00
//Returns set of free hands (keys)
// ISet<string> freeHands()
// {
// HashSet<string> res = new HashSet<string>();
// foreach ( KeyValuePair<string, GameObject> kvp in hand_container)
// {
// if(kvp.Value is null)
// {
// res.Add(kvp.Key);
// }
// }
// return res;
// }
}