コード例 #1
0
        public void TestAnimationWithTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);
            var transform1 = new SpriteTransform(translation: new Vector(4, -5), rotation: 2.4f, scale: 1.5f, color: Color.Blue);
            var transform2 = new SpriteTransform(translation: new Vector(-6, 3), rotation: -1.2f, scale: 3.0f, color: Color.Red);

            // ARRANGE
            var spriteSheet = new SpriteSheet(null, "toto");
            var definition1 = spriteSheet.AddSpriteDefinition("toto1", source, origin);
            var definition2 = spriteSheet.AddSpriteDefinition("toto2", source, origin);
            var template = spriteSheet.AddSpriteAnimationTemplate("tata")
                .AddFrame(definition1, 1.0f, transform1)
                .AddFrame(definition2, 2.5f, transform2);

            var expectedSprite1 = new Sprite(spriteSheet, "toto1");
            var expectedSprite2 = new Sprite(spriteSheet, "toto2");
            var expected = new SpriteAnimation("tata", new[]
            {
                new SpriteAnimationFrame(expectedSprite1, 1.0f, transform1),
                new SpriteAnimationFrame(expectedSprite2, 2.5f, transform2)
            });

            // ACT
            var actual = (SpriteAnimation)template.CreateInstance();

            // ASSERT
            AssertSprite.AnimationEqual(expected, actual);
        }
コード例 #2
0
        public void TestSimpleAnimation()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);

            // ARRANGE
            var spriteSheet = new SpriteSheet(null, "toto");
            var definition1 = spriteSheet.AddSpriteDefinition("toto1", source, origin);
            var definition2 = spriteSheet.AddSpriteDefinition("toto2", source, origin);
            var template = spriteSheet.AddSpriteAnimationTemplate("tata")
                .AddFrame(definition1, 1.0f)
                .AddFrame(definition2, 2.5f);

            var expectedSprite1 = new Sprite(spriteSheet, "toto1");
            var expectedSprite2 = new Sprite(spriteSheet, "toto2");
            var expected = new SpriteAnimation("tata", new[]
            {
                new SpriteAnimationFrame(expectedSprite1, 1.0f),
                new SpriteAnimationFrame(expectedSprite2, 2.5f)
            });

            // ACT
            var actual = (SpriteAnimation)template.CreateInstance();

            // ASSERT
            AssertSprite.AnimationEqual(expected, actual);
        }
コード例 #3
0
        protected override Scene CreateScene()
        {
            var scene = new Scene("MainScene");
            scene.Add(new ColorLayer("Background", Color.CornflowerBlue));

            var spriteSheet = this.CreateSpriteSheet();

            this.entityLayer = new SpriteLayer("EntityMap");
            scene.Add(this.entityLayer);

            this.playerShipEntity = new PlayerShipEntity(this.entityLayer, spriteSheet);
            this.playerShipEntity.BindController(this.InputConfiguration);

            scene.Add(this.playerShipEntity);

            var yellowSprite = new Sprite(spriteSheet, "YellowEnemy") { Position = new Vector(250, 100) };
            var redSprite = new Sprite(spriteSheet, "RedEnemy") { Position = new Vector(300, 100) };
            var blueSprite = new Sprite(spriteSheet, "BlueEnemy") { Position = new Vector(350, 100) };

            this.entityLayer.AddSprite(yellowSprite);
            this.entityLayer.AddSprite(redSprite);
            this.entityLayer.AddSprite(blueSprite);

            return scene;
        }
コード例 #4
0
 private static XElement GetXml(Sprite sprite)
 {
     return new XElement("Sprite",
         new XAttribute("sheetName", sprite.SpriteSheet.Name),
         new XAttribute("name", sprite.SpriteName),
         new XAttribute("position", sprite.Position));
 }
コード例 #5
0
        public void TestCompositeWithTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);

            // ARRANGE
            var spriteSheet = new SpriteSheet(null, "toto");
            var definition = spriteSheet.AddSpriteDefinition("toto", source, origin);
            var transform = new SpriteTransform(translation: new Vector(43, -27), rotation: -1.2f, scale: 5.0f, color: Color.Yellow);
            var template = spriteSheet.AddSpriteCompositeTemplate("tata")
                .AddTemplate(definition, transform);

            var expectedSprite = new Sprite(spriteSheet, "toto")
            {
                Position = new Vector(43, -27),
                Rotation = -1.2f,
                Scale = 5.0f,
                Color = Color.Yellow
            };

            var expected = new SpriteComposite("tata", new[] { expectedSprite });

            // ACT
            var actual = (SpriteComposite)template.CreateInstance();

            // ASSERT
            AssertSprite.CompositeEqual(expected, actual);
        }
