RoleManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. CreateRole("10002");
  42. }
  43. /*
  44. * 读取角色json数据
  45. */
  46. private void ReadRoleData() {
  47. string json = FileManager.Instance.ReadResourceText("Jsons/roles");
  48. if (json != null)
  49. {
  50. Debug.Log("读取到 json 字符串 : " + json);
  51. try {
  52. roleValue = JsonMapper.ToObject(json);
  53. } catch (JsonException e){
  54. Debug.Log(e.ToString());
  55. }
  56. }
  57. }
  58. /*
  59. * 传入ID,从数组中来获取到角色数值
  60. */
  61. private void CreateRole(string id) {
  62. JsonData oneValue = roleValue[id];
  63. if (oneValue != null && oneValue.IsObject) {
  64. RoleAttr attr = new RoleAttr();
  65. // 配置属性
  66. attr.Hp = AssistMethods.ReadInt(oneValue, "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. print("从中获取到了卡片数组 ... " + AssistMethods.ReadString(cards, i));
  77. GameObject cardObj = Instantiate(cardPrefab, cachePlace);
  78. // 挂载卡片组件
  79. CardManager.Instance.LoadCard(cardObj, cardId, role);
  80. // 添加进卡池中
  81. if (role.OriginAttr.Camp == RoleCamp.ARMY) {
  82. BattleFieldManager.Instance.ArmyTakePool.Add(cardObj.transform);
  83. } else {
  84. BattleFieldManager.Instance.EnemyTakePool.Add(cardObj.transform);
  85. }
  86. }
  87. }
  88. }
  89. } else {
  90. throw new System.Exception("id : " + id + "无对应的角色 ... ");
  91. }
  92. }
  93. /*
  94. * 根据角色数值来创建角色
  95. */
  96. private Role CreateRole(RoleAttr value) {
  97. GameObject role = null;
  98. int index;
  99. // 判断是哪个阵营的,则放在指定位置并加入指定数组中
  100. if (value.Camp == RoleCamp.ARMY) {
  101. role = Instantiate(rolePrefab, armyLoc);
  102. BattleFieldManager.Instance.ArmyArray.Add(role.transform);
  103. index = BattleFieldManager.Instance.ArmyArray.Count - 1;
  104. } else {
  105. role = Instantiate(rolePrefab, enemyLoc);
  106. BattleFieldManager.Instance.EnemyArray.Add(role.transform);
  107. index = BattleFieldManager.Instance.EnemyArray.Count - 1;
  108. }
  109. // 第奇数个和偶数个处理
  110. if (index % 2 == 0) {
  111. // 偶数个
  112. role.transform.localPosition = new Vector3(index / 2 * AssistConfig.RoleSpace, 0, 0);
  113. } else {
  114. // 奇数个
  115. float loc = (float)index / 2 + 0.5f;
  116. role.transform.localPosition = new Vector3(loc * AssistConfig.RoleSpace * -1, 0, 0);
  117. }
  118. role.transform.localScale = new Vector3(0.6f, 0.6f, 1);
  119. //role.transform.localPosition = new Vector3(0, 0, 0);
  120. // 添加属性
  121. role.GetComponent<Role>().OriginAttr = value;
  122. role.GetComponent<Role>().CurrentAttr = value.copy();
  123. // 创建血条,放在角色下面
  124. // 转换为屏幕坐标在调整
  125. GameObject followObj = Instantiate(bloodPre, followPanel);
  126. // 转化角色的坐标
  127. Vector3 roleScreen = Camera.main.WorldToScreenPoint(role.transform.position);
  128. followObj.transform.position = roleScreen;
  129. role.GetComponent<Role>().SetBloodTrs(followObj.transform);
  130. // 加载图片资源
  131. string path = "Textures/Roles/" + value.ID;
  132. Texture2D tex = Resources.Load<Texture2D>(path);
  133. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0));
  134. if (sp != null) {
  135. role.GetComponent<SpriteRenderer>().sprite = sp;
  136. }
  137. // 最后将其加入数组中
  138. BattleFieldManager.Instance.RoleArray.Add(role.transform);
  139. return role.GetComponent<Role>();
  140. }
  141. }
  142. }