Example #1
0
        /// <summary>
        /// 触发检测
        /// </summary>
        IEnumerator TriggerDetection()
        {
            int trigger_count = triggers.Length;

            List <int>[] trigger_players = new List <int> [trigger_count];
            float        t = Time.time;

            while (true)
            {
                t = Time.time;
                for (int i = 0; i < triggers.Length; i++)
                {
                    SpaceData data = triggers[i];
                    foreach (var player in players)
                    {
                        if (data.IsTherein(player.position))
                        {
                            // 在这个区域内
                            if (trigger_players[i] == null)
                            {
                                trigger_players[i] = new List <int>();
                            }
                            int id = player.GetInstanceID();
                            if (!trigger_players[i].Contains(id))
                            {
                                trigger_players[i].Add(id);
                                EnterArea(i);
                            }
                        }
                        else
                        {
                            // 在这个区域外
                            if (trigger_players[i] != null)
                            {
                                int id = player.GetInstanceID();
                                if (trigger_players[i].Contains(id))
                                {
                                    trigger_players[i].Remove(id);
                                    ExitArea(i);
                                }
                            }
                        }
                    }
                    if (i % 100 == 0)
                    {
                        yield return(0);
                    }
                }
            }
        }
Example #2
0
        void GenerateOther()
        {
            float min_x = groundData.min_x + 2;
            float max_x = groundData.max_x - 2;
            float min_z = groundData.min_z + 2;
            float max_z = groundData.max_z - 2;

            List <SpaceData> tmp_trees     = new List <SpaceData>();
            List <SpaceData> tmp_decorates = new List <SpaceData>();
            int   min_map_pos = generate.map_edge - generate.ground_max;
            float max_map_pos = generate.map_size + generate.map_edge + generate.ground_max;

            for (float i = min_x; i <= max_x; i++)
            {
                for (float j = min_z; j <= max_z; j++)
                {
                    float      x      = i + MapTools.RandomRange(-0.38f, 0.38f, rand);
                    float      z      = j + MapTools.RandomRange(-0.38f, 0.38f, rand);
                    CreateType create = CreateType.Decorate;
                    Vector3    pos    = new Vector3(x, ground_height, z);

                    // 检查不在任何一座城市内
                    bool onCity = false;
                    bool onDoor = false;

                    lock (city_rects) {
                        for (int k = 0; k < city_rects.Length; k++)
                        {
                            float[] r = city_rects[k];
                            if (pos.x > r[0] && pos.x < r[2] && pos.z < r[3])
                            {
                                if (pos.z > r[1])
                                {
                                    onCity = true;
                                    break;
                                }
                                else if (!onDoor && pos.z > (r[1] - 50))
                                {
                                    // 城门方向50米不创建树
                                    onDoor = true;
                                }
                            }
                        }
                    }

                    if (!onCity)
                    {
                        if (spaceData.IsTherein(pos))
                        {
                            create = CreateType.Tree;
                        }
                        int ran_create_id = MapTools.RandomRange(0, 100, rand);
                        if (create != CreateType.City)
                        {
                            if (!onDoor && ran_create_id < generate.tree_density && create == CreateType.Tree)
                            {
                                float   max_dis    = Mathf.Max(spaceData.scale.x, spaceData.scale.z) / 2.5f;
                                Vector3 center     = spaceData.pos;
                                float   center_dis = Vector3.Distance(pos, center);
                                bool    can_create = center_dis < max_dis;
                                if (can_create)
                                {
                                    float tree_size = MapTools.RandomRange(0.75f, 1.25f, rand);
                                    if (center_dis < max_dis * 0.5f)
                                    {
                                        tree_size *= MapTools.RandomRange(1f, 1.25f, rand);
                                    }
                                    else if (center_dis < max_dis * 0.25f)
                                    {
                                        tree_size *= MapTools.RandomRange(1.25f, 1.5f, rand);
                                    }
                                    else if (center_dis < max_dis * 0.1f)
                                    {
                                        tree_size *= MapTools.RandomRange(1.5f, 2f, rand);
                                    }
                                    int       ran  = MapTools.RandomRange(0, generate.treeObjs[0].objs.Length, rand);
                                    SpaceData tree = new SpaceData(pos, new Vector3(tree_size, tree_size, tree_size), SpaceType.Tree, angle: MapTools.RandomRange(0f, 360f, rand), id: 0, idx: (short)ran);
                                    foreach (var item in tmp_trees)
                                    {
                                        if (item.IsOverlap(tree, 1))
                                        {
                                            can_create = false;
                                        }
                                    }
                                    if (can_create)
                                    {
                                        tmp_trees.Add(tree);
                                    }
                                }
                            }
                            else if (ran_create_id < generate.decorate_density)
                            {
                                bool can_create = true;
                                if (create == CreateType.City)
                                {
                                    can_create = false;
                                }
                                if (can_create)
                                {
                                    int       ran           = MapTools.RandomRange(0, generate.decorateObjs[0].objs.Length, rand);
                                    float     decorate_size = MapTools.RandomRange(0.2f, 0.5f, rand);
                                    SpaceData decorate      = new SpaceData(pos, new Vector3(decorate_size, decorate_size, decorate_size), SpaceType.Decorate, angle: MapTools.RandomRange(0f, 360f, rand), id: 0, idx: (short)ran);
                                    tmp_decorates.Add(decorate);
                                }
                            }
                        }
                    }
                }
            }
            tree_datas     = tmp_trees.ToArray();
            decorate_datas = tmp_decorates.ToArray();
        }