PO/Assets/Scripts/Session.cs

187 lines
5.0 KiB
C#
Raw Normal View History

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
{
public static Session instance;
2022-01-12 10:06:03 +03:00
[SerializeField] private PlayerConfig playerConfig;
[SerializeField] private BattleConfig battleConfig;
[SerializeField] private UnityEvent onVictory;
[SerializeField] private UnityEvent onDefeat;
2022-01-12 10:06:03 +03:00
[Header("Enemies")]
[Space]
[Header("BASE SETTINGS. Don't touch!")]
[Space]
2022-01-13 12:07:16 +03:00
[SerializeField] private List<GameObject> enemySpritePositions = new List<GameObject>();
[SerializeField] private List<Transform> enemyUIPositions = new List<Transform>();
[SerializeField] private GameObject enemyPrefab;
2022-01-12 10:06:03 +03:00
[Header("Player")]
[SerializeField] private Transform playerPosition;
[SerializeField] private GameObject player;
2022-01-12 10:06:03 +03:00
[Space(3)]
[Header("For checking")]
public int numberPhase = 1;
public int quantityEnemies = 0;
[SerializeField] private List<GameObject> currentEnemies = new List<GameObject>();
private void Awake()
{
if (instance != null && instance != this)
{
Debug.LogWarning("2 session on the scene");
Destroy(this);
return;
}
instance = this;
}
2022-01-12 10:06:03 +03:00
public void Start()
{
CreatePlayer();
FirstPhase();
}
public void FirstPhase()
{
var FP = battleConfig.battleCharacteristics.firstPhaseOptions;
int quantity = Random.Range(FP.minimumQuantity, FP.maximumQuantity);
for (int i = 0; i < quantity; i++)
{
quantityEnemies++;
EnemyConfig enemy = Randomizer(i, FP.enemies);
currentEnemies.Add(CreateEnemies(enemy, i));
2022-01-12 10:06:03 +03:00
}
}
public void SecondPhase()
{
if (!battleConfig.battleCharacteristics.SecondPhase)
{
Victory();
return;
}
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));
2022-01-12 10:06:03 +03:00
}
}
public void ThirdPhase()
{
if (!battleConfig.battleCharacteristics.ThirdPhase)
{
Victory();
return;
}
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));
2022-01-12 10:06:03 +03:00
}
}
public void Victory()
{
onVictory?.Invoke();
}
public void Defeat()
{
onDefeat?.Invoke();
}
2022-01-13 12:07:16 +03:00
public void EnemyDeath()
2022-01-12 10:06:03 +03:00
{
quantityEnemies--;
2022-01-13 12:07:16 +03:00
//currentEnemies[index] = null;
if (quantityEnemies == 0)
2022-01-12 10:06:03 +03:00
{
2022-01-12 10:06:03 +03:00
StartCoroutine(ChekingCoroutine());
}
}
IEnumerator ChekingCoroutine()
{
yield return new WaitForSeconds(0.5f);
2022-01-12 10:06:03 +03:00
if (numberPhase == 1 && battleConfig.battleCharacteristics.SecondPhase)
{
numberPhase++;
SecondPhase();
}
else if (numberPhase == 2 && battleConfig.battleCharacteristics.ThirdPhase)
{
numberPhase++;
ThirdPhase();
}
else
{
Victory();
}
}
EnemyConfig Randomizer(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];
}
public GameObject CreateEnemies(EnemyConfig _config, int _position)
2022-01-12 10:06:03 +03:00
{
2022-01-13 12:07:16 +03:00
GameObject _enemy = Instantiate(enemyPrefab, enemyUIPositions[_position]);
Enemy _enemyCode = _enemy.GetComponent<Enemy>();
_enemyCode.enemyPosition = enemySpritePositions[_position];
_enemyCode.enemyConfig = _config;
_enemyCode.NewInformation();
return (_enemy);
2022-01-12 10:06:03 +03:00
}
public void CreatePlayer()
{
player.GetComponent<Player>().playerPosition = playerPosition;
player.GetComponent<Player>().playerConfig = playerConfig;
player.GetComponent<Player>().NewInformation();
}
public void EnemiesStep()
{
for (int i = 0; i < currentEnemies.Count; i++)
{
if (currentEnemies[i] != null)
{
currentEnemies[i].GetComponent<Enemy>().Turn();
}
}
2022-01-12 10:06:03 +03:00
}
}