예제 #1
0
        public SandboxScreen(ScreenNavigation screenNavigation)
        {
            this.screenNavigation = screenNavigation;

            this.player = new Vector(25, 25);
            this.range = 0.25f;
        }
        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);
        }
예제 #3
0
        public void TestSpriteCompositeDrawWithDefaultValuesAndIdentityTransform()
        {
            var source1 = new RectangleInt(10, 10, 20, 20);
            var source2 = new RectangleInt(40, 40, 30, 30);
            var origin = new Vector(4, 7);

            var expected1 = CreateDrawImageParams(source1, origin, destination: new Rectangle(0, 0, 20, 20));
            var expected2 = CreateDrawImageParams(source2, origin, destination: new Rectangle(0, 0, 30, 30));

            // ARRANGE
            var sprite1 = CreateSprite(source1, origin);
            var sprite2 = CreateSprite(source2, origin);
            var spriteComposite = new SpriteComposite("tata", new[] { sprite1, sprite2 });

            DrawImageParams actual1 = null;
            DrawImageParams actual2 = null;
            var callCount = 0;
            var drawContext = CreateDrawContextMock(p =>
            {
                callCount++;
                if (callCount == 1) actual1 = p;
                if (callCount == 2) actual2 = p;
            });

            // ACT
            spriteComposite.Draw(drawContext.Object, SpriteTransform.SpriteIdentity);

            // ASSERT
            drawContext.VerifyAll();
            Assert.AreEqual(2, callCount);
            AssertDrawImageParamsAreEquals(expected1, actual1);
            AssertDrawImageParamsAreEquals(expected2, actual2);
        }
        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);
        }
        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);
        }
예제 #6
0
        public Transform(Transform innerTransform = null,
            Vector? translation = null, float rotation = 0.0f, float scale = 1.0f)
        {
            this.innerTransform = innerTransform;

            this.Translation = translation.HasValue ? translation.Value : Vector.Zero;
            this.Rotation = rotation;
            this.Scale = scale;
        }
예제 #7
0
        public void Update(IGameTiming gameTime)
        {
            if (this.isActive)
            {
                var gameAreaClampRectangle = Rectangle.FromBound(100, 10, 700, 480 - 32);

                var actualVelocity = this.velocity.Scale(gameTime.ElapsedSeconds);
                this.position = this.position.Translate(actualVelocity);

                this.SpriteReference.Position = this.position;

                this.isActive = gameAreaClampRectangle.Intercept(this.position);
            }
        }
예제 #8
0
        public static bool IsHitPolygone(IEnumerable<Vector> polygone, Vector vector)
        {
            var isHit = false;

            polygone.ForEachPair((first, second) =>
            {
                if (((first.Y > vector.Y) != (second.Y > vector.Y))
                    && (vector.X < (second.X - first.X) * (vector.Y - first.Y)
                        / (second.Y - first.Y) + first.X))
                    isHit = !isHit;
            });

            return isHit;
        }
예제 #9
0
        private BulletEntity(SpriteLayer entityLayer, SpriteSheet shipSheet, Vector position, Vector velocity)
        {
            this.entityLayer = entityLayer;
            this.shipSheet = shipSheet;
            this.position = position;
            this.velocity = velocity;
            this.isActive = true;

            this.SpriteReference = new SpriteReference
            {
                Position = this.position,
                SpriteName = "Bullet",
                Sprite = new Sprite(this.shipSheet, "YellowShot")
            };

            this.entityLayer.AddSprite(this.SpriteReference);
        }
예제 #10
0
        public void TestSpriteCompositeDrawWithChangesButIdentityTransform()
        {
            var source1 = new RectangleInt(10, 10, 20, 20);
            var source2 = new RectangleInt(40, 40, 30, 30);
            var origin = new Vector(4, 7);

            var expected1 = CreateDrawImageParams(source1, origin,
                destination: new Rectangle(12, 8, 10, 10), rotation: 4.2f, color: Color.Red);

            var expected2 = CreateDrawImageParams(source2, origin,
                destination: new Rectangle(12, 8, 15, 15), rotation: 4.2f, color: Color.Red);

            // ARRANGE
            var sprite1 = CreateSprite(source1, origin);
            var sprite2 = CreateSprite(source2, origin);
            var spriteComposite = new SpriteComposite("tata", new[] { sprite1, sprite2 })
            {
                Position = new Vector(12, 8),
                Rotation = 4.2f,
                Scale = 0.5f,
                Color = Color.Red
            };

            DrawImageParams actual1 = null;
            DrawImageParams actual2 = null;
            var callCount = 0;
            var drawContext = CreateDrawContextMock(p =>
            {
                callCount++;
                if (callCount == 1) actual1 = p;
                if (callCount == 2) actual2 = p;
            });

            // ACT
            spriteComposite.Draw(drawContext.Object, SpriteTransform.SpriteIdentity);

            // ASSERT
            drawContext.VerifyAll();
            Assert.AreEqual(2, callCount);
            AssertDrawImageParamsAreEquals(expected1, actual1);
            AssertDrawImageParamsAreEquals(expected2, actual2);
        }
        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);
        }
