public async Task TestHitDetection() => await PhysicsTest.Test(50, 25, TestContext, async (app, stPanel) => { for (var x = 4; x < SpaceTime.CurrentSpaceTime.Width - 4; x += 10) { for (var y = 4; y < SpaceTime.CurrentSpaceTime.Height - 4; y += 5) { var e1 = SpaceTime.CurrentSpaceTime.Add(new SpacialElement(x: x, y: y, w: 2, h: 1) { BackgroundColor = RGB.Gray }); } } var eye = SpaceTime.CurrentSpaceTime.Add(new SpacialElement(x: 0, y: 12, w: 2, h: 1) { BackgroundColor = RGB.Red }); var lines = new List <SpacialElement>(); while (eye.Right() < SpaceTime.CurrentSpaceTime.Width) { eye.MoveBy(.05f, 0); foreach (var line in lines) { line.Lifetime.Dispose(); } ; lines.Clear(); foreach (var obstacle in SpaceTime.CurrentSpaceTime.Elements.Where(e => e != eye).ToArray()) { obstacle.BackgroundColor = RGB.Gray; obstacle.SizeOrPositionChanged.Fire(); } foreach (var obstacle in SpaceTime.CurrentSpaceTime.Elements.Where(e => e != eye).ToArray()) { var angle = eye.Center().CalculateAngleTo(obstacle.Center()); var los = HitDetection.HasLineOfSight(eye, obstacle); if (los) { obstacle.BackgroundColor = RGB.Green; obstacle.SizeOrPositionChanged.Fire(); } } await Time.CurrentTime.YieldAsync(); } });
public async Task TestProjectilesHitTargets() => await PhysicsTest.Test(50, 25, TestContext, async (app, stPanel) => { var player = SpaceTime.CurrentSpaceTime.Add(new MainCharacter()); player.Velocity.Angle = 0; player.MoveTo(25, 12); for (var a = 0; a < 360; a += 5) { player.Velocity.Angle = a; var target = SpaceTime.CurrentSpaceTime.Add(new Character()); var loc = player.MoveTowards(a, 12); target.MoveTo(loc.Left, loc.Top); target.AddTag("enemy"); await Time.CurrentTime.DelayAsync(100); using (var testLifetime = new Lifetime()) { var succes = false; Velocity.GlobalImpactOccurred.SubscribeForLifetime(i => { if (succes == true) { Assert.Fail("Success already happened"); } if (i.MovingObject is Projectile && i.ObstacleHit == target) { succes = true; } }, testLifetime); player.Inventory.Items.Add(new Pistol() { AmmoAmount = 1 }); player.Inventory.PrimaryWeapon.TryFire(false); await Time.CurrentTime.DelayAsync(2000); Assert.IsTrue(succes); target.Lifetime.Dispose(); } } });
public async Task TestHitDetectionSmallOverlaps() => await PhysicsTest.Test(50, 25, TestContext, async (app, stPanel) => { var e1 = SpaceTime.CurrentSpaceTime.Add(new SpacialElement(1, 1, 5, 5)); var e2 = SpaceTime.CurrentSpaceTime.Add(new SpacialElement(1, 1, 5.9f, 5.9f)); var expectHit = HitDetection.PredictHit(new HitDetectionOptions() { MovingObject = e2, Obstacles = new SpacialElement[] { e1 }, Angle = 225, Visibility = 10 }); Assert.AreEqual(e1, expectHit.ObstacleHit); var expectMiss = HitDetection.PredictHit(new HitDetectionOptions() { MovingObject = e2, Obstacles = new SpacialElement[] { e1 }, Angle = 45, Visibility = 10 }); Assert.AreEqual(null, expectMiss.ObstacleHit); });
public async Task TestCantGoThroughWallsDown() => await PhysicsTest.Test(50, 25, TestContext, async (app, stPanel) => { await TestCantGoThroughWalls(Direction.Down, app, stPanel); });