Added basic bot actions. Still some bugs

This commit is contained in:
AlexMamontow 2021-08-08 00:32:35 +03:00
parent 3210fc245e
commit b2fb27b639
7 changed files with 238 additions and 52 deletions

View File

@ -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<PlayerState> enemies;
public PlayerState _currentEnemy;
public List<TileInfo> _currentFollowingPath;
public List<TileInfo> _testPath;
private List<TileInfo> _currentFollowingPath;
//public List<TileInfo> _testPath;
private PlayerState _playerState;
private TileMovement _tileMovement;
private const int endIndex = 160;
private TileMovement _tileMovement;
private PlayerActionManager _actionManager;
private void Awake()
{
_playerState = GetComponent<PlayerState>();
_tileMovement = GetComponent<TileMovement>();
_actionManager = GetComponent<PlayerActionManager>();
_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<PlayerState>();
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<TileInfo> 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
}
}

View File

@ -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,

View File

@ -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()

View File

@ -5,8 +5,16 @@ using UnityEngine;
public class Pathfinding : MonoBehaviour
{
public static List<TileInfo> FindPath(TileInfo startTile, TileInfo endTile, float tileOffset)
public static List<TileInfo> FindPath(TileInfo startTile, TileInfo endTile, List<TileInfo> allTiles, float tileOffset)
{
foreach (var tile in allTiles)
{
var pathNode = tile.GetComponent<PathNode>();
pathNode.gCost = 0f;
pathNode.hCost = 0f;
pathNode.fCost = 0f;
pathNode.parent = null;
}
var openNodes = new List<PathNode>();
var closedNodes = new List<PathNode>();
@ -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<PathNode> GetAdjacentNodes(PathNode currentNode)
{
var allAjacentTiles = TileManagment.GetAllTiles(currentNode.GetComponent<TileInfo>());
var allAjacentTiles = TileManagment.GetAllAdjacentTiles(currentNode.GetComponent<TileInfo>());
List<PathNode> adjacentNodes = new List<PathNode>();
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;
}

View File

@ -25,8 +25,11 @@ public class PlayerActionManager : MonoBehaviour
_playerState = GetComponent<PlayerState>();
_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();
}
}

View File

@ -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<TileInfo> GetAllTiles(TileInfo currentTile)
public static List<TileInfo> GetAllAdjacentTiles(TileInfo currentTile)
{
List<TileInfo> allTiles = new List<TileInfo>();
//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<TileInfo> 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<TileInfo> 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);
}
}

View File

@ -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);
}
}