Exemplo n.º 1
0
        public void AddComponent_AddsComponentToEntityAndContainer()
        {
            var entity    = new Mock <IEntity>(MockBehavior.Strict);
            var component = new Mock <IScriptComponent>();

            entity.Setup(_ => _.AddComponent(component.Object));

            _target.AddComponent(entity.Object, component.Object);

            Assert.IsTrue(_target.GetComponents <IScriptComponent>().Any());
            Assert.IsTrue(_target.GetEntitiesWithComponent <IScriptComponent>().Any());
            entity.Verify(_ => _.AddComponent(component.Object), Times.Once);
        }
Exemplo n.º 2
0
        public void PlaySounds(IEntityContainer entityContainer)
        {
            var listeners      = entityContainer.GetEntitiesWithComponent <ListenerComponent>();
            var activeListener = listeners.FirstOrDefault(_ => _.GetComponent <ListenerComponent>().Active);

            if (activeListener != null)
            {
                var listener = activeListener.GetComponent <ListenerComponent>();
                var position = activeListener.GetComponent <PositionComponent>();
                var physics  = activeListener.GetComponent <PhysicsComponent>();
                _listener.Gain = listener.Gain;
                if (position != null)
                {
                    _listener.Position = position.Position;
                }
                if (physics != null)
                {
                    _listener.Velocity = physics.Velocity;
                }
            }

            var entities = entityContainer.GetEntitiesWithComponent <SourceComponent>();

            foreach (var entity in entities)
            {
                var source   = entity.GetComponent <SourceComponent>();
                var position = entity.GetComponent <PositionComponent>();
                var physics  = entity.GetComponent <PhysicsComponent>();

                if (position != null)
                {
                    source.Source.Position = position.Position;
                }
                if (physics != null)
                {
                    source.Source.Velocity = physics.Velocity;
                }

                if (!source.Play)
                {
                    continue;
                }

                source.Source.Play(source.SoundBuffer, source.Loop);
                source.Play = false;
            }
        }
Exemplo n.º 3
0
        public void Simulate(float timeStep, IEntityContainer entityContainer)
        {
            var dt       = timeStep / Iterations;
            var entities = entityContainer.GetEntitiesWithComponent <PhysicsComponent>();

            // Update positions
            for (var i = 0; i < Iterations; i++)
            {
                var newPositions    = new Dictionary <IEntity, Vector3>();
                var newOrientations = new Dictionary <IEntity, Vector3>();
                foreach (var entity in entities)
                {
                    var physicsComponent = entity.GetComponent <PhysicsComponent>();

                    var position = entity.GetComponent <PositionComponent>();
                    if (physicsComponent.Fixed && !physicsComponent.Kinematic)
                    {
                        newPositions.Add(entity, position.Position);
                        newOrientations.Add(entity, position.Rotation);
                    }
                    else
                    {
                        var(newPosition, newOrientation) = CalculatePosition(physicsComponent, position, dt);
                        newPositions.Add(entity, newPosition);
                        newOrientations.Add(entity, newOrientation);
                    }
                }
                foreach (var entity in entities)
                {
                    var position         = newPositions[entity];
                    var orientation      = newOrientations[entity];
                    var physicsComponent = entity.GetComponent <PhysicsComponent>();

                    // Kinematic and non-solid object ignore collisions
                    if (physicsComponent.Kinematic || !physicsComponent.Solid)
                    {
                        ChangePosition(entity, position, orientation);
                    }

                    // Don't calculate collisions if fixed or kinematic
                    if (physicsComponent.Fixed || physicsComponent.Kinematic)
                    {
                        continue;
                    }

                    foreach (var other in entities.Where(_ => _ != entity))
                    {
                        var otherPosition    = newPositions[other];
                        var otherOrientation = newOrientations[other];
                        var result           = DetermineCollision(entity, position, orientation, other, otherPosition, otherOrientation);

                        if (!result.Collision)
                        {
                            continue;
                        }

                        var(newPosition, newOrientation) = InterpolateCollision(entity, other, position, otherPosition, orientation, result);
                        position    = newPosition;
                        orientation = newOrientation;
                    }

                    ChangePosition(entity, position, orientation);
                }
            }

            // Clear force vectors
            foreach (var entity in entities)
            {
                var physicsComponent = entity.GetComponent <PhysicsComponent>();
                physicsComponent.Force = Vector3.Zero;
            }
        }