BuffManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/"+Language.GetCurrentLanguage()+"/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);
  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)
  108. {
  109. for(int i=0; i<buff.GetNumBuffTypes(); i++)
  110. {
  111. if(buff.GetBuffType(i) == Buff.BuffType.Damage)
  112. {
  113. battleObj.MakeDamage(buff.GetConfigValue(i).GetValue(0), buff.launcher);
  114. }
  115. }
  116. }
  117. private void DealSpeedModify(BattleObject battleObj)
  118. {
  119. float add = 0;
  120. float reduce = 0;
  121. List<Buff> buffList = battleObj.GetBuffList();
  122. for(int i=0; i<buffList.Count; i++)
  123. {
  124. Buff buff = buffList[i];
  125. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  126. {
  127. if(buff.GetBuffType(j) == Buff.BuffType.SpeedUp)
  128. {
  129. float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
  130. if(speed > add)
  131. {
  132. add = speed;
  133. }
  134. }
  135. else if(buff.GetBuffType(j) == Buff.BuffType.SlowDown)
  136. {
  137. float speed = buff.GetConfigValue(j).GetValue(battleObj.originMoveSpeed);
  138. if(speed > reduce)
  139. {
  140. reduce = speed;
  141. }
  142. }
  143. }
  144. }
  145. if(reduce == battleObj.originMoveSpeed)
  146. {
  147. battleObj.modifyMoveSpeed = -reduce;
  148. }
  149. else
  150. {
  151. battleObj.modifyMoveSpeed = add - reduce;
  152. }
  153. }
  154. private void DealStuck(BattleObject battleObj)
  155. {
  156. List<Buff> buffList = battleObj.GetBuffList();
  157. for(int i=0; i<buffList.Count; i++)
  158. {
  159. Buff buff = buffList[i];
  160. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  161. {
  162. if(buff.GetBuffType(j) == Buff.BuffType.Stuck)
  163. {
  164. battleObj.enableMove = battleObj.enablePower = false;
  165. return;
  166. }
  167. }
  168. }
  169. battleObj.enableMove = battleObj.enablePower = true;
  170. }
  171. private void DealInvisible(BattleObject battleObj)
  172. {
  173. bool invisible = false;
  174. List<Buff> buffList = battleObj.GetBuffList();
  175. for(int i=0; i<buffList.Count; i++)
  176. {
  177. Buff buff = buffList[i];
  178. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  179. {
  180. if(buff.GetBuffType(j) == Buff.BuffType.Invisible)
  181. {
  182. invisible = true;
  183. break;
  184. }
  185. }
  186. }
  187. battleObj.SetInvisible(invisible, Session.GetInstance().GetBattleSession().myPlayer.team == battleObj.team);
  188. }
  189. private void DealBigger(BattleObject battleObj)
  190. {
  191. float value = 0;
  192. List<Buff> buffList = battleObj.GetBuffList();
  193. for(int i=0; i<buffList.Count; i++)
  194. {
  195. Buff buff = buffList[i];
  196. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  197. {
  198. if(buff.GetBuffType(j) == Buff.BuffType.Bigger)
  199. {
  200. float v = buff.GetConfigValue(j).GetValue(1f);
  201. if(v > value)
  202. {
  203. value = v;
  204. }
  205. }
  206. }
  207. }
  208. float totalValue = 1f + value;
  209. float hpAdd = battleObj.originMaxHp * value;
  210. battleObj.maxHp = battleObj.originMaxHp * totalValue;
  211. battleObj.hp += hpAdd;
  212. if(battleObj is Craft)
  213. {
  214. Craft craft = battleObj as Craft;
  215. Power power = craft.GetPowerManager().GetAttack();
  216. power.modifyValue = (int)(value * (float)power.GetOriginValue());
  217. }
  218. MapObjectScaler.Scale(battleObj, totalValue);
  219. }
  220. private void DealHoldAction(BattleObject battleObj)
  221. {
  222. bool hasHold = false;
  223. List<Buff> buffList = battleObj.GetBuffList();
  224. for(int i=0; i<buffList.Count; i++)
  225. {
  226. Buff buff = buffList[i];
  227. for(int j=0; j<buff.GetNumBuffTypes(); j++)
  228. {
  229. if(buff.GetBuffType(j) == Buff.BuffType.HoldAction)
  230. {
  231. hasHold = true;
  232. }
  233. }
  234. }
  235. battleObj.holdAction = hasHold;
  236. }
  237. }