BuffManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. public class BuffManager
  6. {
  7. public enum State
  8. {
  9. Add,
  10. Remove,
  11. }
  12. public Dictionary<int, BuffData> dataDict;
  13. public BuffManager()
  14. {
  15. dataDict = new Dictionary<int, BuffData>();
  16. InitByXML("XML/Config/All/buff_config");
  17. }
  18. public void InitByXML(string path)
  19. {
  20. TextAsset textAsset = (TextAsset)Resources.Load(path);
  21. XmlDocument xml = new XmlDocument();
  22. xml.LoadXml(textAsset.text);
  23. XmlNode mapNode = xml.SelectSingleNode("data");
  24. XmlNodeList nodeList = mapNode.SelectNodes("item");
  25. for(int i=0; i<nodeList.Count; i++)
  26. {
  27. BuffData data = new BuffData(nodeList[i]);
  28. if(!dataDict.ContainsKey(data.id))
  29. dataDict.Add(data.id, data);
  30. else
  31. Debuger.LogError("buff " + data.id + " has duplicate");
  32. }
  33. }
  34. private static BuffManager instance;
  35. public static BuffManager GetInstance()
  36. {
  37. if(instance == null)
  38. {
  39. instance = new BuffManager();
  40. }
  41. return instance;
  42. }
  43. public BuffData GetData(int id)
  44. {
  45. if(dataDict.ContainsKey(id))
  46. return dataDict[id];
  47. Debuger.LogWarning("can not find buff["+id+"]");
  48. return null;
  49. }
  50. public Buff AddBuff(int id, BattleObject target, BattleObject launcher)
  51. {
  52. BuffData data = GetData(id);
  53. if(data == null || target == null || launcher == null)
  54. {
  55. return null;
  56. }
  57. Buff buff = new Buff(data);
  58. buff.target = target;
  59. buff.launcher = launcher;
  60. BattleObject owner = buff.GetOwner();
  61. owner.AddBuff(buff);
  62. buff.CreateGraphicsEffect(owner.GetBaseTransform());
  63. if(SoundUtils.IsSoundNear(owner.position))
  64. SoundManager.GetInstatnce().effectSound.Play(buff.GetSoundEffect());
  65. DealBuff(owner, buff, State.Add);
  66. return buff;
  67. }
  68. public void RemoveBuff(Buff buff, bool noDeal=false)
  69. {
  70. BattleObject owner = buff.GetOwner();
  71. owner.RemoveBuff(buff);
  72. buff.target = null;
  73. buff.launcher = null;
  74. buff.RemoveGraphicsEffect();
  75. if(!noDeal)
  76. DealBuff(owner, buff, State.Remove);
  77. }
  78. public void DealBuff(BattleObject battleObj, Buff buff, State state)
  79. {
  80. for(int i=0; i<buff.GetNumBuffTypes(); i++)
  81. {
  82. switch(buff.GetBuffType(i))
  83. {
  84. case Buff.BuffType.Damage:
  85. if(state == State.Add)
  86. DealDamage(battleObj, buff, i);
  87. break;
  88. case Buff.BuffType.SpeedUp:
  89. case Buff.BuffType.SlowDown:
  90. DealSpeedModify(battleObj);
  91. break;
  92. case Buff.BuffType.Stuck:
  93. DealStuck(battleObj);
  94. break;
  95. case Buff.BuffType.Invisible:
  96. DealInvisible(battleObj);
  97. break;
  98. case Buff.BuffType.Bigger:
  99. DealBigger(battleObj);
  100. break;
  101. case Buff.BuffType.HoldAction:
  102. DealHoldAction(battleObj);
  103. break;
  104. }
  105. }
  106. }
  107. private void DealDamage(BattleObject battleObj, Buff buff, int index)
  108. {
  109. if(buff.GetBuffType(index) == Buff.BuffType.Damage && buff.launcher is Craft)
  110. {
  111. Craft launcher = buff.launcher as Craft;
  112. float damage = buff.GetConfigValue (index).GetValue(0) * launcher.GetDamage ();
  113. battleObj.MakeDamage(damage, buff.launcher);
  114. }
  115. }
  116. private void DealSpeedModify(BattleObject battleObj)
  117. {
  118. float add = 0;
  119. float reduce = 0;
  120. List<Buff> buffList = battleObj.GetBuffList();
  121. for(int i=0; i<buffList.Count; i++)
  122. {
  123. Buff buff = buffList[i];
  124. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  125. {
  126. if(buff.GetBuffType(j) == Buff.BuffType.SpeedUp)
  127. {
  128. float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
  129. if(speed > add)
  130. {
  131. add = speed;
  132. }
  133. }
  134. else if(buff.GetBuffType(j) == Buff.BuffType.SlowDown)
  135. {
  136. float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
  137. if(speed > reduce)
  138. {
  139. reduce = speed;
  140. }
  141. }
  142. }
  143. }
  144. if(reduce == battleObj.originMoveSpeed)
  145. {
  146. battleObj.modifyMoveSpeed = -reduce;
  147. }
  148. else
  149. {
  150. battleObj.modifyMoveSpeed = add - reduce;
  151. }
  152. }
  153. private void DealStuck(BattleObject battleObj)
  154. {
  155. List<Buff> buffList = battleObj.GetBuffList();
  156. for(int i=0; i<buffList.Count; i++)
  157. {
  158. Buff buff = buffList[i];
  159. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  160. {
  161. if(buff.GetBuffType(j) == Buff.BuffType.Stuck)
  162. {
  163. battleObj.enableMove = battleObj.enablePower = false;
  164. return;
  165. }
  166. }
  167. }
  168. battleObj.enableMove = battleObj.enablePower = true;
  169. }
  170. private void DealInvisible(BattleObject battleObj)
  171. {
  172. bool invisible = false;
  173. List<Buff> buffList = battleObj.GetBuffList();
  174. for(int i=0; i<buffList.Count; i++)
  175. {
  176. Buff buff = buffList[i];
  177. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  178. {
  179. if(buff.GetBuffType(j) == Buff.BuffType.Invisible)
  180. {
  181. invisible = true;
  182. break;
  183. }
  184. }
  185. }
  186. battleObj.SetInvisible(invisible, Session.GetInstance().GetBattleSession().myPlayer.team == battleObj.team);
  187. }
  188. private void DealBigger(BattleObject battleObj)
  189. {
  190. float value = 0;
  191. List<Buff> buffList = battleObj.GetBuffList();
  192. for(int i=0; i<buffList.Count; i++)
  193. {
  194. Buff buff = buffList[i];
  195. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  196. {
  197. if(buff.GetBuffType(j) == Buff.BuffType.Bigger)
  198. {
  199. float v = buff.GetConfigValue(j).GetValue(1f);
  200. if(v > value)
  201. {
  202. value = v;
  203. }
  204. }
  205. }
  206. }
  207. float totalValue = 1f + value;
  208. float hpAdd = battleObj.originMaxHp * value;
  209. battleObj.maxHp = battleObj.originMaxHp * totalValue;
  210. battleObj.hp += hpAdd;
  211. if(battleObj is Craft)
  212. {
  213. Craft craft = battleObj as Craft;
  214. Power power = craft.GetPowerManager().GetAttack();
  215. power.modifyValue = (int)(value * (float)power.GetOriginValue());
  216. }
  217. MapObjectScaler.Scale(battleObj, totalValue);
  218. }
  219. private void DealHoldAction(BattleObject battleObj)
  220. {
  221. bool hasHold = false;
  222. List<Buff> buffList = battleObj.GetBuffList();
  223. for(int i=0; i<buffList.Count; i++)
  224. {
  225. Buff buff = buffList[i];
  226. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  227. {
  228. if(buff.GetBuffType(j) == Buff.BuffType.HoldAction)
  229. {
  230. hasHold = true;
  231. }
  232. }
  233. }
  234. battleObj.holdAction = hasHold;
  235. }
  236. }