BuffManager.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FSEvent;
  5. using FSFile;
  6. using FSRole;
  7. using LitJson;
  8. namespace FSBuff {
  9. /*
  10. * Buff管理器,负责Buff的创建和挂载
  11. */
  12. public class BuffManager : Singleton<BuffManager> {
  13. JsonData buffValue = null; // 保存的Buff数据
  14. public BuffManager() {
  15. ReadBuffData();
  16. InitListener();
  17. }
  18. /*
  19. * 从文本中获取BUFF数据
  20. */
  21. private void ReadBuffData() {
  22. string json = FileManager.Instance.ReadResourceText("Jsons/buffs");
  23. if (json != null) {
  24. Debug.Log("读取到 json 字符串 : " + json);
  25. try {
  26. buffValue = JsonMapper.ToObject(json);
  27. } catch (JsonException e) {
  28. Debug.Log(e.ToString());
  29. }
  30. }
  31. }
  32. /*
  33. * 初始化监听
  34. */
  35. private void InitListener() {
  36. EventListener.Instance.RegisterEvent(EventEnum.EVENT_BUFF_EXECUTE, BuffExecute);
  37. EventListener.Instance.RegisterEvent(EventEnum.EVENT_BUFF_CANCLE, BuffCancle);
  38. }
  39. /*
  40. * 给角色添加挂载BUFF
  41. */
  42. public void LoadBuff(GameObject obj, string id) {
  43. JsonData oneValue = buffValue[id];
  44. if (oneValue != null && oneValue.IsObject) {
  45. Buff buff = new Buff();
  46. // 获取对应的目标
  47. Role role = obj.GetComponent<Role>();
  48. // 给角色挂载Buff
  49. role.AddBuff(buff);
  50. // 对应的目标
  51. buff.Target = role;
  52. // 同时从json对象中提取数据
  53. buff.Value = (int)buffValue[id]["value"];
  54. buff.ID = id;
  55. //// 挂载后直接运行
  56. //buff.Execute();
  57. } else {
  58. throw new System.Exception("没有找到ID所对应的BUFF");
  59. }
  60. }
  61. /*
  62. * Buff执行,根据ID来进行相应的处理
  63. */
  64. private void BuffExecute(Dictionary<string, object> info) {
  65. Debug.Log("Buff 开始运行");
  66. Buff buff = (Buff)info["buff"];
  67. if (buff != null) {
  68. buff.Target.CurrentAttr.Hp -= buff.Value;
  69. }
  70. }
  71. /*
  72. * Buff取消,根据ID来进行相应的处理
  73. */
  74. private void BuffCancle(Dictionary<string, object> info) {
  75. Debug.Log("Buff 时间到");
  76. }
  77. }
  78. }