private ShootDirection GetShootDirection(Direction toDirection) { ShootDirection result; var moveDirection = UtilMapHelpers.GetDirectionVector(toDirection); //Calculate the length of move direction float shootsqrMagnitude = moveDirection.sqrMagnitude; float x, y; //Calculate the right side vector first if (moveDirection.x == 0) { y = 0; x = Mathf.Sqrt(shootsqrMagnitude - y * y); } else if (moveDirection.y == 0) { x = 0; y = Mathf.Sqrt(shootsqrMagnitude - x * x); } else { x = Mathf.Sqrt(shootsqrMagnitude / (1 + Mathf.Pow(moveDirection.x / moveDirection.y, 2))); y = -(moveDirection.x / moveDirection.y) * x; } result.leftSide = new Vector2((int)x, (int)y); result.rightSide = -result.leftSide; return(result); }
void InitValues() { BackgroundSize = UtilMapHelpers.CalculateBackgroundSize(backgroundSR, backgroundMap.transform.lossyScale); TileSize = UtilMapHelpers.CalculateCellSize(BackgroundSize); centerNumber = (int)Mathf.Sqrt(CommonConstants.NUMBER_OF_CELLS) / 2; centerPosition = backgroundMap.transform.position; maxWhirlpoolCount = GameSessionInfoManager.Instance.gameMode == GameMode.Easy ? 4 : 3; //Whirldpools if (whirlpoolPosition.Count == 0) { //Top left Vector2 pos = new Vector2(centerPosition.x - BackgroundSize.x / 2 + TileSize.x / 2, centerPosition.y + BackgroundSize.y / 2 - TileSize.y / 2); whirlpoolPosition.Add(pos); //Top right pos = new Vector2(centerPosition.x + BackgroundSize.x / 2 - TileSize.x / 2, centerPosition.y + BackgroundSize.y / 2 - TileSize.y / 2); whirlpoolPosition.Add(pos); pos = new Vector2(centerPosition.x - BackgroundSize.x / 2 + TileSize.x / 2, centerPosition.y - BackgroundSize.y / 2 + TileSize.y / 2); whirlpoolPosition.Add(pos); //Bottom right pos = new Vector2(centerPosition.x + BackgroundSize.x / 2 - TileSize.x / 2, centerPosition.y - BackgroundSize.y / 2 + TileSize.y / 2); whirlpoolPosition.Add(pos); } }
//Check for the position which enemy attend to move is movable (Obstacle or other Enemy boat placed on it) private bool IsMovable(Direction dir) { //Calculate the attended move position Vector2 attendedMovePos = (Vector2)transform.position + UtilMapHelpers.GetDirectionVector(dir) * MapConstantProvider.Instance.TileSize; //Debug.Log("Attend direction: " + dir); return(!MapConstantProvider.Instance.ContainsPosInUnitDictionary(attendedMovePos)); }
private float GetDeltaAngle(Direction currentDirection, Direction toDirection) { float angle = 0f; if (currentDirection == toDirection) { return(angle); } angle = CalculateDeltaAngle(UtilMapHelpers.GetDirectionAngle(currentDirection), UtilMapHelpers.GetDirectionAngle(toDirection)); return(angle); }
void InitPossiblePositions() { int row = (int)Mathf.Sqrt(CommonConstants.NUMBER_OF_CELLS); int col = row; for (int irow = 0; irow < row; irow++) { for (int icol = 0; icol < col; icol++) { Vector2 pos = new Vector2( (centerPosition.x + UtilMapHelpers.GetHorizontalSign(icol, centerNumber) * (TileSize.x * Mathf.Abs(centerNumber - icol))) + TileSize.x / 2, (centerPosition.y + UtilMapHelpers.GetVerticalSign(irow, centerNumber) * (TileSize.y * Mathf.Abs(centerNumber - irow))) - TileSize.y / 2); possiblePositions.Add(pos); } } }
protected override Direction CalculateNextDirection() { offset = targetTrans.position - transform.position; //Find the min angle between the offset vector and eight direction float minAngle = Vector2.Angle(offset, CommonConstants.DIRECTION_VECTORS[0]); int minIndex = 0; for (int i = 1; i < CommonConstants.DIRECTION_VECTORS.Length; i++) { float angle = Vector2.Angle(offset, CommonConstants.DIRECTION_VECTORS[i]); if (angle <= minAngle) { minAngle = angle; minIndex = i; } } return(UtilMapHelpers.VectorToDirection(CommonConstants.DIRECTION_VECTORS[minIndex])); }
protected override Direction CalculateNextDirection() { offset = targetTrans.position - transform.position; //Init cache value Vector2[] directionVectors = CommonConstants.DIRECTION_VECTORS; int directionVectorsLength = directionVectors.Length; //Find the movable neighbor direction of enemy List <Vector2> movableDirection = new List <Vector2>(); for (int i = 0; i < directionVectorsLength; i++) { if (IsMovable(directionVectors[i])) { //Debug.Log("Movable direction: " + UtilMapHelpers.VectorToDirection(directionVectors[i])); movableDirection.Add(directionVectors[i]); } } //Find the min direction in the movable direction vector list float minAngle = Vector2.Angle(offset, movableDirection[0]); int minIndex = 0; for (int i = 1; i < movableDirection.Count; i++) { float angle = Vector2.Angle(offset, movableDirection[i]); if (angle <= minAngle) { minAngle = angle; minIndex = i; } } //Convert vector to direction return(UtilMapHelpers.VectorToDirection(movableDirection[minIndex])); }
protected void MoveAndRotate(Direction dir) { if (BoatState == BoatState.MoveAndRotate) { return; } DataObjectMove obj = new DataObjectMove(Type, (Vector2)transform.position, transform.rotation, isometricModel.transform.localRotation, currentDirection); allPreviousMoves.Push(obj); //Get input: target position, target direction targetDirection = dir; targetPosition = (Vector2)transform.position + MapConstantProvider.Instance.TileSize * UtilMapHelpers.GetDirectionVector(targetDirection); PlayMovementSound(); if (moveAndRotateCR != null) { StopCoroutine(moveAndRotateCR); } moveAndRotateCR = StartCoroutine(CR_MoveAndRotate(targetPosition, targetDirection, () => OnCompletedRotateAndMove())); }