using System; using System.Collections; using System.Collections.Generic; using System.Linq; using cfg; using cfg.BlacksmithData; using Unity.Mathematics; using UnityEngine; using Random = UnityEngine.Random; public class BattleManager : MonoBehaviour { public static BattleManager ins; [SerializeField] private Transform mousterPoint;//怪所在节点 [SerializeField] private Transform[] mousters;//怪生成区域 [SerializeField] private Transform[] playerParent;//玩家所在节点 public List Mousters => _mousters; [SerializeField] private List _mousters=new List();//怪列表 public int MousterCount => _mousters.Count; [SerializeField] private PlayerRunState[] playerItems;//玩家列表 [SerializeField] private List _mousterTeams = new List();//波次列表 [SerializeField] private List _bullets = new List();//玩家子弹列表 public List SkillStates => _skillStates; [SerializeField] private List _skillStates; [SerializeField] private List id=new List(); public float NowTime => nowTime; private float nowTime=0;//关卡内时间 public int HP => hp; [SerializeField] private int hp;//总血量 public int Injured => injured; private int injured;//受到的伤害 private bool player = false;//运行状态 private int mousterIndex=0;//当前波次 private int exp; private int expLevel; private int maxExp; public BattleEquipment[] BattleEquipments => _battleEquipments; private BattleEquipment[] _battleEquipments = new BattleEquipment[3]; /// /// 是否开启装备栏 /// public bool OpenVideoWeaponry => openVideoWeaponry; private bool openVideoWeaponry; /// /// 是否开启佣兵 /// public bool OpenVideoSoldier => openVideoSoldier; private bool openVideoSoldier; private Dictionary buff = new Dictionary(); private void Awake() { ins = this; } // Start is called before the first frame update void Start() { } #region 准备战斗阶段 /// /// 初始准备阶段装备数据 /// public void ResetBattleData() { _battleEquipments=new BattleEquipment[3]; for (int i = 0; i < _battleEquipments.Length; i++) { _battleEquipments[i] = new BattleEquipment(); } _battleEquipments[0].SetLock(true); openVideoWeaponry = false; openVideoSoldier = false; } /// /// 解锁装备 /// public void OpenVideoWeaponryEvent() { openVideoWeaponry = true; } /// /// 解锁佣兵 /// public void OpenVideoSoldierEvent() { openVideoSoldier = true; foreach (var battle in _battleEquipments) { battle.SetLock(true); } } /// /// 设置道具到指定单位 /// /// /// public void SaveBattleData(int index,BattleEquipment data) { if (!openVideoSoldier) { if (index!=0) { return; } } _battleEquipments[index] = data; } /// /// 添加道具到单位组 /// /// /// public void AddItem(int itemId,out bool bo) { bo = false; OpenAdd(itemId,_battleEquipments[0],ref bo); if (!bo&&openVideoSoldier) { OpenAdd(itemId,_battleEquipments[1],ref bo); } if (!bo&&openVideoSoldier) { OpenAdd(itemId,_battleEquipments[2],ref bo); } } /// /// 添加一个道具到单位 /// /// /// /// private void OpenAdd(int itemID, BattleEquipment data, ref bool bo) { var itemData = JsonTab.Instance.tables.Item.Get(itemID); if (data.weaponID==0) { data.weaponID = itemData.ID; bo = true; return; } if (data.weapon2ID==0) { data.weapon2ID = itemData.ID; bo = true; return; } if (data.armorID==0) { data.armorID = itemData.ID; bo = true; return; } if (data.audioUniversalID==0&&openVideoWeaponry) { data.audioUniversalID = itemData.ID; bo = true; return; } if (data.audioWeaponID==0&&openVideoWeaponry) { data.audioWeaponID = itemData.ID; bo = true; return; } if (data.audioWeapon2ID==0&&openVideoWeaponry) { data.audioWeapon2ID = itemData.ID; bo = true; } } /// /// 获取所有单位道具数量 /// /// /// public void GetItemNum(int itemID,out int num) { num = 0; EquipmentNum(itemID,_battleEquipments[0],ref num); EquipmentNum(itemID,_battleEquipments[1],ref num); EquipmentNum(itemID,_battleEquipments[2],ref num); } /// /// 获取单位该道具的数量 /// /// /// /// private void EquipmentNum(int itemid,BattleEquipment data,ref int num) { if (data.weaponID==itemid) { num++; } if (data.weapon2ID==itemid) { num++; } if (data.armorID==itemid) { num++; } if (data.audioUniversalID==itemid) { num++; } if (data.audioWeaponID==itemid) { num++; } if (data.audioWeapon2ID==itemid) { num++; } } #endregion public void StartBattle() { var allHP = 0; foreach (var equipment in _battleEquipments) { AddNum(equipment.weaponID, ref allHP); AddNum(equipment.weapon2ID, ref allHP); AddNum(equipment.armorID, ref allHP); AddNum(equipment.audioUniversalID, ref allHP); AddNum(equipment.audioWeaponID, ref allHP); AddNum(equipment.audioWeapon2ID, ref allHP); } _skillStates = new List(); hp = allHP; injured = 0; BDebug.Log(hp); nowTime = 0; exp = 0; expLevel = 1; maxExp = JsonTab.Instance.tables.ExpRogue.Get(expLevel).Developexp; playerItems = new PlayerRunState[playerParent.Length]; for (int i = 0; i < playerParent.Length; i++) { var index = i; if (BattleEquipments[i].EquipmentLock) { PrefabPool.ins.LoadObj("Battle_player", (GameObject obj) => { obj.transform.parent = playerParent[index]; obj.transform.localPosition=Vector3.zero; playerItems[index] = new PlayerRunState(); playerItems[index].player = obj.GetComponent(); playerItems[index].equipent = BattleEquipments[index]; playerItems[index].RestState(); }); } } ResetLevelMouster(); player = true; UIMgr.ins.ResetHp(); } public void AddSkillStates(SkillState state) { _skillStates.Add(state); } public void SetInjured(int num) { injured += num; BDebug.Log(injured); UIMgr.ins.ResetHp(); } void ResetLevelMouster() { mousterIndex = 0; BDebug.Log("mouster"); var levelData = JsonTab.Instance.tables.Level.Get(DataManager.GetNowLevel()); _mousterTeams = new List(); for (int i = 0; i < levelData.Levelwaves.Count; i++) { var team = new MousterTeam(); team.SaveMousterData(levelData.Levelwaves[i]); _mousterTeams.Add(team); } } void AddNum(int id,ref int num) { if (id!=0) { var data = JsonTab.Instance.tables.Item.Get(id); num+= JsonTab.Instance.attributeData(data.Includearms, DataManager.GetDrawLevel(data.Includearms)) .HP; } } // Update is called once per frame void Update() { if (!player) { return; } nowTime += Time.deltaTime; foreach (var mouster in _mousters) { mouster.Move(playerParent[0]); } TeamUpdate(); BulletUpdata(); if (playerItems!=null) { foreach (var player in playerItems) { if (player!=null&& MousterCount>0) { player.RunTimeMove(); } } } } /// /// 波次组刷新 /// public void TeamUpdate() { if (mousterIndex<_mousterTeams.Count) { if (_mousterTeams[mousterIndex].Type== MousterTeamType.ready) { _mousterTeams[mousterIndex].Type = MousterTeamType.wait; _mousterTeams[mousterIndex].SetEventDynamicData(); } if (_mousterTeams[mousterIndex].Type==MousterTeamType.wait&&_mousterTeams[mousterIndex].Update()) { _mousterTeams[mousterIndex].Type = MousterTeamType.player; AddMouster(); mousterIndex++; } } else { var bo = true; foreach (var unused in _mousterTeams.Where(mouster => mouster.Type!=MousterTeamType.end)) { bo = false; } if (bo) { BDebug.LogWarning("end"); } } } /// /// 玩家子弹组刷新 /// public void BulletUpdata() { for (int i = _bullets.Count-1; i >= 0; i--) { _bullets[i].BulletUpdate(); } } /// /// 添加玩家子弹 /// /// public void AddBullets(Bullet bullet ) { _bullets.Add(bullet); } /// /// 移除玩家子弹 /// /// public void RemoveBullets(Bullet bullet) { _bullets.Remove(bullet); PrefabPool.ins.RecycleObj(bullet.gameObject); } /// /// 添加怪 /// public void AddMouster() { var index = mousterIndex; foreach (var entity in _mousterTeams[mousterIndex].moustList) { PrefabPool.ins.LoadObj(entity._data.Prefab, (GameObject obj) => { obj.transform.parent = mousterPoint; var pos = new Vector3(Random.Range(mousters[0].position.x, mousters[1].position.x), Random.Range(mousters[0].position.y, mousters[1].position.y), 0); obj.transform.position=pos; AxisLookAt(obj.transform, obj.transform.position - new Vector3(0, -2, 0)); var m = obj.GetComponent(); m.InitMousterData(entity); _mousters.Add(m); _mousterTeams[index].AddObj(m); }); } } /// /// 移除怪 /// /// public void RemoveMouster(Mouster obj) { var index = mousterIndex; if (mousterIndex>=MousterCount) { index = mousterIndex - 1; } bool bo = false; foreach (var mouster in _mousterTeams) { mouster.Remove(obj,ref bo); if (bo) { break; } } _mousters.Remove(obj); PrefabPool.ins.RecycleObj(obj.gameObject); exp++; BDebug.LogWarning(exp); if (exp>=maxExp) { player = false; exp = 0; expLevel++; if (expLevel>15) { return; } maxExp = JsonTab.Instance.tables.ExpRogue.Get(expLevel).Developexp; UIMgr.ins.OpenRogenPanel(); } } public void SetRogen(SkillRogueData data) { if (data.Frontend!=0) { foreach (var state in _skillStates) { state.UpRogen(data); } } else { var skill = JsonTab.Instance.tables.Skill.Get(data.Addskill); var buffdata = JsonTab.Instance.tables.Buff.Get(skill.Skilladdbuff); AddBuff(buffdata.Bufftarget, buffdata); } player = true; } public BuffAddData Buff(int id) { if (buff.TryGetValue(id, out var buff1)) return buff1; var buffData = new BuffAddData(); buff.Add(id,buffData); return buff[id]; } public void AddBuff(int id, BuffData scale) { if (buff.ContainsKey(id)) { BuffAddData objData = new BuffAddData(); switch (scale.Buffeffect1) { case 1: objData.attack = scale.Buffparameter1; break; case 2: objData.cd = scale.Buffparameter1; break; case 3: objData.crit = scale.Buffparameter1; break; case 4: objData.critNum = scale.Buffparameter1; break; case 5: break; case 6: break; case 7: break; case 8: break; case 9: break; case 10: break; } buff[id].SetBuff(objData); } } /// /// 朝向通用接口 /// /// /// public void AxisLookAt(Transform tr_self, Vector3 lookPos) { Vector3 dir = lookPos - tr_self.position; dir.z = 0; float angle = Vector3.SignedAngle(Vector3.up, dir, Vector3.forward); Quaternion rotation = Quaternion.Euler(0, 0, angle); //将欧拉角转换为四元数 tr_self.rotation = rotation; } } public class BattleEquipment { public int playerID=0; public int weaponID=0; public int weapon2ID=0; public int armorID=0; public int audioUniversalID=0; public int audioWeaponID=0; public int audioWeapon2ID=0; public bool EquipmentLock => equipmentLock; private bool equipmentLock = false; public SkillState Weapon => weapon; private SkillState weapon; public SkillState Weapon2 => weapon2; private SkillState weapon2; public SkillState Armor => armor; private SkillState armor; public SkillState AudioUniversal => audioUniversal; private SkillState audioUniversal; public SkillState AudioWeapon => audioWeapon; private SkillState audioWeapon; public SkillState AudioWeapon2 => audioWeapon2; private SkillState audioWeapon2; public void SetLock(bool bo) { equipmentLock = bo; } public void RestSkill() { if (weaponID!=0) { weapon = new SkillState(weaponID,"weaponID"); BattleManager.ins.AddSkillStates(weapon); } if (weapon2ID!=0) { weapon2 = new SkillState(weapon2ID,"weapon2ID"); BattleManager.ins.AddSkillStates(weapon2); } if (armorID!=0) { armor = new SkillState(armorID,"armorID"); BattleManager.ins.AddSkillStates(armor); } if (audioUniversalID!=0) { audioUniversal = new SkillState(audioUniversalID,"audioUniversalID"); BattleManager.ins.AddSkillStates(audioUniversal); } if (audioWeaponID!=0) { audioWeapon = new SkillState(audioWeaponID,"audioWeaponID"); BattleManager.ins.AddSkillStates(audioWeapon); } if (audioWeapon2ID!=0) { audioWeapon2 = new SkillState(audioWeapon2ID,"audioWeapon2ID"); BattleManager.ins.AddSkillStates(audioWeapon2); } } public void RunTime() { weapon?.SkillStateEvent(); weapon2?.SkillStateEvent(); armor?.SkillStateEvent(); audioUniversal?.SkillStateEvent(); audioWeapon?.SkillStateEvent(); audioWeapon2?.SkillStateEvent(); } } public class SkillState { public int id; public ItemData itemData; public SkillData data; public float cd; public float cdTime; public int Attack => attack; private int attack = 0; public float Ratio => ratio; private float ratio = 1; public List> Actions = new List>(); public List WaitActions = new List(); public bool SkillLock => skillLock; private bool skillLock = false; public string Name => name; private string name; public SkillState(int _id,string na) { if (_id==0) { id = 0; return; } name = na; id = _id; itemData = JsonTab.Instance.tables.Item.Get(id); var attributeData = JsonTab.Instance.tables.WeaponSkill.Get(id); var weapon = JsonTab.Instance.attributeData(itemData.Includearms, DataManager.GetDrawLevel(itemData.Includearms)); attack = weapon.Attack; data = JsonTab.Instance.tables.Skill.Get(attributeData.Initialskill); ratio = data.SkillDamage; cd = data.SkillCD / 10000; cdTime = BattleManager.ins.NowTime; Actions = new List>(); skillLock = false; } public void ResetSkill() { } public void SkillCD() { // skillLock = false; cdTime = BattleManager.ins.NowTime; BDebug.LogWarning(name); foreach (var a in Actions) { a?.Invoke(this); } } public void AddAction(Action action) { Actions.Add(action); } public void RemoveAction(Action action) { Actions.Remove(action); } public void AddWaitActions(Action action) { WaitActions.Add(action); } public void RemoveWaitActions(Action action) { WaitActions.Remove(action); } public void SkillStateEvent() { // if (skillLock) // { // foreach (var a in WaitActions) // { // a?.Invoke(); // } // return; // } if (BattleManager.ins.NowTime-cdTime>cd*(1/(1+(float)BattleManager.ins.Buff(itemData.Includearms).cd/10000))) { // skillLock = true; SkillCD(); } } public void UpRogen(SkillRogueData rogueData) { if (rogueData.Frontend==data.ID) { data = JsonTab.Instance.tables.Skill.Get(rogueData.Replace); ratio = data.SkillDamage; cd = data.SkillCD / 10000; } } } public class PlayerRunState { public PlayerItem player; public BattleEquipment equipent; public void RestState() { equipent.RestSkill(); player.SetData(equipent); } public void RunTimeMove() { equipent.RunTime(); } } public class BuffAddData { public int id = 0; public int hp = 0; public int attack = 0; public int cd = 0; public int crit = 0; public int critNum = 0; public void SetBuff(BuffAddData data) { hp= hp + data.hp; attack = attack + data.attack; cd = cd + data.cd; } }