Example #1
0
    public StoredShip findNewTarget(StoredShip storedShip)
    {
        // if ship has no target
        if (storedShip.targetQueue.Count == 0)
        {
            // DETERMINE WHERE SHIPS SHOULD GO -------------------------------------------------------------------------
            if (this.starNumber == Races.get(storedShip.race).getHome().GetComponent <Star>().starNumber)  // if home, go here
            // go to random star
            {
                int targetStar = Random.Range(0, gen.stars.Count - 1);
                if (targetStar >= this.starNumber)
                {
                    targetStar++;                                // avoid targeting home star
                }
                // Smart method
                if (Races.get(storedShip.race).getIntellect() + Random.Range(0, 7) >= 8)
                {
                    // find best path to target from here
                    List <int> path = gen.starGraph.pathfind(this.starNumber, targetStar, this.graphParents);
                    // goes through path
                    foreach (int nextStar in path)
                    {
                        // add next star in path to queue of ship
                        storedShip.queueTarget(gen.stars[nextStar].transform.position);
                    }
                }
                // Dumb method
                else
                {
                    storedShip.queueTarget(gen.stars[targetStar].transform.position); // to go directly to target
                }
            }
            else   // if not at home, go home
                   // going home
            {
                int target = Races.get(storedShip.race).getHome().GetComponent <Star>().starNumber;

                // Smart method
                if (Races.get(storedShip.race).getIntellect() + Random.Range(0, 7) >= 8)
                {
                    List <int> path = gen.starGraph.pathfind(this.starNumber, target, this.graphParents);
                    // goes through path
                    foreach (int nextStar in path)
                    {
                        // add next star in path to queue of ship
                        storedShip.queueTarget(gen.stars[nextStar].transform.position);
                    }
                }
                // Dumb method
                else
                {
                    storedShip.queueTarget(Races.get(storedShip.race).getHome().transform.position);// to go directly home instead
                }
            }
        }
        return(storedShip);
    }
Example #2
0
    GameObject createShip(StoredShip storedShip, float x, float z)
    {
        GameObject     shipSpawn        = createShip(storedShip.race, storedShip.type, x, z, storedShip.fuel, storedShip.hp);
        List <Vector3> targetQueueClone = new List <Vector3>();

        foreach (Vector3 v in storedShip.targetQueue)
        {
            targetQueueClone.Add(new Vector3(v.x, v.y, v.z));
        }
        shipSpawn.GetComponent <EnemyShip>().targetQueue = targetQueueClone;
        return(shipSpawn);
    }
Example #3
0
    public GameObject newShipStarToStar(int fromStar, StoredShip storedShip)      // creates a ship at the given star of given race to travel to a random star
    {
        GameObject shipSpawn;
        // adding an offset where ships are spawned randomly around the same area of the star, to prevent crouding
        float spawnRange = 5f;
        float randomX    = Random.Range(-spawnRange, spawnRange);
        float randomZ    = Random.Range(-spawnRange, spawnRange);

        // set target for ship if none
        storedShip = stars[fromStar].GetComponent <Star>().findNewTarget(storedShip);

        // create ship
        shipSpawn = createShip(storedShip, stars[fromStar].transform.position.x + randomX, stars[fromStar].transform.position.z + randomZ);
        //shipSpawn = createShip(storedShip.race, storedShip.type, stars[fromStar].transform.position.x + randomX, stars[fromStar].transform.position.z + randomZ, storedShip.fuel, storedShip.hp);
        EnemyShip shipComponent = shipSpawn.GetComponent <EnemyShip>();

        shipComponent.hp += healAmount; // heals up at star
        if (shipComponent.hp > 100)
        {
            shipComponent.hp = 100;         // limits hp to 100
        }
        shipComponent.fuel += refuelAmount; // fuels up at star

        shipComponent.state = 1;            // makes sure ship is pursueing target

        // if next target is too far for remaining fuel, stay in this star to fuel up
        if (Vector3.Distance(stars[fromStar].transform.position, shipComponent.targetQueue[0]) * 2.5f > shipComponent.fuel)
        {
            shipComponent.queueTarget(stars[fromStar].transform.position);
        }

        // if ship has nowhere to go, send it to random star or home

        /*
         * if (shipComponent.targetQueue.Count == 0) {
         *  if (Random.Range(0, 3) == 0 && stars[fromStar] != Races.get(storedShip.race).getHome()) {
         *      shipSpawn.GetComponent<EnemyShip>().flyHome();
         *  }
         *  else {
         *      shipSpawn.GetComponent<EnemyShip>().flyTo((GameObject)stars[Random.Range(0, stars.Count)]); // sets the ship to fly to a random star
         *  }
         * }
         */

        return(shipSpawn);
    }
Example #4
0
    void shipLeave()
    {
        if (population > 0)
        {
            StoredShip toLeave = residing[0];
            residing.RemoveAt(0);
            population--;

            GameObject shipSpawn = gen.newShipStarToStar(this.starNumber, toLeave);
            Debug.Log("Ship left");
            // Is ship heading to where it is?
            if (shipSpawn.GetComponent <EnemyShip>().hasTarget())
            {
                Vector3 v2    = shipSpawn.GetComponent <EnemyShip>().targetQueue[0];
                Vector3 v1    = transform.position;
                float   range = 1f;
                if (Mathf.Abs(v1.x - v2.x) < range && Mathf.Abs(v1.z - v2.z) < range)
                {
                    Debug.Log("Ship should have stayed");
                }
            }
        }
    }