Esempio n. 1
0
        /// <summary>
        /// Add an influencer to this controller. If this controller is not managing the required stat then
        /// do nothing.
        /// </summary>
        /// <param name="influencer">The influencer to add.</param>
        /// <returns>True if the influencer was added, otherwise false.</returns>
        public bool TryAddInfluencer(StatInfluencerSO influencer)
        {
            if (m_Memory != null && influencer.generator != null)
            {
                MemorySO[] memories = m_Memory.GetAllMemoriesAbout(influencer.generator);
                for (int i = 0; i < memories.Length; i++)
                {
                    if (memories[i].stat == influencer.stat && memories[i].time + memories[i].cooldown > Time.timeSinceLevelLoad)
                    {
                        return(false);
                    }
                }
            }

            StatSO stat   = GetOrCreateStat(influencer.stat.name);
            bool   isGood = true;

            switch (stat.desiredState.objective)
            {
            case DesiredState.Objective.LessThan:
                if (influencer.maxChange > 0)
                {
                    isGood = false;
                }
                break;

            case DesiredState.Objective.Approximately:
                float currentDelta    = stat.desiredState.targetValue - stat.value;
                float influencedDelta = stat.desiredState.targetValue - (stat.value + influencer.maxChange);
                if (currentDelta < influencedDelta)
                {
                    isGood = false;
                }
                break;

            case DesiredState.Objective.GreaterThan:
                if (influencer.maxChange < 0)
                {
                    isGood = false;
                }
                break;
            }

            if (m_Memory != null)
            {
                m_Memory.AddMemory(influencer, isGood);
            }

            m_StatsInfluencers.Add(influencer);

            return(true);
        }