Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

131 lines
3.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YeetController : MonoBehaviour
{
public GameObject parent { get; set; }
public float yeetVelocity = 5f;
public float yeetDuration = 2f;
public float windupSpeed = 0.01f;
public enum YeetState { Unheld, Held, 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 void Awake()
{
m_body = this.gameObject;
}
public void Hold(GameObject child)
{
_child = child;
_child.transform.parent = parent.transform;
yeetState = YeetState.Held;
m_lineRenderer = gameObject.AddComponent<LineRenderer>();
}
public void Yeet()
{
_child.transform.parent = null;
_child.transform.rotation = parent.transform.rotation;
_child.GetComponent<Rigidbody>().velocity = _child.transform.forward * m_velocityWindup + _child.transform.up * m_velocityWindup;
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.Held:
// draw yeet lines
if(m_velocityWindup < yeetVelocity)
{
m_velocityWindup += windupSpeed;
}
Vector3 velocity = parent.transform.forward * m_velocityWindup + parent.transform.up * m_velocityWindup;
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.Unheld:
_child = null;
break;
}
}
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()
{
parent = m_body;
switch (yeetState)
{
case YeetController.YeetState.Unheld:
if(m_child)
Hold(m_child);
// Grab nearest baby
break;
case YeetController.YeetState.Held:
Yeet();
// Yeet baby
break;
case YeetController.YeetState.Yeeting:
// Cooldown?
break;
}
}
}