using System; using System.Collections; using System.Collections.Generic; using System.Linq; using cfg.BlacksmithData; using Unity.VisualScripting; 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 List Fallings => _fallings; [SerializeField] private List _fallings = 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;//当前波次 public int Exp => exp; private int exp; public int ExpLevel => expLevel; private int expLevel; public int MaxExp => maxExp; private int maxExp; public int EquipmentIndex => equipmentIndex; private int equipmentIndex = 0; 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; equipmentIndex = 0; } /// /// 解锁装备 /// 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[equipmentIndex],ref bo); // OpenAdd(itemId,_battleEquipments[0],ref bo); // if (!bo&&openVideoSoldier) // { // OpenAdd(itemId,_battleEquipments[1],ref bo); // } // if (!bo&&openVideoSoldier) // { // OpenAdd(itemId,_battleEquipments[2],ref bo); // } } public void SetIndex(int idnex) { equipmentIndex = idnex; UIMgr.ins.ResetBattlePlayer(); } /// /// 添加一个道具到单位 /// /// /// /// 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() { SoundSystem.ins.AttackAudio(); 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); DataManager.RemoveItem(equipment.weaponID, (bool b) => { }); DataManager.RemoveItem(equipment.weapon2ID, (bool b) => { }); DataManager.RemoveItem(equipment.armorID, (bool b) => { }); DataManager.RemoveItem(equipment.audioUniversalID, (bool b) => { }); DataManager.RemoveItem(equipment.audioWeaponID, (bool b) => { }); DataManager.RemoveItem(equipment.audioWeapon2ID, (bool b) => { }); } _skillStates = new List(); hp = allHP; injured = 0; BDebug.Log(hp); nowTime = 0; exp = 0; expLevel = 1; maxExp = JsonTab.Instance.tables.ExpRogue.Get(expLevel).Developexp; if (playerItems!=null) { foreach (var p in playerItems) { if (p!=null&&p.player!=null) { PrefabPool.ins.RecycleObj(p.player.gameObject); p.player = null; } } } playerItems = new PlayerRunState[playerParent.Length]; for (int i = 0; i < playerParent.Length; i++) { var index = i; if (BattleEquipments[i].EquipmentLock) { BDebug.Log("111"); PrefabPool.ins.LoadObj("Prefab/player/player",index, (GameObject obj,int idex) => { obj.transform.parent = playerParent[idex]; obj.transform.localPosition=Vector3.zero; playerItems[idex] = new PlayerRunState(); playerItems[idex].player = obj.GetComponent(); playerItems[idex].equipent = BattleEquipments[idex]; playerItems[idex].RestState(); playerItems[idex].player.SetModel(idex+1); }); } } ResetLevelMouster(); player = true; UIMgr.ins.ResetHp(); UIMgr.ins.ResetExp(); UIMgr.ins.OpenCombatPanel(); } public void GetAttribute(ref int allHP,ref int aAttack) { 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); AddAttackNum(equipment.weaponID, ref aAttack); AddAttackNum(equipment.weapon2ID, ref aAttack); AddAttackNum(equipment.armorID, ref aAttack); AddAttackNum(equipment.audioUniversalID, ref aAttack); AddAttackNum(equipment.audioWeaponID, ref aAttack); AddAttackNum(equipment.audioWeapon2ID, ref aAttack); } } public bool BattleStart() { bool bo = false; foreach (var equipment in _battleEquipments) { if (equipment.weaponID!=0) { bo = true; return bo; } if (equipment.weapon2ID!=0) { bo = true; return bo; } if (equipment.armorID!=0) { bo = true; return bo; } if (equipment.audioUniversalID!=0) { bo = true; return bo; } if (equipment.audioWeaponID!=0) { bo = true; return bo; } if (equipment.audioWeapon2ID!=0) { bo = true; return bo; } } return bo; } public void AddFallings(Falling data) { _fallings.Add(data); } public void AddSkillStates(SkillState state) { _skillStates.Add(state); UIMgr.ins.ResetHp(); } public void SetInjured(int num) { injured += num; BDebug.Log(injured); UIMgr.ins.ResetHp(); if (injured>hp) { BattleEnd(false); } } 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; } } void AddAttackNum(int id,ref int num) { if (id!=0) { var data = JsonTab.Instance.tables.Item.Get(id); var weapon = JsonTab.Instance.attributeData(data.Includearms, DataManager.GetDrawLevel(data.Includearms)); num += weapon.Attack; } } public void BattleEnd(bool win) { if (win) { var id = DataManager.GetNowLevel(); for (int i = 0; i < JsonTab.Instance.tables.Level.DataList.Count; i++) { if (JsonTab.Instance.tables.Level.DataList[i].ID==id) { if (i+1=0; i--) { var m= _mousters[i]; _mousters.Remove(m); PrefabPool.ins.RecycleObj(m.gameObject); } for (int i = _bullets.Count - 1; i >= 0; i--) { var m= _bullets[i]; _bullets.Remove(m); PrefabPool.ins.RecycleObj(m.gameObject); } foreach (var battleItem in playerItems) { if (battleItem!=null&&battleItem.player!=null) { PrefabPool.ins.RecycleObj(battleItem.player.gameObject); battleItem.player = null; } } } // Update is called once per frame void Update() { if (!player) { if (playerItems!=null) { foreach (var player in playerItems) { if (player!=null&&player.player!=null) { player.player.Type = playerAniType.wait; } } } return; } nowTime += Time.deltaTime; if (_mousters!=null) { for (var i = _mousters.Count - 1; i >= 0; i--) { _mousters[i].Move(playerParent[0]); } } TeamUpdate(); BulletUpdata(); if (playerItems!=null) { foreach (var player in playerItems) { if (player!=null) { if (MousterCount>0) { player.RunTimeMove(); player.player.Type = playerAniType.play; } else { player.player.Type = playerAniType.wait; } } } } } /// /// 波次组刷新 /// 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) { BattleEnd(true); } } } /// /// 玩家子弹组刷新 /// 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); UIMgr.ins.ResetExp(); 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 if (data.Addskill!=0) { var skill = JsonTab.Instance.tables.Skill.Get(data.Addskill); var buffdata = JsonTab.Instance.tables.Buff.Get(skill.Skilladdbuff); AddBuff(buffdata.Bufftarget, buffdata); } else if (data.Addporp!=0) { _battleEquipments[0].AddRandomSkill(data.Addporp); } else { var skill = JsonTab.Instance.tables.Skill.Get(data.Replace); } 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.hp = scale.Buffparameter1; break; case 3: objData.cd = scale.Buffparameter1; break; case 4: objData.criticalStrike = scale.Buffparameter1; break; case 5: break; case 6: break; case 7: break; case 8: break; case 9: objData.piercing = scale.Buffparameter1; 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 List SkillStates => _skillStates; private List _skillStates = new List(); 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); } _skillStates = new List(); } public void AddRandomSkill(int skillID) { var state = new SkillState(skillID,"RandomState"); _skillStates.Add(state); BattleManager.ins.AddSkillStates(audioWeapon2); } public void RunTime() { weapon?.SkillStateEvent(); weapon2?.SkillStateEvent(); armor?.SkillStateEvent(); audioUniversal?.SkillStateEvent(); audioWeapon?.SkillStateEvent(); audioWeapon2?.SkillStateEvent(); foreach (var state in _skillStates) { state.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 / 1000; 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 / 1000; } } } 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 criticalStrike = 0; public int piercing = 0; public void SetBuff(BuffAddData data) { hp= hp + data.hp; attack = attack + data.attack; cd = cd + data.cd; criticalStrike = criticalStrike +data.criticalStrike; piercing = piercing + data.piercing; } } public class Falling { public int id; public int num; public Falling(int _id, int _num) { id = _id; num = _num; } }