super-hero/assets/cc-game/Test/PhysicTest.ts

106 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-05-08 04:03:33 -07:00
import { _decorator, CCInteger, Collider2D, Component, Contact2DType, EventTouch, IPhysics2DContact, log, Node, PhysicsSystem2D, Sprite, Vec3 } from 'cc';
import { RigidBody2D } from 'cc';
import { NodeBase } from '../scripts/base/NodeBase';
import { EPhysics2DDrawFlags } from 'cc';
import { director } from 'cc';
import { UmUtil } from '../../cc-common/cc-util/UmUtil';
import { Vec2 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('PhysicTest')
export class PhysicTest extends NodeBase {
@property(Sprite)
sprite: Sprite = null!;
@property(CCInteger)
deltaX: number = 0;
rigiBody: RigidBody2D;
start() {
this.rigiBody = this.node.getComponent(RigidBody2D);
// Registering callback functions for a single collider
let collider = this.getComponent(Collider2D);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Shape;
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
this.enableTouch();
UmUtil.delay(this.node, 2, () => {
this.rigiBody.applyLinearImpulseToCenter(new Vec2(0, 75), false);
});
// Registering global contact callback functions
// if (PhysicsSystem2D.instance) {
// PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
// PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
// PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
// PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
// }
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called once when two colliders begin to contact
console.log('onBeginContact');
// director.loadScene("Test");
// selfCollider.node.destroy();
}
onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called once when the contact between two colliders just about to end.
console.log('onEndContact');
}
onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called every time collider contact should be resolved
console.log('onPreSolve');
}
onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// will be called every time collider contact should be resolved
console.log('onPostSolve');
}
update(deltaTime: number) {
// log('test');
if (!this.isTouchEnable) return;
// this.node.position = UmUtil.plusTwoVector3(this.node.position, this.directionVec3);
// this.rigiBody.applyForceToCenter(new Vec2(10, 40), true);
}
directionVec3: Vec3 = Vec3.ZERO;
isTouchEnable = true;
currentPoint: Vec3 = Vec3.ZERO;
protected onTouchStart(event: EventTouch) {
this.currentPoint = new Vec3(this.node.position);
// let point = this.getTouchPointInParentNode(event.getUILocation());
}
protected onTouchEnd(event: EventTouch) { }
protected onTouchCancel(event: EventTouch) { }
protected onTouchMove(event: EventTouch) {
let newPoint = this.getTouchPointInParentNode(event.getUILocation());
this.node.position = newPoint;
// let direction = UmUtil.subtractTwoVector3(newPoint, this.currentPoint);
// direction.normalize();
// direction = UmUtil.scaleVector3(direction, 30);
// this.node.position = UmUtil.plusTwoVector3(this.currentPoint, direction);
// this.currentPoint = this.node.position;
// // this.rigiBody.
// // UmLog.log(`onTouchMove: ${event.getLocation()}`);
// // this.node.worldPosition = event.getLocation().toVec3();
}
getTouchPointInParentNode(touchPoint): Vec3 {
return this.node.parent?.getNodeTransform().convertToNodeSpaceAR(touchPoint.toVec3());
}
}