Example #1
0
        public void MoveToKeyboardOverridesExistingKeyboardComponent()
        {
            // The only way to test this is to change the velocity and see if the new velocity number is used
            // Set velocity to 100, press D, wait one second; we should move 100px to the right.
            var expectedVelocity = 100;

            var entity = new CobaltEntity();

            entity.MoveToKeyboard(1);
            entity.MoveToKeyboard(expectedVelocity);

            KeyboardSystem.GenerateKeysCallback(() =>
            {
                // Press D to move X positively
                return(new Keys[] { Keys.D });
            });

            // Simulate keypress
            var keyboardSystem = new KeyboardSystem();

            keyboardSystem.Add(entity);
            keyboardSystem.Update(1);

            var velocitySystem = new VelocitySystem();

            velocitySystem.Add(entity);
            velocitySystem.Update(1);

            Assert.That(entity.Get <PositionComponent>().X, Is.EqualTo(expectedVelocity));
        }
Example #2
0
        public void ExecuteSystemTest([NUnit.Framework.Range(1, 10, 1)] int totalSystemExecutions)
        {
            //  Setup
            Vector3 velocity         = new Vector3(10, 20, 30);
            Vector3 position         = new Vector3(1, 2, 3);
            Vector3 expectedPosition = position;

            _testEnity = Pools.pool.CreateEntity()
                         .AddPosition(position)
                         .AddVelocity(velocity);

            //Desired. Strong typing when CreateSystem<T> is called.
            VelocitySystem velocitySystem = Pools.pool.CreateSystem <VelocitySystem>() as VelocitySystem;

            //  This will run the system exacly once.
            for (var ex = 0; ex < totalSystemExecutions; ex++)
            {
                //for every execution
                velocitySystem.Execute();

                //we expect it to move by one velocity unit
                expectedPosition += velocity;
            }


            //  Assert
            Assert.AreEqual(expectedPosition, _testEnity.position.position, "The entity position will the original position plus one velocity.");
        }
        public void UpdateUpdatesPositionRelativeToElapsedTime()
        {
            var vX       = 17;
            var vY       = -19;
            var velocity = new VelocityComponent(vX, vY);

            var pX       = 400;
            var pY       = 128;
            var position = new PositionComponent(pX, pY);

            var elapsed = 0.7f;
            var e       = new CobaltEntity();

            e.Set(velocity);
            e.Set(position);

            var system = new VelocitySystem();

            system.Add(e);
            system.Update(elapsed);

            Assert.That(position.X, Is.EqualTo(pX + (elapsed * vX)));
            Assert.That(position.Y, Is.EqualTo(pY + (elapsed * vY)));
        }
        protected override void Initialize()
        {
            _grid = new AdjacencyGrid(_settings.WorldWidth, _settings.WorldHeight, 25);

            _renderSystem             = new RenderSystem(EntityManager, this);
            _transformHierarchySystem = new TransformHierarchySystem(EntityManager, _grid);
            _velocitySystem           = new VelocitySystem(EntityManager);

            // Declare rigidbody collision system
            _rigidbodyCollisionSystem = new SortAndSweep(EntityManager,
                                                         (e) => e.HasComponents(typeof(CircleColliderComponent), typeof(RigidBodyComponent)),
                                                         (e1, e2) => true); // All collisions should be resolved

            _mouthCollisionSystem = new SortAndSweep(EntityManager,
                                                     (e) => e.HasComponents(typeof(CircleColliderComponent)) &&
                                                     (e.HasComponents(typeof(MouthComponent)) || e.HasComponents(typeof(EdibleComponent))),
                                                     // Only resolve if 1 has a food and the other a mouth (but not both)
                                                     (e1, e2) =>
                                                     (e1.HasComponent <MouthComponent>() && e2.HasComponent <EdibleComponent>())
                                                     ^ (e2.HasComponent <MouthComponent>() && e1.HasComponent <EdibleComponent>())
                                                     );

            _weaponHealthCollisionSystem = new SortAndSweep(EntityManager,
                                                            (e) => e.HasComponents(typeof(CircleColliderComponent)) &&
                                                            (e.HasComponent <HealthComponent>() || e.HasComponent <WeaponComponent>()),
                                                            // Only resolve if 1 is a weapon and the other a health
                                                            (e1, e2) =>
                                                            (e1.HasComponent <HealthComponent>() && e2.HasComponent <WeaponComponent>())
                                                            ^ (e2.HasComponent <HealthComponent>() && e1.HasComponent <WeaponComponent>())
                                                            );

            _rigidBodySystem       = new RigidBodySystem(EntityManager);
            _dragSystem            = new DragSystem(EntityManager, _settings.MassDensity);
            _movementControlSystem = new MovementControlSystem(EntityManager);
            _brainSystem           = new BrainSystem(EntityManager, Settings);
            WorldBorderSystem      = new WorldBorderSystem(EntityManager, _worldWidth, _worldHeight);
            _visionSystem          = new VisionSystem(EntityManager, _grid);

            // These systems take a reference to a list of collisions. This means that should collision code checking change, the objects do not.
            RigidbodyCollisionSystem = new RigidBodyCollisionSystem(_rigidbodyCollisionSystem.Collisions);
            MouthFoodCollisionSystem = new MouthFoodCollisionSystem(EntityManager, _mouthCollisionSystem.Collisions);
            InfoRenderSystem         = new InfoRenderSystem(EntityManager, this);
            _noseSystem = new NoseSystem(EntityManager, _grid);

            // Global energy manager. This Ensures a closed system so there is a fixed amount of enegry in the simulation
            _energyManager = new EnergyManager(_settings.InitialWorldEnergy);

            // These are systems that take into account energy and need the energy manager system

            EnergyDeathSystem = new EnergyDeathSystem(EntityManager);
            HealthDeathSystem = new HealthDeathSystem(EntityManager, this, _energyManager);
            OldAgeDeathSystem = new OldAgeDeathSystem(EntityManager, this, _energyManager, _settings.OldAgeEnabled, _settings.OldAgeMultiplier);

            WeaponSystem = new WeaponSystem(EntityManager, _weaponHealthCollisionSystem.Collisions, _energyManager);
            _movementControlEnergyCostSystem = new MovementControlEnergyCostSystem(EntityManager, _energyManager);

            InnovationIdManager innovationIdManager = new InnovationIdManager(100, 100);

            // So textures are constantly refreshed
            _renderSystem.EditorMode = true;
        }