void Start() { // Singleton instance = this; // Set up initial state { ships = new List <FleetShip>(); slots = new FleetShip[MaxShips]; // Build the initial ships formation = Formation.Get(Formation.V, 0); const int defaultSize = 1; for (int i = 0; i < defaultSize; i++) { addShip(); } // Start in default formation foreach (FleetShip ship in ships) { ship.gameObject.transform.localPosition = ship.target; } formCurrent(); } }
void formCurrent() { formation = Formation.Get(formation.type, ships.Count); for (int i = 0; i < ships.Count; i++) { setSlot(ships[i], (i + rotation) % ships.Count); } updateFiring(); }
void form(int newFormationType) { if (ships.Count == 0) { return; } if (newFormationType == formation.type) { rotation = (rotation + 1) % ships.Count; } formation = Formation.Get(newFormationType, ships.Count); formCurrent(); }
void addShip() { if (ships.Count < MaxShips) { // Create the ship GameObject shipObj = GameObject.Instantiate(shipPrefab); FleetShip ship = shipObj.GetComponent <FleetShip>(); ship.fleet = this; ship.name = "Ship" + ships.Count; ship.transform.parent = transform; ships.Add(ship); // If there are any empty slots in the current formation, take the first one for (int i = 0; i < formation.slots.Length; i++) { if (slots[i] == null) { setSlot(ship, i); updateFiring(); return; } } // If no empty slot was found, choose a larger formation { formation = Formation.Get(formation.type, ships.Count); // Position the new ship setSlot(ship, formation.newSlot); // Re-position the old ships for (int i = 0; i < ships.Count - 1; i++) { setSlot(ships[i], formation.slots[ships[i].slot].newIndex); } } updateFiring(); } }