public void Update(float gameSpeed)
        {
            for (int i = 0; i < Compatible.Count; i++)
            {
                var movementEntity = Compatible[i];
                var energy         = movementEntity.GetComponent <EnergyComponent>();
                var movement       = movementEntity.GetComponent <MovementControlComponent>();

                float extertedMove = (float)(Math.Abs(movement.ForwardForce) * CostPerNewtonPerSecond * gameSpeed);

                //Figure out if we can afford the energy for both movement and rotation
                if (!energy.CanAfford(extertedMove))
                {
                    movement.WishForceForward = 0;
                }

                // Deposit any used energy into the energy manager
                _energyManager.DepositEnergy(energy.ChargeEnergy(extertedMove));


                float extertedRotate = (float)(Math.Abs(movement.RotationForce) * CostPerNewtonPerSecond * gameSpeed);

                if (!energy.CanAfford(extertedRotate))
                {
                    movement.WishRotForce = 0;
                }
                _energyManager.DepositEnergy(energy.ChargeEnergy(extertedRotate));
            }
        }
Esempio n. 2
0
        public void Update(uint tick, float _gameSpeed)
        {
            Random r = new Random();

            for (int i = 0; i < Compatible.Count; i++)
            {
                var parentEntity = Compatible[i];
                var reproduction = parentEntity.GetComponent <ReproductionComponent>();

                if (reproduction.Reproduce > reproduction.ReproductionThreshold)
                {
                    var energy = parentEntity.GetComponent <EnergyComponent>();

                    // check if we have the energy
                    if (!energy.CanAfford(reproduction.ReproductionEnergyCost))
                    {
                        continue;
                    }
                    if (energy.Energy - reproduction.ReproductionEnergyCost < reproduction.RequiredRemainingEnergy)
                    {
                        continue;
                    }

                    // charge teh parent energy. Each critter has an efficency rating, so some energy gets wasted an the rest gets sent to the child.
                    float charged = energy.ChargeEnergy(reproduction.ReproductionEnergyCost);

                    float toChild = reproduction.Efficency * charged;
                    float wasted  = charged - toChild;

                    // Let the energy manager know about the wasted energy.
                    _energyManager.DepositEnergy(wasted);

                    // Some set up for the new child, setting positions/giving energy.
                    var babyEntity = Pool.AddEntityFromDefinition(reproduction.ChildDefinitionId, _simulation.JsonSettings, parentEntity.Tag);

                    babyEntity.GetComponent <EnergyComponent>().Energy = toChild;
                    var parentPosition = parentEntity.GetComponent <TransformComponent>().LocalPosition;
                    babyEntity.GetComponent <TransformComponent>().LocalPosition = parentPosition;

                    // The new child will need a mutated brain
                    var originalBrainComponent = parentEntity.GetComponent <BrainComponent>();
                    var originalBrain          = originalBrainComponent.Brain;


                    AbstractBrainGenotype parentGenotype = ((NeatBrainPhenotype)originalBrain).Genotype;
                    NeatBrainGenotype     childGenotype  = (NeatBrainGenotype)parentGenotype.Clone();


                    _muationManager.Mutate(childGenotype, _innovationIdManager);
                    var newBrain = babyEntity.GetComponent <BrainComponent>();
                    newBrain.SetGenotype(childGenotype);

                    // Do not need to pass it type as we have already set the brain, therefore it does not need initializing.
                    newBrain.SetUpLinks(null, _simulation.Settings);
                    newBrain.ParentSpecies = originalBrainComponent.Species;

                    BirthEventInfo e = new BirthEventInfo(parentPosition, tick * _gameSpeed, parentEntity.Id, parentEntity.Tag, babyEntity.Id, babyEntity.Tag, childGenotype, originalBrainComponent.Species.Id);
                    BirthEvent?.Invoke(e);
                }
            }
        }
Esempio n. 3
0
        public void Update(uint tick, float gameSpeed)
        {
            for (int i = 0; i < Compatible.Count; i++)
            {
                float timeElapsed = gameSpeed;

                var entity = Compatible[i];
                var weapon = entity.GetComponent <WeaponComponent>();

                // reset hit marker
                weapon.Hit = 0.0f;
                //Reduce cooldown timers if necessary
                if (weapon.CooldownLeftSeconds > 0)
                {
                    weapon.CooldownLeftSeconds -= timeElapsed;
                }
                if (weapon.AttackTimeLeft > 0)
                {
                    if (entity.TryGetComponent <GraphicsComponent>(out GraphicsComponent graphics))
                    {
                        graphics.Color = Color.Red;
                    }

                    weapon.AttackTimeLeft -= timeElapsed;
                }
                else
                {
                    if (entity.TryGetComponent <GraphicsComponent>(out GraphicsComponent graphics))
                    {
                        graphics.Color = Color.White;
                    }

                    // Remember to set the activation bool to false
                    weapon.Active = 0f;
                }

                // Only attack if activation is over threshold and the cooldowns have expired.
                if (weapon.Activation > weapon.ActivateThreshold && weapon.CooldownLeftSeconds <= 0 && weapon.AttackTimeLeft <= 0)
                {
                    if (entity.RootEntity.HasComponent <EnergyComponent>())
                    {
                        var energy = entity.RootEntity.GetComponent <EnergyComponent>();
                        // check if we have the energy
                        if (!energy.CanAfford(weapon.AttackCost))
                        {
                            continue;
                        }

                        // charge the entity energy for the attack
                        float charged = energy.ChargeEnergy(weapon.AttackCost);
                        _energyManager.DepositEnergy(charged);
                    }



                    weapon.Active = 1.0f;

                    // set up all the timers
                    weapon.CooldownLeftSeconds = weapon.CooldownTimeSeconds;
                    weapon.AttackTimeLeft      = weapon.AttackTimeSeconds;
                }
            }
            foreach (var c in _collisions)
            {
                // Damage the health entity
                // Figure out which was the weapon and which was the health entity
                Entity health;
                Entity weapon;
                if (c.E1.HasComponent <HealthComponent>())
                {
                    health = c.E1;
                    weapon = c.E2;
                }
                else
                {
                    health = c.E2;
                    weapon = c.E1;
                }

                // Check not attached to same entity
                if (health.RootEntity == weapon.RootEntity)
                {
                    continue;
                }


                var weaponComp = weapon.GetComponent <WeaponComponent>();
                if (weaponComp.Active <= 0)
                {
                    continue;
                }

                var healthComp = health.GetComponent <HealthComponent>();

                float damage = (float)(weaponComp.Damage);
                weaponComp.Active = 0;

                var trans = weapon.GetComponent <TransformComponent>();

                healthComp.Health -= damage;
                weaponComp.Hit     = 1.0f;

                if (weaponComp.SiphonEnergy && health.HasComponent <EnergyComponent>() && weapon.RootEntity.HasComponent <EnergyComponent>())
                {
                    var   attackerEnergy = weapon.RootEntity.GetComponent <EnergyComponent>();
                    var   victimEnergy   = health.GetComponent <EnergyComponent>();
                    float taken          = victimEnergy.ChargeEnergy(weaponComp.SiphonAmount);
                    attackerEnergy.DepositEnergy(taken);
                }

                OnAttack?.Invoke(new AttackEventInfo(trans.WorldPosition, tick * gameSpeed, weapon.Id, weapon.Tag, health.Id, health.Tag, damage));
            }
        }