using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; [RequireComponent(typeof(TileMovement))] //[RequireComponent(typeof(PlayerActionManager))] public class PlayerState : MonoBehaviour { public ControlType controlType = ControlType.Player; public TileOwner ownerIndex = TileOwner.Ariost; public CharacterState prevState = CharacterState.Idle; public CharacterState currentState = CharacterState.Idle; //public ActionType currentSubState = ActionType.None; public TileInfo currentTile; public TileInfo targetMoveTile; public TileInfo currentActionTarget; public PlayerAction defaultAction; public PlayerAction currentAction; public Action OnInitializied; public Action OnCharStateChanged; public Action OnSubStateChanged; public Action OnActionChanged; public Action OnDefaultAction; public Action OnActionInterrupt; public Action OnDeath, OnRes; public List enemies; private bool isInitialized = false; // private int _towerCount = 0; private int _playerCount = 0; [SerializeField] private List _crystalls ; private void Awake() { TileManagment.OnInitialized += SetStartParams; CharSpawner.OnPlayerSpawned += ResetEnemies; // DeathChecker.OnPlayerDeathPermanent += ResetEnemies; OnCharStateChanged += OnStateChanged; // _playerCount = FindObjectsOfType().Length; } private void Start() { if (!isInitialized) { SetStartParams(); } //_crystalls = new List() {GameObject.FindObjectOfType()}; } private void Update() { //AddTowerEnemy(); //RemoveTowerEnemy(); List players = new List(SetEnemies()); enemies = new List(enemies); _towerCount = FindObjectsOfType().Length; foreach (PlayerState player in players) { // && player.ownerIndex != gameObject.GetComponent().ownerIndex // (enemies.Count < _towerCount + 3) if ((enemies.Count < _towerCount + GameManager.activePlayers.Count + 3) && FindObjectOfType() != null && player.ownerIndex != gameObject.GetComponent().ownerIndex //player.controlType != FindObjectOfType().GetComponent().controlType && !gameObject.GetComponent() //FindObjectOfType() // && gameObject.GetComponent().ownerIndex != FindObjectOfType().GetComponent().ownerIndex ) { //enemies.Add(player); enemies.Add(FindObjectOfType().GetComponent()); }/* if(gameObject.GetComponent() && gameObject.GetComponent()) { enemies.Add(gameObject.GetComponent()); || FindObjectOfType() != null && enemy.ownerIndex == GameObject.FindObjectOfType().GetComponent().ownerIndex }*/ } //new List(SetEnemies()); //_towerCount = FindObjectsOfType().Length; /* foreach(PlayerState player in players) { if ((enemies.Count < _towerCount + GameManager.activePlayers.Count + 1) && FindObjectOfType() != null && player.ownerIndex != gameObject.GetComponent().ownerIndex && !gameObject.GetComponent()) { enemies.Add(FindObjectOfType().GetComponent()); } }*/ } private List RemoveTowerEnemy() { List players = new List( AddTowerEnemy()); _towerCount = FindObjectsOfType().Length; foreach (PlayerState player in players) { if(FindObjectOfType() != null) { if ((enemies.Count < _towerCount + GameManager.activePlayers.Count)) { if(player.ownerIndex == FindObjectOfType().GetComponent().ownerIndex && player.controlType == FindObjectOfType().GetComponent().controlType) { enemies.Remove( player); } } } } return enemies; } private List AddTowerEnemy() { List players = new List(SetEnemies()); _towerCount = FindObjectsOfType().Length; foreach (PlayerState player in players) { if(FindObjectOfType() != null) { if ((enemies.Count < _towerCount + GameManager.activePlayers.Count)) { //SetTowerEnemyForAI(); if ((enemies.Count < _towerCount + GameManager.activePlayers.Count) && gameObject.GetComponent() && !gameObject.GetComponent() ) { enemies.Add(FindObjectOfType().GetComponent()); } } } } return enemies; //enemies = new List(players); } /* && gameObject.GetComponent() && !gameObject.GetComponent() ) { enemies.Remove(FindObjectOfType().GetComponent()); } } */ // && player.ownerIndex != gameObject.GetComponent().ownerIndex // && //&& !player.GetComponent() //|| !gameObject.GetComponent() //&& player.gameObject.name != gameObject.name //&& player.ownerIndex != gameObject.GetComponent().ownerIndex private void SetTowerEnemyForAI() { if ((enemies.Count < _towerCount + GameManager.activePlayers.Count) && gameObject.GetComponent() && !gameObject.GetComponent() ) { enemies.Add(FindObjectOfType().GetComponent()); } } /* /* List players = enemies; _towerCount = FindObjectsOfType().Length; foreach(PlayerState player in players) { // enemies.Count < _towerCount + 1 || if((enemies.Count < _towerCount + 1) && player.GetComponent() && player.GetComponent().ownerIndex != gameObject.GetComponent().owner //&& player.ownerIndex != player.GetComponent().owner ) { enemies.Add(FindObjectOfType().GetComponent()); } } //enemies = enemies.Add(GameObject.FindObjectOfType().gameObject.GetComponent()); */ public void ResetEnemies() { enemies.Clear(); enemies = SetEnemies(); //_crystalls = SetCrystallEnemies(SetEnemies()); //SetCrystallEnemies(); } private void OnStateChanged(CharacterState newState) { switch (newState) { case CharacterState.Idle: currentAction = defaultAction; if (prevState == CharacterState.Capture) { OnActionInterrupt?.Invoke(); } else { OnDefaultAction?.Invoke(); } break; case CharacterState.Move: currentAction = defaultAction; OnActionInterrupt?.Invoke(); break; } } private void OnEnable() { currentAction = defaultAction; } public void SetNewState(CharacterState newState) { if (currentState != newState) { prevState = currentState; currentState = newState; /*if (newState == CharacterState.Action) { currentSubState = currentAction.actionType; }*/ OnCharStateChanged?.Invoke(newState); } } /*public void SetNewCharSubState(ActionType newActionType) { if (currentSubState != newActionType) { currentSubState = newActionType; OnSubStateChanged?.Invoke(newActionType); } }*/ public void SetCurrentAction(PlayerAction newAction) { if (currentAction != newAction) { currentAction = newAction; OnActionChanged?.Invoke(); } } public void SetStartParams() { currentTile = TileManagment.GetTile(transform.position); currentTile.canMove = false; //currentState = CharacterState.Idle; SetNewState(CharacterState.Idle); currentAction = defaultAction; currentActionTarget = null; enemies = SetEnemies(); //Debug.Log("player state init"); OnInitializied?.Invoke(); isInitialized = true; } private List SetEnemies() { List players = GameManager.activePlayers; List enemies = new List(); foreach (PlayerState player in players) { // && player.ownerIndex != gameObject.GetComponent().ownerIndex if (player.gameObject.name != gameObject.name && player.ownerIndex != gameObject.GetComponent().ownerIndex) { enemies.Add(player); }/* if(gameObject.GetComponent() && gameObject.GetComponent()) { enemies.Add(gameObject.GetComponent()); }*/ } return enemies; } public bool IsAnyActionsAllowed() { return currentTile.tileOwnerIndex == ownerIndex && currentState == CharacterState.Idle; } public void SetDead() { currentTile.canMove = true; if (targetMoveTile) { targetMoveTile.canMove = true; } OnDeath?.Invoke(); gameObject.SetActive(false); } public void SetAlive(Vector3 spawnPos) { transform.position = spawnPos; gameObject.SetActive(true); SetStartParams(); OnRes?.Invoke(); } } public enum CharacterState { Idle, Capture, Move, Action, Frozen, /*Attack, Build, TreeAttack,*/ Dead } public enum ControlType { Player, AI } public enum ActionType { None, Attack, Build, TreeAttack, SuperJump }