关闭

《组合变身小宠物游戏》PetCombine宠物组合(修改更新中)【初学者】

82人阅读 评论(0) 收藏 举报
分类:

这个组合功能功能类似于角色扮演类游戏《妖精组合》中的组合功能,一般是用两个低等级的宠物合成更高等级的宠物。xml表中有合成的公式。

using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class PetCombine : MonoBehaviour {
    public static PetCombine self;
    public GameObject parentObj;
    public GameObject Box1;
    public GameObject Box2;
    public GameObject Box3;
    public Text TextRemindTwoPets;
    public Text TextSorryData;
    public IEnumerator  WaitSecondShow(){//合成的宠物一秒后消失
	yield return new WaitForSeconds(1);
    if (Box3.transform.FindChild("petItem(Clone)")!=null)
        Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
    InitPetHaveData();
        }

    public IEnumerator RemindChooseTwoPets()//选满两个宠物提示
    {

        TextRemindTwoPets.gameObject.SetActive(true);
        yield return new WaitForSeconds(1);
        TextRemindTwoPets.gameObject.SetActive(false);
    }
    public IEnumerator SorryNoData()//数据不足提示
    {
        TextSorryData.gameObject.SetActive(true);
        yield return new WaitForSeconds(1);
        TextSorryData.gameObject.SetActive(false);

    }
    public int ItemNumber
    {
        get
        {
            return Fighting.self.PlayerPetItemList.Count;
        }
    }
    private List<GameObject>gridItemobj = new List<GameObject>();
    private Hashtable recordPetData = new Hashtable();
    void Awake()
    {
        self = this;
    }
    void Start()
    {
        TextRemindTwoPets.gameObject.SetActive(false);
    }
    void OnEnable()
    {       
        InitPetHaveData();      
    }

    void Update()
    {
        if (Input.GetMouseButtonUp(0))//一旦有鼠标点击事件,判断合成
        {
            int id1 = 0, recordid1 = 0;
            int id2 = 0, recordid2 = 0;
            if ((Box1.transform.FindChild("PlayerPetChooseItem(Clone)") == null || Box2.transform.FindChild("PlayerPetChooseItem(Clone)")==null) && Box3.transform.FindChild("petItem(Clone)") != null)
                Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
            if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)") && Box2.transform.FindChild("PlayerPetChooseItem(Clone)"))//选择两个宠物才能组合
            {
                GameObject item1 = Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;//获取第一个宠物数据
                id1 = item1.GetComponent<PlayerPetChooseItem>().data.id;//宠物id
                recordid1 = item1.GetComponent<PlayerPetChooseItem>().recordId;//拥有宠物索引id

                GameObject item2 = Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;//获取第二个宠物数据
                id2 = item2.GetComponent<PlayerPetChooseItem>().data.id;//宠物id
                recordid2 = item2.GetComponent<PlayerPetChooseItem>().recordId;//拥有宠物的索引id
                PetCombineXmlData data = ConfigManager.Instance.GetPetCombineDicData(id1, id2);//确定符合id1和id2的一条数据
                if (data == null)//数据缺少,无法合成
                {
                    //Debug.Log("XML数据缺少,待增加");
                    //StartCoroutine(SorryNoData());
                    if (Box3.transform.FindChild("petItem(Clone)") != null) { Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject); }
                }
                else
                {
                    int CombineId = 0;
                    CombineId = data.CombineId;//该条数据的combineId
                    GameObject gridItem = Resources.Load("Prefabs/item/petItem", typeof(GameObject)) as GameObject;//组合变化出第三个宠物数据

                    if (Box3.transform.FindChild("petItem(Clone)") != null)
                        Debug.Log("原有组合id" + Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id);

                    Debug.Log("新组合id" + CombineId);

                    if (Box3.transform.FindChild("petItem(Clone)") != null && Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id != CombineId)//box3已有宠物,但和现合成的不同》删除原来的并重新加载
                    {//为什么调换宠物没有运行这个判断????
                        Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
                        GameObject obj = GameObject.Instantiate(gridItem) as GameObject;
                        obj.GetComponent<petItem>().UpdatepetItemData(CombineId);
                        obj.transform.parent = Box3.transform;
                        obj.transform.localPosition = Vector3.zero;
                        obj.transform.localScale = Vector3.one;
                    }
                    if (Box3.transform.FindChild("petItem(Clone)") == null)//box3为空,显示合成宠物
                    {
                        GameObject obj = GameObject.Instantiate(gridItem) as GameObject;
                        obj.GetComponent<petItem>().UpdatepetItemData(CombineId);
                        obj.transform.parent = Box3.transform;
                        obj.transform.localPosition = Vector3.zero;
                        obj.transform.localScale = Vector3.one;
                    }
                    if (Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id == CombineId)//box3已有原宠物,且和现在合成宠物相同》不再加载
                    {

                    }


                }

            }
        }
       
    }
    public void InitPetHaveData()
    {
        recordPetData = DataRecord.GetPetList();
        foreach(GameObject obj in gridItemobj)
        {
            Destroy(obj);
        }
        gridItemobj.Clear();
        GameObject BoxgridItem = Resources.Load("Prefabs/item/BoxGrid", typeof(GameObject)) as GameObject;
        GameObject PetgridItem = Resources.Load("Prefabs/item/PlayerPetChooseItem", typeof(GameObject)) as GameObject; 
        foreach (DictionaryEntry itemx in recordPetData)//加载已拥有宠物
        {
           //GameObject BoxgridItem = Resources.Load("Prefabs/item/BoxGrid", typeof(GameObject)) as GameObject;
            if (itemx.Value == null)
                continue;
            //加载格子
            Hashtable data = itemx.Value as Hashtable;//类型转换
            GameObject gridobj = GameObject.Instantiate(BoxgridItem) as GameObject;
            gridobj.name = "BoxGrid" + (gridItemobj.Count + 1).ToString();//添加的预制体的名字,如“BoxGrid1”
            gridobj.transform.parent = parentObj.transform;
            gridobj.transform.localPosition = Vector3.zero;
            gridobj.transform.localScale = Vector3.one;

            gridItemobj.Add(gridobj);
            //加载数据
            GameObject petobj = GameObject.Instantiate(PetgridItem) as GameObject; ;//寻找如“BoxGrid1”父物体
            petobj.GetComponent<PlayerPetChooseItem>().UpdatePlayerPetChooseItemData(int.Parse(itemx.Key.ToString()), DataRecord.TranslateData(data));
            petobj.transform.parent = gridobj.transform;
            petobj.transform.localPosition = Vector3.zero;
            petobj.transform.localScale = Vector3.one;
        }
    }



    public void OnClickBtnBack()
    {
        UIManager.instance.HidePanel(UIPanelNames.PetCombinePanel);
        RoleControl.self.IsCanMove = false;
        RoleControl.self.IsInFight = false;//###
        if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)")!=null)
            { Destroy( Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject); }

        if (Box2.transform.FindChild("PlayerPetChooseItem(Clone)")!=null)
            { Destroy( Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject); }

    }

   
    public void OnClickCombine()
    {
        int id1 = 0, recordid1=0;
        int id2 = 0, recordid2=0;
        if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)") && Box2.transform.FindChild("PlayerPetChooseItem(Clone)"))//选择两个宠物才能组合
        {
            GameObject item1 = Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;//获取第一个宠物数据
             id1 = item1.GetComponent<PlayerPetChooseItem>().data.id;//宠物id
             recordid1 = item1.GetComponent<PlayerPetChooseItem>().recordId;//拥有宠物索引id
  
            GameObject item2 = Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;//获取第二个宠物数据
             id2 = item2.GetComponent<PlayerPetChooseItem>().data.id;//宠物id
             recordid2 = item2.GetComponent<PlayerPetChooseItem>().recordId;//拥有宠物的索引id
        }
        else
        {
         Debug.Log("error");
         StartCoroutine(RemindChooseTwoPets());
            return;
        }
        
        //根据xml表获取到第三个CombinId
        PetCombineXmlData data = ConfigManager.Instance.GetPetCombineDicData(id1, id2);//确定符合id1和id2的一条数据
        if (data == null)//数据缺少,无法合成
        {
            Debug.Log("XML数据缺少,待增加");
            StartCoroutine(SorryNoData());
        }
        else//可以合成
        {
            int CombineId = 0;
            CombineId = data.CombineId;//该条数据的combineId
            if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)")) Destroy(Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject);//销毁原来item1
            if (Box2.transform.FindChild("PlayerPetChooseItem(Clone)")) Destroy(Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject);
            StartCoroutine(WaitSecondShow());//一秒后删除合成的宠物

            DataRecord.DelPet(recordid1);//数据更新
            DataRecord.DelPet(recordid2);
            DataRecord.AddPet(ConfigManager.Instance.GetPetData(CombineId));
        }

    }
}
运行动态图:



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8120次
    • 积分:414
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:1篇
    • 译文:0篇
    • 评论:7条
    心灵鸡汤
    我想,你总觉得自己活得不快乐的原因,大体应该是:既无法忍受目前的状态,又没有能力改变眼前这一切;你虽然可以像只猪一样懒,缺无法像只猪一样懒得心安理得。
    交流学习
    QQ1987885497
    文章分类