Made res\death check and res\death actions. Fixed some bugs.

This commit is contained in:
AlexMamontow 2021-08-10 20:51:43 +03:00
parent 701d8c9fce
commit 0eef64947d
13 changed files with 10811 additions and 575 deletions

View File

@ -0,0 +1,188 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDeathController : MonoBehaviour
{
public static List<PlayerState> players = new List<PlayerState>();
public GameObject deathParticles, resParticles;
public float resurrectTime = 7f;
private List<PlayerState> alivePlayers = new List<PlayerState>();
private List<PlayerState> deadPlayers = new List<PlayerState>();
private List<float> lastDeadTime = new List<float>();
private float updateTime = 1f;
private int spawnSafezone = 1;
public static Action<PlayerState> OnPlayerDeath;
private void Awake()
{
var tmpPlayers = FindObjectsOfType<PlayerState>();
foreach (var player in tmpPlayers)
{
players.Add(player);
}
TileManagment.players = players;
}
private void Start()
{
foreach (var player in players)
{
alivePlayers.Add(player);
lastDeadTime.Add(0f);
}
TileManagment.OnAnyTileCaptured += CheckPlayersDeath;
InvokeRepeating("CheckIfNeedRessurection", 1f, updateTime);
}
private void CheckPlayersDeath()
{
List<PlayerState> thisIterationDeadPlayers = new List<PlayerState>();
foreach (var player in alivePlayers)
{
var playerTile = TileManagment.GetTile(player.transform.position);
var adjacentTiles = TileManagment.GetAllAdjacentTiles(playerTile);
if (playerTile.tileOwnerIndex != player.ownerIndex)
{
int otherTileCounter = 0;
int tileCounter = 0;
foreach (var tile in adjacentTiles)
{
if (tile)
{
tileCounter++;
if (tile.tileOwnerIndex != player.ownerIndex || !tile.canMove)
{
otherTileCounter++;
}
}
}
if (otherTileCounter >= adjacentTiles.Count)
{
thisIterationDeadPlayers.Add(player);
}
}
}
foreach (var player in thisIterationDeadPlayers)
{
MakeDead(player);
}
}
private void MakeDead(PlayerState player)
{
int playerIndex = players.IndexOf(player);
lastDeadTime[playerIndex] = Time.time;
alivePlayers.Remove(player);
deadPlayers.Add(player);
OnPlayerDeath.Invoke(player);
PlayerDeadActions(player);
}
private void CheckIfNeedRessurection()
{
List<PlayerState> needResPlayers = new List<PlayerState>();
foreach (var player in deadPlayers)
{
int playerIndex = players.IndexOf(player);
if (Time.time > resurrectTime + lastDeadTime[playerIndex])
{
needResPlayers.Add(player);
}
}
foreach (var player in needResPlayers)
{
ResPlayer(player);
}
}
private void ResPlayer(PlayerState player)
{
alivePlayers.Add(player);
deadPlayers.Remove(player);
PlayerResActions(player);
//player.gameObject.SetActive(true);
}
private void PlayerDeadActions(PlayerState player)
{
if (deathParticles)
{
Instantiate(deathParticles, player.transform.position, deathParticles.transform.rotation);
}
player.SetNewState(ActionType.Attack, CharacterState.Dead);
List<TileInfo> playerTiles = TileManagment.charTiles[(int)player.ownerIndex];
TileInfo currentTile = TileManagment.GetTile(player.transform.position);
currentTile.canMove = true;
foreach (TileInfo tile in playerTiles)
{
foreach (var enemy in player.enemies)
{
tile.easyCaptureFor.Add(enemy.ownerIndex);
}
}
player.gameObject.SetActive(false);
Debug.Log("player " + player.name + " dead");
}
private void PlayerResActions(PlayerState player)
{
List<TileInfo> playerTiles = TileManagment.charTiles[(int)player.ownerIndex];
foreach (TileInfo tile in playerTiles)
{
tile.easyCaptureFor.Clear();
}
player.transform.position = GetAvailableResPos(player, playerTiles);
TileInfo currentTile = TileManagment.GetTile(player.transform.position);
currentTile.canMove = false;
player.gameObject.SetActive(true);
player.SetStartParams();
Debug.Log("player " + player.name + " res");
if (resParticles)
{
Instantiate(resParticles, player.transform.position, deathParticles.transform.rotation);
}
}
private Vector3 GetAvailableResPos(PlayerState player, List<TileInfo> playerTiles)
{
foreach (TileInfo tile in playerTiles)
{
if (tile.canMove && tile.tileOwnerIndex == player.ownerIndex)
{
var myNeighbourTiles = TileManagment.GetAllAdjacentTiles(tile, player.ownerIndex);
if (myNeighbourTiles.Count >= spawnSafezone)
{
return tile.tilePosition;
}
}
}
Debug.Log("nowhere to spawn");
return Vector3.zero;
}
public void OnKillBtnClick(int playerIndex)
{
PlayerState player = players[playerIndex];
MakeDead(player);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c0e5eb738ab5c74793329d939867a78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 829b13391593fbd43a935f64d40daf8e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 234698f23c9068940b633be5a4f146ea
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ea13d84c4b665e04dad27017bf079e19
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,6 @@ public class AI_Input : MonoBehaviour
public float updateBehaviourIn = 1f; public float updateBehaviourIn = 1f;
public Action OnTouchDown, OnTouchUp; public Action OnTouchDown, OnTouchUp;
public Action OnCurrentPathFinished, OnAttack; public Action OnCurrentPathFinished, OnAttack;
public List<PlayerState> enemies;
public PlayerState _currentEnemy; public PlayerState _currentEnemy;
private List<TileInfo> _currentFollowingPath; private List<TileInfo> _currentFollowingPath;
@ -37,7 +36,16 @@ public class AI_Input : MonoBehaviour
OnCurrentPathFinished += StartPatrolBehaviour; OnCurrentPathFinished += StartPatrolBehaviour;
SetEnemies(); PlayerDeathController.OnPlayerDeath += StopAllActions;
}
private void StopAllActions(PlayerState player)
{
if (player.name == gameObject.name)
{
StopAllCoroutines();
}
} }
private void Start() private void Start()
@ -57,17 +65,6 @@ public class AI_Input : MonoBehaviour
StartPatrolBehaviour(); StartPatrolBehaviour();
} }
private void SetEnemies()
{
var allPlayers = FindObjectsOfType<PlayerState>();
foreach (PlayerState player in allPlayers)
{
if (player.gameObject.name != gameObject.name)
{
enemies.Add(player);
}
}
}
private void StopJoystick(ActionType arg1, CharacterState arg2) private void StopJoystick(ActionType arg1, CharacterState arg2)
{ {
@ -86,7 +83,8 @@ public class AI_Input : MonoBehaviour
{ {
_currentEnemy = null; _currentEnemy = null;
botState = BotState.Patrol; botState = BotState.Patrol;
TileInfo targetTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex); //TileInfo targetTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex);
TileInfo targetTile = TileManagment.GetNearestNeutralTile(_playerState.currentTile, _playerState.ownerIndex);
var startTile = _playerState.currentTile; var startTile = _playerState.currentTile;
_currentFollowingPath = Pathfinding.FindPath(startTile, targetTile, TileManagment.levelTiles, TileManagment.tileOffset); _currentFollowingPath = Pathfinding.FindPath(startTile, targetTile, TileManagment.levelTiles, TileManagment.tileOffset);
if (_currentFollowingPath == null) if (_currentFollowingPath == null)
@ -99,9 +97,11 @@ public class AI_Input : MonoBehaviour
private void CheckState(/*ActionType newType, CharacterState newState*/) private void CheckState(/*ActionType newType, CharacterState newState*/)
{ {
foreach (PlayerState enemy in enemies) if (_playerState.currentState == CharacterState.Dead)
return;
foreach (PlayerState enemy in _playerState.enemies)
{ {
Debug.Log("Check near enemy"); //Debug.Log("Check near enemy");
if (Vector3.Distance(enemy.transform.position, transform.position) <= TileManagment.tileOffset*1.1f) if (Vector3.Distance(enemy.transform.position, transform.position) <= TileManagment.tileOffset*1.1f)
{ {
botState = BotState.Attack; botState = BotState.Attack;
@ -111,7 +111,7 @@ public class AI_Input : MonoBehaviour
} }
if (botState == BotState.Patrol) if (botState == BotState.Patrol)
{ {
foreach (PlayerState enemy in enemies) foreach (PlayerState enemy in _playerState.enemies)
{ {
foreach (TileInfo tile in TileManagment.charTiles[(int)_playerState.ownerIndex]) foreach (TileInfo tile in TileManagment.charTiles[(int)_playerState.ownerIndex])
{ {
@ -151,7 +151,7 @@ public class AI_Input : MonoBehaviour
private void AttackEnemy(PlayerState currentEnemy) private void AttackEnemy(PlayerState currentEnemy)
{ {
Debug.Log("attacking"); //Debug.Log("attacking");
leftInput = Vector2.zero; leftInput = Vector2.zero;
_currentFollowingPath.Clear(); _currentFollowingPath.Clear();
//_actionManager.AttackEnemyOnTile(currentEnemy.currentTile); //_actionManager.AttackEnemyOnTile(currentEnemy.currentTile);
@ -201,7 +201,8 @@ public class AI_Input : MonoBehaviour
var endTile = _currentFollowingPath[_currentFollowingPath.Count - 1]; var endTile = _currentFollowingPath[_currentFollowingPath.Count - 1];
if (!endTile.canMove) if (!endTile.canMove)
{ {
endTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex); endTile = TileManagment.GetNearestNeutralTile(_playerState.currentTile, _playerState.ownerIndex);
//endTile = TileManagment.GetRandomOtherTile(_playerState.ownerIndex);
//Debug.Log("changed target"); //Debug.Log("changed target");
} }
var currentTile = _playerState.currentTile; var currentTile = _playerState.currentTile;
@ -229,9 +230,12 @@ public class AI_Input : MonoBehaviour
while (_currentEnemy && Vector3.Distance(_currentEnemy.transform.position, transform.position) <= TileManagment.tileOffset * 1.1f) while (_currentEnemy && Vector3.Distance(_currentEnemy.transform.position, transform.position) <= TileManagment.tileOffset * 1.1f)
{ {
//Debug.Log("try attack"); //Debug.Log("try attack");
if (_currentEnemy.currentState != CharacterState.Dead)
{
_actionManager.AttackEnemyOnTile(_currentEnemy.currentTile); _actionManager.AttackEnemyOnTile(_currentEnemy.currentTile);
yield return new WaitForSeconds(attackCoolDown); yield return new WaitForSeconds(attackCoolDown);
} }
}
BackToPatrol(); BackToPatrol();
StopAllCoroutines(); StopAllCoroutines();
} }

View File

@ -23,6 +23,9 @@ public class PlayerState : MonoBehaviour
public Action OnCaptureAllow, OnCaptureForbid, OnInitializied; public Action OnCaptureAllow, OnCaptureForbid, OnInitializied;
public Action<CharacterState, ActionType> OnCharStateChanged; public Action<CharacterState, ActionType> OnCharStateChanged;
public Action<ActionType> OnActionChanged; public Action<ActionType> OnActionChanged;
public Action OnDeath;
public List<PlayerState> enemies;
private void Start() private void Start()
{ {
@ -38,13 +41,23 @@ public class PlayerState : MonoBehaviour
_selectionTool.OnBuildingSelected += OnBuildingSelected; _selectionTool.OnBuildingSelected += OnBuildingSelected;
//BuildManagment.OnBuildingSelected += SetPlacingBuildingsState;
//BuildManagment.OnBuildBtnDeactivated += SetIdleState;
//Debug.Log("We are in " +currentTile.name); //Debug.Log("We are in " +currentTile.name);
} }
private void SetEnemies()
{
var allPlayers = PlayerDeathController.players;
foreach (PlayerState player in allPlayers)
{
if (player.gameObject.name != gameObject.name)
{
enemies.Add(player);
}
}
}
private void OnBuildingSelected() private void OnBuildingSelected()
{ {
SetNewState(ActionType.Build, currentState); SetNewState(ActionType.Build, currentState);
@ -62,7 +75,7 @@ public class PlayerState : MonoBehaviour
} }
} }
private void SetNewState(ActionType actionType, CharacterState newState) public void SetNewState(ActionType actionType, CharacterState newState)
{ {
if (currentState != newState) if (currentState != newState)
{ {
@ -73,6 +86,10 @@ public class PlayerState : MonoBehaviour
else else
{ {
CaptureState(false); CaptureState(false);
if (newState == CharacterState.Dead)
{
OnDeath?.Invoke();
}
} }
currentState = newState; currentState = newState;
OnCharStateChanged?.Invoke(newState, actionType); OnCharStateChanged?.Invoke(newState, actionType);
@ -85,11 +102,13 @@ public class PlayerState : MonoBehaviour
} }
private void SetStartParams() public void SetStartParams()
{ {
currentTile = TileManagment.GetTile(transform.position); currentTile = TileManagment.GetTile(transform.position);
currentState = CharacterState.Idle; //currentState = CharacterState.Idle;
//currentAction = ActionType.Attack;
SetNewState(ActionType.Attack, CharacterState.Idle);
SetEnemies();
OnInitializied?.Invoke(); OnInitializied?.Invoke();
} }
@ -103,7 +122,8 @@ public enum CharacterState
{ {
Idle, Idle,
Move, Move,
Action Action,
Dead
} }
public enum TileOwner public enum TileOwner

View File

@ -11,11 +11,12 @@ public class TileManagment : MonoBehaviour
public static List<List<TileInfo>> charTiles = new List<List<TileInfo>>(); public static List<List<TileInfo>> charTiles = new List<List<TileInfo>>();
public static PlayerState[] players; public static List<PlayerState> players = new List<PlayerState>();
public static Action OnAnyTileCaptured;
public List<Material> tileMaterials; public List<Material> tileMaterials;
public static float tileOffset; public static float tileOffset;
public static Vector3[] basicDirections; public static Vector3[] basicDirections;
@ -38,8 +39,7 @@ public class TileManagment : MonoBehaviour
basicDirections = GetBasicDirections(BASIC_DIRECTIONS); basicDirections = GetBasicDirections(BASIC_DIRECTIONS);
players = FindObjectsOfType<PlayerState>(); //Debug.Log(players.Count);
Debug.Log(players.Length);
} }
@ -83,6 +83,8 @@ public class TileManagment : MonoBehaviour
CheckSurroundedTiles(levelTiles, ownerIndex, tile); CheckSurroundedTiles(levelTiles, ownerIndex, tile);
OnAnyTileCaptured?.Invoke();
} }
public static void AssignBuildingToTile(TileInfo tile, GameObject building) public static void AssignBuildingToTile(TileInfo tile, GameObject building)
@ -189,6 +191,28 @@ public class TileManagment : MonoBehaviour
return otherTile; return otherTile;
} }
public static TileInfo GetNearestNeutralTile(TileInfo currentTile, TileOwner owner)
{
var neutralTiles = charTiles[(int)TileOwner.Neutral];
Debug.Log("neutral tiles " + neutralTiles.Count);
TileInfo closestTile = GetRandomOtherTile(owner);
/*foreach (TileInfo tile in neutralTiles)
{
if (tile)
{
float distOld = Vector3.Distance(currentTile.tilePosition, closestTile.tilePosition);
float distNew = Vector3.Distance(currentTile.tilePosition, tile.tilePosition);
if (distNew < distOld && tile.canMove)
{
closestTile = tile;
}
}
}*/
return closestTile;
}
public static Vector3[] GetBasicDirections(int directionsAmount) public static Vector3[] GetBasicDirections(int directionsAmount)
{ {
Vector3[] tempArr = new Vector3[directionsAmount]; Vector3[] tempArr = new Vector3[directionsAmount];

View File

@ -11,32 +11,45 @@ public class TreesSpawner : MonoBehaviour
[Range(0f,0.8f)] [Range(0f,0.8f)]
public float treeCoverKoef = 0.2f; public float treeCoverKoef = 0.2f;
public Transform treesParent; public Transform treesParent;
public List<GameObject> treePrefabs; public List<GameObject> treePrefabs;
private List<List<GameObject>> charTrees = new List<List<GameObject>>(); private List<List<GameObject>> charTrees = new List<List<GameObject>>();
private List<int> requiredCharTrees; //private List<int> requiredCharTrees;
private Queue<GameObject> spawningTrees; private List<Queue<GameObject>> spawningTrees = new List<Queue<GameObject>>();
private void Start() private void Start()
{ {
spawningTrees = new Queue<GameObject>(TileManagment.levelTiles.Count); //spawningTrees = new Queue<GameObject>(TileManagment.levelTiles.Count);
InitCharTrees();
InvokeRepeating("CheckForPlanting", 0f, updateRate); InvokeRepeating("CheckForPlanting", 0f, updateRate);
} }
private void PlantTreeOnRandomTile() private void InitCharTrees()
{ {
/*var treePrefToSpawn = spawningTrees.Dequeue(); for (int i = 0; i < TileManagment.charTiles.Count; i++)
{
List<GameObject> charTreeList = new List<GameObject>();
charTrees.Add(charTreeList); //init empty lists for character trees
Queue<GameObject> charTreeQueue = new Queue<GameObject>(TileManagment.levelTiles.Count);
spawningTrees.Add(charTreeQueue);
}
}
private void PlantTreeOnRandomTile(PlayerState player)
{
int charIndex = (int)player.ownerIndex;
var treePrefToSpawn = spawningTrees[charIndex].Dequeue();
int randIndex; int randIndex;
TileInfo tile; TileInfo tile;
Vector3 playerPos = FindObjectOfType<PlayerState>().transform.position; Vector3 playerPos = player.transform.position;
randIndex = Random.Range(0, TileManagment.ariostTiles.Count); randIndex = Random.Range(0, TileManagment.charTiles[charIndex].Count);
tile = TileManagment.ariostTiles[randIndex]; tile = TileManagment.charTiles[charIndex][randIndex];
GameObject tree; GameObject tree;
if (tile.canBuildHere) if (tile.canBuildHere)
{ {
@ -45,21 +58,21 @@ public class TreesSpawner : MonoBehaviour
tree = Instantiate(treePrefToSpawn, tile.tilePosition, treePrefToSpawn.transform.rotation); tree = Instantiate(treePrefToSpawn, tile.tilePosition, treePrefToSpawn.transform.rotation);
tree.transform.parent = treesParent; tree.transform.parent = treesParent;
TileManagment.AssignBuildingToTile(tile, tree); TileManagment.AssignBuildingToTile(tile, tree);
ariostTrees.Add(tree); charTrees[charIndex].Add(tree);
} }
else else
{ {
spawningTrees.Enqueue(treePrefToSpawn); spawningTrees[charIndex].Enqueue(treePrefToSpawn);
PlantTreeOnRandomTile(); PlantTreeOnRandomTile(player);
return; return;
} }
} }
else else
{ {
spawningTrees.Enqueue(treePrefToSpawn); spawningTrees[charIndex].Enqueue(treePrefToSpawn);
PlantTreeOnRandomTile(); PlantTreeOnRandomTile(player);
return; return;
} */ }
//Debug.Log("player pos " + playerPos); //Debug.Log("player pos " + playerPos);
//Debug.Log("tile pos " + tile.tilePosition); //Debug.Log("tile pos " + tile.tilePosition);
@ -70,32 +83,34 @@ public class TreesSpawner : MonoBehaviour
private void CheckForPlanting() private void CheckForPlanting()
{ {
/*foreach() foreach (var player in TileManagment.players)
requiredAriostTrees = Mathf.FloorToInt(treeCoverKoef * TileManagment.ariostTiles.Count); {
int newTreesForPlanting = requiredAriostTrees - ariostTrees.Count - spawningTrees.Count; int numberOfPlayerTrees = charTrees[(int)player.ownerIndex].Count;
int requirePlayerTrees = Mathf.FloorToInt(treeCoverKoef * TileManagment.charTiles[(int)player.ownerIndex].Count);
int newTreesForPlanting = requirePlayerTrees - numberOfPlayerTrees - spawningTrees[(int)player.ownerIndex].Count;
//Debug.Log("req trees " + requirePlayerTrees);
//Debug.Log("need to spawn " + newTreesForPlanting); //Debug.Log("need to spawn " + newTreesForPlanting);
//Debug.Log("Waiting for spawn " + spawningTrees.Count + " trees");
for (int i = 0; i < newTreesForPlanting; i++) for (int i = 0; i < newTreesForPlanting; i++)
{ {
CreatePlantTask(); CreatePlantTask(player);
}*/ }
} }
private void CreatePlantTask() }
private void CreatePlantTask(PlayerState player)
{ {
int treeRandIndex = Random.Range(0, treePrefabs.Count); int treeRandIndex = Random.Range(0, treePrefabs.Count);
//int tileRandIndex = Random.Range(0, treePrefabs.Count);
float spawnTime = Random.Range(minSpawnRate, maxSpawnRate); float spawnTime = Random.Range(minSpawnRate, maxSpawnRate);
var tree = treePrefabs[treeRandIndex]; var tree = treePrefabs[treeRandIndex];
spawningTrees.Enqueue(tree); spawningTrees[(int)player.ownerIndex].Enqueue(tree);
//Debug.Log("another tree waiting for spawn"); //Debug.Log("another tree waiting for spawn");
StartCoroutine(WaitTillPlant(spawnTime)); StartCoroutine(WaitTillPlant(spawnTime, player));
} }
private IEnumerator WaitTillPlant(float delay) private IEnumerator WaitTillPlant(float delay, PlayerState player)
{ {
yield return new WaitForSeconds(delay); yield return new WaitForSeconds(delay);
PlantTreeOnRandomTile(); PlantTreeOnRandomTile(player);
} }
} }

View File

@ -21,6 +21,7 @@ public class UI_ProgressBar : MonoBehaviour
private CaptureController _captureController; private CaptureController _captureController;
private PlayerActionManager _playerActions; private PlayerActionManager _playerActions;
private PlayerState _playerState;
private bool _isCaptureUIUpdating; private bool _isCaptureUIUpdating;
@ -28,7 +29,7 @@ public class UI_ProgressBar : MonoBehaviour
{ {
_cam = Camera.main.transform; _cam = Camera.main.transform;
foreach (Canvas c in FindObjectsOfType<Canvas>()) foreach (Canvas c in GetComponentsInChildren<Canvas>())
{ {
if (c.renderMode == RenderMode.WorldSpace) if (c.renderMode == RenderMode.WorldSpace)
{ {
@ -41,6 +42,7 @@ public class UI_ProgressBar : MonoBehaviour
} }
_captureController = GetComponent<CaptureController>(); _captureController = GetComponent<CaptureController>();
_playerActions = GetComponent<PlayerActionManager>(); _playerActions = GetComponent<PlayerActionManager>();
_playerState = GetComponent<PlayerState>();
_captureController.OnCaptureStart += OnCaptureStart; _captureController.OnCaptureStart += OnCaptureStart;
_captureController.OnCaptureEnd += OnCaptureEnd; _captureController.OnCaptureEnd += OnCaptureEnd;
@ -49,7 +51,7 @@ public class UI_ProgressBar : MonoBehaviour
_playerActions.OnActionStart += OnActionStart; _playerActions.OnActionStart += OnActionStart;
_playerActions.OnActionSuccess += OnActionStop; _playerActions.OnActionSuccess += OnActionStop;
_playerState.OnDeath += StopUpdate;
} }