101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
using GeometryTD.Entity;
|
|
using GeometryTD.Entity.EntityData;
|
|
using GameFramework.Event;
|
|
using GeometryTD;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.CustomComponent
|
|
{
|
|
public class EnemyManagerComponent : GameFrameworkComponent
|
|
{
|
|
private EntityComponent _entity;
|
|
|
|
private float _spawnEnemyInterval = 5f;
|
|
private float _spawnEnemyTimer;
|
|
private float _spawnEnemyAccelerate = 0.5f;
|
|
private int _spawnEnemyCount = 5;
|
|
private int _spawnEnemyMaxCount = 5000;
|
|
private int _currentEnemyCount;
|
|
private int _spawnDistanceFromPlayer = 10;
|
|
private int _currentSpawnEnemyId = 0;
|
|
|
|
private Transform _player;
|
|
|
|
public void OnInit()
|
|
{
|
|
_entity = GameEntry.Entity;
|
|
|
|
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
|
|
GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
|
|
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
|
}
|
|
|
|
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
_spawnEnemyTimer += elapseSeconds;
|
|
if (_spawnEnemyTimer < _spawnEnemyInterval) return;
|
|
SpawnEnemy();
|
|
_spawnEnemyTimer = 0;
|
|
_spawnEnemyInterval = Mathf.Max(0.1f, _spawnEnemyInterval - _spawnEnemyAccelerate);
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
|
|
GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
|
|
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
|
}
|
|
|
|
private void SpawnEnemy()
|
|
{
|
|
if (_player == null) return;
|
|
for (int i = 0; i < _spawnEnemyCount; i++)
|
|
{
|
|
if (_currentEnemyCount >= _spawnEnemyMaxCount) break;
|
|
_entity.ShowEnemy(new EnemyData(_currentSpawnEnemyId % _spawnEnemyMaxCount, 1001, _player,
|
|
GetRandomPosition(), 10, 2));
|
|
_currentSpawnEnemyId++;
|
|
}
|
|
}
|
|
|
|
private Vector3 GetRandomPosition()
|
|
{
|
|
float x = Random.Range(-1f, 1f);
|
|
float z = Random.Range(-1f, 1f);
|
|
Vector3 dir = new Vector3(x, 0, z).normalized;
|
|
return _player.position + dir * _spawnDistanceFromPlayer;
|
|
}
|
|
|
|
private void OnShowEntitySuccess(object sender, GameEventArgs e)
|
|
{
|
|
if (e is ShowEntitySuccessEventArgs ne)
|
|
{
|
|
if (ne.EntityLogicType == typeof(Enemy))
|
|
{
|
|
_currentEnemyCount++;
|
|
}
|
|
|
|
if (ne.EntityLogicType == typeof(Player))
|
|
{
|
|
_player = ne.Entity.transform;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnShowEntityFailure(object sender, GameEventArgs e)
|
|
{
|
|
}
|
|
|
|
private void OnHideEntityComplete(object sender, GameEventArgs e)
|
|
{
|
|
if (e is HideEntityCompleteEventArgs ne)
|
|
{
|
|
if (ne.EntityGroup.Name == "Enemy")
|
|
{
|
|
_currentEnemyCount--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |