Buff.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Buff
  4. {
  5. public enum YesOrNo
  6. {
  7. No = 0,
  8. Yes = 1
  9. }
  10. private static System.Array yesOrNoArr = System.Enum.GetValues(typeof(YesOrNo));
  11. public static YesOrNo GetYesOrNoByCode(int code)
  12. {
  13. return (YesOrNo)yesOrNoArr.GetValue(code);
  14. }
  15. public enum TargetType
  16. {
  17. Target = 0,
  18. Launcher = 1
  19. }
  20. private static System.Array targetTypeArr = System.Enum.GetValues(typeof(TargetType));
  21. public static TargetType GetTargetTypeByCode(int code)
  22. {
  23. return (TargetType)targetTypeArr.GetValue(code);
  24. }
  25. public enum BuffType
  26. {
  27. None = 0,
  28. Damage = 1,
  29. Heal = 2,
  30. SpeedUp = 3,
  31. SlowDown = 4,
  32. Stuck = 5,
  33. Invisible = 6,
  34. Bigger = 7,
  35. HoldAction = 8,
  36. CloseTarget = 9,
  37. }
  38. private static System.Array buffTypeArr = System.Enum.GetValues(typeof(BuffType));
  39. public static BuffType GetBuffTypeByCode(int code)
  40. {
  41. return (BuffType)buffTypeArr.GetValue(code);
  42. }
  43. public enum RemoveType
  44. {
  45. None = 0,
  46. Move = 1,
  47. Power = 2,
  48. Damage = 3,
  49. Dismove = 4,
  50. Overtime = 5,
  51. }
  52. private static System.Array removeTypeArr = System.Enum.GetValues(typeof(RemoveType));
  53. public static RemoveType GetRemoveTypeByCode(int code)
  54. {
  55. return (RemoveType)removeTypeArr.GetValue(code);
  56. }
  57. public enum EndType
  58. {
  59. TimeUp = 0,
  60. CloseTarget = 1,
  61. }
  62. public BattleObject target;
  63. public BattleObject launcher;
  64. public object paramObj;
  65. private BuffData data;
  66. private ConfigValue[] configValueArr;
  67. private float startTime;
  68. public bool hasCloseTarget;
  69. public Buff(BuffData data)
  70. {
  71. startTime = GameTime.time;
  72. this.data = data;
  73. for(int i=0; i<data.buff_type.Length; i++)
  74. {
  75. if(data.buff_type[i] == BuffType.CloseTarget.GetHashCode())
  76. hasCloseTarget = true;
  77. }
  78. configValueArr = new ConfigValue[data.buff_type.Length];
  79. for(int i=0; i<configValueArr.Length; i++)
  80. {
  81. int value = i < data.value.Length ? data.value[i] : 0;
  82. int valueType = i < data.value_type.Length ? data.value_type[i] : 0;
  83. configValueArr[i] = new ConfigValue(value, valueType);
  84. }
  85. }
  86. public int GetId()
  87. {
  88. return data.id;
  89. }
  90. public string GetName()
  91. {
  92. return data.name;
  93. }
  94. public string GetDescription()
  95. {
  96. return this.data.description;
  97. }
  98. public BattleObject GetOwner()
  99. {
  100. if(this.data.target == TargetType.Target.GetHashCode())
  101. return target;
  102. else if(this.data.target == TargetType.Launcher.GetHashCode())
  103. return launcher;
  104. return null;
  105. }
  106. public YesOrNo GetIsGood()
  107. {
  108. return GetYesOrNoByCode(this.data.is_good);
  109. }
  110. public YesOrNo GetIsNormal()
  111. {
  112. return GetYesOrNoByCode(this.data.is_normal);
  113. }
  114. public BuffType GetBuffType(int index)
  115. {
  116. return GetBuffTypeByCode(this.data.buff_type[index]);
  117. }
  118. public float GetDuration()
  119. {
  120. if(this.data.duration == 0)
  121. return float.MaxValue;
  122. return this.data.duration;
  123. }
  124. public ConfigValue GetConfigValue(int index)
  125. {
  126. return configValueArr[index];
  127. }
  128. public int GetNumBuffTypes()
  129. {
  130. return this.data.buff_type.Length;
  131. }
  132. public string GetGraphicEffect()
  133. {
  134. return this.data.g_effect;
  135. }
  136. private GameObject graphicEffect;
  137. public GameObject CreateGraphicsEffect(Transform parent)
  138. {
  139. GameObject prefab = Resources.Load<GameObject>("Prefabs/Buffs/"+GetGraphicEffect());
  140. if(prefab != null)
  141. {
  142. if(graphicEffect != null)
  143. {
  144. GameObject.Destroy(graphicEffect);
  145. Debuger.LogError(this.ToString()+" already created graphic effect");
  146. }
  147. graphicEffect = GameObject.Instantiate<GameObject>(prefab);
  148. graphicEffect.transform.SetParent(parent);
  149. graphicEffect.transform.localPosition = Vector3.zero;
  150. graphicEffect.transform.localEulerAngles = Vector3.zero;
  151. graphicEffect.transform.localScale = new Vector3(1f, 1f, 1f);
  152. }
  153. return graphicEffect;
  154. }
  155. public void RemoveGraphicsEffect()
  156. {
  157. if(graphicEffect != null)
  158. {
  159. BuffEffect buffEffect = graphicEffect.GetComponent<BuffEffect>();
  160. if(buffEffect != null)
  161. buffEffect.Remove();
  162. else
  163. GameObject.Destroy(graphicEffect);
  164. }
  165. }
  166. public string GetSoundEffect()
  167. {
  168. return this.data.s_effect;
  169. }
  170. public void Update()
  171. {
  172. if(data.end == EndType.TimeUp.GetHashCode())
  173. {
  174. if(GameTime.time - startTime > GetDuration())
  175. {
  176. Remove();
  177. }
  178. }
  179. else if(data.end == EndType.CloseTarget.GetHashCode())
  180. {
  181. if(NumberUtil.distanceVector3(launcher.position, target.position, true) < 1f)
  182. {
  183. Remove();
  184. }
  185. }
  186. if(GameTime.time - startTime > GetDuration())
  187. {
  188. OnOvertime();
  189. }
  190. }
  191. private void Remove()
  192. {
  193. BattleObject tempTarget = this.target;
  194. BattleObject tempLauncher = this.launcher;
  195. BuffManager.GetInstance().RemoveBuff(this);
  196. if(data.endBuffArr.Length > 0)
  197. {
  198. for(int i=0; i<data.endBuffArr.Length; i++)
  199. {
  200. BuffManager.GetInstance().AddBuff(data.endBuffArr[i], tempTarget, tempLauncher);
  201. }
  202. }
  203. }
  204. public void OnMove()
  205. {
  206. for(int i=0; i<data.removeArr.Length; i++)
  207. {
  208. if(data.removeArr[i] == RemoveType.Move.GetHashCode())
  209. {
  210. BuffManager.GetInstance().RemoveBuff(this);
  211. return;
  212. }
  213. }
  214. }
  215. public void OnDamage()
  216. {
  217. if(GameTime.time - startTime < 1f)
  218. return;
  219. for(int i=0; i<data.removeArr.Length; i++)
  220. {
  221. if(data.removeArr[i] == RemoveType.Damage.GetHashCode())
  222. {
  223. BuffManager.GetInstance().RemoveBuff(this);
  224. return;
  225. }
  226. }
  227. }
  228. public void OnUserPower()
  229. {
  230. for(int i=0; i<data.removeArr.Length; i++)
  231. {
  232. if(data.removeArr[i] == RemoveType.Power.GetHashCode())
  233. {
  234. BuffManager.GetInstance().RemoveBuff(this);
  235. return;
  236. }
  237. }
  238. }
  239. public void OnDismove()
  240. {
  241. for(int i=0; i<data.removeArr.Length; i++)
  242. {
  243. if(data.removeArr[i] == RemoveType.Dismove.GetHashCode())
  244. {
  245. BuffManager.GetInstance().RemoveBuff(this);
  246. return;
  247. }
  248. }
  249. }
  250. public void OnOvertime()
  251. {
  252. for(int i=0; i<data.removeArr.Length; i++)
  253. {
  254. if(data.removeArr[i] == RemoveType.Overtime.GetHashCode())
  255. {
  256. BuffManager.GetInstance().RemoveBuff(this);
  257. return;
  258. }
  259. }
  260. }
  261. public override string ToString ()
  262. {
  263. return string.Format ("[Buff] id["+GetId()+"] name["+GetName()+"]");
  264. }
  265. }