Beispiel #1
0
    void Awake()
    {
        cityGenerator = GameObject.FindGameObjectWithTag("GameController").GetComponent <CityGenerator>();

        ValidBorderMaterial   = (Material)Object.Instantiate(Resources.Load("Materials/District Border"));
        InvalidBorderMaterial = (Material)Object.Instantiate(Resources.Load("Materials/District Border"));


        NormalBackgroundMaterial  = (Material)Object.Instantiate(Resources.Load("Materials/District Background"));
        MinimumBackgroundMaterial = (Material)Object.Instantiate(Resources.Load("Materials/Striped District Background"));
        _lockedBackgroundMaterial = (Material)Object.Instantiate(Resources.Load("Materials/District Background"));

        BackgroundMaterial = MinimumBackgroundMaterial;

        PartyColor = evenBackgroundColor;
        NormalBackgroundMaterial.SetColor("_Color", CurrentColor);

        MinimumBackgroundMaterial.SetColor("_StripeColor", CurrentColor);
        MinimumBackgroundMaterial.SetColor("_NonStripeColor", GlazeColor);

        LockedBackgroundMaterial.SetColor("_Color", CurrentLockedColor);

        ValidBorderMaterial.SetColor("_Color", normalBorderColor);
        InvalidBorderMaterial.SetColor("_Color", normalBorderColor);

        CurrentMajority = Constituent.Party.None;
    }
Beispiel #2
0
    public void UpdateMemberData()
    {
        HashSet <Constituent> members = new HashSet <Constituent>(ConstituentsQuery);

        MemberCount       = members.Count();
        VotingMemberCount = VotingConstituentsQuery.Count();

        //create a function to get the neighbors of a constituent
        System.Func <Constituent, IEnumerable <Constituent> > neighborFunction = (c) =>
        {
            return(c.Neighbors.Where((n) => { return members.Contains(n); }));
        };

        //update the set of articulation points. we pass in a neighbor function for a depth first search
        ArticulationPoints = Utils.FindArticulationPoints <Constituent>(members.First(), neighborFunction);

        //update the set of neighbors - find all constituents that share a n edge with a member of this district
        NeighborConstituents = new HashSet <Constituent>(members.SelectMany((member) =>
        {
            return(member.Neighbors.Where((neighbor) => { return neighbor != null && neighbor.District != this; }));
        }));

        //filter the consistuents by the ones in this district and then group them by party
        var partyGrouping = members.GroupBy((obj) => { return(obj.party); });

        Dictionary <Constituent.Party, int> voteCounts = new Dictionary <Constituent.Party, int>();

        foreach (var group in partyGrouping)
        {
            voteCounts.Add(group.Key, group.Count());
        }

        Constituent.Party majority;
        int votesBlue = 0, votesRed = 0, votesYellow = 0;

        voteCounts.TryGetValue(Constituent.Party.Blue, out votesBlue);
        voteCounts.TryGetValue(Constituent.Party.Red, out votesRed);
        voteCounts.TryGetValue(Constituent.Party.Yellow, out votesYellow);
        if (votesBlue > votesRed)
        {
            majority = Constituent.Party.Blue;
            CurrentMajorityPercent = ((float)votesBlue) / VotingMemberCount;
        }
        else if (votesBlue < votesRed)
        {
            majority = Constituent.Party.Red;
            CurrentMajorityPercent = ((float)votesRed) / VotingMemberCount;
        }
        else
        {
            majority = Constituent.Party.None;
            CurrentMajorityPercent = 0.5f;
        }
        VotesBlue   = votesBlue;
        VotesRed    = votesRed;
        VotesYellow = votesYellow;

        //update the background color
        if (majority != CurrentMajority)
        {
            Color newColor = GetBackgroundColor(majority);
            Color oldColor = GetBackgroundColor(CurrentMajority);
            LeanTween.value(gameObject, oldColor, newColor, 0.25f).setOnUpdateColor((currentColor) =>
            {
                PartyColor            = currentColor;
                var interpolatedColor = CurrentColor;

                NormalBackgroundMaterial.SetColor("_Color", interpolatedColor);
                MinimumBackgroundMaterial.SetColor("_StripeColor", interpolatedColor);
                MinimumBackgroundMaterial.SetColor("_NonStripeColor", GlazeColor);

                LockedBackgroundMaterial.SetColor("_Color", CurrentLockedColor);
            });

            CurrentMajority = majority;
        }

        if (VotingMemberCount <= cityGenerator.MinDistrictSize)
        {
            BackgroundMaterial = MinimumBackgroundMaterial;
        }
        else
        {
            BackgroundMaterial = NormalBackgroundMaterial;
        }
    }