A vocabulary rich game teacher about capitalism greed and importance of language in our lives.
Wordweaver is in early development stage.
Casual Story
iOS
Unity
This hobby project is meant to be a great lesson in all aspects of programming. I am exploring Unity and learning how to create more efficient and easier to read code while at the same time reevaluating what I have written and if needed, rewriting existing code to be better. Scripts include: Conveyor tracking, Truck Logic and Train Letter Unloading.
private void Update()
{
// If there is next waypoint drive to it
if (currentWayPoint < wayPoints.Length)
{
if (targetWayPoint == null)
targetWayPoint = wayPoints[currentWayPoint];
DriveLetters();
}
// Check collision with other letter
CheckForNextLetter(nextLetter);
}
private void DriveLetters()
{
// move towards the target
if (!isColliding || indexOfLetter == 0)
{
transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position, speed * Time.deltaTime);
}
// Check if letter in position, change target if it is
if (transform.position == targetWayPoint.position)
{
currentWayPoint++;
if (currentWayPoint >= wayPoints.Length){}
else
{
targetWayPoint = wayPoints[currentWayPoint];
}
}
}
// Checks if letter is colliding next letter
private void CheckForNextLetter(LetterObject nextLetter)
{
if (indexOfLetter != 0 && nextLetter)
{
isColliding = letterCollider.bounds.Intersects(nextLetter.letterCollider.bounds);
}
}
// Get next leter from letter conveyor array
private void GetNextLetter(List<LetterObject> letterList)
{
indexOfLetter = letterList.IndexOf(this);
if (indexOfLetter > 0)
{
nextLetter = letterList[indexOfLetter - 1];
}
}
The Letter Object is made to follow a path between points for conveyor turns. On change with conveyor list (a letter pull to the truck) an actions is triggered to recheck each letter's index on the conveyor to manage object's collision.
The TruckLogic script is made to control the trucks, it updates the letters shown in the truck, checks word in a truck after action by the player and if the word exists, send the truck on its way and spawn another one.
void TruckLetterDisplay()
{
// Reload all sprites inside truck if change happened
for (int i=0; i< slotsLetter.Count; i++)
{
if (i < letterList.Count)
{
slotsLetter[i].rnd.sprite = letterList[i].Icon;
slotsLetter[i].letter = letterList[i];
}
else
{
slotsLetter[i].rnd.sprite = null;
slotsLetter[i].letter = null;
}
}
// Check if truck has a viable word
CheckTruckWord();
}
private void CheckTruckWord()
{
//Construct word
string word = ConstructWord();
if (database.words.Contains(word))
{
// Start drive off coroutine if word exists, start new if word changed into another viable word
if (coroutineRunning)
{
StopCoroutine(truckCoroutine);
coroutineRunning = false;
}
truckCoroutine = TruckDriveOffCoroutine(word, truckNumber);
StartCoroutine(truckCoroutine);
coroutineRunning = true;
}
}
// Make word from letter objects
private string ConstructWord()
{
List<char> chars = new List<char>();
for (int i = 0; i < letterList.Count; i++)
{
chars.Add(letterList[i].LetterChar);
}
return new string(chars.ToArray());
}
// Truck coroutine
private IEnumerator TruckDriveOffCoroutine(string word, int truckNumber)
{
print("Coroutine Start Truck: " + truckNumber);
yield return new WaitForSeconds(1);
print("3");
yield return new WaitForSeconds(1);
print("2");
yield return new WaitForSeconds(1);
print("1");
if (ConstructWord() == word)
{
transform.position = new Vector3(-0.001f, transform.position.y, transform.position.z);
rb.velocity = new Vector2(-2, 0);
CountPoints(word);
print("Truck Driving Out truck: " + truckNumber);
}
else
{
print("Word changed, no truck: " + truckNumber);
}
}
private void CountPoints(string word)
{
database.points += word.Length;
}
// Drive truck
private void Update()
{
// If truck arrived in position stop it
if (transform.position.x < 0.05f && transform.position.x > -0.001f)
{
rb.velocity = new Vector2(0, 0);
transform.position = new Vector3(0.0f, transform.position.y, transform.position.z);
}
// If truck drove off the screen spawn new one
if (transform.position.x < screenBounds.x * -2f)
{
GameObject truck = Instantiate(truckPrefab);
truck.transform.position = new Vector3(screenBounds.x * 2.0f, this.transform.position.y, this.transform.position.z);
TruckLogic script = truck.GetComponent<TruckLogic>();
script.truckNumber = truckNumber;
script.rb.velocity = new Vector2(-2, 0);
database.trucks[truckNumber] = script;
// Destroy old truck
Actions.OnTruckUpdate -= TruckLetterDisplay;
Destroy(this.gameObject);
}
}