WXGame/Blacksmith/Assets/Script/Battle/BattleManager.cs

753 lines
20 KiB
C#
Raw Normal View History

using System;
2024-12-04 17:26:27 +08:00
using System.Collections;
using System.Collections.Generic;
2024-12-09 11:19:14 +08:00
using System.Linq;
using cfg;
using cfg.BlacksmithData;
using Unity.Mathematics;
2024-12-04 17:26:27 +08:00
using UnityEngine;
using Random = UnityEngine.Random;
2024-12-04 17:26:27 +08:00
public class BattleManager : MonoBehaviour
{
public static BattleManager ins;
2024-12-10 17:46:37 +08:00
[SerializeField] private Transform mousterPoint;//怪所在节点
[SerializeField] private Transform[] mousters;//怪生成区域
2024-12-04 17:26:27 +08:00
2024-12-10 17:46:37 +08:00
[SerializeField] private Transform[] playerParent;//玩家所在节点
2024-12-09 11:19:14 +08:00
public List<Mouster> Mousters => _mousters;
2024-12-10 17:46:37 +08:00
[SerializeField] private List<Mouster> _mousters=new List<Mouster>();//怪列表
public int MousterCount => _mousters.Count;
2024-12-10 17:46:37 +08:00
[SerializeField] private PlayerRunState[] playerItems;//玩家列表
[SerializeField] private List<MousterTeam> _mousterTeams = new List<MousterTeam>();//波次列表
[SerializeField] private List<Bullet> _bullets = new List<Bullet>();//玩家子弹列表
public List<SkillState> SkillStates => _skillStates;
[SerializeField] private List<SkillState> _skillStates;
[SerializeField] private List<int> id=new List<int>();
public float NowTime => nowTime;
2024-12-10 17:46:37 +08:00
private float nowTime=0;//关卡内时间
public int HP => hp;
2024-12-11 13:58:55 +08:00
[SerializeField]
2024-12-10 17:46:37 +08:00
private int hp;//总血量
2024-12-09 11:19:14 +08:00
public int Injured => injured;
2024-12-10 17:46:37 +08:00
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];
/// <summary>
/// 是否开启装备栏
/// </summary>
public bool OpenVideoWeaponry => openVideoWeaponry;
private bool openVideoWeaponry;
/// <summary>
/// 是否开启佣兵
/// </summary>
public bool OpenVideoSoldier => openVideoSoldier;
private bool openVideoSoldier;
2024-12-10 17:46:37 +08:00
private Dictionary<int, BuffAddData> buff = new Dictionary<int, BuffAddData>();
private void Awake()
{
ins = this;
}
2024-12-04 17:26:27 +08:00
// Start is called before the first frame update
void Start()
{
}
#region
/// <summary>
/// 初始准备阶段装备数据
/// </summary>
public void ResetBattleData()
{
_battleEquipments=new BattleEquipment[3];
for (int i = 0; i < _battleEquipments.Length; i++)
{
_battleEquipments[i] = new BattleEquipment();
}
2024-12-09 11:19:14 +08:00
_battleEquipments[0].SetLock(true);
openVideoWeaponry = false;
openVideoSoldier = false;
}
/// <summary>
/// 解锁装备
/// </summary>
public void OpenVideoWeaponryEvent()
{
openVideoWeaponry = true;
}
/// <summary>
/// 解锁佣兵
/// </summary>
public void OpenVideoSoldierEvent()
{
openVideoSoldier = true;
2024-12-09 11:19:14 +08:00
foreach (var battle in _battleEquipments)
{
battle.SetLock(true);
}
}
/// <summary>
/// 设置道具到指定单位
/// </summary>
/// <param name="index"></param>
/// <param name="data"></param>
public void SaveBattleData(int index,BattleEquipment data)
{
if (!openVideoSoldier)
{
if (index!=0)
{
return;
}
}
_battleEquipments[index] = data;
}
/// <summary>
/// 添加道具到单位组
/// </summary>
/// <param name="itemId"></param>
/// <param name="bo"></param>
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);
}
}
/// <summary>
/// 添加一个道具到单位
/// </summary>
/// <param name="itemID"></param>
/// <param name="data"></param>
/// <param name="bo"></param>
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;
}
}
/// <summary>
/// 获取所有单位道具数量
/// </summary>
/// <param name="itemID"></param>
/// <param name="num"></param>
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);
}
/// <summary>
/// 获取单位该道具的数量
/// </summary>
/// <param name="itemid"></param>
/// <param name="data"></param>
/// <param name="num"></param>
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);
}
2024-12-10 17:46:37 +08:00
_skillStates = new List<SkillState>();
hp = allHP;
2024-12-09 11:19:14 +08:00
injured = 0;
BDebug.Log(hp);
nowTime = 0;
2024-12-10 17:46:37 +08:00
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;
2024-12-09 11:19:14 +08:00
if (BattleEquipments[i].EquipmentLock)
{
2024-12-11 20:35:20 +08:00
PrefabPool.ins.LoadObj("Prefab/player/player", (GameObject obj) =>
2024-12-09 11:19:14 +08:00
{
obj.transform.parent = playerParent[index];
obj.transform.localPosition=Vector3.zero;
playerItems[index] = new PlayerRunState();
playerItems[index].player = obj.GetComponent<PlayerItem>();
playerItems[index].equipent = BattleEquipments[index];
playerItems[index].RestState();
});
}
}
ResetLevelMouster();
player = true;
2024-12-11 13:58:55 +08:00
UIMgr.ins.ResetHp();
2024-12-11 20:35:20 +08:00
UIMgr.ins.OpenCombatPanel();
}
2024-12-10 17:46:37 +08:00
public void AddSkillStates(SkillState state)
{
_skillStates.Add(state);
2024-12-11 20:35:20 +08:00
UIMgr.ins.ResetHp();
2024-12-10 17:46:37 +08:00
}
2024-12-09 11:19:14 +08:00
public void SetInjured(int num)
{
injured += num;
2024-12-11 13:58:55 +08:00
BDebug.Log(injured);
UIMgr.ins.ResetHp();
2024-12-09 11:19:14 +08:00
}
void ResetLevelMouster()
{
mousterIndex = 0;
BDebug.Log("mouster");
var levelData = JsonTab.Instance.tables.Level.Get(DataManager.GetNowLevel());
_mousterTeams = new List<MousterTeam>();
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;
}
}
2024-12-04 17:26:27 +08:00
// Update is called once per frame
void Update()
{
if (!player)
{
return;
}
nowTime += Time.deltaTime;
2024-12-04 17:26:27 +08:00
foreach (var mouster in _mousters)
{
mouster.Move(playerParent[0]);
}
TeamUpdate();
2024-12-09 11:19:14 +08:00
BulletUpdata();
if (playerItems!=null)
{
foreach (var player in playerItems)
{
if (player!=null&& MousterCount>0)
{
player.RunTimeMove();
}
}
}
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 波次组刷新
/// </summary>
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++;
}
}
2024-12-09 11:19:14 +08:00
else
{
var bo = true;
foreach (var unused in _mousterTeams.Where(mouster => mouster.Type!=MousterTeamType.end))
{
bo = false;
}
if (bo)
{
BDebug.LogWarning("end");
}
}
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 玩家子弹组刷新
/// </summary>
2024-12-09 11:19:14 +08:00
public void BulletUpdata()
{
for (int i = _bullets.Count-1; i >= 0; i--)
{
_bullets[i].BulletUpdate();
}
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 添加玩家子弹
/// </summary>
/// <param name="bullet"></param>
2024-12-09 11:19:14 +08:00
public void AddBullets(Bullet bullet )
{
_bullets.Add(bullet);
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 移除玩家子弹
/// </summary>
/// <param name="bullet"></param>
2024-12-09 11:19:14 +08:00
public void RemoveBullets(Bullet bullet)
{
_bullets.Remove(bullet);
PrefabPool.ins.RecycleObj(bullet.gameObject);
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 添加怪
/// </summary>
public void AddMouster()
{
2024-12-09 11:19:14 +08:00
var index = mousterIndex;
foreach (var entity in _mousterTeams[mousterIndex].moustList)
{
PrefabPool.ins.LoadObj(entity._data.Prefab, (GameObject obj) =>
{
obj.transform.parent = mousterPoint;
2024-12-09 11:19:14 +08:00
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;
2024-12-10 17:46:37 +08:00
AxisLookAt(obj.transform, obj.transform.position - new Vector3(0, -2, 0));
var m = obj.GetComponent<Mouster>();
m.InitMousterData(entity);
_mousters.Add(m);
2024-12-09 11:19:14 +08:00
_mousterTeams[index].AddObj(m);
});
}
}
2024-12-10 17:46:37 +08:00
/// <summary>
/// 移除怪
/// </summary>
/// <param name="obj"></param>
public void RemoveMouster(Mouster obj)
{
2024-12-09 11:19:14 +08:00
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);
2024-12-10 17:46:37 +08:00
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];
}
2024-12-10 17:46:37 +08:00
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);
}
}
/// <summary>
/// 朝向通用接口
/// </summary>
/// <param name="tr_self"></param>
/// <param name="lookPos"></param>
2024-12-09 11:19:14 +08:00
public void AxisLookAt(Transform tr_self, Vector3 lookPos)
{
Vector3 dir = lookPos - tr_self.position;
2024-12-09 11:19:14 +08:00
dir.z = 0;
float angle = Vector3.SignedAngle(Vector3.up, dir, Vector3.forward);
Quaternion rotation = Quaternion.Euler(0, 0, angle); //将欧拉角转换为四元数
tr_self.rotation = rotation;
}
2024-12-10 17:46:37 +08:00
}
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;
2024-12-09 11:19:14 +08:00
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;
2024-12-09 11:19:14 +08:00
public void SetLock(bool bo)
{
equipmentLock = bo;
}
public void RestSkill()
{
if (weaponID!=0)
{
2024-12-09 11:19:14 +08:00
weapon = new SkillState(weaponID,"weaponID");
2024-12-10 17:46:37 +08:00
BattleManager.ins.AddSkillStates(weapon);
}
if (weapon2ID!=0)
{
2024-12-09 11:19:14 +08:00
weapon2 = new SkillState(weapon2ID,"weapon2ID");
2024-12-10 17:46:37 +08:00
BattleManager.ins.AddSkillStates(weapon2);
}
if (armorID!=0)
{
2024-12-09 11:19:14 +08:00
armor = new SkillState(armorID,"armorID");
2024-12-10 17:46:37 +08:00
BattleManager.ins.AddSkillStates(armor);
}
if (audioUniversalID!=0)
{
2024-12-09 11:19:14 +08:00
audioUniversal = new SkillState(audioUniversalID,"audioUniversalID");
2024-12-10 17:46:37 +08:00
BattleManager.ins.AddSkillStates(audioUniversal);
}
if (audioWeaponID!=0)
{
2024-12-09 11:19:14 +08:00
audioWeapon = new SkillState(audioWeaponID,"audioWeaponID");
2024-12-10 17:46:37 +08:00
BattleManager.ins.AddSkillStates(audioWeapon);
}
if (audioWeapon2ID!=0)
{
2024-12-09 11:19:14 +08:00
audioWeapon2 = new SkillState(audioWeapon2ID,"audioWeapon2ID");
2024-12-10 17:46:37 +08:00
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;
2024-12-09 11:19:14 +08:00
public int Attack => attack;
private int attack = 0;
public float Ratio => ratio;
private float ratio = 1;
public List<Action<SkillState>> Actions = new List<Action<SkillState>>();
public List<Action> WaitActions = new List<Action>();
public bool SkillLock => skillLock;
private bool skillLock = false;
2024-12-09 11:19:14 +08:00
public string Name => name;
private string name;
2024-12-09 11:19:14 +08:00
public SkillState(int _id,string na)
{
if (_id==0)
{
id = 0;
return;
}
2024-12-09 11:19:14 +08:00
name = na;
id = _id;
itemData = JsonTab.Instance.tables.Item.Get(id);
var attributeData = JsonTab.Instance.tables.WeaponSkill.Get(id);
2024-12-09 11:19:14 +08:00
var weapon =
JsonTab.Instance.attributeData(itemData.Includearms, DataManager.GetDrawLevel(itemData.Includearms));
attack = weapon.Attack;
data = JsonTab.Instance.tables.Skill.Get(attributeData.Initialskill);
2024-12-09 11:19:14 +08:00
ratio = data.SkillDamage;
cd = data.SkillCD / 10000;
cdTime = BattleManager.ins.NowTime;
2024-12-09 11:19:14 +08:00
Actions = new List<Action<SkillState>>();
skillLock = false;
}
2024-12-10 17:46:37 +08:00
public void ResetSkill()
{
}
public void SkillCD()
{
2024-12-09 11:19:14 +08:00
// skillLock = false;
cdTime = BattleManager.ins.NowTime;
2024-12-09 11:19:14 +08:00
BDebug.LogWarning(name);
foreach (var a in Actions)
{
2024-12-09 11:19:14 +08:00
a?.Invoke(this);
}
}
2024-12-09 11:19:14 +08:00
public void AddAction(Action<SkillState> action)
{
Actions.Add(action);
}
2024-12-09 11:19:14 +08:00
public void RemoveAction(Action<SkillState> action)
{
Actions.Remove(action);
}
public void AddWaitActions(Action action)
{
WaitActions.Add(action);
}
public void RemoveWaitActions(Action action)
{
WaitActions.Remove(action);
}
public void SkillStateEvent()
{
2024-12-09 11:19:14 +08:00
// if (skillLock)
// {
// foreach (var a in WaitActions)
// {
// a?.Invoke();
// }
// return;
// }
2024-12-11 13:58:55 +08:00
if (BattleManager.ins.NowTime-cdTime>cd*(1/(1+(float)BattleManager.ins.Buff(itemData.Includearms).cd/10000)))
{
2024-12-09 11:19:14 +08:00
// skillLock = true;
SkillCD();
2024-12-10 17:46:37 +08:00
}
}
2024-12-09 11:19:14 +08:00
2024-12-10 17:46:37 +08:00
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;
2024-12-04 17:26:27 +08:00
}
}
2024-12-10 17:46:37 +08:00
2024-12-04 17:26:27 +08:00
}
public class PlayerRunState
{
public PlayerItem player;
public BattleEquipment equipent;
2024-12-09 11:19:14 +08:00
public void RestState()
{
equipent.RestSkill();
player.SetData(equipent);
}
public void RunTimeMove()
{
equipent.RunTime();
}
2024-12-10 17:46:37 +08:00
}
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;
}
}