//搜索区域内的 BattleUnit public ArrayList SearchObject(SceneShapeRect shape, int searchType, bool ignoreDead = true) { ArrayList objs = new ArrayList(); PoolSlot slot = mObjPool.UsedList(); while (slot != null) { ObjectBase unit = slot.Content as ObjectBase; if (unit == null) { slot = slot.NextSlot; continue; } if (!ObjectType.IsCanSearch(searchType, unit.Type)) { slot = slot.NextSlot; continue; } Vector3 pos = unit.GetPosition(); if (shape.contains(new Vector2(pos.x, pos.z))) { objs.Add(unit); } slot = slot.NextSlot; } if (!ignoreDead) { for (int i = 0; i < mWaitDestoryObjects.Count; ++i) { ObjectBase obj = mWaitDestoryObjects[i]; if (obj == null) { continue; } if (!ObjectType.IsCanSearch(searchType, obj.Type)) { continue; } Vector3 pos = obj.GetPosition(); if (shape.contains(new Vector2(pos.x, pos.z))) { objs.Add(obj); } } } return(objs); }
// 矩形与多边形重叠(相交或包含) static public bool overlap(SceneShapeRect rect, SceneShapePolygon polygon) { // 矩形有顶点在多边形内 一定相交 if (polygon.contains(rect.leftTop())) { return(true); } if (polygon.contains(rect.rightTop())) { return(true); } if (polygon.contains(rect.rightBottom())) { return(true); } if (polygon.contains(rect.leftBottom())) { return(true); } for (int i = 0; i < polygon.mPts.Count; ++i) { Vector2 pt1 = polygon.mPts[i]; Vector2 pt2 = polygon.mPts[((i + 1) % polygon.mPts.Count)]; // 多边形有顶点在矩形内 一定相交 if (rect.contains(pt1)) { return(true); } // 多边形与矩形有边相交 两图形一定相交 if (Utility.isSegmentIntersect(pt1, pt2, rect.leftTop(), rect.rightTop())) { return(true); } if (Utility.isSegmentIntersect(pt1, pt2, rect.rightTop(), rect.rightBottom())) { return(true); } if (Utility.isSegmentIntersect(pt1, pt2, rect.rightBottom(), rect.leftBottom())) { return(true); } if (Utility.isSegmentIntersect(pt1, pt2, rect.leftBottom(), rect.leftTop())) { return(true); } } return(false); }
// 圆与矩形重叠(相交或包含) static public bool overlap(SceneShapeRound round, SceneShapeRect rect) { if (rect.contains(round.mCenter)) { return(true); } if (round.contains(new Vector2(rect.mLeft, rect.mBottom)) || round.contains(new Vector2(rect.mLeft, rect.mTop)) || round.contains(new Vector2(rect.mRight, rect.mBottom)) || round.contains(new Vector2(rect.mRight, rect.mTop))) { return(true); } float x = rect.mLeft - round.mCenter.x; if (Mathf.Abs(x) > round.mRadius) { return(false); } if (rect.contains(new Vector2(round.mCenter.x + x, round.mCenter.y))) { return(true); } x = rect.mRight - round.mCenter.x; if (Mathf.Abs(x) > round.mRadius) { return(false); } if (rect.contains(new Vector2(round.mCenter.x + x, round.mCenter.y))) { return(true); } x = rect.mBottom - round.mCenter.y; if (Mathf.Abs(x) > round.mRadius) { return(false); } if (rect.contains(new Vector2(round.mCenter.x, round.mCenter.y + x))) { return(true); } x = rect.mTop - round.mCenter.y; if (Mathf.Abs(x) > round.mRadius) { return(false); } if (rect.contains(new Vector2(round.mCenter.x, round.mCenter.y + x))) { return(true); } return(false); }