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); }
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); }
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; }
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); } }
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; }
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); }
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); }
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); }
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); }
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); }
public TiledScreen(ScreenNavigation screenNavigation) { this.screenNavigation = screenNavigation; this.position = Vector.Zero; }
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); }
private static Sprite CreateSprite(SpriteSheet spriteSheet, RectangleInt source, Vector origin) { spriteSheet.AddSpriteDefinition("toto", source, origin); var sprite = new Sprite(spriteSheet, "toto"); return sprite; }
public static BulletEntity Create(SpriteLayer entityLayer, SpriteSheet shipSheet, Vector position, Vector velocity) { return new BulletEntity(entityLayer, shipSheet, position, velocity); }
public static float GetDistance(Vector first, Vector second) { return MathUtil.CalcHypotenuse(Math.Abs(first.X - second.X), Math.Abs(first.Y - second.Y)); }
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); }
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 }); }
public void SetRenderTarget(IPreDrawable painter, Vector size) { this.drawImplementation.SetRenderTarget(painter, size); }
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)); }
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; }
private static Sprite CreateSprite(RectangleInt source, Vector origin) { var spriteSheet = new SpriteSheet(null, "toto"); return CreateSprite(spriteSheet, source, origin); }
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); }
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); } }
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); }
public bool Equals(Vector other) { return this.x - other.x < Epsilon && this.y - other.y < Epsilon; }
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()); }