Example #1
0
        /// <summary>
        /// Use on the teleporter to teleport pac
        /// </summary>
        /// <param name="pac">The pac to teleport</param>
        /// <returns>Return <c>true</c> if one of the teleporter teleport pac.
        /// </returns>
        /// <seealso cref="Teleporter.Teleport(Pac)"/>
        public bool Teleport(Pac pac)
        {
            bool result = false;

            for (int i = 0, maxi = Teleporters.Count; i < maxi && !result; i++)
            {
                result = Teleporters[i].Teleport(pac);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Teleport <paramref name="pac"/> from <see cref="Position1"/> to
        /// <see cref="Position2"/> is pac's position if <see cref="Position1"/>
        /// or vice-versa.
        /// </summary>
        /// <param name="pac">The pac to teleport</param>
        /// <returns>Return <c>true</c> if pac did teleport, <c>false</c> otherwise</returns>
        /// <exception cref="TeleporterException">Thrown when the teleporte has not <see cref="Position2"/> set.</exception>
        public bool Teleport(Pac pac)
        {
            bool result = false;

            if (Activated)
            {
                if (Position2.Equals(new Vector2(-1, -1)))
                {
                    throw new TeleporterException("Teleporter destination has not been configured yet. Where does pac will go without a destination? Nowhere!");
                }

                if (pac.ConvertPositionToTileIndexes().Equals(Position1))
                {
                    pac.Position = new Vector2(Position2.X * Maze.SPRITE_DIMENSION, Position2.Y * Maze.SPRITE_DIMENSION);
                    result       = true;
                }
                else if (pac.ConvertPositionToTileIndexes().Equals(Position2))
                {
                    pac.Position = new Vector2(Position1.X * Maze.SPRITE_DIMENSION, Position1.Y * Maze.SPRITE_DIMENSION);
                    result       = true;
                }
            }

            // If pacman has been teleported, disable the teleporter until pac is away from the teleporter point
            // (this method prevent pac from teleporting in an infinite loop from one spot to another)
            if (result)
            {
                Activated = false;
                Vector2 currentPosition = pac.Position;
                pac.OnPositionChangedAction = (Vector2 position) =>
                {
                    if (!pac.ConvertPositionToTileIndexes().Equals(pac.ConvertPositionToTileIndexes(currentPosition)))
                    {
                        Activated = true;

                        // Delete the delegate
                        pac.OnPositionChangedAction = (Vector2 pos) => { };
                    }
                };
            }

            return(result);
        }