private void SetDynamicObsInfo(ref List <int> effectList, ushort idx, Vector2 startPos, Vector2 endPos, byte polygonIdx, float checkSize = 0.5f)
        {
            float offsetX = endPos.x - startPos.x;
            float offsetY = endPos.y - startPos.y;
            float partX   = Mathf.Abs((endPos.x - startPos.x) / checkSize);
            float partY   = Mathf.Abs((endPos.y - startPos.y) / checkSize);
            int   part    = Mathf.CeilToInt(Mathf.Max(partX, partY));
            int   transX  = gridYCount;

            if (partX > partY)
            {
                transX = 1;
            }

            for (int j = 0; j <= part; j++)
            {
                Vector2 pos;
                if (j == part)
                {
                    pos = endPos;
                }
                else
                {
                    pos = startPos + new Vector2(offsetX * j / part, offsetY * j / part);
                }
                int posIndex = GetGridIndex(pos.x, pos.y);
                if (gridNodeList.Count < posIndex)
                {
                    posIndex = gridNodeList.Count - 1;
                }
                List <int> gridListIdx = new List <int>();
                gridListIdx.Add(posIndex);
                gridListIdx.Add(posIndex - transX);
                gridListIdx.Add(posIndex + transX);

                foreach (int item in gridListIdx)
                {
                    if (item < 0 || gridNodeList.Count <= item)
                    {
                        continue;
                    }
                    if (gridNodeList[item].type != GridNode.Type.Obstacle_Battle)
                    {
                        EditorGridNode node = new EditorGridNode();
                        gridNodeList[item].CopyTo(ref node);
                        _orgNodeMap.Add(item, node);
                    }

                    gridNodeList[item].type = GridNode.Type.Obstacle_Battle;
                    if (gridNodeList[item].AddObsInfo(polygonIdx, idx))
                    {
                        effectList.Add(item);
                    }
                }
            }
        }
        public void ClearDynamicCollisions()
        {
            List <int> removeEid = new List <int>();

            foreach (KeyValuePair <int, DynamicInfo> item in _dynamicMap)
            {
                DynamicInfo info = new DynamicInfo();
                info = item.Value;
                combinedPolygonList[info.polygonIdx].points = null;
                _delayRemovePolygon.Add(combinedPolygonList[info.polygonIdx]);
            }
            foreach (KeyValuePair <int, EditorGridNode> item in _orgNodeMap)
            {
                EditorGridNode node = _orgNodeMap[item.Key];
                _orgNodeMap[item.Key].CopyTo(ref node);
            }
            _orgNodeMap.Clear();
            _dynamicMap.Clear();
            RemoveDelayDynamicPolygon();
        }
        public void RemoveDynamicCollisions(int eid)
        {
            DynamicInfo info = new DynamicInfo();

            if (!_dynamicMap.ContainsKey(eid))
            {
                return;
            }
            info = _dynamicMap[eid];

            for (int i = 0; i < info.effectNode.Count; i++)
            {
                int  nodeIdx = info.effectNode[i];
                bool result  = gridNodeList[nodeIdx].RemoveObsInfo(info.polygonIdx);
                if (result)
                {
                    if (!_orgNodeMap.ContainsKey(nodeIdx))
                    {
                        continue;
                    }
                    if (_orgNodeMap[nodeIdx].obsLists.Count == gridNodeList[nodeIdx].obsLists.Count)
                    {
                        EditorGridNode node = gridNodeList[nodeIdx];
                        _orgNodeMap[nodeIdx].CopyTo(ref node);
                        _orgNodeMap.Remove(nodeIdx);
                    }
                }
            }

            combinedPolygonList[info.polygonIdx].points = null;
            _delayRemovePolygon.Add(combinedPolygonList[info.polygonIdx]);
            _dynamicMap.Remove(eid);

            if (_dynamicMap.Count == 0)
            {
                RemoveDelayDynamicPolygon();
            }
        }
 public void CopyTo(ref EditorGridNode node)
 {
     node.index = this.index;
     node.obsLists.AddRange(this.obsLists);
     node.type = this.type;
 }
        public void OnDrawGizmos()
        {
            Vector3 start = Vector3.zero;
            Vector3 end   = Vector3.zero;

            if (showCube)
            {
                Gizmos.color = Color.gray;
                for (int i = 0; i < polygonList.Count; i++)
                {
                    Polygon2D pol = polygonList[i];

                    for (int j = 0; j < pol.points.Count; j++)
                    {
                        Vector3 pos = new Vector3(pol.points[j].x, drawGizmosHeight, pol.points[j].y);
                        if (j == 0)
                        {
                            Gizmos.color = Color.blue;
                        }
                        if (j == 1)
                        {
                            Gizmos.color = Color.red;
                        }
                        if (j == 2)
                        {
                            Gizmos.color = Color.green;
                        }
                        if (j == 3)
                        {
                            Gizmos.color = Color.gray;
                        }
                        Gizmos.DrawSphere(pos, 0.3f);

                        Gizmos.color = Color.gray;
                        int nextIndex = j + 1;
                        if (nextIndex == pol.points.Count)
                        {
                            nextIndex = 0;
                        }
                        Vector2 selfp1 = pol.points[j];
                        Vector2 selfp2 = pol.points[nextIndex];
                        start.Set(selfp1.x, drawGizmosHeight, selfp1.y);
                        end.Set(selfp2.x, drawGizmosHeight, selfp2.y);
                        Gizmos.DrawLine(start, end);
                    }
                }
            }

            if (showGrids)
            {
                Gizmos.color = Color.grey;
                int countX = gridXCount;
                int countY = gridYCount;

                for (int i = 0; i < countX; i++)
                {
                    float x = i * GridNode.GRID_SIZE + (int)gridRange.xMin;
                    float y = gridRange.yMin;
                    start.Set(x, drawGizmosHeight, y);
                    end.Set(x, drawGizmosHeight, gridRange.yMax);
                    Gizmos.DrawLine(start, end);
                }

                for (int j = 0; j < countY; j++)
                {
                    float x = gridRange.xMin;
                    float y = j * GridNode.GRID_SIZE + (int)gridRange.yMin;

                    start.Set(x, drawGizmosHeight, y);
                    end.Set(gridRange.xMax, drawGizmosHeight, y);
                    Gizmos.DrawLine(start, end);
                }
            }


            for (int i = 0; i < singleLinesList.Count; i++)
            {
                Polygon3D poly = singleLinesList[i];
                for (int j = 0; j < poly.points.Count; j++)
                {
                    Gizmos.color = Color.red;
                    if (j + 1 < poly.points.Count)
                    {
                        Gizmos.DrawLine(poly.points[j], poly.points[j + 1]);
                    }
                    Gizmos.color = j == singleLinePointIndex ? Color.red : Color.green;
                    Gizmos.DrawSphere(poly.points[j], 0.3f);
                }
                Gizmos.color = Color.cyan;
                Gizmos.DrawLine(poly.tempLineA, poly.tempLineB);
            }

            if (showCombindMesh || showMeshWallNode)
            {
                for (int i = 0; i < combinedPolygonList.Count; i++)
                {
                    Polygon2D pol = combinedPolygonList[i];
                    Gizmos.color = i == curSeletedCombinedPolygonIndex ? Color.red : Color.green;

                    for (int j = 1; j <= pol.points.Count; j++)
                    {
                        bool outOfRange = j == pol.points.Count;
                        if (showMeshWallNode && outOfRange)
                        {
                            break;
                        }
                        Vector2 selfp1 = outOfRange ? pol.points[pol.points.Count - 1] : pol.points[j - 1];
                        Vector2 selfp2 = outOfRange ? pol.points[0] : pol.points[j];
                        start.Set(selfp1.x, drawGizmosHeight, selfp1.y);
                        end.Set(selfp2.x, drawGizmosHeight, selfp2.y);

                        if (pol.polygonType == PolygonType.Jump && outOfRange)
                        {
                            continue;
                        }
                        Gizmos.DrawLine(start, end);
                    }
                }
            }

            if (showObs || showArea)
            {
                for (int i = 0; i < gridNodeList.Count; i++)
                {
                    EditorGridNode node = gridNodeList[i];

                    if (node.isObs)
                    {
                        if (showObs)
                        {
                            if (node.type == GridNode.Type.Obstacle_Safe)
                            {
                                Gizmos.color = Color.yellow;
                            }
                            else if (node.type == GridNode.Type.Obstacle_Battle)
                            {
                                Gizmos.color = Color.black;
                            }
                            start.Set(node.rect.center.x, drawGizmosHeight, node.rect.center.y);
                            end.Set(node.rect.size.x, 0, node.rect.size.y);
                            Gizmos.DrawWireCube(start, end);
                        }
                    }
                    else
                    {
                        if (showArea)
                        {
                            if (node.type == GridNode.Type.Safe)
                            {
                                Gizmos.color = Color.green;
                            }
                            else if (node.type == GridNode.Type.Battle)
                            {
                                Gizmos.color = Color.red;
                            }
                            else
                            {
                                continue;
                            }

                            if (i % areaResolution == 0)
                            {
                                start.Set(node.rect.xMin, drawGizmosHeight, node.rect.yMin);
                                end.Set(node.rect.xMax, drawGizmosHeight, node.rect.yMax);
                                Gizmos.DrawLine(start, end);
                                start.Set(node.rect.xMin, drawGizmosHeight, node.rect.yMax);
                                end.Set(node.rect.xMax, drawGizmosHeight, node.rect.yMin);
                                Gizmos.DrawLine(start, end);
                            }
                        }
                    }
                }
            }
        }