RoleManager.cs 6.3 KB

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