コード例 #6
0
        public static void Equal(Sprite expected, Sprite actual, string path = "")
        {
            BaseEqual(expected, actual, path);

            Assert.AreEqual(expected.SpriteSheet, actual.SpriteSheet, path + "SpriteSheet");
            Assert.AreEqual(expected.Origin, actual.Origin, path + "Origin");
            Assert.AreEqual(expected.FlipHorizontally, actual.FlipHorizontally, path + "FlipHorizontally");
            Assert.AreEqual(expected.FlipVertically, actual.FlipVertically, path + "FlipVertically");
        }
コード例 #7
0
        public void TestComposite()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);

            // ARRANGE
            var spriteSheet = new SpriteSheet(null, "toto");
            var definition = spriteSheet.AddSpriteDefinition("toto", source, origin);
            var template = spriteSheet.AddSpriteCompositeTemplate("tata")
                .AddTemplate(definition);

            var expectedSprite = new Sprite(spriteSheet, "toto");
            var expected = new SpriteComposite("tata", new[] { expectedSprite });

            // ACT
            var actual = (SpriteComposite)template.CreateInstance();

            // ASSERT
            AssertSprite.CompositeEqual(expected, actual);
        }
コード例 #8
0
        public static SpriteLayer SpriteLayerFromXml(GameResourceManager gameResourceManager, XElement layerElement)
        {
            var layerName = layerElement.Attribute("name").Value;
            var layer = new SpriteLayer(layerName);
            XmlRepository.BaseFromXml(layer, layerElement);

            foreach (var element in layerElement.Element("Sprites").Elements())
            {
                var sheetName = element.Attribute("sheetName").Value;
                var name = element.Attribute("name").Value;
                var position = element.Attribute("position").Value;

                var sprite = new Sprite(gameResourceManager.GetSpriteSheet(sheetName), name)
                {
                    Position = MathUtil.ParseVector(position)
                };

                layer.AddSprite(sprite);
            }

            return layer;
        }
コード例 #9
0
        private static Sprite CreateSprite(SpriteSheet spriteSheet, RectangleInt source, Vector origin)
        {
            spriteSheet.AddSpriteDefinition("toto", source, origin);
            var sprite = new Sprite(spriteSheet, "toto");

            return sprite;
        }
コード例 #10
0
        public int Draw(IDrawContext drawContext, Sprite sprite, Transform transform)
        {
            var source = this.definitions[sprite.SpriteName];

            var finalTransform = new SpriteTransform(transform, sprite.Position, sprite.Rotation, sprite.Scale, sprite.Color)
                .GetSpriteFinal();

            var destination = new Rectangle(
                finalTransform.Translation.X, finalTransform.Translation.Y,
                source.Rectangle.Width * finalTransform.Scale, source.Rectangle.Height * finalTransform.Scale);
            //var destination = new Rectangle(0, 0, source.Rectangle.Width, source.Rectangle.Height);
            //destination = finalTransform.ApplyFor(destination);

            var origin = sprite.Origin.HasValue ? sprite.Origin.Value : source.Origin;

            var visibilityCheckRectangle = new Rectangle(
                destination.X - (origin.X / source.Rectangle.Width) * destination.Width,
                destination.Y - (origin.Y / source.Rectangle.Height) * destination.Height,
                destination.Width, destination.Height);

            if (drawContext.Camera.Viewport.IsVisible(visibilityCheckRectangle))
            {
                if (sprite.ShouldDrawPixelPrecision)
                    destination = destination.Round();

                drawContext.DrawImage(new DrawImageParams
                {
                    Texture = this.Texture,
                    Source = source.Rectangle,
                    Destination = destination,
                    Rotation = finalTransform.Rotation,
                    Origin = origin,
                    Color = finalTransform.Color,
                    ImageEffect = GetImageEffect(sprite)
                });

                return 1;
            }

            return 0;
        }
コード例 #11
0
        private static ImageEffect GetImageEffect(Sprite sprite)
        {
            if (sprite.FlipHorizontally && !sprite.FlipVertically) return ImageEffect.FlipHorizontally;
            if (sprite.FlipHorizontally && sprite.FlipVertically) return ImageEffect.FlipHorizontally | ImageEffect.FlipVertically;
            if (!sprite.FlipHorizontally && sprite.FlipVertically) return ImageEffect.FlipVertically;

            return ImageEffect.None;
        }
