RoleManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /*
  9. * 角色管理
  10. * 未来角色的创建和销毁都在这里
  11. */
  12. namespace FSRole {
  13. public class RoleManager : MonoBehaviour {
  14. // 角色预制体
  15. public GameObject rolePrefab;
  16. // 我方位置
  17. public Transform armyLoc;
  18. // 地方位置
  19. public Transform enemyLoc;
  20. // 角色
  21. public ArrayList roles;
  22. // 保存的角色数据
  23. public JsonData roleValue;
  24. // Use this for initialization
  25. void Start() {
  26. roles = new ArrayList();
  27. ReadRoleData();
  28. CreateRole("1");
  29. CreateRole("2");
  30. }
  31. /*
  32. * 读取角色json数据
  33. */
  34. private void ReadRoleData() {
  35. string json = FileManager.Instance.ReadResourceText("Jsons/roles");
  36. if (json != null)
  37. {
  38. Debug.Log("读取到 json 字符串 : " + json);
  39. try {
  40. roleValue = JsonMapper.ToObject(json);
  41. } catch (JsonException e){
  42. Debug.Log(e.ToString());
  43. }
  44. }
  45. }
  46. /*
  47. * 传入ID,从数组中来获取到角色数值
  48. */
  49. private void CreateRole(string id) {
  50. JsonData oneValue = roleValue[id];
  51. if (oneValue != null && oneValue.IsObject) {
  52. RoleAttr attr = new RoleAttr();
  53. // 配置属性
  54. attr.Hp = (int)oneValue["Hp"];
  55. attr.Mp = (int)oneValue["Mp"];
  56. attr.Def = (int)oneValue["Def"];
  57. attr.Camp = ((int)oneValue["Camp"] == 1) ? RoleCamp.ARMY : RoleCamp.ENEMY;
  58. attr.ID = int.Parse(id);
  59. CreateRole(attr);
  60. } else {
  61. throw new System.Exception("id : " + id + "无对应的角色 ... ");
  62. }
  63. }
  64. /*
  65. * 根据角色数值来创建角色
  66. */
  67. private void CreateRole(RoleAttr value) {
  68. GameObject role = null;
  69. // 判断是哪个阵营的,则放在指定位置下
  70. if (value.Camp == RoleCamp.ARMY) {
  71. role = Instantiate(rolePrefab, armyLoc);
  72. } else {
  73. role = Instantiate(rolePrefab, enemyLoc);
  74. }
  75. role.transform.localScale = new Vector3(0.6f, 0.6f, 1);
  76. role.transform.localPosition = new Vector3(0, 0, 0);
  77. // 添加属性
  78. role.GetComponent<Role>().OriginAttr = value;
  79. role.GetComponent<Role>().CurrentAttr = value;
  80. // 加载图片资源
  81. string path = "Textures/Roles/" + string.Format("{0:d3}", value.ID);
  82. Texture2D tex = Resources.Load<Texture2D>(path);
  83. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0));
  84. if (sp != null) {
  85. role.GetComponent<SpriteRenderer>().sprite = sp;
  86. }
  87. // 最后将其加入数组中
  88. BattleFieldManager.Instance.GetRoleArray().Add(role.transform);
  89. }
  90. }
  91. }