Inheritance: PhysicalEntity, IForwardMovablePhysicalEntity
Example #1
0
        public Character(
            string tilesetName,
            int tileId,
            string name,
            Vector2 position,
            Vector2 size,
            float direction,
            Type shapeType = null
            )
            : base(tilesetName, tileId, name)
        {
            shapeType = shapeType ?? typeof(CircleShape);
            ConstructorInfo ctor = shapeType.GetConstructor(new[] {typeof(Vector2), typeof(Vector2) });

            if (ctor == null)
            {
                throw new Exception("Class " + shapeType.FullName + " has no constructor " + shapeType.Name +
                                    "(Vector2 v), " +
                                    "hence Character cannot create his PhysicalEntity Shape.");
            }

            Rotation = direction;

            Shape shape = (Shape)ctor.Invoke(new object[] { position, size });
            PhysicalEntity = new ForwardMovablePhysicalEntity(position, shape, direction: direction);
        }
        public void ResolveSlideCollissionOrthogonal()
        {
            var initPosition = new Vector2(0.5f,0f);
            var shape = new RectangleShape(initPosition, new Vector2(1,1));
            IForwardMovablePhysicalEntity pe = new ForwardMovablePhysicalEntity(initPosition, shape, 1, MathHelper.Pi / 2);
            pe.InelasticCollision = true;

            ICollisionChecker collisionChecker = new CollisionCheckerMock(new List<IPhysicalEntity>() {pe});
            var movementPhysics = new MovementPhysics();
            ICollisionResolver collisionResolver = new MomentumCollisionResolver(collisionChecker, movementPhysics);

            movementPhysics.Move(pe);

            collisionResolver.ResolveCollisions();

            Assert.Equal(pe.Position.X, 0, 2);
            Assert.Equal(pe.Position.Y, 0, 2);
        }
        public void ResolveSlideCollission45()
        {
            var initPosition = new Vector2(0.5f, 0f);
            var shape = new RectangleShape(initPosition, new Vector2(1, 1));

            float angle = 135f;

            var angleRads = MathHelper.ToRadians(angle);
            IForwardMovablePhysicalEntity pe = new ForwardMovablePhysicalEntity(initPosition, shape, 1, angleRads)
                {InelasticCollision = true};

            ICollisionChecker collisionChecker = new CollisionCheckerMock(new List<IPhysicalEntity>() {pe});
            var movementPhysics = new MovementPhysics();
            ICollisionResolver collisionResolver = new MomentumCollisionResolver(collisionChecker, movementPhysics);

            movementPhysics.Move(pe);

            collisionResolver.ResolveCollisions();

            Assert.Equal(pe.Position.X, 0f, 2);
            Assert.Equal(pe.Position.Y, -0.71f, 2);
        }