using System.Collections; using System.Collections.Generic; using UnityEngine; using FSRole; using FSBattle; using LitJson; using FSFile; using FSAssist; using UnityEngine.UI; using FSCard; /* * 角色管理 * 未来角色的创建和销毁都在这里 */ namespace FSRole { public class RoleManager : MonoBehaviour { // 角色预制体 public GameObject rolePrefab; // 我方位置 public Transform armyLoc; // 地方位置 public Transform enemyLoc; // 角色 public ArrayList roles; // 保存的角色数据 public JsonData roleValue; // 跟随面板 public Transform followPanel; // 血量的预制体 public GameObject bloodPre; // 卡片预制体 public GameObject cardPrefab; // 缓存地 public Transform cachePlace; // Use this for initialization void Start() { roles = new ArrayList(); ReadRoleData(); CreateRole("00000"); CreateRole("10002"); } /* * 读取角色json数据 */ private void ReadRoleData() { string json = FileManager.Instance.ReadResourceText("Jsons/roles"); if (json != null) { Debug.Log("读取到 json 字符串 : " + json); try { roleValue = JsonMapper.ToObject(json); } catch (JsonException e){ Debug.Log(e.ToString()); } } } /* * 传入ID,从数组中来获取到角色数值 */ private void CreateRole(string id) { JsonData oneValue = roleValue[id]; if (oneValue != null && oneValue.IsObject) { RoleAttr attr = new RoleAttr(); // 配置属性 attr.Hp = AssistMethods.ReadInt(oneValue, "hp"); attr.MaxHp = attr.Hp; attr.Mp = AssistMethods.ReadInt(oneValue, "mp"); attr.Camp = (AssistMethods.ReadInt(oneValue, "camp") == 1) ? RoleCamp.ARMY : RoleCamp.ENEMY; attr.ID = id; Role role = CreateRole(attr); JsonData cards = oneValue["cards"]; if (cards != null) { if (cards.IsArray) { for (int i = 0; i < cards.Count; i++) { string cardId = AssistMethods.ReadString(cards, i); GameObject cardObj = Instantiate(cardPrefab, cachePlace); // 挂载卡片组件 CardManager.Instance.LoadCard(cardObj, cardId, role); // 添加进卡池中 if (role.OriginAttr.Camp == RoleCamp.ARMY) { BattleFieldManager.Instance.ArmyTakePool.Add(cardObj.transform); } else { BattleFieldManager.Instance.EnemyTakePool.Add(cardObj.transform); } } } } } else { throw new System.Exception("id : " + id + "无对应的角色 ... "); } } /* * 根据角色数值来创建角色 */ private Role CreateRole(RoleAttr value) { GameObject role = null; int index; // 判断是哪个阵营的,则放在指定位置并加入指定数组中 if (value.Camp == RoleCamp.ARMY) { role = Instantiate(rolePrefab, armyLoc); BattleFieldManager.Instance.ArmyArray.Add(role.transform); index = BattleFieldManager.Instance.ArmyArray.Count - 1; } else { role = Instantiate(rolePrefab, enemyLoc); BattleFieldManager.Instance.EnemyArray.Add(role.transform); index = BattleFieldManager.Instance.EnemyArray.Count - 1; } // 第奇数个和偶数个处理 if (index % 2 == 0) { // 偶数个 role.transform.localPosition = new Vector3(index / 2 * AssistConfig.RoleSpace, 0, 0); } else { // 奇数个 float loc = (float)index / 2 + 0.5f; role.transform.localPosition = new Vector3(loc * AssistConfig.RoleSpace * -1, 0, 0); } role.transform.localScale = new Vector3(0.6f, 0.6f, 1); //role.transform.localPosition = new Vector3(0, 0, 0); // 添加属性 role.GetComponent().OriginAttr = value; role.GetComponent().CurrentAttr = value.copy(); // 创建血条,放在角色下面 // 转换为屏幕坐标在调整 GameObject followObj = Instantiate(bloodPre, followPanel); // 转化角色的坐标 Vector3 roleScreen = Camera.main.WorldToScreenPoint(role.transform.position); followObj.transform.position = roleScreen; role.GetComponent().SetBloodTrs(followObj.transform); // 加载图片资源 string path = "Textures/Roles/" + value.ID; Texture2D tex = Resources.Load(path); Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0)); if (sp != null) { role.GetComponent().sprite = sp; } // 最后将其加入数组中 BattleFieldManager.Instance.RoleArray.Add(role.transform); return role.GetComponent(); } } }