// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-05-20
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
using System.Collections;
using UnityEngine;
namespace G
{
partial class GameLevel
{
const int WAVE_INDEX = 0;
const int DELAY_INDEX = 1;
const int AMOUNT_INDEX = 2;
const int POINT_INDEX = 3;
const int SPAWN_INDEX = 4;
#region Field
public Transform pt_summonfog;
///
/// 1 enemy 2 boss 3
///
private int _spawnType = -1;
///
///
///
private float _spawnDelay = 1f;
///
/// 刷敌波数
///
private int _minionWave = 0;
private float _minionTime = 0f;
private int _minionCount;
///
/// boss
///
private int _bossWave = -1;
private float _bossTime = 0f;
private int _bossCount;
///
/// 刷敌波数
///
private int _curWave;
private int _maxWave;
///
/// 敌人数量
///
private int _maxEnemyCount = 16;
///
/// 敌人数量(包括boss)
///
private int _enemyCount;
private int _enemyKill;
private int _rndPoint;
private bool _countdown;
//
private float _sumonTime;
private int _summonCount;
private Vector3 _summonPos;
private int[] _summonInfo;
private float _spawnStartTime = -9999999f;
private bool _isChangeRampageLight;//暴走场景灯光
#endregion
#region Property
///
///
///
public int curWave
{
get => _curWave;
set
{
_curWave = value;
GlobalNotifier.PostNotification(GlobalDefine.EVENT_STAGE_STATE, (_curWave + "/" + _maxWave), "wave"); ;
}
}
///
///
///
public int maxWave
{
get => _maxWave;
set => _maxWave = value;
}
///
/// 场上怪物数量
///
public int enemyCount
{
get => _enemyCount;
set
{
_enemyCount = value;
GlobalNotifier.PostNotification(GlobalDefine.EVENT_STAGE_STATE, _enemyCount.ToString(), "enemy");
}
}
///
///
///
public int maxEnemyCount => _maxEnemyCount;
///
///
///
public int enemyKillCount => _enemyKill;
#endregion
#region Method
///
///
///
public void StartSpawn()
{
_spawnType = 1;
_spawnDelay = 0f;
enemyCount = 0;
_countdown = false;
_spawnStartTime = 0f;
_isChangeRampageLight = true;
if (stageMainLight)
{
stageMainLight.intensity = 1.5f;
}
var stageInfo = this.curStageItem;
if (stageInfo.minionWaves != null && stageInfo.minionWaves.Length > 0)
{
maxWave = GetWave(stageInfo.minionWaves[stageInfo.minionWaves.Length - 1]);
curWave = GetWave(stageInfo.minionWaves[0]);
}
SpawnProps();
if (stageMode == GameMode.Escort)
{
EntityManager.Instance.CreateCart(new EntityInfo
{
itemId = 1,
z = 0.5f,
});
}
////显示剧情
//if (stageInfo.type == 3)
//{
//
// _spawnType = -2;
// return;
//}
//ChangeBGM(false);
}
///
///
///
public void StopSpawn()
{
_spawnType = -2;
_minionWave = -1;
_bossWave = -1;
_spawnStartTime = -9999999f;
if (!_isChangeRampageLight)
{
StartCoroutine(LightChangeBright());
}
}
private void SpawnProps()
{
var propInfos = curStageItem.propInfos;
if (propInfos != null && propInfos.Length > 0)
{
int pc = Random.Range(propInfos[0], propInfos[1]);
int sp = propInfos[2];
int id = propInfos[3];
var rndPointIndexs = F.ListPool.Get();
for (int i = 0; i < MAX_PROP_COUNT; i++)
{
rndPointIndexs.Add(i);
}
for (int i = 0; i < pc && i < MAX_PROP_COUNT; i++)
{
var rndIndex = sp;
if (sp == 0)
{
if (rndPointIndexs.Count > 0)
{
var index = Random.Range(0, rndPointIndexs.Count);
rndIndex = rndPointIndexs[index];
rndPointIndexs.RemoveAt(index);
}
else
{
rndIndex = Random.Range(0, MAX_PROP_COUNT);
}
}
if (rndIndex < MAX_PROP_COUNT)
{
var rndPos = _propPoints[rndIndex];
EntityInfo ei = new EntityInfo()
{
itemId = id,
x = rndPos.x,
z = rndPos.z,
};
EntityManager.Instance.CreateProp(ei);
}
}
F.ListPool.Release(rndPointIndexs);
}
}
///
///
///
public void EnemyDead()
{
enemyCount--;
_enemyKill++;
CheckSpawnFinish();
}
void CheckSpawnFinish()
{
if (enemyCount <= 0 && _spawnType < 0)
{
FinishStage();
}
}
public void MinionKill(int id)
{
EntityMainPlayer.Instance.KillEnemy();
OnMinionKill(1);
}
///
///
///
///
public void BossKill(int _enemykind)
{
EntityMainPlayer.Instance.KillEnemy();
OnBossKill(1);
//if (!infinitymode && bosscount <= 0)
//{
// bossremain--;
// if (bossremain <= 0)
// {
// script_IngameUI.WaveSet(100);
// }
//}
//ChangeBGM(false);
if (normalMode && FunctionProxy.Instance.GetFunctionUnlock(FunctionProxy.FunctionId.文学))
{
int chapterId = curChapterId;
var dropBook = BookProxy.Instance.DropBookByChapter(chapterId);
if (dropBook != null)
{
Vector3 a = Random.onUnitSphere * Random.Range(0.18f, 0.26f);
EnemyHelper.Instance.CreateBox(141, dropBook.id, transform.position + a);
}
}
}
public Transform cut_boss;
public void BossCutin(int _enemykind)
{
cut_boss.gameObject.SetActive(true);
cut_boss.GetComponent().SetCutinTexture(_enemykind);
cut_boss.gameObject.SetActive(true);
cut_boss.GetComponent().CutinOn(new Vector3(3f, 2f, 1f), new Vector3(0.5f, 2f, 1f), new Vector3(7f, 7f, 0f), 0.1f, 1.6f, 1.8f, true);
//ChangeBGM(true);
}
///
///
///
///
///
public Vector3 GetSpawnPoint(int index)
{
Vector3 result = Vector3.zero;
if (_spawnPoints.Count > 0)
{
if (index == 0)
{
_rndPoint = (_rndPoint + 1) % _spawnPoints.Count;
result = _spawnPoints[_rndPoint];
}
else
{
result = _spawnPoints[(index - 1) % _spawnPoints.Count];
}
}
else
{
#if UNITY_EDITOR
Debug.Log("spawnpoint=0");
#endif
}
if (stageMode == GameMode.Escort)
{
if (EntityCart.Instance)
return EntityCart.Instance.position + result + Vector3.forward * Random.Range(-1f, 1f);// +Vector3.right * (Random.Range(-1, 1) + 0.5f) * 3f;
}
return result;
}
///
///
///
///
///
private int GetEnemyIdByIndex(int index)
{
var enemies = curLevelItem.enemies;
if (enemies != null && index > 0 && index <= enemies.Length)
{
int result = enemies[index - 1];
return result > 0 ? result : index;
}
return index;
}
///
///
///
///
///
///
public bool SpawnMinion(int id, Vector3 spawnPoint)
{
if (id > 0)
{
var info = new EntityInfo
{
name = "enemy",
itemId = id,
position = spawnPoint,
factorA = this.stageFactorA,
factorB = this.stageFactorB,
};
EntityManager.Instance.CreateMinion(info);
pt_summonfog.position = spawnPoint;
pt_summonfog.GetComponent().Play();
return true;
}
return false;
}
///
/// 小兵
///
///
private void SpawnMinion(int[] waveInfo)
{
//第4个参数开始
var minionId = GetEnemyIdByIndex(GlobalUtils.GetRandom(waveInfo, SPAWN_INDEX));
#if UNITY_EDITOR
Debug.Log("生成小兵 " + minionId);
#endif
if (SpawnMinion(minionId, GetSpawnPoint(GetPoint(waveInfo))))
{
}
}
///
/// BOSS
///
///
///
private void SpawnMinion(int[] waveInfo, Vector3 spawnPoint)
{
//第3个参数开始
var minionId = GetEnemyIdByIndex(GlobalUtils.GetRandom(waveInfo, 2));
#if UNITY_EDITOR
Debug.Log("召唤小兵" + minionId);
#endif
if (SpawnMinion(minionId, spawnPoint))
{
}
}
///
///
///
///
///
///
public bool SpawnBoss(int id, Vector3 spawnPoint)
{
if (id > 0)
{
var info = new EntityInfo
{
name = "boss",
itemId = id,
position = spawnPoint,
factorA = stageFactorA,
factorB = stageFactorB,
};
EntityManager.Instance.CreateBoss(info);
return true;
}
return false;
}
///
///
///
///
private void SpawnBoss(int[] waveInfo)
{
var bossId = GetEnemyIdByIndex(GlobalUtils.GetRandom(waveInfo, SPAWN_INDEX));
#if UNITY_EDITOR
Debug.Log("SpawnBoss" + bossId);
#endif
if (SpawnBoss(bossId, GetSpawnPoint(GetPoint(waveInfo))))
{
_countdown = true;
}
}
public void Summon(int amount, Vector3 summonPos)
{
if (_summonCount <= 0 && Time.time - _sumonTime > 5f)
{
_sumonTime = Time.time;
_summonCount = amount;
_summonPos = summonPos;
InvokeRepeating(nameof(Summon_p), 0.1f, 0.5f);
}
}
private void Summon_p()
{
if (enemyCount > 0)
{
if (_summonInfo != null)
{
Vector3 position = _summonPos + Random.onUnitSphere * 0.3f;
position.y = 0f;
SpawnMinion(_summonInfo, position);
enemyCount++;
_summonCount--;
}
}
else
{
_summonCount = 0;
CancelInvoke(nameof(Summon_p));
}
if (_summonCount <= 0)
{
_summonCount = 0;
CancelInvoke(nameof(Summon_p));
}
}
///
///
///
///
///
private static int GetWave(int[] info)
{
return info[WAVE_INDEX];
}
///
///
///
///
///
private static float GetDelay(int[] info)
{
return info[DELAY_INDEX] * 0.001f;
}
///
///
///
///
///
private static int GetAmount(int[] info)
{
return info[AMOUNT_INDEX];
}
///
///
///
///
///
private static int GetPoint(int[] info)
{
return info[POINT_INDEX];
}
private void UpdateSpawn()
{
//minion
if (_spawnType == 1)
{
//统计时间
_minionTime += Time.deltaTime;
if (_spawnDelay > 0f)
{
_spawnDelay -= Time.deltaTime;
}
else
{
var minionWaveInfos = this.curStageItem.minionWaves;
if (minionWaveInfos != null && _minionWave < minionWaveInfos.Length)
{
if (_minionWave < 0)
{
_minionWave = 0;
var first = minionWaveInfos[0];
_spawnDelay = GetDelay(first);
}
else
{
var waveInfo = minionWaveInfos[_minionWave];
if (curWave >= GetWave(waveInfo))
{
var amount = GetAmount(waveInfo);
for (int i = 0; i < amount; i++)
{
SpawnMinion(waveInfo);
}
enemyCount += amount;
_minionCount -= amount;
_minionWave += 1;
//
if (_minionWave < minionWaveInfos.Length)
{
var next = minionWaveInfos[_minionWave];
if (curWave >= GetWave(next))
{
_spawnDelay = GetDelay(next);
}
}
}
else
{
if (enemyCount <= 0)
{
_spawnDelay = GetDelay(waveInfo);
curWave = GetWave(waveInfo);
}
}
}
}
else
{
_minionWave = -1;
_spawnType = 2;
}
}
}
// boss
else if (_spawnType == 2)
{
_bossTime += Time.deltaTime;
if (_spawnDelay > 0f)
{
_spawnDelay -= Time.deltaTime;
}
else
{
var bossWaveInfos = this.curStageItem.bossWaves;
if (bossWaveInfos != null && bossWaveInfos.Length > 0)
{
if (_bossWave < 0)
{
_bossWave = 0;
var first = bossWaveInfos[0];
_spawnDelay = GetDelay(first);
_bossCount = GetAmount(first);
if (bossWaveInfos.Length > 1)
{
_summonInfo = bossWaveInfos[1];
}
}
else if (_bossWave == 0)
{
var waveInfo = bossWaveInfos[_bossWave];
if (_bossCount > 0)
{
SpawnBoss(waveInfo);
enemyCount++;
_bossCount--;
if (_bossCount <= 0)
{
_bossWave += 1;
}
else
{
_spawnDelay = GetDelay(waveInfo);
}
}
}
else
{
_bossWave = -1;
_spawnType = -2;
CheckSpawnFinish();
}
}
else
{
_bossWave = -1;
_spawnType = -2;
CheckSpawnFinish();
}
}
}
else
{
//Summon
UpdateSumon();
}
_spawnStartTime += Time.deltaTime;
if (_isChangeRampageLight && isRampage)
{
_isChangeRampageLight = false;
StartCoroutine(LightChangeDark());
}
//UpdateCountdown();
}
///
/// 场景灯光颜色渐变
///
private IEnumerator LightChangeDark()
{
float light = 1.5f;
while (light >= 0.6f)
{
light -= Time.deltaTime/2;
if (stageMainLight)
{
stageMainLight.intensity = light;
}
yield return null;
}
}
private IEnumerator LightChangeBright()
{
float light = 0.6f;
while (light <= 1.5f)
{
light += Time.deltaTime / 2;
if (stageMainLight)
{
stageMainLight.intensity = light;
}
yield return null;
}
}
private void UpdateSumon()
{
}
private void UpdateCountdown()
{
//if (enemyCount <= 3 && !_countdown)
//{
// var minions = EntityManager.Instance.FindAllMinionInScene();
// foreach (var minion in minions)
// {
// minion.CountDown();
// }
// _countdown = true;
//}
}
#endregion
}
}