Ejemplo n.º 1
0
    public void ChooseSuccessor(FlockAgent leaderAgent)
    {
        FlockAgent newLeader = GetAgentOfType("Basic", leaderAgent.flockId);

        if (newLeader != null)
        {
            newLeader.flockLeader = newLeader;
            newLeader.type        = "Leader";

            if (leaderAgent.isPlayer)
            {
                newLeader.isPlayer = true;
            }
            MeshFilter newLeaderMesh = newLeader.GetComponentInChildren <MeshFilter>();
            newLeaderMesh.sharedMesh = leaderMesh;
        }



        if (newLeader != null)
        {
            foreach (FlockAgent agent in agents)
            {
                if (agent.flockLeader == leaderAgent)
                {
                    agent.flockLeader = newLeader;
                }
            }
        }
    }
Ejemplo n.º 2
0
    void debugNearbyObjects(FlockAgent agent, List <Transform> context)
    {
        Renderer r = agent.GetComponentInChildren <Renderer>();

        r.material.color = Color.Lerp(Color.white, Color.red, context.Count / 6f);
        //agent.GetComponentInChildren<SpriteRenderer>().color = Color.Lerp(Color.white, Color.red, context.Count / 6f);
    }
    public void Start()
    {
        //Setup Prefs
        dataStorage.SetupFlockPrefs();
        dataStorage.SetupPredPrefs();

        //Check if first time setup
        if (PlayerPrefs.GetInt("startingCount", 0) == 0)
        {
            dataStorage.SaveFlockPrefs();
            dataStorage.SavePredPrefs();
        }

        float radius = flock.startingCount * flock.agentDensity * 5;

        //Spawn in the predator away from murmuration
        Vector3 predatorPos = Random.insideUnitSphere * flock.startingCount * flock.agentDensity * 5 * 4;
        float   predRadius  = radius * 2;

        while ((predatorPos.x < predRadius && predatorPos.x > predRadius * -1) ||
               (predatorPos.z < predRadius && predatorPos.z > predRadius * -1) ||
               (predatorPos.y < predRadius && predatorPos.y > predRadius * -1))
        {
            predatorPos = Random.insideUnitSphere * flock.startingCount * flock.agentDensity * 5 * 4;
        }
        predatorPos  += flock.focalPoint;
        predatorAgent = Instantiate(
            predatorPrefab,
            predatorPos,
            Random.rotation,
            transform
            );
        predatorAgent.name = "Predator";
        predatorAgent.Initialize(flock);
        flock.predatorAgent = predatorAgent;

        //Spawn in the flock of birds
        for (int i = 0; i < flock.startingCount; i++)
        {
            Vector3    pos      = (Random.insideUnitSphere * flock.startingCount * flock.agentDensity * 5) + flock.focalPoint;
            FlockAgent starling = Instantiate(
                flockAgent,
                pos,
                Random.rotation,
                transform
                );
            starling.name = "" + i;
            starling.Initialize(flock, predatorAgent);
            flock.agents.Add(starling);
            flock.agentsTransform.Add(starling.transform);
            StartCoroutine(starling.starlingAnimate(starling.GetComponentInChildren <Animator>()));
            grid.Populate(i, pos);
        }

        //Setup Camera
        cam = Instantiate(camPrefab, flock.focalPoint, camPrefab.transform.rotation);
        cam.Initialise(flock, flock.agents[0].transform, flock.agents[0].transform, predatorAgent.transform);

        Menu.runSimulation = true;
    }
Ejemplo n.º 4
0
    public void updatePopulation()
    {
        //if number fish in our species is less than the number the model provided
        if (population < m_targetNumber)
        {
            //find how many fish we are off by
            int popDifference = m_targetNumber - population;

            //and create that many fish
            for (int y = 0; y < popDifference; y++)
            {
                FlockAgent newAgent = Instantiate(
                    m_FlockAgent,
                    m_AIManager.RandomPosition(),
                    Quaternion.Euler(UnityEngine.Random.Range(-20, 20), UnityEngine.Random.Range(0, 360), 0),
                    transform);
                newAgent.name = "Agent " + agents.Count;
                newAgent.Initialize(this);
                agents.Add(newAgent);
            }
        }
        //or we remove fish as long as we have too many
        else
        {
            while (population > m_targetNumber)
            {
                var target = m_FlockAgent.GetComponentInChildren <Transform>();
                Destroy(target.GetChild(target.childCount - 1).gameObject); // kill youngest child
                agents.RemoveAt(target.childCount - 1);
            }
        }
    }
Ejemplo n.º 5
0
    void InstantiateLeader(Color leaderColor, bool isPlayer, int id, Vector2 center)
    {
        FlockAgent leaderAgent = Instantiate(
            agentLeaderPrefab,
            center,
            Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
            transform
            );


        leaderAgent.GetComponentInChildren <Renderer>().material.SetColor("_Color", leaderColor);
        leaderAgent.name = "Leader Agent " + id;
        leaderAgent.Initialize(this, isPlayer, id, "Leader");
        agents.Add(leaderAgent);
    }
Ejemplo n.º 6
0
    // Start is called before the first frame update
    void OnEnable()
    {
        gameEnd       = false;
        agents        = new List <FlockAgent>();
        BaseBoidColor = agentBasicPrefab.GetComponentInChildren <Renderer>().sharedMaterial.color;

        squareMaxSpeed        = maxSpeed * maxSpeed;
        squareNeighborRadius  = neighborRadius * neighborRadius;
        squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;

        InstantiateFlock(new Vector2(25, 0), NewColor(249, 57, 67), 1, 10, 1f, true);
        InstantiateFlock(new Vector2(-25, 0), NewColor(252, 176, 179), 2, 10, 1f, true);
        InstantiateFlock(new Vector2(0, -25), NewColor(126, 178, 221), 3, 10, 1f, true);
        InstantiateFlock(new Vector2(0, 25), NewColor(68, 94, 147), 4, 10, 1f, true);
        InstantiateFlock(new Vector2(15, 15), BaseBoidColor, 0, (startingCount - 40) / 4, 2f, false);
        InstantiateFlock(new Vector2(-15, 15), BaseBoidColor, 0, (startingCount - 40) / 4, 2f, false);
        InstantiateFlock(new Vector2(15, -15), BaseBoidColor, 0, (startingCount - 40) / 4, 2f, false);
        InstantiateFlock(new Vector2(-15, -15), BaseBoidColor, 0, (startingCount - 40) / 4 + (startingCount - 40) % 4, 2f, false);



        //for (int i = 0; i < startingCount - 1; i++)
        //{
        //    FlockAgent newAgent = Instantiate(
        //        agentBasicPrefab,
        //        Random.insideUnitCircle * startingCount * AgentDensity,
        //        Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
        //        transform
        //        );

        //    newAgent.name = "Basic Agent " + i;
        //    newAgent.Initialize(this, false, 0, "Basic");
        //    agents.Add(newAgent);
        //}
        //InstantiateLeader(NewColor(249, 57, 67), true, 1);
        //InstantiateLeader(NewColor(252, 176, 179), false, 2);
        //InstantiateLeader(NewColor(126, 178, 221), false, 3);
        //InstantiateLeader(NewColor(68, 94, 147), false, 4);
    }
Ejemplo n.º 7
0
 public bool RemoveAgent(FlockAgent agent)
 {
     agent.GetComponentInChildren <SpriteRenderer>().enabled = false;
     return(agents.Remove(agent));
 }