public void PlayAnimation_ShouldChangeCurrentAnimation_WhenOtherAnimationWasSet()
        {
            // Arrange
            const string name1      = "animation 1";
            var          animation1 = CreateAnimation();

            const string name2      = "animation 2";
            var          animation2 = CreateAnimation();

            var component = new SpriteAnimationComponent();

            component.AddAnimation(name1, animation1);
            component.AddAnimation(name2, animation2);
            component.PlayAnimation(name1);

            // Assume
            Assume.That(component.CurrentAnimation.HasValue, Is.True);

            // Act
            component.PlayAnimation(name2);

            // Assert
            Assert.That(component.CurrentAnimation.HasValue, Is.True);
            Debug.Assert(component.CurrentAnimation != null, "component.CurrentAnimation != null");
            Assert.That(component.CurrentAnimation.Value.Name, Is.EqualTo(name2));
            Assert.That(component.CurrentAnimation.Value.Animation, Is.EqualTo(animation2));
        }
예제 #2
0
            private void CreateCampfireAnimation(double x, double y)
            {
                var campfire = new Entity();

                campfire.AddComponent(new Transform2DComponent
                {
                    Translation = new Vector2(x, y),
                    Rotation    = 0,
                    Scale       = Vector2.One
                });
                campfire.AddComponent(new SpriteRendererComponent());
                var spriteAnimationComponent = new SpriteAnimationComponent();

                campfire.AddComponent(spriteAnimationComponent);

                spriteAnimationComponent.AddAnimation("main", _assetStore.GetAsset <SpriteAnimation>(AssetsIds.CampfireAnimation));
                spriteAnimationComponent.PlayAnimation("main");
                spriteAnimationComponent.PlayInLoop = true;

                var random = new Random();

                spriteAnimationComponent.Position = random.NextDouble();

                Scene.AddEntity(campfire);
            }
예제 #3
0
        public Entity CreateAnimatedSprite(Scene scene, double x, double y, Random random)
        {
            var entity = new Entity();

            scene.AddEntity(entity);

            entity.AddComponent(new Transform2DComponent
            {
                Translation = new Vector2(x, y),
                Rotation    = 0,
                Scale       = Vector2.One
            });
            entity.AddComponent(new SpriteRendererComponent());

            var spriteAnimationComponent = new SpriteAnimationComponent
            {
                PlayInLoop = true
            };

            entity.AddComponent(spriteAnimationComponent);

            spriteAnimationComponent.AddAnimation("Explosion", _assetStore.GetAsset <SpriteAnimation>(AssetsIds.ExplosionAnimation));
            spriteAnimationComponent.PlayAnimation("Explosion");
            spriteAnimationComponent.Position = random.NextDouble();

            return(entity);
        }
예제 #4
0
        public void SerializeAndDeserialize_WhenCurrentAnimationIsNull()
        {
            // Arrange
            var animation        = CreateAnimation();
            var animationAssetId = AssetId.CreateUnique();

            var component = new SpriteAnimationComponent();

            component.AddAnimation("animation", animation);
            component.Position      = 0.7;
            component.PlaybackSpeed = 1.3;
            component.PlayInLoop    = true;

            AssetStore.GetAssetId(animation).Returns(animationAssetId);
            AssetStore.GetAsset <SpriteAnimation>(animationAssetId).Returns(animation);

            // Act
            var actual = SerializeAndDeserialize(component);

            // Assert
            Assert.That(actual.Animations, Has.Count.EqualTo(1));
            Assert.That(actual.Animations.Single().Key, Is.EqualTo("animation"));
            Assert.That(actual.Animations.Single().Value, Is.EqualTo(animation));
            Assert.That(actual.CurrentAnimation, Is.Null);
            Assert.That(actual.IsPlaying, Is.False);
            Assert.That(actual.Position, Is.EqualTo(component.Position));
            Assert.That(actual.PlaybackSpeed, Is.EqualTo(component.PlaybackSpeed));
            Assert.That(actual.PlayInLoop, Is.EqualTo(component.PlayInLoop));
        }
