278 lines
7.6 KiB
C#
278 lines
7.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class Session : MonoBehaviour
|
|
{
|
|
public static Session main;
|
|
[SerializeField] private PlayerConfig playerConfig;
|
|
|
|
[SerializeField] private UnityEvent onVictory;
|
|
[SerializeField] private UnityEvent onDefeat;
|
|
[SerializeField] private UnityEvent onInventory;
|
|
[SerializeField] private UnityEvent onNextLevel;
|
|
|
|
|
|
[Header("Enemies")]
|
|
[Space (3)]
|
|
|
|
[SerializeField] private List<Transform> enemyUIPositions = new List<Transform>();
|
|
[SerializeField] private List<GameObject> enemyPrefabs;
|
|
[Header("Player")]
|
|
[Space]
|
|
|
|
[SerializeField] private GameObject player;
|
|
[SerializeField] private Transform playerPosition;
|
|
[HideInInspector] public GameObject playerLink;
|
|
|
|
[Header("UI")]
|
|
[Space]
|
|
[SerializeField] private PhaseCounter phaseCounter;
|
|
|
|
[Space(3)]
|
|
[Header("For checking")]
|
|
[SerializeField] private BattleConfig battleConfig;
|
|
public int numberPhase = 1;
|
|
public int quantityEnemies = 0;
|
|
public bool waitingEndingRound = false;
|
|
[SerializeField] private BattleConfig nextLevel;
|
|
public List<GameObject> currentEnemies = new List<GameObject>();
|
|
|
|
private void Awake()
|
|
{
|
|
if (main != null && main != this)
|
|
{
|
|
Debug.LogWarning("2 session on the scene");
|
|
Destroy(this);
|
|
return;
|
|
}
|
|
main = this;
|
|
battleConfig = SearchNextLevel();
|
|
nextLevel = SearchNextLevel();
|
|
Debug.Log(battleConfig.name + " " + nextLevel.name);
|
|
}
|
|
public void Start()
|
|
{
|
|
phaseCounter.NewMaxPhase(battleConfig);
|
|
CreatePlayer();
|
|
FirstPhase();
|
|
}
|
|
|
|
public void FirstPhase()
|
|
{
|
|
var FP = battleConfig.battleCharacteristics.firstPhaseOptions;
|
|
int quantity = Random.Range(FP.minimumQuantity, FP.maximumQuantity + 1);
|
|
for (int i = 0; i < quantity; i++)
|
|
{
|
|
quantityEnemies++;
|
|
EnemyConfig enemy = Randomizer(i, FP.enemies);
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
|
|
|
}
|
|
}
|
|
public void SecondPhase()
|
|
{
|
|
|
|
if (!battleConfig.battleCharacteristics.SecondPhase)
|
|
{
|
|
Victory();
|
|
return;
|
|
}
|
|
phaseCounter.NewPhase();
|
|
var SP = battleConfig.battleCharacteristics.SecondPhaseOptions;
|
|
int quantity = Random.Range(SP.minimumQuantity, SP.maximumQuantity);
|
|
for (int i = 0; i < quantity; i++)
|
|
{
|
|
quantityEnemies++;
|
|
EnemyConfig enemy = Randomizer(i, SP.enemies);
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
|
|
|
}
|
|
}
|
|
public void ThirdPhase()
|
|
{
|
|
if (!battleConfig.battleCharacteristics.ThirdPhase)
|
|
{
|
|
Victory();
|
|
return;
|
|
}
|
|
phaseCounter.NewPhase();
|
|
|
|
var TP = battleConfig.battleCharacteristics.ThirdPhaseOptions;
|
|
int quantity = Random.Range(TP.minimumQuantity, TP.maximumQuantity);
|
|
for (int i = 0; i < quantity; i++)
|
|
{
|
|
quantityEnemies++;
|
|
EnemyConfig enemy = Randomizer(i, TP.enemies);
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
|
|
|
}
|
|
}
|
|
|
|
//EventLogic
|
|
public void Victory()
|
|
{
|
|
onVictory?.Invoke();
|
|
}
|
|
public void Defeat()
|
|
{
|
|
onDefeat?.Invoke();
|
|
}
|
|
public void Inventory()
|
|
{
|
|
onInventory?.Invoke();
|
|
}
|
|
|
|
|
|
//EventLogic end
|
|
|
|
// RandomBlock
|
|
|
|
EnemyConfig Randomizer(int _position, List<EnemyConfig> _enemyConfigs)
|
|
{
|
|
for (int i = 0; i < _enemyConfigs.Count; i++)
|
|
{
|
|
if (_enemyConfigs[i].enemyCharacteristics.minimumPosition < _position + 1
|
|
|| _enemyConfigs[i].enemyCharacteristics.maximumPosition > _position + 1)
|
|
{
|
|
return RandomInRandomizer(_position, _enemyConfigs);
|
|
}
|
|
}
|
|
Debug.LogError("WARNING! no config found for position" + _position.ToString());
|
|
return null;
|
|
}
|
|
EnemyConfig RandomInRandomizer(int _position, List<EnemyConfig> _enemyConfigs)
|
|
{
|
|
int _numberEnemy;
|
|
_numberEnemy = Random.Range(0, _enemyConfigs.Count);
|
|
|
|
if (_enemyConfigs[_numberEnemy].enemyCharacteristics.minimumPosition > _position + 1
|
|
|| _enemyConfigs[_numberEnemy].enemyCharacteristics.maximumPosition < _position + 1)
|
|
{
|
|
return Randomizer(_position, _enemyConfigs);
|
|
}
|
|
return _enemyConfigs[_numberEnemy];
|
|
}
|
|
|
|
// RandomBlock end
|
|
|
|
//EnemyBlock
|
|
|
|
public GameObject CreateEnemies(EnemyConfig _config, int _position)
|
|
{
|
|
for (int i = 0; i < enemyPrefabs.Count; i++)
|
|
{
|
|
if (enemyPrefabs[i].GetComponent<Enemy>().enemyConfig == _config)
|
|
{
|
|
GameObject _enemy = Instantiate(enemyPrefabs[i], enemyUIPositions[_position]);
|
|
_enemy.GetComponent<Enemy>().currentPosition = _position;
|
|
return (_enemy);
|
|
}
|
|
}
|
|
Debug.LogError("enemy prefab not found. Please, check battleConfig and enemyPrefabs");
|
|
return null;
|
|
|
|
}
|
|
public void SwitchEnemyPosition()
|
|
{
|
|
for (int i = 0; i < currentEnemies.Count; i++)
|
|
{
|
|
currentEnemies[i].GetComponent<Enemy>().currentPosition = i;
|
|
currentEnemies[i].transform.position = enemyUIPositions[currentEnemies[i].GetComponent<Enemy>().currentPosition].position;
|
|
|
|
}
|
|
}
|
|
public void EnemiesStep()
|
|
{
|
|
for (int i = 0; i < currentEnemies.Count; i++)
|
|
{
|
|
if (currentEnemies[i] != null)
|
|
{
|
|
currentEnemies[i].GetComponent<Enemy>().Turn();
|
|
}
|
|
}
|
|
}
|
|
public void EnemyDeath(GameObject _deadEnemy)
|
|
{
|
|
quantityEnemies--;
|
|
for (int i = 0; i < currentEnemies.Count; i++)
|
|
{
|
|
if (currentEnemies[i] == _deadEnemy)
|
|
{
|
|
Destroy(currentEnemies[i]);
|
|
currentEnemies.RemoveAt(i);
|
|
}
|
|
}
|
|
if (quantityEnemies == 0)
|
|
{
|
|
|
|
StartCoroutine(ChekingCoroutine());
|
|
}
|
|
SwitchEnemyPosition();
|
|
}
|
|
IEnumerator ChekingCoroutine()
|
|
{
|
|
waitingEndingRound = true;
|
|
currentEnemies.Clear();
|
|
yield return new WaitForSeconds(0.2f);
|
|
if (numberPhase == 1 && battleConfig.battleCharacteristics.SecondPhase)
|
|
{
|
|
numberPhase++;
|
|
SecondPhase();
|
|
}
|
|
else if (numberPhase == 2 && battleConfig.battleCharacteristics.ThirdPhase)
|
|
{
|
|
numberPhase++;
|
|
ThirdPhase();
|
|
}
|
|
else
|
|
{
|
|
Victory();
|
|
}
|
|
waitingEndingRound = false;
|
|
}
|
|
//EnemyBlock end
|
|
|
|
public void CreatePlayer()
|
|
{
|
|
Instantiate(player, playerPosition);
|
|
Player.main.playerConfig = playerConfig;
|
|
Player.main.NewInformation();
|
|
}
|
|
|
|
|
|
public void LaunchNextLevel()
|
|
{
|
|
if (nextLevel == null)
|
|
{
|
|
DataHolder.main.mission = null;
|
|
DataHolder.main.ReturnInMainMenu();
|
|
return;
|
|
}
|
|
phaseCounter.NewMaxPhase(nextLevel);
|
|
onNextLevel?.Invoke();
|
|
}
|
|
public void NextLevel()
|
|
{
|
|
|
|
battleConfig = nextLevel;
|
|
nextLevel = SearchNextLevel() ;
|
|
|
|
FirstPhase();
|
|
}
|
|
public BattleConfig SearchNextLevel()
|
|
{
|
|
if(DataHolder.main.mission != null)
|
|
{
|
|
return DataHolder.main.NextBattle();
|
|
}
|
|
else
|
|
{
|
|
return DataHolder.main.RandomLevel(DataHolder.complexityLevels.all);
|
|
}
|
|
}
|
|
} |