using System; using System.Collections; using System.Collections.Generic; 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; [SerializeField] private List _mousters=new List(); [SerializeField] private Vector3 lookVec; public int MousterCount => _mousters.Count; [SerializeField] private PlayerRunState[] playerItems; [SerializeField] private List _mousterTeams = new List(); public float NowTime => nowTime; private float nowTime=0; public int HP => hp; private int hp; private bool player = false; private int mousterIndex=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 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(); } openVideoWeaponry = false; openVideoSoldier = false; } /// /// 解锁装备 /// public void OpenVideoWeaponryEvent() { openVideoWeaponry = true; } /// /// 解锁佣兵 /// public void OpenVideoSoldierEvent() { openVideoSoldier = 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); } hp = allHP; BDebug.Log(hp); nowTime = 0; playerItems = new PlayerRunState[playerParent.Length]; for (int i = 0; i < playerParent.Length; i++) { var index = i; 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].equipent.RestSkill(); }); } ResetLevelMouster(); player = true; } 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) { AxisLookAt(mouster.transform, playerParent[0].position); mouster.Move(playerParent[0]); } TeamUpdate(); 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++; } } } public void AddMouster() { foreach (var entity in _mousterTeams[mousterIndex].moustList) { PrefabPool.ins.LoadObj(entity._data.Prefab, (GameObject obj) => { obj.transform.parent = mousterPoint; obj.transform.position=mousters[Random.Range(0,mousters.Length)].position; var m = obj.GetComponent(); m.InitMousterData(entity); _mousters.Add(m); }); } } public void RemoveMouster(Mouster obj) { _mousters.Remove(obj); PrefabPool.ins.RecycleObj(obj.gameObject); } void AxisLookAt(Transform tr_self, Vector3 lookPos) { Vector3 dir = lookPos - tr_self.position; 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 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 RestSkill() { if (weaponID!=0) { weapon = new SkillState(weaponID); } if (weapon2ID!=0) { weapon2 = new SkillState(weapon2ID); } if (armorID!=0) { armor = new SkillState(armorID); } if (audioUniversalID!=0) { audioUniversal = new SkillState(audioUniversalID); } if (audioWeaponID!=0) { audioWeapon = new SkillState(audioWeaponID); } if (audioWeapon2ID!=0) { audioWeapon2 = new SkillState(audioWeapon2ID); } } 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 List Actions = new List(); public List WaitActions = new List(); public bool SkillLock => skillLock; private bool skillLock = false; public SkillState(int _id) { if (_id==0) { id = 0; return; } id = _id; itemData = JsonTab.Instance.tables.Item.Get(id); var attributeData = JsonTab.Instance.tables.WeaponSkill.Get(id); data = JsonTab.Instance.tables.Skill.Get(attributeData.Initialskill); cd = data.SkillCD / 10000; cdTime = BattleManager.ins.NowTime; Actions = new List(); skillLock = false; } public void SkillCD() { skillLock = false; cdTime = BattleManager.ins.NowTime; foreach (var a in Actions) { a?.Invoke(); } } 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) { skillLock = true; foreach (var a in Actions) { a?.Invoke(); } } } } public class PlayerRunState { public PlayerItem player; public BattleEquipment equipent; public void RunTimeMove() { equipent.RunTime(); } }