예제 #5
0
        public Entity CreateBird()
        {
            var entity = new Entity {
                Name = "Bird"
            };

            entity.AddComponent(new Transform2DComponent
            {
                Translation = Vector2.Zero,
                Rotation    = 0,
                Scale       = new Vector2(2, 2)
            });
            entity.AddComponent(new SpriteRendererComponent {
                SortingLayerName = "Bird"
            });
            entity.AddComponent(new InputComponent
            {
                InputMapping = new InputMapping
                {
                    ActionMappings =
                    {
                        new ActionMapping
                        {
                            ActionName      = "Flap",
                            HardwareActions =
                            {
                                new HardwareAction
                                {
                                    HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(Key.Space)
                                }
                            }
                        }
                    }
                }
            });
            entity.AddComponent(new BirdIdleFlyingComponent());
            var spriteAnimationComponent = new SpriteAnimationComponent {
                PlayInLoop = true
            };

            spriteAnimationComponent.AddAnimation("Flap", _assetStore.GetAsset <SpriteAnimation>(new AssetId(new Guid("FD8C8E61-A4B0-43C9-A89D-B22554B8A3F7"))));
            entity.AddComponent(spriteAnimationComponent);
            entity.AddComponent(new BirdFlapAnimationComponent());
            entity.AddComponent(new RectangleColliderComponent
            {
                Dimension = new Vector2(32 - 2, 24 - 2)
            });
            entity.AddComponent(new BirdSoundComponent(
                                    audioPlayer: _audioPlayer,
                                    wingSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("4ee1890b-3b92-45bb-9bee-a81e270f61d6"))),
                                    hitSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("7224f1b5-1471-4741-b720-0a10fc99ea53"))),
                                    dieSound: _assetStore.GetAsset <ISound>(new AssetId(new Guid("d1235819-13d0-419f-a50d-71478c1ad9bd")))
                                    ));
            return(entity);
        }
        public void Stop_ShouldThrow_WhenThereIsNoCurrentAnimation()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);

            // Act
            // Assert
            Assert.That(() => component.Stop(), Throws.InvalidOperationException);
        }
        public void RemoveAnimation_ShouldThrow_WhenAnimationToRemoveIsCurrentAnimation()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);
            component.PlayAnimation(name);

            // Act
            // Assert
            Assert.That(() => { component.RemoveAnimation(name); }, Throws.InvalidOperationException);
        }
        public void RemoveAnimation_ShouldRemoveAnimationFromAnimations()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);

            // Assume
            Assume.That(component.Animations, Has.Count.EqualTo(1));

            // Act
            component.RemoveAnimation(name);

            // Assert
            Assert.That(component.Animations, Has.Count.Zero);
        }
        public void AddAnimation_ShouldAddAnimationToAnimations()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            // Assume
            Assume.That(component.Animations, Has.Count.Zero);

            // Act
            component.AddAnimation(name, animation);

            // Assert
            Assert.That(component.Animations, Has.Count.EqualTo(1));
            Assert.That(component.Animations, Contains.Key(name));
            Assert.That(component.Animations[name], Is.EqualTo(animation));
        }
        public void PlayAnimation_ShouldSetIsPlayingToTrueAndPositionToZero()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);
            component.Position = 0.5;

            // Assume
            Assume.That(component.IsPlaying, Is.False);

            // Act
            component.PlayAnimation(name);

            // Assert
            Assert.That(component.IsPlaying, Is.True);
            Assert.That(component.Position, Is.Zero);
        }
        public void PlayAnimation_ShouldSetCurrentAnimation_WhenItIsNotSet()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);

            // Assume
            Assume.That(component.CurrentAnimation.HasValue, Is.False);

            // Act
            component.PlayAnimation(name);

            // Assert
            Assert.That(component.CurrentAnimation.HasValue, Is.True);
            Debug.Assert(component.CurrentAnimation != null, "component.CurrentAnimation != null");
            Assert.That(component.CurrentAnimation.Value.Name, Is.EqualTo(name));
            Assert.That(component.CurrentAnimation.Value.Animation, Is.EqualTo(animation));
        }
        public void Pause_ShouldSetIsPlayingToFalseAndKeepCurrentPosition()
        {
            // Arrange
            const string name      = "animation";
            var          animation = CreateAnimation();
            var          component = new SpriteAnimationComponent();

            component.AddAnimation(name, animation);
            component.PlayAnimation(name);
            component.Position = 0.5;

            // Assume
            Assume.That(component.IsPlaying, Is.True);

            // Act
            component.Pause();

            // Assert
            Assert.That(component.IsPlaying, Is.False);
            Assert.That(component.Position, Is.EqualTo(0.5));
        }
        public void SaveAndLoad_ShouldSaveSceneToFileAndThenLoadItFromFile_GivenSceneWithEntityWithSpriteAnimationComponent()
        {
            // Arrange
            var scene = SystemUnderTest.SceneFactory.Create();

            var entityWithSpriteAnimation = CreateNewEntityWithRandomName();
            var spriteAnimationComponent  = new SpriteAnimationComponent();

            entityWithSpriteAnimation.AddComponent(spriteAnimationComponent);
            scene.AddEntity(entityWithSpriteAnimation);

            spriteAnimationComponent.AddAnimation("animation", SystemUnderTest.AssetStore.GetAsset <SpriteAnimation>(AssetsIds.TestSpriteAnimation));
            spriteAnimationComponent.PlayAnimation("animation");
            spriteAnimationComponent.Position      = 0.7;
            spriteAnimationComponent.PlaybackSpeed = 1.3;
            spriteAnimationComponent.PlayInLoop    = true;

            // Act
            SystemUnderTest.SceneLoader.Save(scene, _sceneFilePath);
            var loadedScene = SystemUnderTest.SceneLoader.Load(_sceneFilePath);

            // Assert
            AssertScenesAreEqual(loadedScene, scene);
            AssertEntitiesAreEqual(loadedScene.RootEntities.Single(), entityWithSpriteAnimation);
            var loadedSpriteAnimationComponent = loadedScene.RootEntities.Single().GetComponent <SpriteAnimationComponent>();

            Assert.That(loadedSpriteAnimationComponent.Animations, Has.Count.EqualTo(1));
            Assert.That(loadedSpriteAnimationComponent.Animations.Single().Key, Is.EqualTo("animation"));
            Assert.That(SystemUnderTest.AssetStore.GetAssetId(loadedSpriteAnimationComponent.Animations.Single().Value),
                        Is.EqualTo(AssetsIds.TestSpriteAnimation));
            Assert.That(loadedSpriteAnimationComponent.CurrentAnimation, Is.Not.Null);
            Debug.Assert(loadedSpriteAnimationComponent.CurrentAnimation != null, "loadedSpriteAnimationComponent.CurrentAnimation != null");
            Assert.That(loadedSpriteAnimationComponent.CurrentAnimation.Value.Name, Is.EqualTo("animation"));
            Assert.That(SystemUnderTest.AssetStore.GetAssetId(loadedSpriteAnimationComponent.CurrentAnimation.Value.Animation),
                        Is.EqualTo(AssetsIds.TestSpriteAnimation));
            Assert.That(loadedSpriteAnimationComponent.IsPlaying, Is.True);
            Assert.That(loadedSpriteAnimationComponent.Position, Is.EqualTo(spriteAnimationComponent.Position));
            Assert.That(loadedSpriteAnimationComponent.PlaybackSpeed, Is.EqualTo(spriteAnimationComponent.PlaybackSpeed));
            Assert.That(loadedSpriteAnimationComponent.PlayInLoop, Is.EqualTo(spriteAnimationComponent.PlayInLoop));
        }