wewewe

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

public class RoadRandom : MonoBehaviour
{
    private float StartLandLength;
    private float MidLandLength;
    private float EndLandLength;

    public List<GameObject> CornList = new List<GameObject>();

//    private GameObject obj;
    private Transform obj;
    private int CurDistance;
    private int RandomDistance = 20;//30
    //随机一段路的路长度
    private int RemainDistance = 15;//20
    //触发生成随机路段点
    private int SumWeight;
    private List<int> WeightList;
    private List<int> TimesList;
    private int MinCliffIndex = 3;
    private float alreadyDis = 0;
    //记录每一次随机的路段的长度,随机完之后清零,下一次继续记录
    private float offsetX = 0.04f;//二级路面的偏移量,由于路面的大小不一致,需要手动添加一个偏移量
    private float XOffset = 0.04f;//一级路面的偏移量,由于路面的大小不一致,需要手动添加一个偏移量
    //从加载一直到结束的计数
    private int roadType;
    //随机的路段类型 1,2,3,4,5,6
    private int randomNum;
    bool isCliff = false;
    // Cliff 悬崖
    private int StartRandomRoad = 3000;
    //开始第一次要随机的路段点,在100米处开始随机路面  加上中间过渡路面 136
    private int LastRoadType = 0;
    //2级路面
    private float Land2Height = 3.05f;
    //2.1
    //开始随机路段的前的游戏距离,以3000为标准
    private float RandomRoadLength = 1146;
    //3006.5f //1146表示换算成游戏引擎里面的距离,来判断是否加载路面
    //随机路段总长度
    private bool isRandom = false;
    //路段的Y轴偏移量
    private float YOffset = -2.1f;
    private float MonsterYOffset = 0f;
    private List<MonsterEntity> RandomMonsterList;
    private List<List<int>> monsterWeightList;
    private List<int> MonsterSumList;
    private float LandHalfHeight = 0f;

//    private GameObject end_2;
    //中间过渡场景路面高度
    private float MidSceneHeight = 4.72f;
    private ArrayList sceneRandomList;
    //已经添加的中间场景的数量
    private int addedMidSceneNum = 0;
    //是否改变背景
    private bool isChangeBg = false;
    //添加中间场景的距离倍数
    //    private float addMidSceneDisTimes = 3000;
    private float addMidSceneDisTimes = 1146;
    //此倍数是引擎里面的距离
    private float startFirstRandomRoadPoint = 1134.54f;
    //父级路面
    public GameObject parentRoad;
    public GameObject monstersParent;
    float scale ;

    private BtcAcc btnAcc;
    private StopBoss btnStopBoss;
    //路的z轴
    private int roadDepth = 50 ;
    
    // 1134.54f = 2970*0.382f

    void Awake(){
        Init ();
    }


    void Init(){
        GetCommponent getComponent = GetComponent<GetCommponent> ();
        parentRoad = getComponent.defaultGameObject[0];
        monstersParent = getComponent.defaultGameObject[1];
        for (int i = 0; i < 3; i++) {
//            CornList.Add (ResManager.Load("Prefab/Monster/CornsHeap/CornHeap_" + (i+1)));
            CornList.Add (ResourcesUpdate.GetInstance().bundle.Load(string.Format("{0}{1}","CornHeap_",(i+1)),typeof(GameObject))as GameObject);
        }

    }

    void Start ()
    {
        sceneRandomList = new ArrayList{ 2, 3, 4 };//随机场景的序号
        //        RandomMonsterList = MonsterParseXML.monsterDict [LandParseXML.landList [LandParseXML.index].Id];
        //        RandomMonsterList = MonsterParseXML.GetMonsterListById(LandParseXML.landList [LandParseXML.index].Id);
        RandomMonsterList = MonsterParseXML.GetMonsterListById (LandParseXML.GetLand (LandParseXML.index).Id);
//        bgimageload = gameObject.GetComponent<LoadBgImage> ();
        TempValue.GetInstance ().curLandType = (int)Land_type.Dusk_land;
        LandParseXML.index = 0;
        InitLandLength ();
        //玩家行驶距离
        InitCurParm ();
        InitCurMonster ();

        btnAcc = GameObject.FindGameObjectWithTag ("BtnAcc").GetComponent<BtcAcc>();
        btnStopBoss = GameObject.FindGameObjectWithTag ("StopBoss").GetComponent<StopBoss>();
        //第一次加载怪物
//        Monsters_Temp = Monsters_Dusk;
    }

