|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class YeetController : InputBehaviour
- {
- [Tooltip("This is where the babies are held")]
- public GameObject handCarryingPosition;
- public GameObject parent { get; set; }
-
- public float yeetVelocity = 5f;
- public float yeetDuration = 2f;
- public float windupSpeed = 0.01f;
-
- [SerializeField]
- private Animator m_YeetAnimator;
-
- public Material trajectoryLineMaterial;
-
- public enum YeetState { Unheld, Held, Preyeet, Yeeting };
- public YeetState yeetState { get; private set; } = YeetState.Unheld;
-
- private GameObject _child;
- private float m_time;
-
- private GameObject m_child;
- private GameObject m_body;
-
- private float m_velocityWindup;
-
- private LineRenderer m_lineRenderer;
-
- private Vector3 preyeetPosition;
-
- private void Awake()
- {
- m_body = this.gameObject;
- }
-
- public void Hold(GameObject child)
- {
- _child = child;
- _child.transform.parent = parent.transform;
- _child.GetComponent<YeetHandle>().held = true;
- _child.GetComponent<YeetHandle>().lastHeld = parent;
-
- yeetState = YeetState.Held;
-
- m_YeetAnimator.SetBool("Carry", true);
- m_YeetAnimator.SetBool("Yeet", true);
- }
-
- public void Preyeet()
- {
- preyeetPosition = parent.transform.position;
- m_lineRenderer = gameObject.AddComponent<LineRenderer>();
- m_lineRenderer.material = trajectoryLineMaterial;
-
- yeetState = YeetState.Preyeet;
- }
-
- public void Yeet()
- {
- m_YeetAnimator.SetBool("Yeet", false);
- m_YeetAnimator.SetBool("Carry", false);
-
- _child.transform.parent = null;
- _child.transform.rotation = parent.transform.rotation;
- _child.GetComponent<YeetHandle>().held = false;
-
- _child.GetComponent<Rigidbody>().velocity = calculateVelocityVector();
- yeetState = YeetState.Yeeting;
- m_time = yeetDuration;
- m_velocityWindup = 0;
-
- Destroy(m_lineRenderer);
- }
-
- void Start()
- {
-
- }
-
- void Update()
- {
- switch(yeetState)
- {
- case YeetState.Yeeting:
- m_time -= Time.deltaTime;
-
- if(m_time <= 0f)
- {
- Debug.Log("YeetController.Update: Yeet finished");
- yeetState = YeetState.Unheld;
- }
- break;
- case YeetState.Preyeet:
- //TODO: Stop player
- parent.transform.position = preyeetPosition;
-
- _child.transform.position = handCarryingPosition.transform.position;
-
- if (m_velocityWindup < yeetVelocity)
- {
- m_velocityWindup += windupSpeed;
- }
-
- Vector3 velocity = calculateVelocityVector();
-
- float t;
- t = (-1f * velocity.y) / Physics.gravity.y;
- t = 2f * t;
-
- m_lineRenderer.positionCount = 10;
- Vector3 trajectoryPoint;
-
- for (int i = 0; i < m_lineRenderer.positionCount; i++)
- {
- float time = t * i / (float)(m_lineRenderer.positionCount);
- trajectoryPoint = parent.transform.position + velocity * time + 0.5f * Physics.gravity * time * time;
- m_lineRenderer.SetPosition(i, trajectoryPoint);
- }
- break;
- case YeetState.Held:
- //TODO: slow player
-
- // Stop child from wriggling
- _child.transform.position = handCarryingPosition.transform.position;
- break;
- case YeetState.Unheld:
- _child = null;
- break;
- }
- }
-
- private Vector3 calculateVelocityVector()
- {
- return parent.transform.forward * m_velocityWindup + parent.transform.up * m_velocityWindup;
- }
-
- private void OnTriggerEnter(Collider collider)
- {
- Debug.Log("PlayerInputController.OnTriggerEnter: arrived");
- m_child = collider.gameObject;
- }
-
- private void OnTriggerExit(Collider collider)
- {
- m_child = null;
- }
-
- private void OnYeet(InputAction.CallbackContext args)
- {
- parent = m_body;
- switch (yeetState)
- {
- case YeetController.YeetState.Unheld:
- if(m_child)
- Hold(m_child);
- // Grab nearest baby
- break;
- case YeetState.Preyeet:
- Yeet();
- break;
- case YeetController.YeetState.Held:
- // Yeet baby
- Preyeet();
- break;
- case YeetController.YeetState.Yeeting:
- // Cooldown?
- break;
- }
- }
-
- public override void RegisterInput(PlayerInput playerInput)
- {
- playerInput.currentActionMap.FindAction("Yeet").performed += OnYeet;
- }
-
- public override void UnregisterInput(PlayerInput playerInput)
- {
- playerInput.currentActionMap.FindAction("Yeet").performed -= OnYeet;
- }
- }
|