예제 #1
0
    private void SetAttackAndDefense()
    {
        Attackers.Clear();
        Defenders.Clear();

        List <Actor> units_within_range = FindObjectsOfType <Actor>().
                                          Where(actor => Vector3.Distance(actor.transform.position, transform.position) < influence_zone_radius).
                                          ToList();

        if (NodeFaction != null)
        {
            // attack and defense is determined by who currently claims the node

            for (int i = 0; i < units_within_range.Count; i++)
            {
                Actor actor = units_within_range[i];

                if (actor != null)
                {
                    if (actor.CurrentFaction.IsHostileTo(NodeFaction))
                    {
                        if (!Attackers.Contains(actor) && actor != null)
                        {
                            Attackers.Add(actor);
                        }
                    }
                    else
                    {
                        if (!Defenders.Contains(actor) && actor != null)
                        {
                            Defenders.Add(actor);
                        }
                    }
                }
            }
        }
        else
        {
            // the attackers are the faction with the most units; everybody else can try to kill them
            // NOTE: the faction that has the most units for the last boost gains the node (of course, then they have to keep it)

            var faction_units = units_within_range
                                .GroupBy(unit => unit.CurrentFaction,
                                         (faction, factions) => new {
                Key   = faction,
                Count = factions.Count()
            })
                                .OrderByDescending(faction => faction.Count);

            Attackers.AddRange(units_within_range.Where(unit => unit.CurrentFaction == faction_units.First().Key));
        }
    }