    void InitCurParm ()
    {
        WeightList = new List<int> ();
        TimesList = new List<int> ();
        //LandParseXML.GetLand(LandParseXML.index).Id
        //        LandEntity temp = LandParseXML.landList [LandParseXML.index];
        LandEntity temp = LandParseXML.GetLand (LandParseXML.index);

        TimesList.Add (temp.times1);
        TimesList.Add (temp.times2);
        TimesList.Add (temp.times3);
        TimesList.Add (temp.times4);
        TimesList.Add (temp.times5);
        TimesList.Add (temp.times6);

        WeightList.Add (SumWeight += temp.weight1);
        WeightList.Add (SumWeight += temp.weight2);
        WeightList.Add (SumWeight += temp.weight3);
        WeightList.Add (SumWeight += temp.weight4);
        WeightList.Add (SumWeight += temp.weight5);
        WeightList.Add (SumWeight += temp.weight6);

    }

    void InitCurMonster ()
    {
        monsterWeightList = new List<List<int>> ();
        List<int> singleWeightList;
        MonsterSumList = new List<int> ();
        MonsterEntity temp;
        int flag;
        int count = RandomMonsterList.Count;
        for (int i = 0; i < count; i++) {
            flag = 0;
            singleWeightList = new List<int> ();
            temp = RandomMonsterList [i];
            singleWeightList.Add (flag += temp.SunFlower);
            singleWeightList.Add (flag += temp.Stone);
            singleWeightList.Add (flag += temp.ShutUp);
            singleWeightList.Add (flag += temp.CornHeap);
            singleWeightList.Add (flag += temp.Empty);
            MonsterSumList.Add (flag);
            monsterWeightList.Add (singleWeightList);
        }
    }

    void Update ()
    {
        //*2.6178f 显示玩家距离的系数(玩家距离并非游戏引擎距离)
//        CurDistance = (int)(UserInfos.GetInstance ().distance * 2.6178f);//现在是从0开始,记录车子跑的距离
        CurDistance = UserInfos.GetInstance ().finialDistance;//现在是从0开始,记录车子跑的当前距离
        if (RandomRoadLength > startFirstRandomRoadPoint) {//100是相当于3000米时,判断开始加载随机路面
            //如果随机的路长和当前车子跑的距离小于14米(引擎距离)时,随机下一段路,随机下一段路的路长为30米(引擎距离)
            if (RandomRoadLength - CurDistance * 0.382f < RemainDistance) {
                RandomSingleLand ();
            }
        } else if (CurDistance > StartRandomRoad - RemainDistance) {//StartRandomRoad -  RandomDis 等到汽车行驶到一个点,然后触发随机路面
            RandomSingleLand ();
        }
        if (isChangeBg) {
            ChangeBgTexture ();
        }
    }

    void RandomSingleLand ()
    {
        if (isRandom) {
            return;
        }
        isRandom = true;
        //添加过渡场景
        bool result = NewMidScene (RandomRoadLength);

        //设置路的类型
//        if (result) {
//            SetCurSceneLand (TempValue.GetInstance ().curLandType);
//        }
        //LandParseXML.GetLand(LandParseXML.index).Id
        //        LandEntity le = LandParseXML.landList [LandParseXML.index];
        LandEntity le = LandParseXML.GetLand (LandParseXML.index);
        //开始随机
        int count = WeightList.Count;
        int times = 0;
        int i = 0;
        //现在默认随机 RandomDistance = 30米
        while (alreadyDis <= RandomDistance) { //玩家的距离减去开始的3000米之后,行驶的距离和已经创建路的2/3处对比,判断是否创建下一级路面 
            randomNum = (int)(Random.value * SumWeight);//随机一个权重值
            for (i = 0; i < count; i++) {
                if (WeightList [i] > randomNum) {//和配置表中的权重对比
                    if (isCliff) {
                        roadType = (int)(Random.value * MinCliffIndex) + 1;
                        isCliff = false;
                    } else {
                        roadType = i + 1;//
                        isCliff = (roadType >= MinCliffIndex);//判断是否为悬崖
                    }
                    //创建路段
                    times = TimesList [roadType - 1];
                    createRoad (roadType, times);
                    LastRoadType = roadType > 3 ? 0 : roadType;
                    break;
                }
            }
        }
        RandomRoadLength += alreadyDis;
        alreadyDis = 0;

        if ((CurDistance + RandomDistance) > le.MINDIS) {
            LandParseXML.index++;
            RandomMonsterList = MonsterParseXML.GetMonsterListById (LandParseXML.GetLand (LandParseXML.index).Id);
            InitCurParm ();
        }
        isRandom = false;
    }

    bool NewMidScene (float length)
    {
        int temp = (int)(length / addMidSceneDisTimes);
        if (temp <= addedMidSceneNum) {
            return false;
        }
        //切换场景前添加悬崖
        if (LastRoadType <= (int)LANDTYPE.THIRD) {
            createRoad ((int)LANDTYPE.FOURTH, 1);
            RandomRoadLength += alreadyDis;
            alreadyDis = 0;
            LastRoadType = (int)LANDTYPE.FOURTH;
        }
//        float tempPos = length/2.6178f;
        float tempPos = length;
        //36.5中间场景的长度的一半
        GameObject ob1 = NGUITools.AddToChild (parentRoad, ItemPool.GetInstance ().GetItem ("MidScene"));
        ob1.transform.localPosition = new Vector3 (tempPos + 36.5f, MidSceneHeight, 7f);
        float scale = ItemPool.GetInstance ().GetScale ("MidScene");
        ob1.transform.localScale = new Vector3 (scale,scale,1);

        addedMidSceneNum++;
        ResetCarInMidScene ();
        //72.5f 中间场景的总长度
        RandomRoadLength += 72.5f;
        isChangeBg = true;
        SetRandomBgIndex ();
        createRoad ((int)LANDTYPE.FOURTH, 1);
        RandomRoadLength += alreadyDis;
        alreadyDis = 0;
        LastRoadType = (int)LANDTYPE.FOURTH;
        return true;
    }
        
    /// <summary>
    /// 0、暂停前一个场景的Boss
    /// 1、判断跑过中间场景的次数
    /// 2、在中间场景重新设置汽车配置
    /// 3、在中间场景,不再积蓄飞行模式
    /// </summary>
    void ResetCarInMidScene ()
    {
        //暂停前一个场景的Boss
        GameObject.FindGameObjectWithTag ("Player").GetComponent<CarControlor> ().ChangeBossAccordingToNextLevel (false);
        CarConfig.GetInstance ().throughMidScenetimes++;//统计路过中间场景的次数
        GameObject.FindGameObjectWithTag ("Player").GetComponent<CarControlor> ().ResetCarVelocityInMidScene ();//以前是用碰撞检测来实现,现在改为加载中间场景时设置
        btnAcc.enabled = false;
//        btnStopBoss.stopBossIsEnable = false;
//        btnStopBoss.HideStopBossBtn ();//暂时保留,在过渡场景判断
    }
        

    /// <summary>
    /// 改变游戏背景Texture Update
    /// </summary>
    void ChangeBgTexture ()
    {
        int temp = (int)((UserInfos.GetInstance ().distance) / (addMidSceneDisTimes + 18));
        if (addedMidSceneNum <= 0 || temp <= addedMidSceneNum - 1) {
            return;
        }
//        ChangeRandomBg ();
        isChangeBg = false;
    }

    void createRoad (int roadType, int times)
    {
        //StartLand + MidLand + EndLand 根据游戏距离,切换场景类型、背景类型、路面类型
        //创建路面 
        switch (roadType) {
        case (int)LANDTYPE.FIRST:
            AddRoadLevel1 (times);
            break;
        case (int)LANDTYPE.SECOND:
            //加载2级路面
            AddRoadLevel2 (times);
            break;
        case (int)LANDTYPE.THIRD:
            //加载3级路面
            AddRoadLevel3 (times);
            break;
        case (int)LANDTYPE.FOURTH:
        case (int)LANDTYPE.FIFTH:
        case (int)LANDTYPE.SIXTH:
            //加载6级路面
            AddRoadLevel456 (times);
            break;
        }
    }

    /// <summary>
    /// 添加一级路面
    /// </summary>EndYOffset
    /// <param name="times">Times.</param>
    void AddRoadLevel1 (int times)
    {
        switch (LastRoadType) {
        case 1:
            break;
        case 2:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            alreadyDis += EndLandLength;

            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_1")).transform;  
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + XOffset, YOffset, roadDepth);
            alreadyDis += StartLandLength;

            break;
        case 3:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            break;
        default :
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + XOffset, YOffset, roadDepth);
            alreadyDis += StartLandLength;

            break;
        }

        RandomMonster (times, MonsterYOffset);
        //添加路面RandomMonsterList = MonsterParseXML.monsterDict [LandParseXML.landList[LandParseXML.index ].Id];
        for (int i = 0; i < times; i++) {
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Mid_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            obj.localRotation = Quaternion.identity;
            alreadyDis += MidLandLength;

        }
    }

    /// <summary>
    /// 添加二级路面
    /// </summary>
    /// <param name="roadType">Road type.</param>
    /// <param name="times">Times.</param>
    void AddRoadLevel2 (int times)
    {
        switch (LastRoadType) {
        case 1:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            alreadyDis += EndLandLength;

            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offsetX, Land2Height + YOffset, roadDepth);

            alreadyDis += StartLandLength;

            break;
        case 2:
            break;
        case 3:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);

            break;
        default :
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offsetX, Land2Height + YOffset, roadDepth);
            alreadyDis += StartLandLength;

            break;
        }

        RandomMonster2 (times, Land2Height + MonsterYOffset);
        for (int i = 0; i < times; i++) {
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Mid_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            alreadyDis += MidLandLength;
        }
    }

    /// <summary>
    /// 添加三级路面
    /// </summary>
    /// <param name="roadType">Road type.</param>
    /// <param name="times">Times.</param>
    void AddRoadLevel3 (int times)
    {
        switch (LastRoadType) {
        case 1:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis - StartLandLength + offsetX, Land2Height + YOffset, roadDepth);
            break;
        case 2:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis - StartLandLength + XOffset, YOffset, roadDepth);

            break;
        case 3:
            break;
        default :
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offsetX, Land2Height + YOffset, roadDepth);
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Start_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + XOffset, YOffset, roadDepth);
            alreadyDis += StartLandLength;
            break;
        }

        RandomMonster (times, MonsterYOffset);
        RandomMonster2 (times, Land2Height + MonsterYOffset);
        for (int i = 0; i < times; i++) {
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Mid_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Mid_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            alreadyDis += MidLandLength;
        }
    }

    /// <summary>
    /// 4\5\6添加悬崖
    /// </summary>
    /// <param name="roadType">Road type.</param>
    /// <param name="times">Times.</param>
    void AddRoadLevel456 (int times)
    {
        switch (LastRoadType) {
        case 1:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            alreadyDis += EndLandLength;

            break;
        case 2:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            alreadyDis += EndLandLength;

            break;
        case 3:
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_2")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, Land2Height + YOffset, roadDepth);
            obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"End_1")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            alreadyDis += EndLandLength;
            break;
        default :
            break;
        }

        for (int i = 0; i < times; i++) {
        obj = NGUITools.AddToChild (parentRoad, SetCurSceneLand (TempValue.GetInstance ().curLandType,"Mid_1")).transform;
        obj.GetComponent<SpriteRenderer> ().enabled = false;
        obj.GetComponent<PolygonCollider2D> ().enabled = false;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis, YOffset, roadDepth);
            alreadyDis += MidLandLength;

        }
    }

    /// <summary>
    /// 随机怪物
    /// </summary>
    /// <param name="times">Times.</param>
    /// <param name="Yoffset">Yoffset.</param>
    void RandomMonster (int times, float Yoffset)
    {
        List<float> refpoint = RandomMonsterList [times].RefreshPoint;
        float OnceLandLength = times * MidLandLength;
        if (refpoint.Count < 1) {
            return;
        }
        int SumMonsterWeight = MonsterSumList [times - 1];
        List<int> list = monsterWeightList [times - 1];
        int tempRefpointCount = refpoint.Count;
        int tempListCount = list.Count;
        for (int i = 0; i < tempRefpointCount; i++) {
            int Num = (int)(Random.value * SumMonsterWeight);//随机一个权重值
            for (int j = 0; j < tempListCount; j++) {
                if (list [j] > Num) {//和配置表中的权重对比
                    createMonster (OnceLandLength * refpoint [i], j, Yoffset + LandHalfHeight);
                    break;
                }
            }
        }
    }
    //创建怪物
    void createMonster (float offset, int type, float Yoffset)
    {
        switch (type) {
        case 0: //Sunflower
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("SunflowerRun")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.4f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("SunflowerRun");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Pumpkin")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.4f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Pumpkin");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Cactus")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.4f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Cactus");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Wind_Sway")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.4f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Wind_Sway");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 0.4f, 5), Quaternion.identity);
            break;
        case 1: //Stone
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Stone1")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Stone1");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Bone")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Bone");
                obj.localScale = new Vector3 (scale,scale,1);
                obj.localRotation = Quaternion.identity;
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Grass")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Grass");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Puddles_Wave")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Puddles_Wave");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, 5), Quaternion.identity);
            break;
        case 2: //Mouse
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("MouseRun")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("MouseRun");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Apple")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Apple");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Duck")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Duck");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Frog_Jump")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Frog_Jump");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, 5), Quaternion.identity);
            break;
        case 3: //CornHeap
            //设置玉米堆方法,返回一个玉米GameObject
//            Stopwatch st = new Stopwatch();
//            st.Start();
            GameObject objCornHeap = (GameObject)Instantiate (GetRandomCornHeapType (), new Vector3 (RandomRoadLength + alreadyDis + offset, 0.5f, roadDepth), Quaternion.identity);
//            st.Stop();
//            GLogger.LogWarning("=============================================================生成玉米堆时间" + st.ElapsedMilliseconds.ToString() );
            break;
        case 4: //Empty
            obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Empty")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.2f, roadDepth);
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 0.3f, 5), Quaternion.identity);
            break;
        }
    }

    void RandomMonster2 (int times, float Yoffset)
    {
        List<float> refpoint = RandomMonsterList [times].RefreshPoint;
        float OnceLandLength = times * MidLandLength;
        if (refpoint.Count < 1) {
            return;
        }
        int SumMonsterWeight = MonsterSumList [times - 1];
        List<int> list = monsterWeightList [times - 1];
        int tempRefpointCount = refpoint.Count;
        int tempListCount = list.Count;
        for (int i = 0; i < tempRefpointCount; i++) {
            int Num = (int)(Random.value * SumMonsterWeight);//随机一个权重值
            for (int j = 0; j < tempListCount; j++) {
                if (list [j] > Num) {//和配置表中的权重对比
                    createMonster2 (OnceLandLength * refpoint [i], j, Yoffset + LandHalfHeight);
                    break;
                }
            }
        }
    }
    //创建怪物
    void createMonster2 (float offset, int type, float Yoffset)
    {
        switch (type) {
        case 0: //Sunflower
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("SunflowerRun")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("SunflowerRun");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Pumpkin")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Pumpkin");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Cactus")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Cactus");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Wind_Sway")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Wind_Sway");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, 5), Quaternion.identity);//0.84 0.9899795
            break;
        case 1: //Stone
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Stone1")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Stone1");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Bone")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Bone");
                obj.localScale = new Vector3 (scale,scale,1);
                obj.localRotation = Quaternion.identity;
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Grass")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Grass");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Puddles_Wave")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Puddles_Wave");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, 5), Quaternion.identity);//1.2
            break;
        case 2: //Mouse
            switch(TempValue.GetInstance().curLandType){
            case 0:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("MouseRun")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("MouseRun");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 1:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Ground_Apple")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Ground_Apple");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 2:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Night_Duck")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Night_Duck");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            case 3:
                obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Rain_Frog_Jump")).transform;
                obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, roadDepth);
                scale = ItemPool.GetInstance ().GetScale ("Rain_Frog_Jump");
                obj.localScale = new Vector3 (scale,scale,1);
                break;
            }
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 2f, 5), Quaternion.identity);//0.669744f
            break;
        case 3: //CornHeap
            //设置玉米堆方法,返回一个玉米GameObject
