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

View File

@ -23,6 +23,9 @@ public class PlayerState : MonoBehaviour
public Action OnCaptureAllow, OnCaptureForbid, OnInitializied;
public Action<CharacterState, ActionType> OnCharStateChanged;
public Action<ActionType> OnActionChanged;
public Action OnDeath;
public List<PlayerState> enemies;
private void Start()
{
@ -38,13 +41,23 @@ public class PlayerState : MonoBehaviour
_selectionTool.OnBuildingSelected += OnBuildingSelected;
//BuildManagment.OnBuildingSelected += SetPlacingBuildingsState;
//BuildManagment.OnBuildBtnDeactivated += SetIdleState;
//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()
{
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)
{
@ -73,6 +86,10 @@ public class PlayerState : MonoBehaviour
else
{
CaptureState(false);
if (newState == CharacterState.Dead)
{
OnDeath?.Invoke();
}
}
currentState = newState;
OnCharStateChanged?.Invoke(newState, actionType);
@ -85,11 +102,13 @@ public class PlayerState : MonoBehaviour
}
private void SetStartParams()
public void SetStartParams()
{
currentTile = TileManagment.GetTile(transform.position);
currentState = CharacterState.Idle;
//currentState = CharacterState.Idle;
//currentAction = ActionType.Attack;
SetNewState(ActionType.Attack, CharacterState.Idle);
SetEnemies();
OnInitializied?.Invoke();
}
@ -103,7 +122,8 @@ public enum CharacterState
{
Idle,
Move,
Action
Action,
Dead
}
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 PlayerState[] players;
public static List<PlayerState> players = new List<PlayerState>();
public static Action OnAnyTileCaptured;
public List<Material> tileMaterials;
public static float tileOffset;
public static Vector3[] basicDirections;
@ -38,8 +39,7 @@ public class TileManagment : MonoBehaviour
basicDirections = GetBasicDirections(BASIC_DIRECTIONS);
players = FindObjectsOfType<PlayerState>();
Debug.Log(players.Length);
//Debug.Log(players.Count);
}
@ -83,6 +83,8 @@ public class TileManagment : MonoBehaviour
CheckSurroundedTiles(levelTiles, ownerIndex, tile);
OnAnyTileCaptured?.Invoke();
}
public static void AssignBuildingToTile(TileInfo tile, GameObject building)
@ -189,6 +191,28 @@ public class TileManagment : MonoBehaviour
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)
{
Vector3[] tempArr = new Vector3[directionsAmount];

View File

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

View File

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