Exemplo n.º 1
0
        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();
            }
        });
Exemplo n.º 2
0
        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();
                }
            }
        });
Exemplo n.º 3
0
        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);
        });
Exemplo n.º 4
0
        private async Task TestCantGoThroughWalls(Direction d, CliTestHarness app, SpaceTimePanel stPanel)
        {
            SpacialAwareness.OnNudge.SubscribeForLifetime((ev) => Assert.Fail("Nudging not allowed"), app);
            var st   = stPanel.SpaceTime;
            var wall = st.Add(new SpacialElement()
            {
                BackgroundColor = RGB.DarkRed
            });
            ILocationF movingObjectLocation;
            float      movementAngle;
            float      expected;
            Func <SpacialElement, float> actual;

            if (d == Direction.Right)
            {
                movingObjectLocation = LocationF.Create((int)(st.Width * .25f), st.Height * .5f - .5f);
                movementAngle        = 0;
                wall.ResizeTo(.1f, st.Height);
                wall.MoveTo((int)(st.Width * .75f), 0);

                expected = wall.Left;
                actual   = m => m.Right();
            }
            else if (d == Direction.Left)
            {
                movingObjectLocation = LocationF.Create((int)(st.Width * .75f), st.Height * .5f - .5f);
                movementAngle        = 180;
                wall.ResizeTo(.1f, st.Height);
                wall.MoveTo((int)(st.Width * .25f) - .1f, 0);

                expected = wall.Right();
                actual   = m => m.Left;
            }
            else if (d == Direction.Up)
            {
                movingObjectLocation = LocationF.Create(st.Width * .5f - .5f, (int)(st.Height * .75f));
                movementAngle        = 270;
                wall.ResizeTo(st.Width, .1f);
                wall.MoveTo(0, (int)(st.Height * .25f) - .1f);

                expected = wall.Bottom();
                actual   = m => m.Top;
            }
            else if (d == Direction.Down)
            {
                movingObjectLocation = LocationF.Create(st.Width * .5f - .5f, (int)(st.Height * .25f));
                movementAngle        = 90;
                wall.ResizeTo(st.Width, .1f);
                wall.MoveTo(0, (int)(st.Height * .75f));

                expected = wall.Top;
                actual   = m => m.Bottom();
            }
            else
            {
                throw new NotSupportedException();
            }

            for (var speed = 5; speed < 1000; speed *= 2)
            {
                Console.WriteLine($"Speed: {speed}");
                var movingObject = st.Add(new SpacialElement(1, 1, movingObjectLocation.Left, movingObjectLocation.Top)
                {
                    BackgroundColor = RGB.Blue
                });
                var v = new Velocity(movingObject);
                await st.DelayAsync(500);

                v.Angle = movementAngle;
                v.Speed = speed;
                await st.DelayAsync(20000);

                PhysicsTest.AssertClose(expected, actual(movingObject), .2f);
                Console.WriteLine($"Wall.Left == {expected}, movingObject.Right() == {actual(movingObject)}");
                movingObject.Lifetime.Dispose();
            }
        }
Exemplo n.º 5
0
 public async Task TestCantGoThroughWallsDown() => await PhysicsTest.Test(50, 25, TestContext, async (app, stPanel) =>
 {
     await TestCantGoThroughWalls(Direction.Down, app, stPanel);
 });