Exemplo n.º 1
0
        public void Initialize(Species species)
        {
            _builder = GetComponent <CreatureBuilder>();

            _species = species;
            _species.OnMutationAdded   += OnMutationAdded;
            _species.OnMutationRemoved += OnMutationRemoved;

            _senseses = GetComponentInChildren <Senses>();

            _steering = GetComponent <SteeringController>();
            _steering.AddBehaviour <PredatorAvoidance>();
            _steering.AddBehaviour <Pursuit>();
            _steering.AddBehaviour <Arrive>();
            _steering.AddBehaviour <Wander>();
            _steering.DisableAll();

            _health  = species.GetStat(StatType.Health);
            Calories = species.CalorieConsumption / 2.0f;

            for (int i = 0; i < _species.MutationCount; i++)
            {
                OnMutationAdded(_species.GetMutation(i));
            }

            UpdateStats();

            RethinkState();

            name += " -" + species.Name;
        }
Exemplo n.º 2
0
        protected override float DoUtilityCalculation()
        {
            if (!Species.CanEat(FoodType.Meat))
            {
                return(0.0f);
            }

            float distance    = GetDistanceToTarget();
            float sight       = Species.GetStat(StatType.Sight);
            float consumption = Creature.Species.CalorieConsumption;

            if (distance > sight * 2)
            {
                return(0.0f);
            }

            float value = 0.8f * (1.0f - Creature.Calories / (consumption * 3.0f))
                          + 0.15f * (_target.CorpseCalories / consumption)
                          + 0.05f * (1.0f - distance / Species.GetStat(StatType.Sight) * 2.0f)
                          - 0.2f * (_target.DamagePerSecond / Creature.DamagePerSecond)
                          - 0.1f * (1.0f - Creature.Health / Species.GetStat(StatType.Health))
                          + 0.1f * (1.0f - _target.Health / _target.Species.GetStat(StatType.Health))
                          + (_target.IsAttacking(Creature) ? 0.15f : 0.0f);

            if (IsCurrent && !Done)
            {
                value += 0.2f;
            }

            return(value);
        }
Exemplo n.º 3
0
        protected override float DoUtilityCalculation()
        {
            float greatestThreat = 0.0f;

            float healthPercent = Creature.Health / Species.GetStat(StatType.Health);

            foreach (var threat in Senses.GetVisibleCreatures())
            {
                if (threat.Species == Species || !threat.Species.CanEat(FoodType.Meat))
                {
                    continue;
                }

                float distance = Vector2.Distance(threat.transform.position, Creature.transform.position);

                float value = 1.0f * (threat.DamagePerSecond / Creature.DamagePerSecond)
                              + 0.5f * healthPercent
                              + 0.5f * (1.0f - distance / (threat.Species.GetStat(StatType.Sight) * 3.0f))
                              - (Creature.Species.EaterType == EaterType.Herbivore ? 0.0f : 0.4f);

                greatestThreat = Mathf.Max(value, greatestThreat);
            }

            return(greatestThreat);
        }
Exemplo n.º 4
0
        public override void Update()
        {
            if (_food == null)
            {
                Done = true;
                return;
            }

            float distanceToFood = Vector2.Distance(Creature.transform.position, _food.transform.position);

            if (distanceToFood > Species.GetStat(StatType.Range))
            {
                _arrive.enabled = true;
                return;
            }
            _arrive.enabled = false;

            float eatAmount = _food.LoseCalories(Species.GetStat(StatType.Foraging) * Time.deltaTime);

            Creature.GainCalories(eatAmount);
        }
Exemplo n.º 5
0
        protected override float DoUtilityCalculation()
        {
            if (!Creature.Species.CanEat(_food.FoodType))
            {
                return(0.0f);
            }

            // TODO : Take foraging into account

            float distance    = Vector2.Distance(_food.transform.position, Creature.transform.position);
            float consumption = Creature.Species.CalorieConsumption;
            float value       = 0.85f * (1.0f - Creature.Calories / (consumption * 3.0f))
                                + 0.1f * (_food.Calories / consumption)
                                + 0.05f * (1.0f - distance / Species.GetStat(StatType.Sight));

            if (IsCurrent && !Done)
            {
                value += 0.1f;
            }

            return(value);
        }
Exemplo n.º 6
0
        public override void Update()
        {
            if (_target == null)
            {
                Done = true;
                return;
            }

            float distance = GetDistanceToTarget();

            if (distance > Species.GetStat(StatType.Sight) * 2.0f)      // Ran away
            {
                Done = true;
                return;
            }

            if (GetDistanceToTarget() <= Species.GetStat(StatType.Range) &&
                Time.time - _lastAttack > Species.GetStat(StatType.AttackSpeed))
            {
                _lastAttack = Time.time;
                _target.TakeDamage(Species.GetStat(StatType.Damage));
            }
        }
Exemplo n.º 7
0
 private void UpdateStats()
 {
     _health         = Mathf.Clamp(_health, 0.0f, _species.GetStat(StatType.Health));
     _steering.Speed = _species.GetStat(StatType.MovementSpeed);
     _senseses.Range = _species.GetStat(StatType.Sight);
 }
Exemplo n.º 8
0
        public void Reproduce(Vector2 position, Species species)
        {
            // TODO : Random chance to create new species

            Species childSpecies = species;

            float newSpeciesRn = Random.Range(0.0f, 1.0f);

            if (newSpeciesRn < NewSpeciesChance)
            {
                childSpecies = species.Clone("Child");
                childSpecies.OnResearchComplete += OnResearchComplete;
                Species.Add(childSpecies);
            }

            Vector2  childOffset = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * species.GetStat(StatType.Range) * 1.5f;
            Creature child       = Instantiate(CreaturePrefab, position + childOffset, Quaternion.identity).GetComponent <Creature>();

            child.Initialize(childSpecies);
        }