コード例 #12
0
        public HitBase GetHit(Vector position, ICamera camera, WorldTransform worldTransform, Sprite sprite)
        {
            var source = this.definitions[sprite.SpriteName];
            var spriteRectangle =
                new Rectangle(
                    worldTransform.Offset.X + sprite.Position.X,
                    worldTransform.Offset.X + sprite.Position.Y,
                    source.Rectangle.Width,
                    source.Rectangle.Height)
                .Scale(camera.ZoomFactor)
                .Translate(camera.GetSceneTranslationVector(worldTransform.ParallaxScrollingVector));

            return spriteRectangle.Intercept(position)
                ? new SpriteHit(sprite)
                : null;
        }
コード例 #13
0
        private SpriteLayer CreateSpriteLayer()
        {
            var texture = this.ResourceManager.GetTexture(@"Sandbox\LinkSheet");
            var sheet = new SpriteSheet(texture, "Link");
            sheet.AddSpriteDefinition("Link01", new RectangleInt(3, 3, 16, 22), new Vector(8, 11));
            sheet.AddSpriteDefinition("Sleep01", new RectangleInt(45, 219, 32, 40));

            var la1 = sheet.AddSpriteDefinition("LinkAnim01", new RectangleInt(187, 270, 28, 28), new Vector(07, 25));
            var la2 = sheet.AddSpriteDefinition("LinkAnim02", new RectangleInt(225, 273, 31, 22), new Vector(07, 19));
            var la3 = sheet.AddSpriteDefinition("LinkAnim03", new RectangleInt(268, 270, 26, 28), new Vector(06, 19));
            var la4 = sheet.AddSpriteDefinition("LinkAnim04", new RectangleInt(303, 266, 17, 35), new Vector(06, 19));
            var la5 = sheet.AddSpriteDefinition("LinkAnim05", new RectangleInt(328, 269, 26, 30), new Vector(17, 19));
            var la6 = sheet.AddSpriteDefinition("LinkAnim06", new RectangleInt(355, 273, 32, 22), new Vector(23, 19));
            var la7 = sheet.AddSpriteDefinition("LinkAnim07", new RectangleInt(389, 271, 24, 25), new Vector(16, 22));
            var la8 = sheet.AddSpriteDefinition("LinkAnim08", new RectangleInt(423, 268, 16, 31), new Vector(08, 28));

            //var linkAnimTemplate = new SpriteAnimationTemplate("LinkAnim")
            var linkAnimTemplate = sheet.AddSpriteAnimationTemplate("LinkAnim")
                .AddFrame(la1, 0.1f)
                .AddFrame(la2, 0.1f)
                .AddFrame(la3, 0.1f)
                .AddFrame(la4, 0.1f)
                .AddFrame(la5, 0.1f)
                .AddFrame(la6, 0.1f)
                .AddFrame(la7, 0.1f)
                .AddFrame(la8, 0.1f);

            var crystal = sheet.AddSpriteDefinition("Crystal", new RectangleInt(336, 221, 10, 14), new Vector(4, 6));

            //this.linkCompositeTemplate = new SpriteCompositeTemplate("CompositeLink")
            this.linkCompositeTemplate = sheet.AddSpriteCompositeTemplate("CompositeLink")
                .AddTemplate(linkAnimTemplate)
                .AddTemplate(crystal, new SpriteTransform(translation: new Vector(20, 0)));

            //sheet.Save(@"C:\Users\Pascal\Dev\DotNet\GitHub\XNAGameEngine2D\Link SpriteSheet.xml");

            var link01 = new Sprite(sheet, "Link01") { Position = this.player, Color = Color.Blue, Scale = 2, ShouldDrawPixelPrecision = true };
            this.sleep01 = new Sprite(sheet, "Sleep01") { Position = new Vector(125, 25), FlipVertically = true };

            this.ResourceManager.AddSpriteSheet(sheet);

            var spriteLayer = new SpriteLayer("Sprites");
            spriteLayer.AddSprite(link01);
            spriteLayer.AddSprite(this.sleep01);
            spriteLayer.ParallaxScrollingVector = new Vector(4.0f, 8.0f);
            spriteLayer.Offset = new Vector(50, 50);

            return spriteLayer;
        }