123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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<Role>().OriginAttr = value;
- role.GetComponent<Role>().CurrentAttr = value.copy();
- // 创建血条,放在角色下面
- // 转换为屏幕坐标在调整
- GameObject followObj = Instantiate(bloodPre, followPanel);
- // 转化角色的坐标
- Vector3 roleScreen = Camera.main.WorldToScreenPoint(role.transform.position);
- followObj.transform.position = roleScreen;
- role.GetComponent<Role>().SetBloodTrs(followObj.transform);
- // 加载图片资源
- string path = "Textures/Roles/" + value.ID;
- Texture2D tex = Resources.Load<Texture2D>(path);
- Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0));
- if (sp != null) {
- role.GetComponent<SpriteRenderer>().sprite = sp;
- }
- // 最后将其加入数组中
- BattleFieldManager.Instance.RoleArray.Add(role.transform);
- return role.GetComponent<Role>();
- }
- }
- }
|