From b2fb27b63905308011e4ed2265448971e6f0a139 Mon Sep 17 00:00:00 2001 From: AlexMamontow Date: Sun, 8 Aug 2021 00:32:35 +0300 Subject: [PATCH] Added basic bot actions. Still some bugs --- Assets/AI_Input.cs | 230 +++++++++++++++++++++----- Assets/Scenes/Level_1.unity | 2 +- Assets/Scripts/CaptureController.cs | 3 +- Assets/Scripts/Pathfinding.cs | 17 +- Assets/Scripts/PlayerActionManager.cs | 14 +- Assets/Scripts/TileManagment.cs | 21 ++- Assets/Scripts/TileMovement.cs | 3 +- 7 files changed, 238 insertions(+), 52 deletions(-) diff --git a/Assets/AI_Input.cs b/Assets/AI_Input.cs index 36e98969..dabc5ec9 100644 --- a/Assets/AI_Input.cs +++ b/Assets/AI_Input.cs @@ -5,66 +5,220 @@ using System; public class AI_Input : MonoBehaviour { + public BotState botState = BotState.Patrol; public Vector2 leftInput, rightInput; + public float agressiveTime = 5f; + public float attackTime = 2f; public Action OnTouchDown, OnTouchUp; + public Action OnCurrentPathFinished; + public List enemies; + public PlayerState _currentEnemy; - public List _currentFollowingPath; - public List _testPath; + private List _currentFollowingPath; + //public List _testPath; private PlayerState _playerState; - private TileMovement _tileMovement; - - private const int endIndex = 160; + private TileMovement _tileMovement; + private PlayerActionManager _actionManager; private void Awake() { _playerState = GetComponent(); _tileMovement = GetComponent(); + _actionManager = GetComponent(); + + _actionManager.OnActionEnd += BackToPatrol; + //_playerState.OnCharStateChanged += RecalculatePath; - _tileMovement.OnStartMovement += RecalculatePath; - _playerState.OnInitializied += StartBehaviour; + _tileMovement.OnFinishMovement += CheckState; + _tileMovement.OnStartMovement += StopJoystick; + _playerState.OnInitializied += StartPatrolBehaviour; + + OnCurrentPathFinished += StartPatrolBehaviour; + + SetEnemies(); } - private void StartBehaviour() + private void BackToPatrol(ActionType arg1, CharacterState arg2) { - var endTile = TileManagment.levelTiles[endIndex]; + StartPatrolBehaviour(); + } + + private void SetEnemies() + { + var allPlayers = FindObjectsOfType(); + foreach (PlayerState player in allPlayers) + { + if (player.gameObject.name != gameObject.name) + { + enemies.Add(player); + } + } + //Debug.Log("added " + enemies.Count); + } + + private void StopJoystick(ActionType arg1, CharacterState arg2) + { + leftInput = Vector2.zero; + } + + private void MoveTo(TileInfo tile) + { + if (_currentFollowingPath.Count > 0) + { + leftInput = TileManagment.GetJoystickDirection(_playerState.currentTile, tile); + } + } + + private void StartPatrolBehaviour() + { + botState = BotState.Patrol; + TileInfo targetTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex); + //Debug.Log("move to " + targetTile.name); var startTile = _playerState.currentTile; - _currentFollowingPath = Pathfinding.FindPath(startTile, endTile, TileManagment.tileOffset); - _testPath = _currentFollowingPath; + _currentFollowingPath = Pathfinding.FindPath(startTile, targetTile, TileManagment.levelTiles, TileManagment.tileOffset); + Pathfinding.FindPath(_playerState.currentTile, TileManagment.GetTile(enemies[0].transform.position), TileManagment.levelTiles, TileManagment.tileOffset); + MoveTo(_currentFollowingPath[1]); } - private void Start() + private IEnumerator CalmDown(float time) { + yield return new WaitForSeconds(time); + _currentEnemy = null; + StartPatrolBehaviour(); + StopAllCoroutines(); + } + + private IEnumerator TryToAttack(float attackCoolDown) + { + while (_currentEnemy) + { + _actionManager.AttackEnemyOnTile(_currentEnemy.currentTile); + yield return new WaitForSeconds(attackCoolDown); + } + } + + private void SetBehaviour(BotState state) + { + switch (state) + { + case BotState.Patrol: + MoveToNextPoint(); + break; + case BotState.Agressive: + MoveToEnemy(_currentEnemy); + break; + case BotState.Attack: + AttackEnemy(_currentEnemy); + break; + } + } + + + private void CheckState(ActionType newType, CharacterState newState) + { + foreach (PlayerState enemy in enemies) + { + Debug.Log("Check near enemy"); + if (Vector3.Distance(enemy.transform.position, transform.position) <= TileManagment.tileOffset*1.1) + { + botState = BotState.Attack; + _currentEnemy = enemy; + break; + } + } + if (botState == BotState.Patrol) + { + foreach (PlayerState enemy in enemies) + { + foreach (TileInfo tile in TileManagment.charTiles[(int)_playerState.ownerIndex]) + { + if ((enemy.transform.position - tile.tilePosition).magnitude < Mathf.Epsilon) + { + botState = BotState.Agressive; + _currentEnemy = enemy; + StartCoroutine(CalmDown(agressiveTime)); + } + } + if (_currentEnemy != null) + { + break; + } + } + } + + SetBehaviour(botState); + } + + private void AttackEnemy(PlayerState currentEnemy) + { + Debug.Log("attacking"); + leftInput = Vector2.zero; + //_actionManager.AttackEnemyOnTile(currentEnemy.currentTile); + StartCoroutine(TryToAttack(0.2f)); + StartCoroutine(CalmDown(attackTime)); + } + + private void MoveToEnemy(PlayerState currentEnemy) + { + List allAdjacentTilesToTarget = TileManagment.GetAllAdjacentTiles(currentEnemy.currentTile); + TileInfo adjacentTarget = allAdjacentTilesToTarget[0]; + foreach (var tile in allAdjacentTilesToTarget) + { + if (tile.canMove) + { + adjacentTarget = tile; + break; + } + } + TileInfo currentPos = TileManagment.GetTile(transform.position); + Debug.Log(adjacentTarget); + RecalculatePath(currentPos, adjacentTarget); + if (_currentFollowingPath == null) + { + StartPatrolBehaviour(); + return; + } + MoveToNextPoint(); + } + + private void RecalculatePath(TileInfo curentPos, TileInfo target) + { + _currentFollowingPath.Clear(); + _currentFollowingPath = Pathfinding.FindPath(curentPos, target, TileManagment.levelTiles, TileManagment.tileOffset); + } + + private void MoveToNextPoint() + { + if (_currentFollowingPath.Count > 0) //when stop movement, calculating path that begins from target tile + { + if (_playerState.currentTile == _currentFollowingPath[_currentFollowingPath.Count - 1]) + { + OnCurrentPathFinished?.Invoke(); + return; + } + + var endTile = _currentFollowingPath[_currentFollowingPath.Count - 1]; + if (!endTile.canMove) + { + endTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex); + Debug.Log("changed target"); + } + var currentTile = _playerState.currentTile; + _currentFollowingPath.Clear(); + _currentFollowingPath = Pathfinding.FindPath(currentTile, endTile, TileManagment.levelTiles, TileManagment.tileOffset); + MoveTo(_currentFollowingPath[1]); + } } - private void RecalculatePath(ActionType newType, CharacterState newState) + public enum BotState { - /*if (_currentFollowingPath.Count <= 0) - return;*/ - /*if (_testPath.Count > 0) - { - var endTile = _testPath[_testPath.Count - 1]; - var currentTile = _testPath[1]; - //_currentFollowingPath.Clear(); - _testPath = Pathfinding.FindPath(currentTile, endTile, TileManagment.tileOffset); - Debug.Log("recalculated, currentTile is " + currentTile.name); - }*/ - - } - - private void Update() - { - var cuurentTile = _playerState.currentTile; - int nextTileIndex = _currentFollowingPath.IndexOf(cuurentTile)+1; - if (nextTileIndex <= _currentFollowingPath.Count - 1) - { - leftInput = TileManagment.GetJoystickDirection(cuurentTile, _currentFollowingPath[nextTileIndex]); - } - else - { - leftInput = Vector2.zero; - } + Patrol, + Agressive, + Attack, + Dead } + } diff --git a/Assets/Scenes/Level_1.unity b/Assets/Scenes/Level_1.unity index fec99a46..50414480 100644 --- a/Assets/Scenes/Level_1.unity +++ b/Assets/Scenes/Level_1.unity @@ -9094,7 +9094,6 @@ MonoBehaviour: - {fileID: 2100000, guid: 11cfdc9193510f14a800c079f03262bb, type: 2} - {fileID: 2100000, guid: 491734c35c2ab2a489382e51b21de431, type: 2} - {fileID: 2100000, guid: 8b4d0931cd71e884580506919f308711, type: 2} - pathTiles: [] --- !u!1001 &752477394 PrefabInstance: m_ObjectHideFlags: 0 @@ -23260,6 +23259,7 @@ MonoBehaviour: m_EditorClassIdentifier: leftInput: {x: 0, y: 0} rightInput: {x: 0, y: 0} + _currentFollowingPath: [] --- !u!1 &1826148571 stripped GameObject: m_CorrespondingSourceObject: {fileID: 4991598031878838981, guid: fa5ecf8ffbe22c7459c44494cee176d9, diff --git a/Assets/Scripts/CaptureController.cs b/Assets/Scripts/CaptureController.cs index 9efdc750..d9fb6ad8 100644 --- a/Assets/Scripts/CaptureController.cs +++ b/Assets/Scripts/CaptureController.cs @@ -38,7 +38,8 @@ public class CaptureController : MonoBehaviour //Debug.Log("start tile cap"); _ownerIndex = _playerState.ownerIndex; _tileManagment.ChangeTileOwner(_playerState.currentTile, _ownerIndex); - + _playerState.currentTile.canMove = false; + } private void StopCapturingTile() diff --git a/Assets/Scripts/Pathfinding.cs b/Assets/Scripts/Pathfinding.cs index 66abb2bb..2c7d1155 100644 --- a/Assets/Scripts/Pathfinding.cs +++ b/Assets/Scripts/Pathfinding.cs @@ -5,8 +5,16 @@ using UnityEngine; public class Pathfinding : MonoBehaviour { - public static List FindPath(TileInfo startTile, TileInfo endTile, float tileOffset) + public static List FindPath(TileInfo startTile, TileInfo endTile, List allTiles, float tileOffset) { + foreach (var tile in allTiles) + { + var pathNode = tile.GetComponent(); + pathNode.gCost = 0f; + pathNode.hCost = 0f; + pathNode.fCost = 0f; + pathNode.parent = null; + } var openNodes = new List(); var closedNodes = new List(); @@ -15,6 +23,7 @@ public class Pathfinding : MonoBehaviour startNode.gCost = 0f; startNode.hCost = Vector3.Distance(startNode.transform.position, endNode.transform.position); startNode.fCost = startNode.gCost + startNode.hCost; + startNode.parent = null; openNodes.Add(startNode); while (openNodes.Count > 0) @@ -35,7 +44,7 @@ public class Pathfinding : MonoBehaviour openNodes.Remove(currentNode); closedNodes.Add(currentNode); - Debug.Log(currentNode.name); + //Debug.Log(currentNode.name); foreach (PathNode newNode in GetAdjacentNodes(currentNode)) { if (newNode) @@ -67,7 +76,7 @@ public class Pathfinding : MonoBehaviour private static List GetAdjacentNodes(PathNode currentNode) { - var allAjacentTiles = TileManagment.GetAllTiles(currentNode.GetComponent()); + var allAjacentTiles = TileManagment.GetAllAdjacentTiles(currentNode.GetComponent()); List adjacentNodes = new List(); foreach (TileInfo tile in allAjacentTiles) @@ -90,7 +99,7 @@ public class Pathfinding : MonoBehaviour currentNode = currentNode.parent; } result.Reverse(); - Debug.Log("path found"); + //Debug.Log("path found"); return result; } diff --git a/Assets/Scripts/PlayerActionManager.cs b/Assets/Scripts/PlayerActionManager.cs index a3c6ca2f..a38a7308 100644 --- a/Assets/Scripts/PlayerActionManager.cs +++ b/Assets/Scripts/PlayerActionManager.cs @@ -25,8 +25,11 @@ public class PlayerActionManager : MonoBehaviour _playerState = GetComponent(); _playerState.OnActionChanged += SetNewCurrentAction; - CustomInput.OnTouchDown += StartTargeting; - CustomInput.OnTouchUp += StopTargeting; + if (_playerState.controlType == ControlType.Player) + { + CustomInput.OnTouchDown += StartTargeting; + CustomInput.OnTouchUp += StopTargeting; + } SetNewCurrentAction(ActionType.Attack); } @@ -145,6 +148,13 @@ public class PlayerActionManager : MonoBehaviour return _actionProgress; } + public void AttackEnemyOnTile(TileInfo target) + { + _target = target; + StopTargeting(); + } + + } diff --git a/Assets/Scripts/TileManagment.cs b/Assets/Scripts/TileManagment.cs index 644fc956..64304af0 100644 --- a/Assets/Scripts/TileManagment.cs +++ b/Assets/Scripts/TileManagment.cs @@ -75,7 +75,7 @@ public class TileManagment : MonoBehaviour charTiles[(int)ownerIndex].Add(tile); //Debug.Log(GetOtherTiles(tile).Count); - //CheckSurroundedTiles(levelTiles, ownerIndex, tile); + CheckSurroundedTiles(levelTiles, ownerIndex, tile); //Debug.Log("Captured " + tile.name); } @@ -143,7 +143,7 @@ public class TileManagment : MonoBehaviour return dir2; } - public static List GetAllTiles(TileInfo currentTile) + public static List GetAllAdjacentTiles(TileInfo currentTile) { List allTiles = new List(); //int notMyTiles = 0; @@ -159,6 +159,17 @@ public class TileManagment : MonoBehaviour return allTiles; } + public static TileInfo GetRandomOtherTile(TileOwner owner) + { + int randomIndex = UnityEngine.Random.Range(0, levelTiles.Count - 1); + while ((levelTiles[randomIndex].tileOwnerIndex == owner) && (levelTiles[randomIndex].canMove == false)) + { + randomIndex = UnityEngine.Random.Range(0, levelTiles.Count - 1); + } + TileInfo otherTile = levelTiles[randomIndex]; + return otherTile; + } + public static Vector3[] GetBasicDirections(int directionsAmount) { Vector3[] tempArr = new Vector3[directionsAmount]; @@ -173,9 +184,9 @@ public class TileManagment : MonoBehaviour public static void CheckSurroundedTiles(List tiles, TileOwner ownerIndex, TileInfo startTile) { - /*foreach (TileInfo tile in tiles) + /*foreach (TileInfo tile in charTiles[(int)ownerIndex]) { - tile.isChecked = false; + tile.whoCanEasyGetTile = TileOwner.Neutral; }*/ List firstAdjacentTiles = GetOtherTiles(startTile, ownerIndex); foreach (TileInfo tile in firstAdjacentTiles) @@ -231,6 +242,6 @@ public class TileManagment : MonoBehaviour { tile.whoCanEasyGetTile = ownerIndex; } - Debug.Log("Surrounded " + surroundedTiles.Count); + //Debug.Log("Surrounded " + surroundedTiles.Count); } } diff --git a/Assets/Scripts/TileMovement.cs b/Assets/Scripts/TileMovement.cs index 0440d477..b0d86059 100644 --- a/Assets/Scripts/TileMovement.cs +++ b/Assets/Scripts/TileMovement.cs @@ -79,6 +79,7 @@ public class TileMovement : MonoBehaviour { OnStartMovement?.Invoke(ActionType.Attack, CharacterState.Move); _playerState.currentTile.canMove = true; + targetMoveTile.canMove = false; transform.DOMove(targetMoveTile.tilePosition, nextTileMoveTime).OnComplete(()=> FinishMovementActions(targetMoveTile)); transform.LookAt(targetMoveTile.tilePosition); } @@ -87,7 +88,7 @@ public class TileMovement : MonoBehaviour private void FinishMovementActions( TileInfo currentTile) { _playerState.currentTile = currentTile; - _playerState.currentTile.canMove = false; + //_playerState.currentTile.canMove = false; OnFinishMovement?.Invoke(ActionType.Attack, CharacterState.Idle); } }