/// <summary> /// Converts the source and destionation from board coordinates to array coordinates and attempts to move it from /// the source to the destination leaving an empty tile behind /// </summary> /// <param name="boardName">The board to place within</param> /// <param name="source">Source position in board space</param> /// <param name="destination">Destination position in board space</param> /// <returns></returns> public void MoveObjectAtPosition(string boardName, Vector2 source, Vector2 destination) { var board = GetBoard(boardName); var sourcePosition = boardPositionConverter.FromBoardCoordinates(source, board); var destinationPosition = boardPositionConverter.FromBoardCoordinates(destination, board); if (!positionValidator.IsPositionValid(board, destinationPosition)) { throw new RobotOutOfBoundsException(destination); } var objectAtSource = board.BoardItems[sourcePosition.X, sourcePosition.Y]; board.BoardItems[destinationPosition.X, destinationPosition.Y] = objectAtSource; objectAtSource.Position = destination; board.BoardItems[sourcePosition.X, sourcePosition.Y] = new DefaultBoardItem(source); }
public bool MoveForward(string boardName) { var robot = GetRobot(boardName); var board = boardService.GetBoard(boardName); var directionVector = Vector2.MatrixFromDirection(robot.Direction); var newLocation = directionVector + robot.Position; if (!positionValidator.IsPositionValid(board, newLocation)) { throw new RobotOutOfBoundsException(newLocation); } boardService.MoveObjectAtPosition(boardName, robot.Position, newLocation); return(true); }
//int lastUsedPos; //Replaced by GetRandomPosition //public Transform GetRandomMapItemGenPos() //{ // int randIndex = UnityEngine.Random.Range(0, mapItemGenPos.Count); // if(randIndex == lastUsedPos) // { // //Debug.Log("Select another pos"); // randIndex = randIndex++ % mapItemGenPos.Count; // } // lastUsedPos = randIndex; // return mapItemGenPos[randIndex]; //} /// <summary> /// Finds random position on map and returns it if the pCondition is met /// </summary> public Vector2?GetRandomPosition(IPositionValidator pCondition = null, int pIteration = 0) { if (pCondition == null) { pCondition = this; } if (DebugData.TestGenerateItems) { return(debug_GetRandomPosition()); } Vector2 topLeft = TopLeftCorner.position; Vector2 botRight = BotRightCorner.position; Vector2 randomPos = new Vector2(Random.Range(topLeft.x, botRight.x), Random.Range(topLeft.y, botRight.y)); Utils.DebugDrawCross(randomPos, Color.red, 1); if (pCondition.IsPositionValid(randomPos)) { return(randomPos); } const int max_iterations = 20; if (pIteration > max_iterations) { //not error - this means that there are probably too much items on map already // => no need to generate more Debug.Log("Couldnt find good position to generate item"); return(null); } return(GetRandomPosition(pCondition, pIteration + 1)); }