126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
|
import { PhysicsSystem2D, Contact2DType, Collider2D } from 'cc';
|
||
|
import { _decorator, Component, Node } from 'cc';
|
||
|
import { UmLog } from '../../../cc-common/cc-util/UmLog';
|
||
|
import { EPhysics2DDrawFlags } from 'cc';
|
||
|
import { Sprite } from 'cc';
|
||
|
import { Color } from 'cc';
|
||
|
import { RigidBody2D } from 'cc';
|
||
|
import { IPhysics2DContact } from 'cc';
|
||
|
import { UmClientEvent } from '../../../cc-common/cc-util/UmOneToMultiListener';
|
||
|
const { ccclass, property } = _decorator;
|
||
|
|
||
|
@ccclass('ColliderObject')
|
||
|
export class ColliderObject extends Component {
|
||
|
@property(Collider2D) colliderComponent: Collider2D = null!;
|
||
|
|
||
|
touchingCountMap: Map<Node, number> = new Map;
|
||
|
private debugDrawFlags: number = 0;
|
||
|
isDebugDrawEnable = false;
|
||
|
// collider: Collider2D = null;
|
||
|
|
||
|
public onBeginContactCallback: ((a: Collider2D, b: Collider2D) => void) | undefined;
|
||
|
public onEndContactCallback: ((a: Collider2D, b: Collider2D) => void) | undefined;
|
||
|
|
||
|
onLoad() {
|
||
|
// this.debugDrawFlags = PhysicsSystem2D.instance.debugDrawFlags;
|
||
|
UmClientEvent.on("off_contact", this.offContact.bind(this));
|
||
|
|
||
|
}
|
||
|
|
||
|
protected onDestroy(): void {
|
||
|
UmClientEvent.off("off_contact", this.offContact.bind(this));
|
||
|
}
|
||
|
|
||
|
get collider(): Collider2D
|
||
|
{
|
||
|
if (!this.colliderComponent)
|
||
|
this.colliderComponent = this.node?.getComponent(Collider2D);
|
||
|
|
||
|
return this.colliderComponent;
|
||
|
}
|
||
|
|
||
|
public registerBeginContact(callback: any = null, isDebugDrawEnable: boolean = false) {
|
||
|
this.isDebugDrawEnable = isDebugDrawEnable;
|
||
|
this.collider?.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||
|
this.onBeginContactCallback = callback;
|
||
|
|
||
|
// this.rigiBody2D.enabled = true;
|
||
|
}
|
||
|
|
||
|
public registerEndContact(callback: any = null, isDebugDrawEnable: boolean = false) {
|
||
|
// this.isDebugDrawEnable = isDebugDrawEnable;
|
||
|
this.collider?.on(Contact2DType.END_CONTACT, this.onEndContact, this);
|
||
|
this.onEndContactCallback = callback;
|
||
|
}
|
||
|
|
||
|
public offContact() {
|
||
|
// UmLog.log("offContact");
|
||
|
// this.onBeginContactCallback = null;
|
||
|
// this.onEndContactCallback = null;
|
||
|
if (this.collider)
|
||
|
{
|
||
|
this.collider?.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||
|
this.collider?.off(Contact2DType.END_CONTACT, this.onEndContact, this);
|
||
|
// this.collider.enabled = false;
|
||
|
// this.rigiBody2D.enabled = false;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
onEnable() {
|
||
|
// if (this.isDebugDrawEnable)
|
||
|
// PhysicsSystem2D.instance.debugDrawFlags = this.debugDrawFlags | EPhysics2DDrawFlags.Shape;
|
||
|
}
|
||
|
onDisable() {
|
||
|
// if (this.isDebugDrawEnable)
|
||
|
// PhysicsSystem2D.instance.debugDrawFlags = this.debugDrawFlags;
|
||
|
}
|
||
|
|
||
|
addContact(c: Collider2D) {
|
||
|
let count = this.touchingCountMap.get(c.node) || 0;
|
||
|
this.touchingCountMap.set(c.node, ++count);
|
||
|
|
||
|
let sprite = c.getComponent(Sprite);
|
||
|
if (sprite) {
|
||
|
sprite.color = Color.RED;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
removeContact(c: Collider2D) {
|
||
|
let count = this.touchingCountMap.get(c.node) || 0;
|
||
|
--count;
|
||
|
if (count <= 0) {
|
||
|
this.touchingCountMap.delete(c.node);
|
||
|
|
||
|
let sprite = c.getComponent(Sprite);
|
||
|
if (sprite) {
|
||
|
sprite.color = Color.WHITE;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
this.touchingCountMap.set(c.node, count);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
onBeginContact(a: Collider2D, b: Collider2D) {
|
||
|
// UmLog.log("otherCollider.group" + b.group);
|
||
|
this.onBeginContactCallback?.(a, b);
|
||
|
if (this.isDebugDrawEnable) {
|
||
|
// this.addContact(a);
|
||
|
// this.addContact(b);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
onEndContact(a: Collider2D, b: Collider2D) {
|
||
|
this.onEndContactCallback?.(a, b);
|
||
|
if (this.isDebugDrawEnable) {
|
||
|
// this.removeContact(a);
|
||
|
// this.removeContact(b);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|