|
|
- 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 AudioClip[] yeetClips;
- public AudioClip[] childClips;
-
- 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 AudioSource m_audioSource;
- private Transform m_OldChildParent;
-
- private Vector3 preyeetPosition;
-
- public PlayerInputController playerController;
-
-
- private void Awake()
- {
- m_body = this.gameObject;
- m_audioSource = gameObject.AddComponent<AudioSource>();
- m_audioSource.spatialBlend = 0f;
- }
-
- public void Hold(GameObject child)
- {
- m_audioSource.clip = childClips[Random.Range(0, childClips.Length)];
-
- _child = child;
- m_OldChildParent = _child.transform.parent;
- _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);
-
- m_audioSource.Play();
-
-
- playerController.m_playerSpeed /= 3;
- }
-
- public void Preyeet()
- {
- preyeetPosition = parent.transform.position;
- m_lineRenderer = gameObject.AddComponent<LineRenderer>();
- m_lineRenderer.material = trajectoryLineMaterial;
-
- yeetState = YeetState.Preyeet;
-
- }
-
- public void Yeet()
- {
- m_audioSource.clip = yeetClips[Random.Range(0, yeetClips.Length)];
-
- m_YeetAnimator.SetBool("Yeet", false);
- m_YeetAnimator.SetBool("Carry", false);
-
- _child.transform.parent = m_OldChildParent;
- _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);
- m_audioSource.Play();
-
- playerController.m_playerSpeed *= 3;
- }
-
- 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)
- {
- m_child = collider.gameObject;
- }
-
- private void OnTriggerExit(Collider collider)
- {
- m_child = null;
- }
-
- private void OnYeet(InputAction.CallbackContext args)
- {
-
- Debug.Log($"Player Yeeting: {args.phase}");
-
- parent = m_body;
- switch (yeetState)
- {
- case YeetController.YeetState.Unheld:
- if (m_child/* && args.phase == InputActionPhase.Performed*/)
- Hold(m_child);
- // Grab nearest baby
- break;
- case YeetState.Preyeet:
- /*if (args.phase == InputActionPhase.Performed)*/
- Yeet();
- break;
- case YeetController.YeetState.Held:
- if (args.phase == InputActionPhase.Started)
- Preyeet();
- // Yeet baby
-
- break;
- case YeetController.YeetState.Yeeting:
- // Cooldown?
- break;
- }
- }
-
- public override void RegisterInput(PlayerInput playerInput)
- {
- playerInput.currentActionMap.FindAction("Yeet").canceled += OnYeet;
- playerInput.currentActionMap.FindAction("Yeet").started += OnYeet;
- }
-
- public override void UnregisterInput(PlayerInput playerInput)
- {
- playerInput.currentActionMap.FindAction("Yeet").canceled -= OnYeet;
- playerInput.currentActionMap.FindAction("Yeet").started -= OnYeet;
- }
- }
|