Skip to content

Instantly share code, notes, and snippets.

@Butter1484
Created May 13, 2017 21:04
Show Gist options
  • Save Butter1484/ac095ca8e3ef9f19bfc311b8342a5060 to your computer and use it in GitHub Desktop.
Save Butter1484/ac095ca8e3ef9f19bfc311b8342a5060 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour {
private Text[] rollTexts = new Text[21];
private Text [] frameTexts = new Text[10];
// Use this for initialization
void Start () {
RollText[] rollTextObjects = GetComponentsInChildren<RollText>();
FrameText[] frameTextObjects = GetComponentsInChildren<FrameText>();
int i = 0;
foreach (RollText obj in rollTextObjects)
{
rollTexts[i] = obj.GetComponent<Text>();
rollTexts[i].text = "";
i++;
}
int j = 0;
foreach(FrameText obj in frameTextObjects)
{
frameTexts[j] = obj.GetComponent<Text>();
frameTexts[j].text = "";
j++;
}
}
public void FillRolls(List<int> rolls)
{
string rollsString = FormatRolls(rolls);
for(int i = 0; i < rollsString.Length; i++)
{
rollTexts[i].text = rollsString[i].ToString();
}
}
public void FillFrames(List<int> frames)
{
for (int i=0; i <frames.Count; i++)
{
frameTexts[i].text = frames[i].ToString();
}
}
public static string FormatRolls(List<int> rolls)
{
string output = "";
for (int i = 0; i < rolls.Count; i++)
{
if((output.Length % 2 != 0 || output.Length == 20) && rolls[i] + rolls[i-1] == 10)
{
output += "/";
}
else if(rolls[i] == 0)
{
output += "-";
}
else if (rolls[i] == 10)
{
output += "X";
if (output.Length < 18)
{
output += " ";
}
}
else
{
output += rolls[i].ToString();
}
}
return output;
}
public void ResetScores()
{
foreach(Text text in rollTexts)
{
text.text = "";
}
foreach (Text text in frameTexts)
{
text.text = "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment