示例#1
0
    private void UpdateLeader()
    {
        if (neighbors.Count == 0 && group.Count != 1)
        {
            group.Clear();
            group.Add(this);
            return;
        }
        for (int i = 0; i < group.Count; i++)
        {
            if (group[i].leader.id != id)
            {
                group.RemoveAt(i);
            }
        }

        if (group.Count > SwarmManager.singleton.currentMaxGroupSize)
        {
            while (group.Count > SwarmManager.singleton.currentMaxGroupSize)
            {
                Groupable farthest = group.OrderByDescending(g => Vector3.Distance(g.transform.position, transform.position)).First();
                farthest.ResetMySelf();
                triggerCollider.enabled = false;
            }
        }
    }
示例#2
0
 // Use this for initialization
 void Start()
 {
     enemy        = GetComponent <Enemy>();
     groupable    = GetComponent <Groupable>();
     rig          = GetComponent <Rigidbody2D>();
     boidBehavior = GetComponent <BoidBehavior>();
 }
示例#3
0
    private void UpdateOther()
    {
        if (neighbors.Count == 0)
        {
            return;
        }
        if (group.Count != 1)
        {
            group.Clear();
            group.Add(this);
        }

        if (neighbors.Contains(leader))
        {
            return;
        }

        ConnectionTest c = ConnectionToID(leader.id);

        if (c.connected)
        {
            return;
        }
        float     newLeaderID = c.connectedIds.OrderByDescending(id => id).First();
        Groupable newLeader   = SwarmManager.singleton.GetGroupableFromID(newLeaderID);

        foreach (float id in c.connectedIds)
        {
            SwarmManager.singleton.GetGroupableFromID(id).ResetOnNewLeader(newLeader);
        }
    }
示例#4
0
 public Group(Point left, Point right, Connection c)
 {
     leftChild   = left;
     rightChild  = right;
     left.group  = this;
     right.group = this;
     internalConnetctions.Add(c);
     isStable = true;
 }
示例#5
0
 private void AddToGroup(Groupable g)
 {
     if (leader.group.Contains(g))
     {
         return;
     }
     leader.group.Add(g);
     g.leader = leader;
     g.enemy.ResetPath();
 }
示例#6
0
    // Use this for initialization
    protected virtual void Start()
    {
        enemy           = GetComponent <Enemy>();
        movement        = GetComponent <MoveEnemy>();
        pathfinding     = GetComponent <AStarPathfinding>();
        groupable       = GetComponent <Groupable>();
        playerTransform = GameManager.singleton.Player.transform;

        movement.ReachedPathEnd += Movement_ReachedPathEnd;
    }
示例#7
0
 // Use this for initialization
 void Start()
 {
     spriteRenderer  = GetComponentInChildren <SpriteRenderer>();
     startCol        = spriteRenderer.color;
     inGroupColor    = Random.ColorHSV(0.5f, 1, 0.5f, 1, 0.5f, 1, 1, 1);
     enemy           = GetComponent <Enemy>();
     triggerCollider = GetComponent <CircleCollider2D>();
     id     = Random.Range(0, 100f);
     leader = this;
     group.Add(this);
 }
示例#8
0
 private void ResetMySelf()
 {
     if (leader.group.Contains(this))
     {
         leader.group.Remove(this);
     }
     leader = this;
     group.Clear();
     group.Add(this);
     enemy.ResetPath();
 }
示例#9
0
    private void OnTriggerExit2D(Collider2D c)
    {
        if (!c.CompareTag("Enemy"))
        {
            return;
        }
        Groupable g = c.GetComponent <Groupable>();

        if (neighbors.Contains(g))
        {
            neighbors.Remove(g);
        }
    }
示例#10
0
 private void ResetOnNewLeader(Groupable newLeader)
 {
     if (leader.group.Contains(this))
     {
         leader.group.Remove(this);
     }
     leader = newLeader;
     if (leader.id != id)
     {
         group.Clear();
         group.Add(this);
     }
     enemy.ResetPath();
 }
示例#11
0
    private void OnTriggerEnter2D(Collider2D c)
    {
        if (!c.CompareTag("Enemy"))
        {
            return;
        }
        Groupable g = c.GetComponent <Groupable>();

        if (!neighbors.Contains(g))
        {
            neighbors.Add(g);
        }
        if (leader.id > g.leader.id && leader.group.Count < maxGroupCount)
        {
            AddToGroup(g);
        }
    }
示例#12
0
 // Use this for initialization
 protected virtual void Start()
 {
     rig             = GetComponent <Rigidbody2D>();
     player          = GameManager.singleton.Player.GetComponent <Player>();
     playerTransform = player.transform;
     groupable       = GetComponent <Groupable>();
     minSightAngle  /= 2;
     maxSightAngle  /= 2;
     maxVelocity     = maxVelocity / 100f;
     currentState    = idleState;
     chargeTimer     = chargedTime;
     currentEnergy   = maxEnergy;
     emptyCol        = SwarmManager.singleton.emptyEnergyColor;
     fullCol         = SwarmManager.singleton.fullEnergyColor;
     spriteRenderer  = GetComponentInChildren <SpriteRenderer>();
     startCol        = spriteRenderer.color;
 }
示例#13
0
    //hält Gruppe zusammen
    public Vector3 CohesionGroup(Groupable g, Vector3 movementDir)
    {
        Vector3 centreForce = Vector3.zero;

        foreach (Groupable thisG in g.neighborsInGroup)
        {
            if (!thisG)
            {
                continue;
            }
            centreForce += thisG.transform.position;
        }
        if (g.neighborsInGroup.Count != 0)
        {
            centreForce = (centreForce / g.neighborsInGroup.Count) - transform.position;
        }
        //centreForce = centreForce / g.group.Count + movementDir;
        return(centreForce.normalized);
    }
示例#14
0
    //zieht Gruppe auseinander
    public Vector3 SeparationGroup(Groupable g)
    {
        Vector3 separationForce = Vector3.zero;
        Vector3 pos             = transform.position;
        float   rad             = g.GetNeighborsRadius();
        float   minDist         = SwarmManager.singleton.boidSeperationDistance;
        Vector3 dir;
        float   dist;

        foreach (Groupable thisG in g.neighbors)
        {
            if (!thisG)
            {
                continue;
            }
            dir  = pos - thisG.transform.position;
            dist = dir.magnitude;
            if (dist < minDist)
            {
                separationForce += dir * (1 - dist / rad);
            }
        }
        return(separationForce.normalized);
    }
示例#15
0
    public Groupable GetGroupableFromID(float id)
    {
        Groupable thisG = groupables.Find(g => g.id == id);

        return(thisG);
    }