123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using FSRole;
- using FSBattle;
- using LitJson;
- using FSFile;
- /*
- * 角色管理
- * 未来角色的创建和销毁都在这里
- */
- namespace FSRole {
- public class RoleManager : MonoBehaviour {
- // 角色预制体
- public GameObject rolePrefab;
- // 我方位置
- public Transform armyLoc;
- // 地方位置
- public Transform enemyLoc;
- // 角色
- public ArrayList roles;
- // 保存的角色数据
- public JsonData roleValue;
- // Use this for initialization
- void Start() {
- roles = new ArrayList();
- ReadRoleData();
- CreateRole("1");
- CreateRole("2");
- }
- /*
- * 读取角色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 = (int)oneValue["Hp"];
- attr.Mp = (int)oneValue["Mp"];
- attr.Def = (int)oneValue["Def"];
- attr.Camp = ((int)oneValue["Camp"] == 1) ? RoleCamp.ARMY : RoleCamp.ENEMY;
- attr.ID = int.Parse(id);
- CreateRole(attr);
- } else {
- throw new System.Exception("id : " + id + "无对应的角色 ... ");
- }
- }
- /*
- * 根据角色数值来创建角色
- */
- private void CreateRole(RoleAttr value) {
- GameObject role = null;
- // 判断是哪个阵营的,则放在指定位置下
- if (value.Camp == RoleCamp.ARMY) {
- role = Instantiate(rolePrefab, armyLoc);
- } else {
- role = Instantiate(rolePrefab, enemyLoc);
- }
- 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;
- // 加载图片资源
- string path = "Textures/Roles/" + string.Format("{0:d3}", 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.GetRoleArray().Add(role.transform);
- }
- }
- }
|