protected override void Move() { step = moveSpeed * Time.deltaTime; if (disableMovement) { return; } FlankInfo flankInfo = spawnManager.GetFlankInfo(GetPosition()); if (canMove) { TryMoveFlankingTowards(_moveToTarget, flankInfo.flankTarget, step); TryMoveFlankingTowards(_moveToPosition, flankInfo.flankPosition, step); TryMoveFlankingTowards(_moveToPlayer, finalPosition, step); } else { // If enemy finished moving, needs to start attacking. if (!attackStarted) { startAttacking.Invoke(); attackStarted = true; } } CheckIfReached(ref _moveToTarget, flankInfo.flankTarget, ref _moveToPosition); CheckIfReached(ref _moveToPosition, flankInfo.flankPosition, ref _moveToPlayer); canMove = !(Vector3.Distance(transform.position, finalPosition) < minDistanceToTargetPosition && _moveToPlayer); }
/// <summary> /// Attempt to place a piece on the specific co-ordinate /// </summary> /// <param name="x">The X value of the cell to place on</param> /// <param name="y">The Y value of the cell to place on</param> private void AttemptMove(int x, int y) { // For purposes of this, n is the average width/height of the grid. // Test O(1) if (!playing) { MustBePlayingError(); return; } // Move must follow these rules: // 0) The square you place in must not have a piece already in it // 1) Must be adjacent to an opponents piece // 2) Must outflank an opponents piece (and by extension, be able to flip them) // 3) If outflanks in multiple directions, all flip // 4) If no moves are available you must pass. // 5) If moves are availble, you can't pass. // 6) If neither player has avalable moves, the game ends. // To make sure i'm checking in a decent // O(1) lookup on an array if (GetCell(x, y) != CellValues.blank) { // Breaks rule 0, there's somethign already there Speak("You can't place a token there, the space is already occupied."); return; } // O(9) checks all 9 squares in a 3x3 for opponents (including it's own square) if (!OpponentAdjacent(x, y)) { // Breaks rule 1, return and let them try again // MessageBox.Show("R1"); Speak("You can't place a token there, you must be adjacent to an opponent's token"); return; } int dScore = 0; // Runs get flanks, checks approiximately 3n cells // (for 8x8 grid, 5this would be approximately 24, can check up to 27, but will probably be less) FlankInfo flanks = GetFlanks(x, y); if (!flanks.CanFlank) { // Breaks rule 2, return and let them try again // MessageBox.Show("R2"); Speak("You can't place a token there, you must be outflanking at least one enemy piece."); return; } else { // Stop any existing messages StopSpeak(); // Complies with rule 1 and 2, flip flanks in all directions // MessageBox.Show("R2"); for (int i = 0; i < flanks.FlankPoints.Length; i++) { ref Stack <Point> points = ref flanks.FlankPoints[i]; while (points.Count != 0) { Point p = points.Pop(); SetCell(p.X, p.Y, currentPlayer.CellValue); dScore++; } } }