RoleManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FSRole;
  5. using FSBattle;
  6. using LitJson;
  7. using FSFile;
  8. using FSAssist;
  9. using UnityEngine.UI;
  10. using FSCard;
  11. /*
  12. * 角色管理
  13. * 未来角色的创建和销毁都在这里
  14. */
  15. namespace FSRole {
  16. public class RoleManager : MonoBehaviour {
  17. // 角色预制体
  18. public GameObject rolePrefab;
  19. // 我方位置
  20. public Transform armyLoc;
  21. // 地方位置
  22. public Transform enemyLoc;
  23. // 角色
  24. public ArrayList roles;
  25. // 保存的角色数据
  26. public JsonData roleValue;
  27. // 跟随面板
  28. public Transform followPanel;
  29. // 血量的预制体
  30. public GameObject bloodPre;
  31. // 卡片预制体
  32. public GameObject cardPrefab;
  33. // 缓存地
  34. public Transform cachePlace;
  35. // Use this for initialization
  36. void Start() {
  37. roles = new ArrayList();
  38. ReadRoleData();
  39. CreateRole("00000");
  40. CreateRole("10002");
  41. }
  42. /*
  43. * 读取角色json数据
  44. */
  45. private void ReadRoleData() {
  46. string json = FileManager.Instance.ReadResourceText("Jsons/roles");
  47. if (json != null)
  48. {
  49. Debug.Log("读取到 json 字符串 : " + json);
  50. try {
  51. roleValue = JsonMapper.ToObject(json);
  52. } catch (JsonException e){
  53. Debug.Log(e.ToString());
  54. }
  55. }
  56. }
  57. /*
  58. * 传入ID,从数组中来获取到角色数值
  59. */
  60. private void CreateRole(string id) {
  61. JsonData oneValue = roleValue[id];
  62. if (oneValue != null && oneValue.IsObject) {
  63. RoleAttr attr = new RoleAttr();
  64. // 配置属性
  65. attr.Hp = AssistMethods.ReadInt(oneValue, "hp");
  66. attr.MaxHp = attr.Hp;
  67. attr.Mp = AssistMethods.ReadInt(oneValue, "mp");
  68. attr.Camp = (AssistMethods.ReadInt(oneValue, "camp") == 1) ? RoleCamp.ARMY : RoleCamp.ENEMY;
  69. attr.ID = id;
  70. Role role = CreateRole(attr);
  71. JsonData cards = oneValue["cards"];
  72. if (cards != null) {
  73. if (cards.IsArray) {
  74. for (int i = 0; i < cards.Count; i++) {
  75. string cardId = AssistMethods.ReadString(cards, i);
  76. GameObject cardObj = Instantiate(cardPrefab, cachePlace);
  77. // 挂载卡片组件
  78. CardManager.Instance.LoadCard(cardObj, cardId, role);
  79. // 添加进卡池中
  80. if (role.OriginAttr.Camp == RoleCamp.ARMY) {
  81. BattleFieldManager.Instance.ArmyTakePool.Add(cardObj.transform);
  82. } else {
  83. BattleFieldManager.Instance.EnemyTakePool.Add(cardObj.transform);
  84. }
  85. }
  86. }
  87. }
  88. } else {
  89. throw new System.Exception("id : " + id + "无对应的角色 ... ");
  90. }
  91. }
  92. /*
  93. * 根据角色数值来创建角色
  94. */
  95. private Role CreateRole(RoleAttr value) {
  96. GameObject role = null;
  97. int index;
  98. // 判断是哪个阵营的,则放在指定位置并加入指定数组中
  99. if (value.Camp == RoleCamp.ARMY) {
  100. role = Instantiate(rolePrefab, armyLoc);
  101. BattleFieldManager.Instance.ArmyArray.Add(role.transform);
  102. index = BattleFieldManager.Instance.ArmyArray.Count - 1;
  103. } else {
  104. role = Instantiate(rolePrefab, enemyLoc);
  105. BattleFieldManager.Instance.EnemyArray.Add(role.transform);
  106. index = BattleFieldManager.Instance.EnemyArray.Count - 1;
  107. }
  108. // 第奇数个和偶数个处理
  109. if (index % 2 == 0) {
  110. // 偶数个
  111. role.transform.localPosition = new Vector3(index / 2 * AssistConfig.RoleSpace, 0, 0);
  112. } else {
  113. // 奇数个
  114. float loc = (float)index / 2 + 0.5f;
  115. role.transform.localPosition = new Vector3(loc * AssistConfig.RoleSpace * -1, 0, 0);
  116. }
  117. role.transform.localScale = new Vector3(0.6f, 0.6f, 1);
  118. //role.transform.localPosition = new Vector3(0, 0, 0);
  119. // 添加属性
  120. role.GetComponent<Role>().OriginAttr = value;
  121. role.GetComponent<Role>().CurrentAttr = value.copy();
  122. // 创建血条,放在角色下面
  123. // 转换为屏幕坐标在调整
  124. GameObject followObj = Instantiate(bloodPre, followPanel);
  125. // 转化角色的坐标
  126. Vector3 roleScreen = Camera.main.WorldToScreenPoint(role.transform.position);
  127. followObj.transform.position = roleScreen;
  128. role.GetComponent<Role>().SetBloodTrs(followObj.transform);
  129. // 加载图片资源
  130. string path = "Textures/Roles/" + value.ID;
  131. Texture2D tex = Resources.Load<Texture2D>(path);
  132. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0));
  133. if (sp != null) {
  134. role.GetComponent<SpriteRenderer>().sprite = sp;
  135. }
  136. // 最后将其加入数组中
  137. BattleFieldManager.Instance.RoleArray.Add(role.transform);
  138. return role.GetComponent<Role>();
  139. }
  140. }
  141. }