180 lines
5.3 KiB
C#
180 lines
5.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Rendering.PostProcessing;
|
|
|
|
public class PlayerController : OrbitalPositionBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private MeshRenderer m_Renderer;
|
|
[SerializeField] private Text m_ScoreText;
|
|
[SerializeField] private Material m_DefaultMaterial;
|
|
[SerializeField] private Material m_FreeHitMaterial;
|
|
[SerializeField] private PostProcessVolume m_PostProcessVolume;
|
|
|
|
private static PlayerController Instance;
|
|
public static bool IsPlayerAttached => Instance.m_OrbitalPosition.m_IsAttachedToRings;
|
|
|
|
private static int s_HighScore = 20;
|
|
public static int s_PlayerScore;
|
|
|
|
private Vector3 m_SuicidePoint;
|
|
private float m_DeathLerp;
|
|
|
|
private bool m_HasFreeHitActive;
|
|
private bool m_HasInvincibility;
|
|
private bool m_KillingItself;
|
|
|
|
protected override void OnStart()
|
|
{
|
|
Instance = this;
|
|
s_HighScore = Mathf.Max(s_HighScore, PlayerPrefs.GetInt("HighScore", 0));
|
|
Debug.Log($"Loaded high score of [{s_HighScore}]");
|
|
|
|
GlobalOrbitalPositionManager.SetPlayer(m_OrbitalPosition);
|
|
|
|
m_OrbitalPosition.m_ObjectRadius = 0.1f;
|
|
m_OrbitalPosition.m_SpinSpeed = 0.2f;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
m_ScoreText.text = s_PlayerScore.ToString();
|
|
|
|
m_ScoreText.color = s_PlayerScore > s_HighScore ? Color.yellow : Color.white;
|
|
m_Renderer.material = m_HasFreeHitActive ? m_FreeHitMaterial : m_DefaultMaterial;
|
|
|
|
if (GlobalInput.IsScreenClicked && GlobalOrbitalPositionManager.AllowPlayerInput)
|
|
{
|
|
m_OrbitalPosition.m_AttachedRing += 1;
|
|
}
|
|
|
|
if (!m_KillingItself)
|
|
return;
|
|
|
|
transform.position = Vector3.Lerp(m_SuicidePoint, m_SuicidePoint.normalized * 20f, m_DeathLerp);
|
|
m_DeathLerp += Time.deltaTime;
|
|
}
|
|
|
|
public static void AttachPlayer()
|
|
{
|
|
Instance.m_OrbitalPosition.m_IsAttachedToRings = true;
|
|
}
|
|
|
|
private IEnumerator OnGrowPlayerModifier()
|
|
{
|
|
transform.localScale *= 1.5f;
|
|
m_OrbitalPosition.m_ObjectRadius *= 1.5f;
|
|
|
|
yield return new WaitForSecondsRealtime(15f);
|
|
|
|
transform.localScale /= 1.5f;
|
|
m_OrbitalPosition.m_ObjectRadius /= 1.5f;
|
|
}
|
|
|
|
private IEnumerator OnShrinkPlayerModifier()
|
|
{
|
|
transform.localScale *= 0.6f;
|
|
m_OrbitalPosition.m_ObjectRadius *= 0.6f;
|
|
|
|
yield return new WaitForSecondsRealtime(15f);
|
|
|
|
transform.localScale /= 0.6f;
|
|
m_OrbitalPosition.m_ObjectRadius /= 0.6f;
|
|
}
|
|
|
|
private IEnumerator TriggerInvincibility()
|
|
{
|
|
m_HasFreeHitActive = false;
|
|
m_HasInvincibility = true;
|
|
|
|
yield return new WaitForSecondsRealtime(1f);
|
|
|
|
m_HasInvincibility = false;
|
|
}
|
|
|
|
public override void OnCollision(OrbitalPositionBehaviour other)
|
|
{
|
|
if (!m_OrbitalPosition.m_IsAttachedToRings)
|
|
return;
|
|
|
|
if (other.CompareTag("Enemy"))
|
|
{
|
|
if (m_HasInvincibility)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (m_HasFreeHitActive)
|
|
{
|
|
StartCoroutine(TriggerInvincibility());
|
|
return;
|
|
}
|
|
|
|
GlobalOrbitalPositionManager.RestartSimulation();
|
|
m_SuicidePoint = transform.position;
|
|
m_KillingItself = true;
|
|
m_DeathLerp = 0f;
|
|
}
|
|
|
|
else if (other.CompareTag("PlayerMod"))
|
|
{
|
|
switch (((PlayerModifier)other).Modifier)
|
|
{
|
|
case PlayerModifier.Modifiers.GainPoints:
|
|
s_PlayerScore += 100;
|
|
break;
|
|
|
|
case PlayerModifier.Modifiers.FreeHit:
|
|
m_HasFreeHitActive = true;
|
|
break;
|
|
|
|
case PlayerModifier.Modifiers.GrowPlayer:
|
|
StartCoroutine(OnGrowPlayerModifier());
|
|
break;
|
|
|
|
case PlayerModifier.Modifiers.ShrinkPlayer:
|
|
StartCoroutine(OnShrinkPlayerModifier());
|
|
break;
|
|
|
|
case PlayerModifier.Modifiers.SpeedUp:
|
|
StartCoroutine(GlobalOrbitalPositionManager.Instance.StartPlayerSpeedupModifier());
|
|
break;
|
|
|
|
case PlayerModifier.Modifiers.ClearAllEnemies:
|
|
EnemyController.KillAllEnemies();
|
|
break;
|
|
|
|
default:
|
|
Debug.Log($"Collision with unknown modifier occured [{((PlayerModifier)other).Modifier}]");
|
|
break;
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
Debug.Log("Unknown collision occured");
|
|
}
|
|
}
|
|
|
|
public override void OnSimulationRestart()
|
|
{
|
|
m_KillingItself = false;
|
|
m_Renderer.enabled = true;
|
|
|
|
m_HasFreeHitActive = false;
|
|
|
|
m_Renderer.material.color = Color.green;
|
|
|
|
s_HighScore = Math.Max(s_HighScore, s_PlayerScore);
|
|
PlayerPrefs.SetInt("HighScore", s_HighScore);
|
|
s_PlayerScore = 0;
|
|
}
|
|
|
|
public override void OnReachCentre()
|
|
{
|
|
GlobalOrbitalPositionManager.RestartSimulation();
|
|
}
|
|
}
|