unity开发 宝箱掉落与产出

下面展示 宝箱掉落与产出

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
namespace MR_LBS.Client.Unity3D
{
    
    
    public class CardFall
    {
    
    
        private string sort;
        private int count;

        public string Sort {
    
     get => sort; set => sort = value; }
        public int Count {
    
     get => count; set => count = value; }
    }

    /// <summary>
    /// 战斗掉落宝箱类型
    /// </summary>
    public enum BoxFallType
    {
    
    
        Unkonw = 0,
        woodBox_D = 1,//木宝箱
        woodBox_C = 2,//木宝箱
        woodBox_B = 3,//木宝箱
        woodBox_A = 4,//木宝箱
        silverBox = 5,//银宝箱
        goldBox = 6,//金宝箱
        DiamondsBox = 7,//钻石宝箱
        CoinBox = 8,//财神宝箱
        Box_S = 9//S宝箱

    }

    public class BoxFall
    {
    
    
        private BoxFallType boxFallType;
        private List<BoxFallCard> cardList;
        private int coinNum;
        private string iocnName;

        public BoxFallType BoxFallType {
    
     get => boxFallType; set => boxFallType = value; }
        public int CoinNum {
    
     get => coinNum; set => coinNum = value; }
        public List<BoxFallCard> CardList {
    
     get => cardList; set => cardList = value; }
        public string IocnName {
    
     get => iocnName; set => iocnName = value; }
    }
    public class BoxFallCard
    {
    
    
        private Monster monster;
        private int count;

        public int Count {
    
     get => count; set => count = value; }
        public Monster Monster {
    
     get => monster; set => monster = value; }
    }

    public class BattleSplitController : MonoBehaviour
    {
    
    
        public static BattleSplitController instance;



        /// <summary>
        /// 掉落卡牌列表
        /// </summary>
        public List<CardFall> cardFallList = new List<CardFall>();

        public List<BoxFall> boxFallList = new List<BoxFall>();

        /// <summary>
        /// 玩家不同等级解锁的灵兽类型
        /// 前6个是1级,7-12是2级,其余是3级
        /// 3001代表建筑卡001
        /// </summary>
        private int[] unlockCardList = {
    
     1, 2, 4, 7, 11, 15, 3, 5, 6, 8, 13, 14, 9, 10, 12, 3001 };
        /// <summary>
        /// 使用的列表下标,根据玩家等级控制
        /// </summary>
        private int unlockCardList_index = 5;

        /// <summary>
        /// 宝箱预设体
        /// </summary>
        private GameObject fallBoxPrefab;
        /// <summary>
        /// 卡牌预设体
        /// </summary>
        private GameObject fallcardPrefab;

        BattleController battleCon;
        private void Awake()
        {
    
    
            instance = this;
            fallBoxPrefab = Resources.Load<GameObject>("Objects/FallBoxPrefab");
            fallcardPrefab = Resources.Load<GameObject>("Objects/FallCardPrefab");
        }
        void Start()
        {
    
    

        }

        /// <summary>
        /// 玩家不同等级解锁的灵兽类型
        /// </summary>
        public void UnlockCardList()
        {
    
    
            if (battleCon.petMonsterType.Level == 1)
            {
    
    
                unlockCardList_index = 6;
            }
            else if (battleCon.petMonsterType.Level == 2)
            {
    
    
                unlockCardList_index = 12;
            }
            else if (battleCon.petMonsterType.Level >= 3)
            {
    
    
                unlockCardList_index = unlockCardList.Length - 1;
            }
        }

        /// <summary>
        /// 对战时,对方灵兽死亡掉落卡牌和宝箱几率的最大积数,5即代表1/5
        /// </summary>
        private int cardFallProbability = 5;

        private int cardNumLimit_Min = 17;//卡牌掉落数量限制
        private int cardNumLimit_Max = 32;
        private int coinNumLimit_Min = 360;//宝箱金币掉落数量限制
        private int coinNumLimit_Max = 1700;

        /// <summary>
        /// 敌方非S灵兽死亡掉落物品事件
        /// </summary>
        public void DieFallControl(string sort, Vector3 initPos)
        {
    
    
            if (Random.Range(0, cardFallProbability) == 1)
            {
    
    
                FallCard_MonsterDie(sort, initPos);
            }
        }

        /// <summary>
        /// 敌方S灵兽死亡掉落宝箱事件
        /// </summary>
        /// <param name="initPos">宝箱生成位置</param>
        public void DieFallControl(Vector3 initPos)
        {
    
    
            FallBox_MonsterDie(initPos);

            //if (Random.Range(0, cardFallProbability) == 1)
            //{
    
    
            //    FallBox_MonsterDie(initPos);
            //}
        }

