123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml;
- public class BuffManager
- {
- public enum State
- {
- Add,
- Remove,
- }
- public Dictionary<int, BuffData> dataDict;
- public BuffManager()
- {
- dataDict = new Dictionary<int, BuffData>();
- InitByXML("XML/Config/All/buff_config");
- }
- public void InitByXML(string path)
- {
- TextAsset textAsset = (TextAsset)Resources.Load(path);
-
- XmlDocument xml = new XmlDocument();
- xml.LoadXml(textAsset.text);
-
- XmlNode mapNode = xml.SelectSingleNode("data");
-
- XmlNodeList nodeList = mapNode.SelectNodes("item");
- for(int i=0; i<nodeList.Count; i++)
- {
- BuffData data = new BuffData(nodeList[i]);
- if(!dataDict.ContainsKey(data.id))
- dataDict.Add(data.id, data);
- else
- Debuger.LogError("buff " + data.id + " has duplicate");
- }
- }
- private static BuffManager instance;
- public static BuffManager GetInstance()
- {
- if(instance == null)
- {
- instance = new BuffManager();
- }
- return instance;
- }
- public BuffData GetData(int id)
- {
- if(dataDict.ContainsKey(id))
- return dataDict[id];
- Debuger.LogWarning("can not find buff["+id+"]");
- return null;
- }
- public Buff AddBuff(int id, BattleObject target, BattleObject launcher)
- {
- BuffData data = GetData(id);
- if(data == null || target == null || launcher == null)
- {
- return null;
- }
- Buff buff = new Buff(data);
- buff.target = target;
- buff.launcher = launcher;
- BattleObject owner = buff.GetOwner();
- owner.AddBuff(buff);
- buff.CreateGraphicsEffect(owner.GetBaseTransform());
- if(SoundUtils.IsSoundNear(owner.position))
- SoundManager.GetInstatnce().effectSound.Play(buff.GetSoundEffect());
- DealBuff(owner, buff, State.Add);
- return buff;
- }
- public void RemoveBuff(Buff buff, bool noDeal=false)
- {
- BattleObject owner = buff.GetOwner();
- owner.RemoveBuff(buff);
- buff.target = null;
- buff.launcher = null;
- buff.RemoveGraphicsEffect();
- if(!noDeal)
- DealBuff(owner, buff, State.Remove);
- }
- public void DealBuff(BattleObject battleObj, Buff buff, State state)
- {
- for(int i=0; i<buff.GetNumBuffTypes(); i++)
- {
- switch(buff.GetBuffType(i))
- {
- case Buff.BuffType.Damage:
- if(state == State.Add)
- DealDamage(battleObj, buff, i);
- break;
- case Buff.BuffType.SpeedUp:
- case Buff.BuffType.SlowDown:
- DealSpeedModify(battleObj);
- break;
- case Buff.BuffType.Stuck:
- DealStuck(battleObj);
- break;
- case Buff.BuffType.Invisible:
- DealInvisible(battleObj);
- break;
- case Buff.BuffType.Bigger:
- DealBigger(battleObj);
- break;
- case Buff.BuffType.HoldAction:
- DealHoldAction(battleObj);
- break;
- }
- }
- }
- private void DealDamage(BattleObject battleObj, Buff buff, int index)
- {
- if(buff.GetBuffType(index) == Buff.BuffType.Damage && buff.launcher is Craft)
- {
- Craft launcher = buff.launcher as Craft;
- float damage = buff.GetConfigValue (index).GetValue(0) * launcher.GetDamage ();
- battleObj.MakeDamage(damage, buff.launcher);
- }
- }
- private void DealSpeedModify(BattleObject battleObj)
- {
- float add = 0;
- float reduce = 0;
- List<Buff> buffList = battleObj.GetBuffList();
- for(int i=0; i<buffList.Count; i++)
- {
- Buff buff = buffList[i];
- for(int j=0; j<buff.GetNumBuffTypes(); j++)
- {
- if(buff.GetBuffType(j) == Buff.BuffType.SpeedUp)
- {
- float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
- if(speed > add)
- {
- add = speed;
- }
- }
- else if(buff.GetBuffType(j) == Buff.BuffType.SlowDown)
- {
- float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
- if(speed > reduce)
- {
- reduce = speed;
- }
- }
- }
- }
- if(reduce == battleObj.originMoveSpeed)
- {
- battleObj.modifyMoveSpeed = -reduce;
- }
- else
- {
- battleObj.modifyMoveSpeed = add - reduce;
- }
- }
- private void DealStuck(BattleObject battleObj)
- {
- List<Buff> buffList = battleObj.GetBuffList();
- for(int i=0; i<buffList.Count; i++)
- {
- Buff buff = buffList[i];
- for(int j=0; j<buff.GetNumBuffTypes(); j++)
- {
- if(buff.GetBuffType(j) == Buff.BuffType.Stuck)
- {
- battleObj.enableMove = battleObj.enablePower = false;
- return;
- }
- }
- }
- battleObj.enableMove = battleObj.enablePower = true;
- }
- private void DealInvisible(BattleObject battleObj)
- {
- bool invisible = false;
- List<Buff> buffList = battleObj.GetBuffList();
- for(int i=0; i<buffList.Count; i++)
- {
- Buff buff = buffList[i];
- for(int j=0; j<buff.GetNumBuffTypes(); j++)
- {
- if(buff.GetBuffType(j) == Buff.BuffType.Invisible)
- {
- invisible = true;
- break;
- }
- }
- }
- battleObj.SetInvisible(invisible, Session.GetInstance().GetBattleSession().myPlayer.team == battleObj.team);
- }
- private void DealBigger(BattleObject battleObj)
- {
- float value = 0;
- List<Buff> buffList = battleObj.GetBuffList();
- for(int i=0; i<buffList.Count; i++)
- {
- Buff buff = buffList[i];
- for(int j=0; j<buff.GetNumBuffTypes(); j++)
- {
- if(buff.GetBuffType(j) == Buff.BuffType.Bigger)
- {
- float v = buff.GetConfigValue(j).GetValue(1f);
- if(v > value)
- {
- value = v;
- }
- }
- }
- }
- float totalValue = 1f + value;
- float hpAdd = battleObj.originMaxHp * value;
- battleObj.maxHp = battleObj.originMaxHp * totalValue;
- battleObj.hp += hpAdd;
- if(battleObj is Craft)
- {
- Craft craft = battleObj as Craft;
- Power power = craft.GetPowerManager().GetAttack();
- power.modifyValue = (int)(value * (float)power.GetOriginValue());
- }
- MapObjectScaler.Scale(battleObj, totalValue);
- }
- private void DealHoldAction(BattleObject battleObj)
- {
- bool hasHold = false;
- List<Buff> buffList = battleObj.GetBuffList();
- for(int i=0; i<buffList.Count; i++)
- {
- Buff buff = buffList[i];
- for(int j=0; j<buff.GetNumBuffTypes(); j++)
- {
- if(buff.GetBuffType(j) == Buff.BuffType.HoldAction)
- {
- hasHold = true;
- }
- }
- }
- battleObj.holdAction = hasHold;
- }
- }
|