ZoeAppClass/Assets/Jogos/00 - Models/Crosswords/Scripts/CrosswordManager.cs

352 lines
9.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CrosswordManager : MonoBehaviour
{
public Transform lettersOrganizer;
int linesCount = 0;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
[SerializeField]
public List<List<CrosswordSlotClass>> columns = new List<List<CrosswordSlotClass>>();
public List<int> freeSlots = new List<int>();
public List<string> phrases = new List<string>();
public GameObject textPrefab;
public Transform textsOrganizer;
List<Text> allChildren = new List<Text>();
List<Text> reservedSlots = new List<Text>();
CrosswordSlotClass startingPoint;
int successSpawning = 0;
int spawnIndex, rng1, rng2;
public void Awake()
{
for(int i = 0; i < lettersOrganizer.childCount;i++)
{
freeSlots.Add(i);
}
allChildren.AddRange(lettersOrganizer.GetComponentsInChildren<Text>());
RadomizeLetters();
CountLines();
SetupColumns();
RenameChildren();
SpawnWordList();
}
public void Update()
{
//Debug.Log(Input.mousePosition);
}
public void CountLines()
{
linesCount = lettersOrganizer.childCount / 21;
}
public void SetupColumns()
{
for (int i = 0; i < 21; i++)
{
List<CrosswordSlotClass> toAdd = new List<CrosswordSlotClass>();
columns.Add(toAdd);
}
for (int a = 0; a < allChildren.Count; a += 21)
{
for (int n = 0; n < 21; n++)
{
columns[n].Add(new CrosswordSlotClass(allChildren[a+n], true));
}
}
StartCoroutine(PrepareToSpawn());
}
public void RenameChildren()
{
int i = 0;
for (int a = 0; a < linesCount; a++)
{
for (int n = 0; n < 21; n++)
{
int simNum = a + 1;
allChildren[i].name = alphabet[n] + simNum.ToString("00");
i++;
}
}
}
public void SpawnWordList()
{
for (int i = 0; i < phrases.Count;i++)
{
Text temp = Instantiate(textPrefab,textsOrganizer,false).GetComponent<Text>();
temp.text = "• " + phrases[i];
}
}
public void RadomizeLetters()
{
foreach(Text a in allChildren)
{
a.text = alphabet[Random.Range(0,alphabet.Length)].ToString();
}
}
IEnumerator SpawnWord()
{
string toSpawn = phrases[spawnIndex];
for(int i = 0; i < reservedSlots.Count; i++)
{
reservedSlots[i].text = toSpawn[i].ToString();
reservedSlots[i].color = Color.red;
}
spawnIndex++;
yield return null;
}
IEnumerator PrepareToSpawn()
{
for(int i = 0; i < phrases.Count; i++)
{
yield return StartCoroutine(PrepareSlots());
yield return StartCoroutine(SpawnWord());
}
Debug.Log(successSpawning);
}
IEnumerator PrepareSlots()
{
reservedSlots.Clear();
rng1 = Random.Range(0,columns.Count);
rng2 = Random.Range(0,linesCount);
startingPoint = columns[rng1][rng2];
if (!IsHorizontal())
{
yield return StartCoroutine(DoVertical());
}
else
{
yield return StartCoroutine(DoHorizontal());
}
yield return null;
}
IEnumerator DoVertical()
{
bool done = false;
while (!done)
{
for(int r = 0; r < 35; r++)
{
if (rng2 + phrases[spawnIndex].Length < columns[rng2].Count)
{
for(int n = 0; n < phrases[spawnIndex].Length; n++)
{
if(columns[rng1][rng2+n].avaliable)
{
reservedSlots.Add(columns[rng1][rng2+n].text);
columns[rng1][rng2+n].text.color = Color.green;
}
else
{
reservedSlots.Clear();
if (rng1 > 0)
{
rng1--;
}
else
{
rng1 = columns.Count - 1;
}
if (rng2 > 0)
{
rng2--;
}
else
{
rng2 = linesCount - phrases[spawnIndex].Length;
}
n = phrases[spawnIndex].Length;
}
}
if(reservedSlots.Count == phrases[spawnIndex].Length)
{
for(int n = 0; n < phrases[spawnIndex].Length; n++)
{
columns[rng1][rng2+n].avaliable = false;
}
done = true;
successSpawning++;
break;
}
}
else
{
if (rng2 > 0)
{
rng2--;
}
else
{
rng2 = 20;
}
}
}
done = true;
}
yield return null;
}
IEnumerator DoHorizontal()
{
bool done = false;
while (!done)
{
for (int r = 0; r < 35; r++)
{
if (rng1 + phrases[spawnIndex].Length < columns.Count)
{
for(int n = 0; n < phrases[spawnIndex].Length; n++)
{
if(columns[rng1+n][rng2].avaliable)
{
reservedSlots.Add(columns[rng1+n][rng2].text);
columns[rng1+n][rng2].text.color = Color.blue;
}
else
{
reservedSlots.Clear();
if (rng2 > 0)
{
rng2--;
}
else
{
rng2 = linesCount - 1;
}
if (rng1 > 0)
{
rng1 --;
}
else
{
rng1 = columns.Count - 1;
}
n = phrases[spawnIndex].Length;
}
}
if(reservedSlots.Count == phrases[spawnIndex].Length)
{
for(int n = 0; n < phrases[spawnIndex].Length; n++)
{
columns[rng1+n][rng2].avaliable = false;
}
done = true;
successSpawning++;
break;
}
}
else
{
if (rng1 > 0)
{
rng1--;
}
else
{
rng1 = columns.Count - 5;
}
}
}
done = true;
}
yield return null;
}
public bool ExpectedDiff(int n1, int n2, int diff)
{
int result = n2 - n1;
if (result == diff)
{
//Debug.Log("Result = " + result.ToString());
return true;
}
else
{
return false;
}
}
public bool IsHorizontal()
{
int rng = Random.Range(0,2);
if (rng == 0)
{
return false;
}
else
{
return true;
}
}
public int FindIndex(int n1, int n2, int dif)
{
int diff = n2 - n1;
int index = 0;
if (diff > dif)
{
bool found = false;
while (!found)
{
for (int n = n1; n < freeSlots.Count; n--)
{
if (ExpectedDiff(freeSlots[n1],freeSlots[n],dif))
{
found = true;
index = n + freeSlots[n];
break;
}
else
{
found = true;
index = -1;
break;
}
}
}
}
else if (diff < dif)
{
bool found = false;
while (!found)
{
for (int n = n1; n < freeSlots.Count; n++)
{
if (ExpectedDiff(freeSlots[n1],freeSlots[n],dif))
{
found = true;
index = n + freeSlots[n];
break;
}
else
{
found = true;
index = -1;
break;
}
}
}
}
return index;
}
}