Exemplo n.º 1
0
        private CommandResult ProcessDestinationCoordinate(Context context, Board board, Coordinate destinationCoordinate)
        {
            Coordinate currentCoordinate = context.Player.Coordinate;
            BoardExitDirection boardExitDirection = GetBoardExitDirection(currentCoordinate, destinationCoordinate);
            BoardExit boardExit = board.Exits.SingleOrDefault(arg => arg.Coordinate == currentCoordinate && arg.Direction == boardExitDirection);

            if (boardExit != null)
            {
                Board destinationBoard = context.GetBoardById(boardExit.DestinationBoardId);

                context.EnqueueCommand(new BoardChangeCommand(destinationBoard, boardExit.DestinationCoordinate));

                return CommandResult.Succeeded;
            }

            if (!board.CoordinateIntersects(destinationCoordinate))
            {
                return CommandResult.Failed;
            }

            ActorInstance targetActorInstance = board.ActorInstanceLayer[destinationCoordinate];

            if (targetActorInstance != null)
            {
                TouchDirection? touchDirection = GetTouchDirection(currentCoordinate, destinationCoordinate);

                if (touchDirection != null)
                {
                    context.RaiseEvent(targetActorInstance.OnTouchedByPlayer, new PlayerTouchedActorInstanceEvent(targetActorInstance, touchDirection.Value));
                }
            }

            return context.Player.ChangeLocation(context.CurrentBoard, destinationCoordinate) ? CommandResult.Succeeded : CommandResult.Failed;
        }
        private CommandResult MoveActorInstance(Context context, Coordinate destinationCoordinate)
        {
            Board board = context.GetBoardById(_actorInstance.BoardId);

            if (!board.CoordinateIntersects(destinationCoordinate))
            {
                return CommandResult.Failed;
            }

            Coordinate currentCoordinate = _actorInstance.Coordinate;
            ActorInstance targetActorInstance = board.ActorInstanceLayer[destinationCoordinate];
            TouchDirection? touchDirection = GetTouchDirection(currentCoordinate, destinationCoordinate);

            if (targetActorInstance != null)
            {
                if (touchDirection != null)
                {
                    context.RaiseEvent(targetActorInstance.OnTouchedByActorInstance, new ActorInstanceTouchedActorInstanceEvent(_actorInstance, targetActorInstance, touchDirection.Value));
                }

                return CommandResult.Failed;
            }
            if (context.Player.Coordinate == destinationCoordinate)
            {
                if (touchDirection != null)
                {
                    context.RaiseEvent(context.Player.OnTouchedByActorInstance, new ActorInstanceTouchedPlayerEvent(_actorInstance, context.Player, touchDirection.Value));
                }

                return CommandResult.Failed;
            }
            if (!_actorInstance.ChangeCoordinate(context.CurrentBoard, context.Player, destinationCoordinate))
            {
                return CommandResult.Failed;
            }

            return context.RaiseEvent(_actorInstance.OnMoved, new ActorInstanceMovedEvent(_actorInstance, destinationCoordinate)) == EventResult.Canceled ? CommandResult.Failed : CommandResult.Succeeded;
        }