//            Stopwatch st = new Stopwatch();
//            st.Start();
            GameObject objCornHeap = (GameObject)Instantiate (GetRandomCornHeapType (), new Vector3 (RandomRoadLength + alreadyDis + offset, 2.2f, roadDepth), Quaternion.identity);
//            st.Stop();
//            GLogger.LogWarning("=============================================================生成玉米堆时间" + st.ElapsedMilliseconds.ToString() );
            break;
        case 4: //Empty
            obj = NGUITools.AddToChild (monstersParent, ItemPool.GetInstance ().GetItem ("Empty")).transform;
            obj.localPosition = new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, roadDepth);
//            obj = (GameObject)Instantiate (Monsters_Temp [type], new Vector3 (RandomRoadLength + alreadyDis + offset, 2.5f, 5), Quaternion.identity);
            break;
        }
    }

    GameObject GetRandomCornHeapType ()
    {
        GameObject corn;
        if (TempValue.GetInstance ().curLandType == (int)Land_type.Night_land) {
            corn = Instantiate (CornList [Random.Range (0, 3)]) as GameObject;
        } else {
            corn = Instantiate (CornList [Random.Range (0, 2)]) as GameObject;
        }
        return corn;
    }

    private string startLandName = null;
    private string LandFullName = null;
    GameObject SetCurSceneLand (int landType,string endLandName)
    {
        switch (landType) {
        case (int)Land_type.Dusk_land:
            startLandName = "Dusk_land_";
            break;
        case (int)Land_type.Ground_land:
            startLandName = "Ground_land_";
            break;
        case (int)Land_type.Night_land:
            startLandName = "Night_land_";
            break;
        case (int)Land_type.Rain_land:
            startLandName = "Rain_land_";
            break;
        }
        LandFullName = string.Format ("{0}{1}",startLandName,endLandName);
        return ItemPool.GetInstance ().GetItem (LandFullName);
    }

    void InitLandLength ()
    {
//        StartLandLength = start_1.GetComponent<Land> ().GetComponent<SpriteRenderer> ().bounds.size.x;
        StartLandLength = 0.25f;
//        MidLandLength = mid_1.GetComponent<Land> ().GetComponent<SpriteRenderer> ().bounds.size.x;
        MidLandLength = 2.27f;
//        EndLandLength = end_1.GetComponent<Land> ().GetComponent<SpriteRenderer> ().bounds.size.x;
        EndLandLength = 0.25f;
    }
        
    int SetRandomBgIndex ()
    {
        int tempIndex = Random.Range (0, sceneRandomList.Count);
        int tempId = (int)sceneRandomList [tempIndex];
        sceneRandomList.RemoveAt (tempIndex);
//        int tempId = 4;
        if (sceneRandomList.Count <= 0) {
            sceneRandomList.Add (2);
            sceneRandomList.Add (3);
            sceneRandomList.Add (4);
        }

//        switch (tempId) {
//        case 1:
//            Monsters_Temp = Monsters_Dusk;
//            break;
//        case 2:
//            Monsters_Temp = Monsters_Ground;
//            break;
//        case 3:
//            Monsters_Temp = Monsters_Night;
//            break;
//        case 4:
//            Monsters_Temp = Monsters_Rain;
//            break;
//        }

        TempValue.GetInstance ().curLandType = tempId - 1;
        return tempId;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值