예제 #12
0
        public void TestSpriteDrawWithScaleAndTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);
            var transform = new SpriteTransform(scale: 0.25f);

            var expected = CreateDrawImageParams(source, origin,
                destination: new Rectangle(0, 0, 40, 40));

            // ARRANGE
            var sprite = CreateSprite(source, origin);
            sprite.Scale = 8.0f;

            DrawImageParams actual = null;
            var drawContext = CreateDrawContextMock(p => actual = p);

            // ACT
            sprite.Draw(drawContext.Object, transform);

            // ASSERT
            drawContext.VerifyAll();
            AssertDrawImageParamsAreEquals(expected, actual);
        }
예제 #13
0
        public void TestSpriteDrawWithRotationAndIdentityTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);

            var expected = CreateDrawImageParams(source, origin,
                destination: new Rectangle(0, 0, 20, 20), rotation: 12.345f);

            // ARRANGE
            var sprite = CreateSprite(source, origin);
            sprite.Rotation = 12.345f;

            DrawImageParams actual = null;
            var drawContext = CreateDrawContextMock(p => actual = p);

            // ACT
            sprite.Draw(drawContext.Object, SpriteTransform.SpriteIdentity);

            // ASSERT
            drawContext.VerifyAll();
            AssertDrawImageParamsAreEquals(expected, actual);
        }
예제 #14
0
        public void TestSpriteDrawWithPositionAndTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);
            var transform = new SpriteTransform(translation: new Vector(11, 13));

            var expected = CreateDrawImageParams(source, origin, destination: new Rectangle(-1, 37, 20, 20));

            // ARRANGE
            var sprite = CreateSprite(source, origin);
            sprite.Position = new Vector(-12, 24);

            DrawImageParams actual = null;
            var drawContext = CreateDrawContextMock(p => actual = p);

            // ACT
            sprite.Draw(drawContext.Object, transform);

            // ASSERT
            drawContext.VerifyAll();
            AssertDrawImageParamsAreEquals(expected, actual);
        }
예제 #15
0
 public TiledScreen(ScreenNavigation screenNavigation)
 {
     this.screenNavigation = screenNavigation;
     this.position = Vector.Zero;
 }
예제 #16
0
 public void BindController(IInputMapper inputConfiguration)
 {
     inputConfiguration.GetDigitalButton("Left").MapTo(gt => this.shipVelocity = shipVelocity.Translate(gt.ElapsedSeconds * -250, 0));
     inputConfiguration.GetDigitalButton("Right").MapTo(gt => this.shipVelocity = shipVelocity.Translate(gt.ElapsedSeconds * 250, 0));
     inputConfiguration.GetDigitalButton("Up").MapTo(gt => this.shipVelocity = shipVelocity.Translate(0, gt.ElapsedSeconds * -150));
     inputConfiguration.GetDigitalButton("Down").MapTo(gt => this.shipVelocity = shipVelocity.Translate(0, gt.ElapsedSeconds * 150));
     inputConfiguration.GetDigitalButton("Fire Weapon").MapTo(this.FireWeapon);
 }
예제 #17
0
        private static Sprite CreateSprite(SpriteSheet spriteSheet, RectangleInt source, Vector origin)
        {
            spriteSheet.AddSpriteDefinition("toto", source, origin);
            var sprite = new Sprite(spriteSheet, "toto");

            return sprite;
        }
예제 #18
0
 public static BulletEntity Create(SpriteLayer entityLayer, SpriteSheet shipSheet, Vector position, Vector velocity)
 {
     return new BulletEntity(entityLayer, shipSheet, position, velocity);
 }
예제 #19
0
 public static float GetDistance(Vector first, Vector second)
 {
     return MathUtil.CalcHypotenuse(Math.Abs(first.X - second.X), Math.Abs(first.Y - second.Y));
 }
예제 #20
0
 public static float GetAngle(Vector first, Vector second)
 {
     var deltaX = second.X - first.X;
     var deltaY = second.Y - first.Y;
     return (float)Math.Atan2(deltaY, deltaX);
 }
예제 #21
0
        public void DrawRectangle(DrawLineParams param)
        {
            var bottomLeft = new Vector(param.VectorFrom.X, param.VectorTo.Y);
            var topRight = new Vector(param.VectorTo.X, param.VectorFrom.Y);

            this.DrawLine(new DrawLineParams { VectorFrom = param.VectorFrom, VectorTo = topRight, Color = param.Color, Width = param.Width });
            this.DrawLine(new DrawLineParams { VectorFrom = topRight, VectorTo = param.VectorTo, Color = param.Color, Width = param.Width });
            this.DrawLine(new DrawLineParams { VectorFrom = param.VectorTo, VectorTo = bottomLeft, Color = param.Color, Width = param.Width });
            this.DrawLine(new DrawLineParams { VectorFrom = bottomLeft, VectorTo = param.VectorFrom, Color = param.Color, Width = param.Width });
        }
