gold_sprint_lpr/Assets/Scripts/HighscoreLettersManager.cs
2018-03-13 16:51:53 +01:00

74 lines
2.1 KiB
C#

using UnityEngine;
using System.Collections;
using System;
using System.Linq;
using UnityEngine.UI;
public class HighscoreLettersManager : MonoBehaviour
{
private DateTime lastInput;
public int InputDelay = 150;
public GameObject[] Letters;
public Text ResultText;
public string XAxis;
public int rank;
public TimeSpan RunTime = new TimeSpan(0,0,1,30,500);
private int _selectionIndex = 0;
private bool selectionChanging;
bool IsSpawned
{
get { return lastInput == null || (DateTime.Now - lastInput).Milliseconds < InputDelay; }
}
// Use this for initialization
void Start()
{
if (Letters != null && Letters.Any())
Letters[0].GetComponent<HighscoreLetterManager>().IsActive = true;
}
// Update is called once per frame
void Update()
{
if (ResultText != null)
ResultText.text = UpdateText();
if (!IsSpawned || selectionChanging)
{
if (!string.IsNullOrEmpty(XAxis))
{
var axis = Input.GetAxis(XAxis);
if (Math.Abs(axis) == 1)
ChangeSelection(axis == 1);
}
}
}
private void ChangeSelection(bool toRight)
{
selectionChanging = true;
lastInput = DateTime.Now;
var oldSelection = _selectionIndex;
if (toRight) _selectionIndex++;
else _selectionIndex--;
if (_selectionIndex < 0) _selectionIndex = Letters.Length - 1;
else if (_selectionIndex >= Letters.Length) _selectionIndex = 0;
Letters[oldSelection].GetComponent<HighscoreLetterManager>().IsActive = false;
Letters[_selectionIndex].GetComponent<HighscoreLetterManager>().IsActive = true;
selectionChanging = false;
}
private string UpdateText()
{
var pseudo = string.Empty;
foreach (var letter in Letters)
pseudo += letter.GetComponentInChildren<Text>().text;
return string.Format("{0}/ - \t{1}\t - {2}:{3}:{4}", rank, pseudo, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds);
}
}