        public void FallCard_MonsterDie(string sort, Vector3 initPos)
        {
    
    
            GameObject tempFall = Instantiate(fallcardPrefab, initPos, fallcardPrefab.transform.rotation);

            //tempFall.GetComponent<SpriteRenderer>().sprite = 
            //tempFall.transform.Find("Icon").GetComponent<SpriteRenderer>().sprite =
            //tempFall.transform.Find("Num").GetComponent<Text>().text =
            StartCoroutine(FallMoveControl(tempFall));
            bool getSign = false;
            foreach (var temp in cardFallList)
            {
    
    
                if (temp.Sort == sort)
                {
    
    
                    temp.Count++;
                    getSign = true;
                    break;
                }
            }
            if (!getSign)
            {
    
    
                CardFall cardFalls = new CardFall();
                cardFalls.Sort = sort;
                cardFalls.Count = 1;
                cardFallList.Add(cardFalls);
            }

        }

        public void FallBox_MonsterDie(Vector3 initPos)
        {
    
    
            BoxFall boxFalls = new BoxFall();
            BoxAddEvent(boxFalls);
            //GameObject tempFall = Instantiate(fallBoxPrefab, initPos, fallBoxPrefab.transform.rotation);
            //tempFall.transform.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("UserInterface/" + boxFalls.IocnName);

            //StartCoroutine(FallMoveControl(tempFall));

        }

        public void FallBox_Result()
        {
    
    
            BoxFall boxFalls = new BoxFall();
            BoxAddEvent(boxFalls);

            //结算面板展示
            //GameObject tempFall = Instantiate(fallBoxPrefab, initPos, fallBoxPrefab.transform.rotation);
            //tempFall.transform.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("UserInterface/" + boxFalls.IocnName);

            //StartCoroutine(FallMoveControl(tempFall));

        }

        IEnumerator FallMoveControl(GameObject fallPrefab)
        {
    
    
            yield return new WaitForSeconds(1);
            fallPrefab.transform.DOMove(MainView.instance.fallRewardImage.position, 1);
        }


