//function used to perform the move of the enemy public void performEnemyMove(MonteCarloPosition _p) { //increments the total moves this._totalMoves++; //sets old enemy position to current enemy position MonteCarloPosition _oldPos = FindPlayer(_enemyVal); //finds the adjacent tiles for the enemy to check for collision with the player object, returning true if the player object is adjacent to the enemy object bool _opponentCheck = CheckOpponentAdjacent(); //checks if the opponent check value is true if (_opponentCheck) { //reduces the player health by 1 this._playerHealth--; } else { //sets board value of the old enemy position to 0, whilst setting the new enemy position to the player value _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0; _boardValues [_p.GetX(), _p.GetZ()] = _enemyVal; //finds the adjacent tiles for the enemy to check for collision with the player object, returning true if the player object is adjacent to the enemy object bool _opponentCheck2 = CheckOpponentAdjacent(); //checks if the opponent check value is true if (_opponentCheck2) { //reduces the player health by 1 this._playerHealth--; } } }
//function used to move the enemy object public void MoveEnemy(GameObject _enemyObj) { //sets the speed of the enemy _speed = 2.0f; Node _enemyNode; //checks if the enemy moving variable is true if (_enemyMoving == true) { //sets the enemy object to look in the direction of movement _enemyObj.transform.LookAt(GridSystem._grid [_positionToMoveTo.GetX(), _positionToMoveTo.GetZ()]._obj.transform.position); //sets the enemy object to move towards the position calulated in MCTS _enemyObj.transform.position = Vector3.MoveTowards(_enemyObj.transform.position, GridSystem._grid[_positionToMoveTo.GetX(), _positionToMoveTo.GetZ()]._obj.transform.position, _speed * Time.deltaTime); //speed increased by it's value multiplied by delta time _speed += _speed * Time.deltaTime; //checks if the enemy object has reached it's desired position if (_enemyObj.transform.position == GridSystem._grid[_positionToMoveTo.GetX(), _positionToMoveTo.GetZ()]._obj.transform.position) { //sets enemy moving to false _enemyMoving = false; //sets enemy node to the node moved to _enemyNode = GridSystem.FindSelectedObject(_enemyObj, MonteCarloBoard._enemyVal); //sets the node's enemy node value to true GridSystem._grid [_enemyNode._gridX, _enemyNode._gridZ]._enemyNode = true; //checks whether the enemy object can damage the player bool _damagePlayer = CheckForAttack(); //checks if the damage player variable is true if (_damagePlayer) { //damages the player DamagePlayer(); } //moves to next enemy in enemies array NextEnemy(); } } }
//function used to perform the move of the player public void performPlayerMove(MonteCarloPosition _p) { //increments the total moves this._totalMoves++; //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight bool _opponentCheck = FindPlayerShootTiles(); //checks if the opponent check value is true if (_opponentCheck) { //reduces the enemy health by 1 this._enemyHealth--; } //performs player action if opponent check value is false else { //sets old player position to current player position MonteCarloPosition _oldPos = FindPlayer(_playerVal); //randomly decides whether to move the player 1 or 2 places int move = (int)Random.Range(0, 2); //if move value is 0, moves the player 1 position if (move == 0) { //sets board value of the old player position to 0, whilst setting the new player position to the player value _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0; _boardValues [_p.GetX(), _p.GetZ()] = _playerVal; //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight bool _opponentCheck2 = FindPlayerShootTiles(); //checks if the opponent check value is true if (_opponentCheck2) { //reduces the enemy health by 1 this._enemyHealth--; } } //if move value is not 0, moves the player 2 positions else { //sets board value of the old player position to 0, whilst setting the new player position to the player value _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0; _boardValues [_p.GetX(), _p.GetZ()] = _playerVal; //finds the legal positions for the player List <MonteCarloPosition> _legalPositions = this.GetLegalPositions(_playerVal); //sets a new int to the number of legal positions found int _totalLegalPossibilities = _legalPositions.Count; //selects a random number between 0 and the number of legal positions int _selectRandom = (int)(Random.Range(0, _totalLegalPossibilities)); //sets the second move position to that of the randomly selected legal position MonteCarloPosition _p2 = _legalPositions [_selectRandom]; //sets board value of the old player position to 0, whilst setting the new player position to the player value _boardValues [_p.GetX(), _p.GetZ()] = 0; _boardValues [_p2.GetX(), _p2.GetZ()] = _playerVal; //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight bool _opponentCheck2 = FindPlayerShootTiles(); //checks if the opponent check value is true if (_opponentCheck2) { //reduces the enemy health by 1 this._enemyHealth--; } } } }