252 lines
6.7 KiB
C#
252 lines
6.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
public class BattleOverallBuff : BuffElement
|
|
{
|
|
public float amount;
|
|
|
|
public BattleOverallBuff(float amount)
|
|
{
|
|
this.amount = amount;
|
|
}
|
|
}
|
|
|
|
public class 夺取 : BattleOverallBuff
|
|
{
|
|
public 夺取(float amount) : base(amount)
|
|
{
|
|
}
|
|
|
|
public void OnStartingEffect(GameObject ob)
|
|
{
|
|
BattleManager.Instance.EnemySpawnRelicProp += amount;
|
|
BattleManager.Instance.BattleBuffs.Remove(this);
|
|
}
|
|
}
|
|
|
|
public class 鼓舞士兵 : BattleOverallBuff
|
|
{
|
|
public float hpAddition;
|
|
public float attackAddition;
|
|
public 鼓舞士兵(float amount, float hpAddition, float attackAddition) : base(amount)
|
|
{
|
|
this.hpAddition = hpAddition;
|
|
this.attackAddition = attackAddition;
|
|
}
|
|
|
|
public void OnStartingEffect(GameObject ob)
|
|
{
|
|
BattleManager.Instance.BattleBlobs.ForEach(
|
|
b =>
|
|
{
|
|
b.MaxHP += (int)hpAddition;
|
|
b.AttackDamageMultiplier += attackAddition;
|
|
});
|
|
BattleManager.Instance.BattleBuffs.Remove(this);
|
|
}
|
|
}
|
|
|
|
public class 不屈 : BattleOverallBuff
|
|
{
|
|
|
|
public 不屈(float amount) : base(amount)
|
|
{
|
|
}
|
|
|
|
public void OnStartingEffect(GameObject ob)
|
|
{
|
|
}
|
|
|
|
public void OnUpdatingEffect()
|
|
{
|
|
BattleManager.Instance.BattleBlobs.ForEach(b =>
|
|
{
|
|
b.CurrentHP += amount * Time.deltaTime * b.MaxHP;
|
|
});
|
|
}
|
|
}
|
|
|
|
public class 赶尸 : BattleOverallBuff
|
|
{
|
|
|
|
public 赶尸(float amount) : base(amount)
|
|
{
|
|
}
|
|
|
|
public void OnStartingEffect(GameObject ob)
|
|
{
|
|
BattleManager.Instance.OnEnemyDie += (enemy) =>
|
|
{
|
|
//TODO: 把敌人加入为友方
|
|
if (Random.Range(0f, 1f) < amount)
|
|
{
|
|
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
public class BattleManager : Singleton<BattleManager>
|
|
{
|
|
public List<GameObject> EnemyPrefabs = new();
|
|
public List<int> SpawnWeight = new();
|
|
public Transform EnemySpawnLocation;
|
|
|
|
public BattleWall Wall;
|
|
|
|
public GameObject BattleBlobPrefab;
|
|
|
|
public Action<BattleBlob> onBlobRetire;
|
|
|
|
public CancellationTokenSource SpawnEnemyToken = new();
|
|
|
|
public float EnemySpawnRelicProp = 0.1f;
|
|
|
|
public List<BuffElement> BattleBuffs = new();
|
|
|
|
public List<BattleBlob> BattleBlobs = new();
|
|
|
|
public Action<EnemyController> OnEnemyDie;
|
|
|
|
//每秒产兵量
|
|
public float SpawnInter => 10-((GameProcedureManager.Instance.CurrentDifficulty *15)/1000f) ;
|
|
|
|
private void Start()
|
|
{
|
|
if (SpawnWeight.Count != EnemyPrefabs.Count)
|
|
{
|
|
SpawnWeight = (from t in EnemyPrefabs select 1).ToList();
|
|
}
|
|
|
|
SpawnEnemyToken = new();
|
|
|
|
GameProcedureManager.Instance.OnDay += () =>
|
|
{
|
|
SpawnEnemyToken.Cancel();
|
|
SpawnEnemyToken = new();
|
|
FindObjectsByType<EnemyController>(FindObjectsInactive.Include, FindObjectsSortMode.None).ToList()
|
|
.Where(e => e.Health != 0).ToList().ForEach(e => e.Health = 0);
|
|
};
|
|
|
|
GameProcedureManager.Instance.OnNight += () =>
|
|
{
|
|
SpawnEnemies(SpawnEnemyToken.Token).Forget();
|
|
};
|
|
|
|
OnEnemyDie += (EnemyController enemy) =>
|
|
{
|
|
//掉遗物
|
|
if (Random.Range(0f, 1f) < EnemySpawnRelicProp)
|
|
{
|
|
print("TODO: 生成遗物");
|
|
}
|
|
};
|
|
// BattleManager.Instance.onBlobRetire += blob => blob.gameObject.SetActive(true);
|
|
}
|
|
|
|
public async UniTaskVoid SpawnEnemies(CancellationToken token)
|
|
{
|
|
if (GameProcedureManager.Instance.DayCount == 0) return;
|
|
while(true){
|
|
await UniTask.WaitForSeconds(SpawnInter).AttachExternalCancellation(token);
|
|
//GameProcedureManager.Instance.WoodCount += 1;
|
|
Debug.Log("Spawn");
|
|
AudioManager.Instance.SpawnEnemySFX(GetComponent<AudioSource>());
|
|
var prefab = EnemyPrefabs.WeightPick(SpawnWeight);
|
|
var generatedEnemy = Instantiate(prefab, EnemySpawnLocation.position, Quaternion.identity);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddBuff(BuffElement element)
|
|
{
|
|
BattleBuffs.Add(element);
|
|
element.OnStartingEffect(this.gameObject);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
BattleBuffs.ForEach(b=>b.OnBeforeComputing());
|
|
BattleBuffs.ForEach(b=>b.OnUpdatingEffect());
|
|
BattleBuffs.ForEach(b=>b.OnAfterComputing());
|
|
|
|
//作弊部分
|
|
if (Input.GetKeyDown(KeyCode.S))
|
|
SpawnBattleBlob<BattleBlobWarrior>(null);
|
|
if (Input.GetKeyDown(KeyCode.A))
|
|
SpawnBattleBlob<BattleBlobArcher>(null);
|
|
}
|
|
|
|
public T SpawnBattleBlob<T>(BlobController controller) where T: BattleBlobJob
|
|
{
|
|
var go = Instantiate(BattleBlobPrefab);
|
|
go.transform.position = new Vector3(transform.position.x, EnemySpawnLocation.position.y, transform.position.z) + Vector3.right * Random.Range(-1, 1);
|
|
var result = go.AddComponent<T>();
|
|
|
|
go.GetComponent<BattleBlob>().originalBlob = controller;
|
|
go.GetComponent<BattleBlob>().Job = typeof(T);
|
|
controller?.gameObject.SetActive(false);
|
|
BattleBlobs.Add(go.GetComponent<BattleBlob>());
|
|
return result;
|
|
}
|
|
|
|
public BattleBlobJob SpawnBattleBlob(Type typeOfBlob, BlobController controller)
|
|
{
|
|
var go = Instantiate(BattleBlobPrefab);
|
|
go.transform.position = transform.position + Vector3.right * Random.Range(-1, 1);
|
|
var result = go.AddComponent(typeOfBlob);
|
|
|
|
go.GetComponent<BattleBlob>().originalBlob = controller;
|
|
go.GetComponent<BattleBlob>().Job = typeOfBlob;
|
|
controller?.gameObject.SetActive(false);
|
|
BattleBlobs.Add(go.GetComponent<BattleBlob>());
|
|
return result as BattleBlobJob;
|
|
}
|
|
|
|
public void Add团肉之墙(float amount)
|
|
{
|
|
Wall.AddBuff(new 团肉之强(amount));
|
|
}
|
|
|
|
public void Add加固(float amount)
|
|
{
|
|
Wall.AddBuff(new 加固(amount));
|
|
}
|
|
|
|
public void Add不死性(float amount)
|
|
{
|
|
Wall.AddBuff(new 不死性(amount));
|
|
}
|
|
|
|
public void Add夺取(float amount)
|
|
{
|
|
AddBuff(new 夺取(amount));
|
|
}
|
|
|
|
public void Add鼓舞士兵(float hp, float attack)
|
|
{
|
|
AddBuff(new 鼓舞士兵(0, hp, attack));
|
|
}
|
|
|
|
public void Add不屈(float amout)
|
|
{
|
|
AddBuff(new 不屈(amout));
|
|
}
|
|
|
|
public void Add赶尸(float amout)
|
|
{
|
|
AddBuff(new 赶尸(amout));
|
|
}
|
|
}
|