        /// <summary>
        /// 随机宝箱,添加到结算列表
        /// </summary>
        public void BoxAddEvent(BoxFall boxFalls)
        {
    
    
            //随机宝箱类型
            int randomTemp = Random.Range(1, 1001);
            if (randomTemp < 800)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.woodBox_D;
                BoxToReward(boxFalls, 20, 100, 7, 19, 5, 6, 3, 4, 2, 3, 0, 0);
                boxFalls.IocnName = "ic_chest_wood1";
            }
            else if (randomTemp >= 800 && randomTemp < 883)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.woodBox_C;
                BoxToReward(boxFalls, 20, 100, 0, 0, 17, 32, 0, 0, 0, 0, 0, 0);
                boxFalls.IocnName = "ic_chest_c1";
            }
            else if (randomTemp >= 883 && randomTemp < 936)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.woodBox_B;
                BoxToReward(boxFalls, 20, 100, 0, 0, 0, 0, 17, 32, 0, 0, 0, 0);
                boxFalls.IocnName = "ic_chest_b1";
            }
            else if (randomTemp >= 936 && randomTemp < 959)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.woodBox_A;
                BoxToReward(boxFalls, 20, 100, 0, 0, 0, 0, 0, 0, 17, 32, 0, 0);
                boxFalls.IocnName = "ic_chest_a1";
            }
            else if (randomTemp >= 959 && randomTemp < 979)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.silverBox;
                BoxToReward(boxFalls, 101, 200, 13, 26, 10, 12, 7, 9, 3, 5, 0, 0);
                boxFalls.IocnName = "ic_chest_silver1";
            }
            else if (randomTemp >= 979 && randomTemp < 989)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.goldBox;
                BoxToReward(boxFalls, 201, 300, 21, 31, 16, 19, 11, 14, 5, 8, 0, 0);
                boxFalls.IocnName = "ic_chest_gold1";
            }
            else if (randomTemp >= 989 && randomTemp < 994)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.DiamondsBox;
                BoxToReward(boxFalls, 301, 400, 29, 36, 22, 26, 15, 19, 7, 11, 0, 0);
                boxFalls.IocnName = "ic_chest_jewel1";
            }
            else if (randomTemp >= 994 && randomTemp < 999)
            {
    
    
                boxFalls.BoxFallType = BoxFallType.CoinBox;
                BoxToReward(boxFalls, 360, 1700, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                boxFalls.IocnName = "ic_chest_wealth1";
            }
            else
            {
    
    
                boxFalls.BoxFallType = BoxFallType.Box_S;
                BoxToReward(boxFalls, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                boxFalls.IocnName = "ic_chest_s1";
            }
            Debug.Log(boxFalls);

            boxFallList.Add(boxFalls);
        }

        /// <summary>
        /// 宝箱转化成奖励
        /// </summary>
        public void BoxToReward(BoxFall boxFalls, int coinMin, int coinMax, int numMin_D, int numMax_D, int numMin_C, int numMax_C, int numMin_B, int numMax_B, int numMin_A, int numMax_A, int numMin_S, int numMax_S)
        {
    
    
            List<BoxFallCard> boxFallCard = new List<BoxFallCard>();
            boxFalls.CardList = boxFallCard;
            boxFalls.CoinNum = Random.Range(coinMin / 10, (coinMax / 10) + 1);//拆分的对战无地雷,金币除以10
            RandomReward(boxFallCard, RareGrade.D, numMin_D, numMax_D);
            RandomReward(boxFallCard, RareGrade.C, numMin_C, numMax_C);
            RandomReward(boxFallCard, RareGrade.B, numMin_B, numMax_B);
            RandomReward(boxFallCard, RareGrade.A, numMin_A, numMax_A);
            RandomReward(boxFallCard, RareGrade.S, numMin_S, numMax_S);
        }

        /// <summary>
        /// 战斗结束宝箱结算
        /// </summary>
        public void OpenBoxEvent()
        {
    
    

        }

        /// <summary>
        /// 战斗结束宝箱结算
        /// </summary>
        public void BoxResult()
        {
    
    

        }

        public void CardFallUpdate()
        {
    
    

        }

        /// <summary>
        /// 随机宝箱奖励
        /// </summary>
        /// <param name="rareGrade"></param>
        /// <param name="numMin"></param>
        /// <param name="numMax"></param>
        private void RandomReward(List<BoxFallCard> boxFallCardList, RareGrade rareGrade, int numMin, int numMax)
        {
    
    
            int num = Random.Range(numMin, numMax);
            for (int i = 0; i < num; i++)
            {
    
    
                //从解锁池中随机拿一个sort
                string sort = GetSort(rareGrade);
                Debug.Log(sort);
                //判断此宝箱中是否已有当前卡牌
                bool have = false;
                foreach (var temp in boxFallCardList)
                {
    
    
                    if (temp.Monster.Sort == sort)
                    {
    
    
                        temp.Count++;
                        have = true;
                        break;
                    }
                }
                if (!have)
                {
    
    
                    BoxFallCard boxFallCard = new BoxFallCard();
                    Monster monster = new Monster();
                    boxFallCard.Monster = monster;
                    monster.Sort = sort;
                    monster.RareGrade = rareGrade;
                    boxFallCard.Count = 1;
                    boxFallCardList.Add(boxFallCard);
                }

            }

        }

        /// <summary>
        /// 随机生成一张卡牌
        /// </summary>
        /// <param name="rareGrade"></param>
        /// <returns></returns>
        public string GetSort(RareGrade rareGrade)
        {
    
    
            int sortTemp = unlockCardList[Random.Range(0, unlockCardList_index)];
            string sort = "";
            if (rareGrade != RareGrade.S)
            {
    
    
                if (sortTemp < 1000)
                {
    
    
                    sort = (sortTemp * 4 + 1 - rareGrade).ToString();
                    if (sort.Length == 1)
                        sort = "10100100" + sort;
                    else if (sort.Length == 2)
                        sort = "1010010" + sort;
                    else
                        sort = "101001" + sort;
                }
                //建筑
                else
                {
    
    
                    sort = (sortTemp * 4 + 1 - rareGrade).ToString();
                    if (sort.Length == 1)
                        sort = "10100300" + sort;
                    else if (sort.Length == 2)
                        sort = "1010030" + sort;
                    else
                        sort = "101003" + sort;
                }
            }
            else
            {
    
    
                if (sortTemp < 1000)
                {
    
    
                    //sort = (sortTemp * 4 -1).ToString();
                    if (sort.Length == 1)
                        sort = "10100200" + sort;
                    else if (sort.Length == 2)
                        sort = "1010020" + sort;
                    else
                        sort = "101002" + sort;
                }
                //建筑
                else
                {
    
    
                    //sort = (sortTemp * 4 -1).ToString();
                    if (sort.Length == 1)
                        sort = "10100400" + sort;
                    else if (sort.Length == 2)
                        sort = "1010040" + sort;
                    else
                        sort = "101004" + sort;
                }
            }

            return sort;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43505432/article/details/110800409