Files
U10-MobileGame/Assets/Scripts/PlayerController.cs
2025-11-27 11:21:14 +00:00

95 lines
2.7 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : OrbitalPositionBehaviour
{
public static int s_PlayerScore;
private static int s_HighScore = 20;
private static PlayerController s_Instance;
public static bool IsPlayerAttached => s_Instance.m_OrbitalPosition.m_IsAttachedToRings;
public static void AttachPlayer() { s_Instance.m_OrbitalPosition.m_IsAttachedToRings = true; }
private Vector3 m_SuicidePoint;
private bool m_KillingItself;
private float m_DeathLerp;
public MeshRenderer m_Renderer;
public Text m_ScoreText;
protected override void OnStart()
{
s_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();
if (s_PlayerScore > s_HighScore)
m_ScoreText.color = Color.yellow;
else
m_ScoreText.color = Color.white;
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 override void OnCollision(OrbitalPositionBehaviour other)
{
if (!m_OrbitalPosition.m_IsAttachedToRings)
return;
if (other.CompareTag("Enemy"))
{
GlobalOrbitalPositionManager.RestartSimulation();
m_SuicidePoint = transform.position;
m_KillingItself = true;
m_DeathLerp = 0f;
}
else if (other.CompareTag("PlayerMod"))
{
other.OnCollision(this);
Debug.Log("Collided with player mod");
}
else
{
Debug.Log("Unknown collision occured");
}
}
public override void OnSimulationRestart()
{
m_KillingItself = false;
m_Renderer.enabled = true;
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();
}
}