2022-01-12 10:06:03 +03:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
|
|
|
public class Session : MonoBehaviour
|
|
|
|
|
{
|
2022-01-14 10:42:43 +03:00
|
|
|
|
public static Session main;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
[SerializeField] private PlayerConfig playerConfig;
|
|
|
|
|
|
|
|
|
|
[SerializeField] private UnityEvent onVictory;
|
2022-01-13 11:00:37 +03:00
|
|
|
|
[SerializeField] private UnityEvent onDefeat;
|
2022-01-17 17:49:27 +03:00
|
|
|
|
[SerializeField] private UnityEvent onInventory;
|
|
|
|
|
[SerializeField] private UnityEvent onNextLevel;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Header("Enemies")]
|
2022-01-17 17:49:27 +03:00
|
|
|
|
[Space (3)]
|
2022-01-12 10:06:03 +03:00
|
|
|
|
|
2022-01-13 12:07:16 +03:00
|
|
|
|
[SerializeField] private List<Transform> enemyUIPositions = new List<Transform>();
|
2022-01-15 10:29:16 +03:00
|
|
|
|
[SerializeField] private List<GameObject> enemyPrefabs;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
[Header("Player")]
|
2022-01-17 17:49:27 +03:00
|
|
|
|
[Space]
|
|
|
|
|
|
2022-01-18 22:10:57 +03:00
|
|
|
|
[SerializeField] private GameObject equipmentParent;
|
2022-01-13 11:00:37 +03:00
|
|
|
|
[SerializeField] private GameObject player;
|
2022-01-15 10:29:16 +03:00
|
|
|
|
[SerializeField] private Transform playerPosition;
|
2022-01-15 21:47:03 +03:00
|
|
|
|
[HideInInspector] public GameObject playerLink;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
|
2022-01-17 17:49:27 +03:00
|
|
|
|
[Header("UI")]
|
|
|
|
|
[Space]
|
|
|
|
|
[SerializeField] private PhaseCounter phaseCounter;
|
2022-01-20 02:46:04 +03:00
|
|
|
|
[SerializeField] private Transform incidentPosition;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
[Space(3)]
|
|
|
|
|
[Header("For checking")]
|
2022-01-17 17:49:27 +03:00
|
|
|
|
[SerializeField] private BattleConfig battleConfig;
|
2022-01-12 10:06:03 +03:00
|
|
|
|
public int numberPhase = 1;
|
|
|
|
|
public int quantityEnemies = 0;
|
2022-01-15 10:29:16 +03:00
|
|
|
|
public bool waitingEndingRound = false;
|
2022-01-13 11:00:37 +03:00
|
|
|
|
|
2022-01-20 02:46:04 +03:00
|
|
|
|
public List<GameObject> currentEnemies = new List<GameObject>();
|
|
|
|
|
public MissionConfig mission;
|
|
|
|
|
public int missionChecker = 0;
|
2022-01-13 11:00:37 +03:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2022-01-14 10:42:43 +03:00
|
|
|
|
if (main != null && main != this)
|
2022-01-13 11:00:37 +03:00
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("2 session on the scene");
|
|
|
|
|
Destroy(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-14 10:42:43 +03:00
|
|
|
|
main = this;
|
2022-01-20 02:46:04 +03:00
|
|
|
|
mission = DataHolder.main.mission;
|
2022-01-17 17:49:27 +03:00
|
|
|
|
battleConfig = SearchNextLevel();
|
2022-01-20 02:46:04 +03:00
|
|
|
|
if(battleConfig == null)
|
|
|
|
|
{
|
|
|
|
|
Incident();
|
|
|
|
|
battleConfig = SearchNextLevel();
|
|
|
|
|
}
|
2022-01-13 11:00:37 +03:00
|
|
|
|
}
|
2022-01-12 10:06:03 +03:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2022-01-17 17:49:27 +03:00
|
|
|
|
phaseCounter.NewMaxPhase(battleConfig);
|
2022-01-12 10:06:03 +03:00
|
|
|
|
CreatePlayer();
|
|
|
|
|
FirstPhase();
|
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
public void FirstPhase()
|
|
|
|
|
{
|
|
|
|
|
var FP = battleConfig.battleCharacteristics.firstPhaseOptions;
|
2022-01-14 10:42:43 +03:00
|
|
|
|
int quantity = Random.Range(FP.minimumQuantity, FP.maximumQuantity + 1);
|
2022-01-12 10:06:03 +03:00
|
|
|
|
for (int i = 0; i < quantity; i++)
|
|
|
|
|
{
|
|
|
|
|
quantityEnemies++;
|
|
|
|
|
EnemyConfig enemy = Randomizer(i, FP.enemies);
|
2022-01-13 11:00:37 +03:00
|
|
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void SecondPhase()
|
|
|
|
|
{
|
|
|
|
|
if (!battleConfig.battleCharacteristics.SecondPhase)
|
|
|
|
|
{
|
|
|
|
|
Victory();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
phaseCounter.NewPhase();
|
2022-01-12 10:06:03 +03:00
|
|
|
|
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);
|
2022-01-13 11:00:37 +03:00
|
|
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
2022-01-14 10:42:43 +03:00
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void ThirdPhase()
|
|
|
|
|
{
|
|
|
|
|
if (!battleConfig.battleCharacteristics.ThirdPhase)
|
|
|
|
|
{
|
|
|
|
|
Victory();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
phaseCounter.NewPhase();
|
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
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);
|
2022-01-13 11:00:37 +03:00
|
|
|
|
currentEnemies.Add(CreateEnemies(enemy, i));
|
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:49:27 +03:00
|
|
|
|
//EventLogic
|
2022-01-12 10:06:03 +03:00
|
|
|
|
public void Victory()
|
|
|
|
|
{
|
|
|
|
|
onVictory?.Invoke();
|
|
|
|
|
}
|
2022-01-13 11:00:37 +03:00
|
|
|
|
public void Defeat()
|
|
|
|
|
{
|
|
|
|
|
onDefeat?.Invoke();
|
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
public void Inventory()
|
2022-01-12 10:06:03 +03:00
|
|
|
|
{
|
2022-01-17 17:49:27 +03:00
|
|
|
|
onInventory?.Invoke();
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//EventLogic end
|
2022-01-14 10:42:43 +03:00
|
|
|
|
|
|
|
|
|
// RandomBlock
|
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
EnemyConfig Randomizer(int _position, List<EnemyConfig> _enemyConfigs)
|
2022-01-14 10:42:43 +03:00
|
|
|
|
{
|
|
|
|
|
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)
|
2022-01-12 10:06:03 +03:00
|
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 10:42:43 +03:00
|
|
|
|
// RandomBlock end
|
|
|
|
|
|
2022-01-17 17:49:27 +03:00
|
|
|
|
//EnemyBlock
|
|
|
|
|
|
2022-01-13 11:00:37 +03:00
|
|
|
|
public GameObject CreateEnemies(EnemyConfig _config, int _position)
|
2022-01-12 10:06:03 +03:00
|
|
|
|
{
|
2022-01-15 10:29:16 +03:00
|
|
|
|
for (int i = 0; i < enemyPrefabs.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (enemyPrefabs[i].GetComponent<Enemy>().enemyConfig == _config)
|
|
|
|
|
{
|
|
|
|
|
GameObject _enemy = Instantiate(enemyPrefabs[i], enemyUIPositions[_position]);
|
2022-01-15 12:11:34 +03:00
|
|
|
|
_enemy.GetComponent<Enemy>().currentPosition = _position;
|
2022-01-15 10:29:16 +03:00
|
|
|
|
return (_enemy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Debug.LogError("enemy prefab not found. Please, check battleConfig and enemyPrefabs");
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
2022-01-15 12:11:34 +03:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-17 17:49:27 +03:00
|
|
|
|
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
|
2022-01-15 12:11:34 +03:00
|
|
|
|
|
2022-01-12 10:06:03 +03:00
|
|
|
|
public void CreatePlayer()
|
|
|
|
|
{
|
2022-01-15 10:29:16 +03:00
|
|
|
|
Instantiate(player, playerPosition);
|
|
|
|
|
Player.main.playerConfig = playerConfig;
|
|
|
|
|
Player.main.NewInformation();
|
2022-01-13 11:00:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-17 17:49:27 +03:00
|
|
|
|
public void LaunchNextLevel()
|
2022-01-13 11:00:37 +03:00
|
|
|
|
{
|
2022-01-20 02:46:04 +03:00
|
|
|
|
|
|
|
|
|
onNextLevel?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
public void LaunchMainMenu()
|
|
|
|
|
{
|
|
|
|
|
DataHolder.main.mission = null;
|
|
|
|
|
DataHolder.main.ReturnInMainMenu();
|
|
|
|
|
}
|
|
|
|
|
public void NextLevel()
|
|
|
|
|
{
|
|
|
|
|
battleConfig = SearchNextLevel() ;
|
|
|
|
|
if (battleConfig == null)
|
2022-01-13 11:00:37 +03:00
|
|
|
|
{
|
2022-01-17 17:49:27 +03:00
|
|
|
|
DataHolder.main.mission = null;
|
|
|
|
|
DataHolder.main.ReturnInMainMenu();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-20 02:46:04 +03:00
|
|
|
|
phaseCounter.NewMaxPhase(battleConfig);
|
2022-01-17 17:49:27 +03:00
|
|
|
|
FirstPhase();
|
|
|
|
|
}
|
|
|
|
|
public BattleConfig SearchNextLevel()
|
|
|
|
|
{
|
2022-01-20 02:46:04 +03:00
|
|
|
|
if(mission != null)
|
2022-01-17 17:49:27 +03:00
|
|
|
|
{
|
2022-01-20 02:46:04 +03:00
|
|
|
|
return NextBattle();
|
2022-01-17 17:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return DataHolder.main.RandomLevel(DataHolder.complexityLevels.all);
|
2022-01-13 11:00:37 +03:00
|
|
|
|
}
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|
2022-01-20 02:46:04 +03:00
|
|
|
|
public BattleConfig NextBattle()
|
|
|
|
|
{
|
|
|
|
|
missionChecker++;
|
|
|
|
|
if (missionChecker > mission.missionItems.Count)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return mission.missionItems[missionChecker - 1].battle;
|
|
|
|
|
}
|
2022-01-18 22:10:57 +03:00
|
|
|
|
public void UseEquipment()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < DataHolder.main.PlayerEquipment.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
Instantiate(DataHolder.main.PlayerEquipment[i], equipmentParent.transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-20 02:46:04 +03:00
|
|
|
|
public void Incident()
|
|
|
|
|
{
|
|
|
|
|
if (mission == null || missionChecker >= mission.missionItems.Count) return;
|
|
|
|
|
|
|
|
|
|
Instantiate(mission.missionItems[missionChecker - 1].incident, incidentPosition);
|
|
|
|
|
}
|
2022-01-12 10:06:03 +03:00
|
|
|
|
}
|