修改四叉树中形状的debug绘制以及删除逻辑

This commit is contained in:
gongxh 2025-05-26 17:10:31 +08:00
parent e0c9444c2d
commit 4269c88ff9
3 changed files with 42 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "kunpocc", "name": "kunpocc",
"version": "1.0.35", "version": "1.0.36",
"description": "基于creator3.0+的kunpocc库", "description": "基于creator3.0+的kunpocc库",
"main": "./dist/kunpocc.cjs", "main": "./dist/kunpocc.cjs",
"module": "./dist/kunpocc.mjs", "module": "./dist/kunpocc.mjs",

View File

@ -4,7 +4,7 @@
* @Description: * @Description:
*/ */
import { Color, Graphics, Intersection2D, rect, Rect } from "cc"; import { Graphics, Intersection2D, rect, Rect } from "cc";
import { Box } from "./Box"; import { Box } from "./Box";
import { Circle } from "./Circle"; import { Circle } from "./Circle";
import { Polygon } from "./Polygon"; import { Polygon } from "./Polygon";
@ -51,7 +51,7 @@ export const QTConfig = {
export class QuadTree { export class QuadTree {
/** @internal */ /** @internal */
private _draw: Graphics; private _graphics: Graphics;
/** @internal */ /** @internal */
private _shapes_map: Map<number, Shape[]>; // 根据类型存储形状对象 private _shapes_map: Map<number, Shape[]>; // 根据类型存储形状对象
/** @internal */ /** @internal */
@ -66,14 +66,14 @@ export class QuadTree {
* *
* @param rect * @param rect
* @param level 0 * @param level 0
* @param draw cc中用于绘制树的绘制组件 * @param graphics cc中用于绘制树的绘制组件
*/ */
constructor(rect: Rect, level: number = 0, draw: Graphics = undefined) { constructor(rect: Rect, level: number = 0, graphics: Graphics = undefined) {
this._shapes_map = new Map(); this._shapes_map = new Map();
this._trees = []; this._trees = [];
this._level = level || 0; this._level = level || 0;
this._bounds = rect; this._bounds = rect;
this._draw = draw; this._graphics = graphics;
} }
/** /**
@ -145,7 +145,7 @@ export class QuadTree {
} }
let shapes = this._shapes_map.get(key); let shapes = this._shapes_map.get(key);
for (const other_shape of shapes) { for (const other_shape of shapes) {
if (!other_shape.invalid && shape !== other_shape && isCollide(shape, other_shape)) { if (other_shape.valid && shape !== other_shape && isCollide(shape, other_shape)) {
result.push(other_shape); result.push(other_shape);
} }
} }
@ -159,7 +159,7 @@ export class QuadTree {
public update(root?: QuadTree): void { public update(root?: QuadTree): void {
root = root || this; root = root || this;
let isRoot = (root === this); let isRoot = (root === this);
isRoot && this._strokeClear() isRoot && this.graphicsClear();
this._updateIgnoreShapes(root); this._updateIgnoreShapes(root);
this._updateShapes(root); this._updateShapes(root);
// 递归刷新子象限 // 递归刷新子象限
@ -167,10 +167,10 @@ export class QuadTree {
tree.update(root); tree.update(root);
} }
this._removeChildTree(); this._removeChildTree();
this._drawTreeBound(root); this.graphicsTreeBound(root);
if (isRoot && this._draw) { if (isRoot && this._graphics) {
this._draw.stroke(); this._graphics.stroke();
} }
} }
@ -244,10 +244,10 @@ export class QuadTree {
let halfheight = bounds.height * 0.5; let halfheight = bounds.height * 0.5;
let nextLevel = this._level + 1; let nextLevel = this._level + 1;
this._trees.push( this._trees.push(
new QuadTree(rect(bounds.center.x, bounds.center.y, halfwidth, halfheight), nextLevel, this._draw), new QuadTree(rect(bounds.center.x, bounds.center.y, halfwidth, halfheight), nextLevel, this._graphics),
new QuadTree(rect(x, bounds.center.y, halfwidth, halfheight), nextLevel, this._draw), new QuadTree(rect(x, bounds.center.y, halfwidth, halfheight), nextLevel, this._graphics),
new QuadTree(rect(x, y, halfwidth, halfheight), nextLevel, this._draw), new QuadTree(rect(x, y, halfwidth, halfheight), nextLevel, this._graphics),
new QuadTree(rect(bounds.center.x, y, halfwidth, halfheight), nextLevel, this._draw) new QuadTree(rect(bounds.center.x, y, halfwidth, halfheight), nextLevel, this._graphics)
); );
} }
@ -268,7 +268,7 @@ export class QuadTree {
} }
for (let i = len - 1; i >= 0; i--) { for (let i = len - 1; i >= 0; i--) {
let shape = this._ignore_shapes[i]; let shape = this._ignore_shapes[i];
if (shape.invalid) { if (!shape.valid) {
this._ignore_shapes.splice(i, 1); this._ignore_shapes.splice(i, 1);
continue; continue;
} }
@ -285,7 +285,7 @@ export class QuadTree {
let len = shapes.length; let len = shapes.length;
for (let i = len - 1; i >= 0; i--) { for (let i = len - 1; i >= 0; i--) {
let shape = shapes[i]; let shape = shapes[i];
if (shape.invalid) { if (!shape.valid) {
shapes.splice(i, 1); shapes.splice(i, 1);
continue; continue;
} }
@ -299,7 +299,7 @@ export class QuadTree {
this._trees[quadrant].insert(shapes.splice(i, 1)[0]); this._trees[quadrant].insert(shapes.splice(i, 1)[0]);
} }
} }
shape.drawShape(this._draw); shape.drawShape(this._graphics);
} }
} }
} }
@ -323,30 +323,28 @@ export class QuadTree {
} }
/** 画出当前树的边界 @internal */ /** 画出当前树的边界 @internal */
private _drawTreeBound(root: QuadTree): void { private graphicsTreeBound(root: QuadTree): void {
if (!this._draw) { if (!this._graphics) {
return; return;
} }
this._draw.lineWidth = 4;
this._draw.strokeColor = Color.BLUE;
if (this._trees.length > 0) { if (this._trees.length > 0) {
this._draw.moveTo(this._bounds.x, this._bounds.center.y); this._graphics.moveTo(this._bounds.x, this._bounds.center.y);
this._draw.lineTo(this._bounds.x + this._bounds.width, this._bounds.center.y); this._graphics.lineTo(this._bounds.x + this._bounds.width, this._bounds.center.y);
this._draw.moveTo(this._bounds.center.x, this._bounds.y); this._graphics.moveTo(this._bounds.center.x, this._bounds.y);
this._draw.lineTo(this._bounds.center.x, this._bounds.y + this._bounds.height); this._graphics.lineTo(this._bounds.center.x, this._bounds.y + this._bounds.height);
} }
if (this == root) { if (this == root) {
this._draw.moveTo(this._bounds.xMin, this._bounds.yMin); this._graphics.moveTo(this._bounds.xMin, this._bounds.yMin);
this._draw.lineTo(this._bounds.xMax, this._bounds.yMin); this._graphics.lineTo(this._bounds.xMax, this._bounds.yMin);
this._draw.lineTo(this._bounds.xMax, this._bounds.yMax); this._graphics.lineTo(this._bounds.xMax, this._bounds.yMax);
this._draw.lineTo(this._bounds.xMin, this._bounds.yMax); this._graphics.lineTo(this._bounds.xMin, this._bounds.yMax);
this._draw.lineTo(this._bounds.xMin, this._bounds.yMin); this._graphics.lineTo(this._bounds.xMin, this._bounds.yMin);
} }
} }
/** 清除绘制 @internal */ /** 清除绘制 @internal */
private _strokeClear(): void { private graphicsClear(): void {
this._draw && this._draw.clear(); this._graphics && this._graphics.clear();
} }
} }

View File

@ -14,9 +14,6 @@ export abstract class Shape {
*/ */
public tag: number = -1; public tag: number = -1;
/** 被标记为无效 下次更新时删除 */
public invalid: boolean = false;
/** 缩放 */ /** 缩放 */
public scale: number; // 缩放 public scale: number; // 缩放
@ -32,6 +29,9 @@ export abstract class Shape {
/** 旋转角度 @internal */ /** 旋转角度 @internal */
protected _rotation: number; protected _rotation: number;
/** 是否有效 下次更新时删除 @internal */
private _valid: boolean = true;
constructor(tag: number) { constructor(tag: number) {
this.tag = tag; this.tag = tag;
this.scale = 1.0; this.scale = 1.0;
@ -61,9 +61,17 @@ export abstract class Shape {
return this._rotation; return this._rotation;
} }
public get valid(): boolean {
return this._valid;
}
/** 包围盒 子类重写 */ /** 包围盒 子类重写 */
public abstract getBoundingBox(): Rect; public abstract getBoundingBox(): Rect;
public destroy(): void {
this._valid = false;
}
/** @internal */ /** @internal */
public drawShape(draw: Graphics): void { public drawShape(draw: Graphics): void {