Exemplo n.º 1
0
        private static Message ChaseTarget(Tile target, Tile chaser)
        {
            // The chaser will try to go in the directions that is closest to the player.

            // TODO: diff is a bad name but I can't think of a better on
            var diff = chaser.Location.NormalizedVector(target.Location);

            // diff suggests either go vertically or horizontally. Which is better?

            var verticalMove = chaser.Location + new Point(0, diff.Y);
            var horizontalMove = chaser.Location + new Point(diff.X, 0);

            if ( GetVectorLengthPointPoint(verticalMove, target.Location) < GetVectorLengthPointPoint(horizontalMove, target.Location) )
            {
                // TODO: should this check for a collision on this move? If there is collision should monster try the horizontal move?
                return chaser.BuildMoveMessage(verticalMove);
            }

            if (GetVectorLengthPointPoint(horizontalMove, target.Location) < GetVectorLengthPointPoint(verticalMove, target.Location) )
            {
                // TODO: should this check for a collision on this move? If there is collision should monster try the vertical move?
                return chaser.BuildMoveMessage(horizontalMove);
            }

            // if either is as good then, y is preferable to x
            // TODO: Make this better, it's pretty atbitrary.
            var direction = diff.Y != 0 ? new Point(0, diff.Y) : new Point(diff.X, 0);
            return chaser.BuildMoveMessage(chaser.Location + direction);
        }
Exemplo n.º 2
0
        private static Message ChaseTarget(Tile target, Tile chaser, TileSet statics)
        {
            var movementVector = chaser.Location.NormalizedVector(target.Location);

            var bestMove = new[]{
                chaser.Location + new Point(0, movementVector.Y), //vertical
                chaser.Location + new Point(movementVector.X, 0)} //horizontal
                    .OrderBy(m => m.Distance(target.Location)) // order on distance from player
                    .FirstOrDefault(m => !statics.CollisionDetection(chaser, m).Collision); //pick first where there is no collision

            return bestMove != default(Point)
                ? chaser.BuildMoveMessage(bestMove)
                : null;
        }
Exemplo n.º 3
0
        private Message MovePlayer(Tile player, ControllerMessage message)
        {
            if (player.IsActive)
            {
                var destination = InputMap.Transform(message.Body, player.Location, 1);
                return player.BuildMoveMessage(destination);
            }

            return null;
        }