I am coding a game in Unity and I would like help optimizing horribly messy code that I don't know how to optimize. I'm thinking that maybe a for loop would help, but I apparently am bad at using for loops. Here is the code in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
class listComparer : IComparer<int>
{
public int Compare(int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
// CompareTo() method
return x.CompareTo(y);
}
}
public class battleController : MonoBehaviour
{
//Enemies
public GameObject activeEnemies;
//Text Box
public battleTextBox daTextBox;
//Getting data from beforehand
public string enemy1ID;
public string enemy2ID;
public string enemy3ID;
public string enemy4ID;
public string enemy5ID;
public string enemy6ID;
public string flavorText;
//enemy speeds
public int enemy1speed;
public int enemy2speed;
public int enemy3speed;
public int enemy4speed;
public int enemy5speed;
public int enemy6speed;
public int totalTurnOrder;
public bool texting;
//The protagonists
public characterstats Micheal;
public characterstats Zage;
public characterstats Orego;
void Start()
{
StartCoroutine("RealStart");
}
IEnumerator RealStart()
{
// Getting all speed values
enemy1speed = activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().speed;
if (enemy2ID != "none")
{
enemy2speed = activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().speed;
}
else
{
enemy2speed = -1;
}
if (enemy3ID != "none")
{
enemy3speed = activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().speed;
}
else
{
enemy3speed = -1;
}
if (enemy4ID != "none")
{
enemy4speed = activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().speed;
}
else
{
enemy4speed = -1;
}
if (enemy5ID != "none")
{
enemy5speed = activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().speed;
}
else
{
enemy5speed = -1;
}
if (enemy6ID != "none")
{
enemy6speed = activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().speed;
}
else
{
enemy6speed = -1;
}
List<int> speeds = new List<int>();
speeds.Add(enemy1speed);
if (enemy2speed != -1)
{
speeds.Add(enemy2speed);
}
if (enemy3speed != -1)
{
speeds.Add(enemy3speed);
}
if (enemy4speed != -1)
{
speeds.Add(enemy4speed);
}
if (enemy5speed != -1)
{
speeds.Add(enemy5speed);
}
if (enemy6speed != -1)
{
speeds.Add(enemy6speed);
}
speeds.Add(Micheal.speed);
speeds.Add(Zage.speed);
speeds.Add(Orego.speed);
listComparer speedSort = new listComparer();
speeds.Sort(speedSort);
if (speeds.IndexOf(enemy1speed) == 0)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(enemy2speed) == 0)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(enemy3speed) == 0)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(enemy4speed) == 0)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(enemy5speed) == 0)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(enemy6speed) == 0)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 1;
}
if (speeds.IndexOf(Micheal.speed) == 0)
{
Micheal.turnOrder = 1;
}
if (speeds.IndexOf(Zage.speed) == 0)
{
Zage.turnOrder = 1;
}
if (speeds.IndexOf(Orego.speed) == 0)
{
Orego.turnOrder = 1;
}
if (speeds.IndexOf(enemy1speed) == 1)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(enemy2speed) == 1)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(enemy3speed) == 1)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(enemy4speed) == 1)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(enemy5speed) == 1)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(enemy6speed) == 1)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 2;
}
if (speeds.IndexOf(Micheal.speed) == 1)
{
Micheal.turnOrder = 2;
}
if (speeds.IndexOf(Zage.speed) == 1)
{
Zage.turnOrder = 2;
}
if (speeds.IndexOf(Orego.speed) == 1)
{
Orego.turnOrder = 2;
}
if (speeds.IndexOf(enemy1speed) == 2)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(enemy2speed) == 2)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(enemy3speed) == 2)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(enemy4speed) == 2)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(enemy5speed) == 2)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(enemy6speed) == 2)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 3;
}
if (speeds.IndexOf(Micheal.speed) == 2)
{
Micheal.turnOrder = 3;
}
if (speeds.IndexOf(Zage.speed) == 2)
{
Zage.turnOrder = 3;
}
if (speeds.IndexOf(Orego.speed) == 2)
{
Orego.turnOrder = 3;
}
if (speeds.IndexOf(enemy1speed) == 3)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(enemy2speed) == 3)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(enemy3speed) == 3)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(enemy4speed) == 3)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(enemy5speed) == 3)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(enemy6speed) == 3)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 4;
}
if (speeds.IndexOf(Micheal.speed) == 3)
{
Micheal.turnOrder = 4;
}
if (speeds.IndexOf(Zage.speed) == 3)
{
Zage.turnOrder = 4;
}
if (speeds.IndexOf(Orego.speed) == 3)
{
Orego.turnOrder = 4;
}
if (speeds.IndexOf(enemy1speed) == 4)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(enemy2speed) == 4)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(enemy3speed) == 4)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(enemy4speed) == 4)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(enemy5speed) == 4)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(enemy6speed) == 4)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 5;
}
if (speeds.IndexOf(Micheal.speed) == 4)
{
Micheal.turnOrder = 5;
}
if (speeds.IndexOf(Zage.speed) == 4)
{
Zage.turnOrder = 5;
}
if (speeds.IndexOf(Orego.speed) == 4)
{
Orego.turnOrder = 5;
}
if (speeds.IndexOf(enemy1speed) == 5)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(enemy2speed) == 5)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(enemy3speed) == 5)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(enemy4speed) == 5)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(enemy5speed) == 5)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(enemy6speed) == 5)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 6;
}
if (speeds.IndexOf(Micheal.speed) == 5)
{
Micheal.turnOrder = 6;
}
if (speeds.IndexOf(Zage.speed) == 5)
{
Zage.turnOrder = 6;
}
if (speeds.IndexOf(Orego.speed) == 5)
{
Orego.turnOrder = 6;
}
if (speeds.IndexOf(enemy1speed) == 6)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(enemy2speed) == 6)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(enemy3speed) == 6)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(enemy4speed) == 6)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(enemy5speed) == 6)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(enemy6speed) == 6)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 7;
}
if (speeds.IndexOf(Micheal.speed) == 6)
{
Micheal.turnOrder = 7;
}
if (speeds.IndexOf(Zage.speed) == 6)
{
Zage.turnOrder = 7;
}
if (speeds.IndexOf(Orego.speed) == 6)
{
Orego.turnOrder = 7;
}
if (speeds.IndexOf(enemy1speed) == 7)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(enemy2speed) == 7)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(enemy3speed) == 7)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(enemy4speed) == 7)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(enemy5speed) == 7)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(enemy6speed) == 7)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 8;
}
if (speeds.IndexOf(Micheal.speed) == 7)
{
Micheal.turnOrder = 8;
}
if (speeds.IndexOf(Zage.speed) == 7)
{
Zage.turnOrder = 8;
}
if (speeds.IndexOf(Orego.speed) == 7)
{
Orego.turnOrder = 8;
}
if (speeds.IndexOf(enemy1speed) == 8)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(enemy2speed) == 8)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(enemy3speed) == 8)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(enemy4speed) == 8)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(enemy5speed) == 8)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(enemy6speed) == 8)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder = 9;
}
if (speeds.IndexOf(Micheal.speed) == 8)
{
Micheal.turnOrder = 9;
}
if (speeds.IndexOf(Zage.speed) == 8)
{
Zage.turnOrder = 9;
}
if (speeds.IndexOf(Orego.speed) == 8)
{
Orego.turnOrder = 9;
}
totalTurnOrder = speeds.Count;
if (activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder ||
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder ||
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder ||
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder ||
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder ||
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
texting = true;
StartCoroutine(daTextBox.PrintText(flavorText, 2, 3f, 0.025f));
yield return new WaitWhile(() => texting);
}
if (activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy1ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy2ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy3ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy4ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy5ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().turnOrder == totalTurnOrder)
{
activeEnemies.transform.Find(enemy6ID).gameObject.GetComponent<enemyStats>().myTurn();
}
if (Micheal.turnOrder == totalTurnOrder)
{
Micheal.myTurn();
}
if (Zage.turnOrder == totalTurnOrder)
{
Zage.myTurn();
}
if (Orego.turnOrder == totalTurnOrder)
{
Orego.myTurn();
}
if (Micheal.turnIsMine == true || Zage.turnIsMine == true || Orego.turnIsMine == true)
{
StartCoroutine(daTextBox.PrintText(flavorText, 2, 3f, 0.025f));
}
}
}
Thank you so much to anyone who figures this out. P.S. This code is for determining the turn order in an RPG battle system
CodePudding user response:
I would recommend searching about object oriented architecture and trying to create an Enemy class. Then, you can create a List and iterate through each enemy as such:
int highestSpeed = -1;
Enemy fastestEnemy = -1;
foreach(Enemy enemy in Enemies) {
if(enemy.speed > highestSpeed) {
highestSpeed = enemy.speed;
fastestEnemy = enemy;
}
}
if(fastestEnemy == null)
//no enemy had a speed higher than -1
Alternatively, if you want a simpler approach, put the ids and speeds into an array and do the same thing:
public string[] EnemyIDs = new string[6];
EnemyIDs[0] = .....
EnemyIDs[1] = .....
//So on and so forth, remembering that arrays are zero based, so EnemyIDs[0] is the first element of the array, and that EnemyIDs[6] is not going to work, since it will be accessing the 7th object of a size 6 array (Out of bounds exception)
public int[] EnemySpeeds = new int[6];
for(int i = 0; i < 6; i ) {
if(EnemyIDs[i] == "None")
EnemySpeeds[i] = -1;
else
EnemySpeeds[i] = activeEnemies.transform.Find(EnemyIDs[i]).gameObject.GetComponent<enemyStats>().speed;
}
int highestSpeed = -1;
int fastestEnemyID = -1;
for(int i = 0; i < 6; i ) {
if(EnemySpeeds[i] > highestSpeed) {
highestSpeed = EnemySpeeds[i];
fastestEnemyID = EnemyIDs[i];
}
}
Have fun!