예제 #22
0
 public void SetRenderTarget(IPreDrawable painter, Vector size)
 {
     this.drawImplementation.SetRenderTarget(painter, size);
 }
예제 #23
0
 private void MoveCamera(float offsetX, float offsetY)
 {
     this.position += new Vector(offsetX, offsetY);
     this.Camera.MoveTo((int)Math.Round(this.position.X), (int)Math.Round(this.position.Y));
 }
예제 #24
0
 private static DrawImageParams CreateDrawImageParams(RectangleInt source, Vector origin,
     Texture texture = null, Rectangle? destination = null, float rotation = 0.0f, Color? color = null)
 {
     var expected = new DrawImageParams
     {
         Texture = texture,
         Source = source,
         Destination = destination.HasValue ? destination.Value : Rectangle.Empty,
         Rotation = rotation,
         Origin = origin,
         Color = color.HasValue ? color.Value : Color.White,
         ImageEffect = ImageEffect.None
     };
     return expected;
 }
예제 #25
0
 private static Sprite CreateSprite(RectangleInt source, Vector origin)
 {
     var spriteSheet = new SpriteSheet(null, "toto");
     return CreateSprite(spriteSheet, source, origin);
 }
예제 #26
0
        public void TestSpriteDrawWithColorAndTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);
            var transform = new SpriteTransform(color: new Color(128, 128, 128, 128));

            var expected = CreateDrawImageParams(source, origin,
                destination: new Rectangle(0, 0, 20, 20), color: new Color(111, 122, 94, 72));

            // ARRANGE
            var sprite = CreateSprite(source, origin);
            sprite.Color = new Color(222, 244, 188, 144);

            DrawImageParams actual = null;
            var drawContext = CreateDrawContextMock(p => actual = p);

            // ACT
            sprite.Draw(drawContext.Object, transform);

            // ASSERT
            drawContext.VerifyAll();
            AssertDrawImageParamsAreEquals(expected, actual);
        }
예제 #27
0
        public void Update(IGameTiming gameTime)
        {
            var velocityClampRectangle = Rectangle.FromBound(-8, -5, 8, 5);
            var gameAreaClampRectangle = Rectangle.FromBound(100, 10, 700, 480 - 32);

            this.shipVelocity = this.shipVelocity.Clamp(velocityClampRectangle);
            this.shipPosition = this.shipPosition.Translate(this.shipVelocity).Clamp(gameAreaClampRectangle);

            this.SpriteReference.Position = this.shipPosition;

            this.shipVelocity = Vector.Zero;

            foreach (var bullet in bullets)
            {
                bullet.Update(gameTime);
            }
        }
예제 #28
0
        public void TestSpriteDrawWithDefaultValuesAndTransform()
        {
            var source = new RectangleInt(10, 10, 20, 20);
            var origin = new Vector(4, 7);
            var transform = new SpriteTransform(translation: new Vector(3, 5), rotation: HalfPI, scale: 0.5f, color: Color.Blue);

            var expected = CreateDrawImageParams(source, origin,
                destination: new Rectangle(3, 5, 10, 10), rotation: HalfPI, color: Color.Blue);

            // ARRANGE
            var sprite = CreateSprite(source, origin);

            DrawImageParams actual = null;
            var drawContext = CreateDrawContextMock(p => actual = p);

            // ACT
            sprite.Draw(drawContext.Object, transform);

            // ASSERT
            drawContext.VerifyAll();
            AssertDrawImageParamsAreEquals(expected, actual);
        }
예제 #29
0
 public bool Equals(Vector other)
 {
     return this.x - other.x < Epsilon && this.y - other.y < Epsilon;
 }
예제 #30
0
        public void SetRenderTarget(IPreDrawable painter, Vector size)
        {
            if (!this.renderTargetDictionary.TryGetValue(painter, out this.renderTargetWrap))
            {
                this.renderTargetWrap = new RenderTargetWrap();
                this.renderTargetDictionary.Add(painter, this.renderTargetWrap);
            }

            if (size.X > this.renderTargetWrap.ActualSize.X || size.Y > this.renderTargetWrap.ActualSize.Y)
            {
                this.renderTargetWrap.ActualSize = new Vector((int)(size.X * 1.2f), (int)(size.Y * 1.2f));

                this.renderTargetWrap.RenderTarget2D = new RenderTarget2D(this.spriteBatch.GraphicsDevice,
                    (int)this.renderTargetWrap.ActualSize.X, (int)this.renderTargetWrap.ActualSize.Y);
            }

            this.renderTargetWrap.Size = size;

            this.spriteBatch.GraphicsDevice.SetRenderTarget(this.renderTargetWrap.RenderTarget2D);

            this.SpriteBatchBegin();
            this.spriteBatch.GraphicsDevice.Clear(GameFramework.Color.Transparent.ToXnaColor());
        }