private Stat CreateStat(float baseValue, StatType statType)
    {
        StatSO statData = AssetManager.Instance.StatDatabase.Find(x => x.StatType == statType);
        Stat   stat     = new Stat(baseValue, statData.MinValue, statData.MaxValue);

        return(stat);
    }
        /// <summary>
        /// Checks to see if any stats are not in their desired states. If any are not then
        /// look in memories to see if the character knows of a place to address that. If
        /// a place is recalled and they are ready to return then they move towards it.
        /// Otherwise wander.
        /// </summary>
        protected override void UpdateMove()
        {
            StatSO[] stats = statsController.GetStatsNotInDesiredState();

            nearestMemoryOfInterest = null;
            focusedStat             = null;
            float sqrMagnitude = float.PositiveInfinity;

            for (int i = 0; i < stats.Length; i++)
            {
                //Debug.Log(gameObject.name + " desires to " + stats[i].goal + " " + stats[i].name + " to " + stats[i].desiredState.targetValue);
                //Debug.Log(stats[i].name + " is currently " + stats[i].value);

                // Find the nearest place that is in current memory that helps achieve one of the characters goals
                MemorySO[] memories = memory.GetMemoriesInfluencingStat(stats[i].name);
                for (int y = 0; y < memories.Length; y++)
                {
                    if (memories[y].readyToReturn)
                    {
                        if (stats[i].goal == DesiredState.Goal.Increase && memories[y].influence < 0)
                        {
                            continue;
                        }
                        else if (stats[i].goal == DesiredState.Goal.Decrease && memories[y].influence > 0)
                        {
                            continue;
                        }

                        Collider col = memories[y].about.GetComponent <Collider>();
                        if (!col.bounds.Contains(transform.position))
                        {
                            float distance = Vector3.SqrMagnitude(memories[y].about.transform.position - gameObject.transform.position);
                            if (distance < sqrMagnitude)
                            {
                                focusedStat             = stats[i];
                                nearestMemoryOfInterest = memories[y];
                                sqrMagnitude            = distance;
                            }
                        }
                    }
                }

                // Head to the remembered place
                if (nearestMemoryOfInterest != null)
                {
                    //Debug.Log("It looks like " + nearestMemoryOfInterest.about + " is the best place to achieve that goal");

                    GameObject target = nearestMemoryOfInterest.about;
                    Collider   col    = target.GetComponent <Collider>();
                    SetCurrentTargetWithinTriggerOf(col);
                    return;
                }
            }

            // if no place in memory wander randomly
            base.UpdateMove();
        }
Exemple #3
0
        /// <summary>
        /// Retrive all memories (long and short term) about influencers of a stat.
        /// </summary>
        /// <param name="statTemplate">Template of the stat we are interested in.</param>
        /// <returns>A set of influencers known to affect the desired stat.</returns>
        public MemorySO[] GetMemoriesInfluencingStat(StatSO statTemplate)
        {
            MemorySO[] shortTermMemories = m_ShortTermMemories.Where(m => m.stat.name == statTemplate.name).ToArray <MemorySO>();
            MemorySO[] longTermMemories  = m_LongTermMemories.Where(m => m.stat.name == statTemplate.name).ToArray <MemorySO>();

            MemorySO[] all = new MemorySO[shortTermMemories.Length + longTermMemories.Length];
            shortTermMemories.CopyTo(all, 0);
            longTermMemories.CopyTo(all, shortTermMemories.Length);

            return(all);
        }
        internal List <Brain> GetAllActorsWith(StatSO statTemplate)
        {
            //TODO cache the results of this filter
            List <Brain> results = new List <Brain>();

            for (int i = 0; i < ActiveBrains.Count; i++)
            {
                if (ActiveBrains[i].HasStat(statTemplate))
                {
                    results.Add(ActiveBrains[i]);
                }
            }
            return(results);
        }
Exemple #5
0
        private void SetupMemory()
        {
            GameObject go = new GameObject("Memory");

            controller = new GameObject().AddComponent <MemoryController>();

            shortTermInfluencer         = new GameObject("ShortTermInfluencer");
            shortTermNegativeInfluencer = new GameObject("ShortTermInfluencer1");

            StatSO shortTermTestStat = ScriptableObject.CreateInstance <StatSO>();

            shortTermTestStat.name = "ShortTermTest";

            shortTermMemory           = ScriptableObject.CreateInstance <MemorySO>();
            shortTermMemory.about     = shortTermInfluencer;
            shortTermMemory.stat      = shortTermTestStat;
            shortTermMemory.influence = 5;
            shortTermMemory.cooldown  = 0.1f;

            StatSO shortTermNegativeInfluenceTestStat = ScriptableObject.CreateInstance <StatSO>();

            shortTermNegativeInfluenceTestStat.name = "ShortTermNegativeInfluence";

            shortTermMemoryNegativeInflunce           = ScriptableObject.CreateInstance <MemorySO>();
            shortTermMemoryNegativeInflunce.about     = shortTermNegativeInfluencer;
            shortTermMemoryNegativeInflunce.stat      = shortTermNegativeInfluenceTestStat;
            shortTermMemoryNegativeInflunce.influence = -5;

            longTermInfluencer = new GameObject("LongTermInfluencer");

            StatSO longTermTestStat = ScriptableObject.CreateInstance <StatSO>();

            longTermTestStat.name = "LongTermTest";

            longTermMemory           = ScriptableObject.CreateInstance <MemorySO>();
            longTermMemory.about     = longTermInfluencer;
            longTermMemory.stat      = longTermTestStat;
            longTermMemory.influence = 50;
            longTermMemory.cooldown  = 0;

            // Validate setup
            Assert.Zero(controller.GetShortTermMemories().Length, "There are short term memories in a new Memory Controller");
            Assert.Zero(controller.GetLongTermMemories().Length, "There are long term memories in a new Memory Controller");
        }
Exemple #6
0
        internal virtual void FinishBehaviour()
        {
            IsExecuting = false;
            EndTime     = 0;

            for (int i = 0; i < m_CharacterInfluences.Length; i++)
            {
                if (m_CharacterInfluences[i].applyOnCompletion)
                {
                    StatSO stat = Brain.GetOrCreateStat(m_CharacterInfluences[i].statTemplate);
                    stat.Value += m_CharacterInfluences[i].maxChange;
                }
            }

            if (m_OnEndCue != null)
            {
                Brain.Actor.Prompt(m_OnEndCue);
            }
        }
Exemple #7
0
        /// <summary>
        /// If all the conditions of this state are satisfied then this will return true.
        /// </summary>
        public bool IsSatisfiedFor(StatsTracker statsTracker)
        {
            if (statTemplate != null)
            {
                StatSO stat = statsTracker.GetOrCreateStat(statTemplate);

                switch (objective)
                {
                case Objective.LessThan:
                    if (stat.NormalizedValue >= normalizedTargetValue)
                    {
                        return(false);
                    }
                    break;

                case Objective.Approximately:
                    return(Mathf.Approximately(stat.NormalizedValue, normalizedTargetValue));

                case Objective.GreaterThan:
                    if (stat.NormalizedValue <= normalizedTargetValue)
                    {
                        return(false);
                    }
                    break;
                }
            }

            // Check substates
            for (int i = 0; i < m_SubStates.Count; i++)
            {
                if (!m_SubStates[i].IsSatisfiedFor(statsTracker))
                {
                    return(false);
                }
            }

            return(true);
        }