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(); AddNewEnemies(enemies.Count); } private void AddNewEnemies(int amount) { if (amount == 0) { var newEnemy = GameObject.Find("Player"); if (newEnemy != null) { enemies.Add(newEnemy.GetComponent()); } } } private List AddTowerEnemy() { 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 + 3) && FindObjectOfType() != null && player.ownerIndex != gameObject.GetComponent().ownerIndex && !gameObject.GetComponent() && gameObject.GetComponent().ownerIndex != FindObjectOfType().GetComponent().ownerIndex ) { //enemies.Add(player); enemies.Add(FindObjectOfType().GetComponent()); } else if(GetComponent() && enemies.Count == 0) { enemies.Add(gameObject.GetComponent().GetComponent()); } } return enemies; } private void SetTowerEnemyForAI() { if ((enemies.Count < _towerCount + GameManager.activePlayers.Count) && gameObject.GetComponent() && !gameObject.GetComponent() ) { enemies.Add(FindObjectOfType().GetComponent()); } } public void ResetEnemies() { enemies.Clear(); enemies = SetEnemies(); } 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; OnCharStateChanged?.Invoke(newState); } } 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); } } 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 }