Commit 21489ab8 authored by Vũ Gia Vương's avatar Vũ Gia Vương

add dynamic rigibody

parent 6afb5718
This diff is collapsed.
...@@ -7,22 +7,40 @@ const { ccclass, property } = cc._decorator; ...@@ -7,22 +7,40 @@ const { ccclass, property } = cc._decorator;
export default class FragmentController extends cc.Component { export default class FragmentController extends cc.Component {
private _collider: cc.PolygonCollider = null; private _collider: cc.PolygonCollider = null;
private _physicsCollider: cc.PhysicsPolygonCollider = null;
private _rg: cc.RigidBody = null;
onLoad() { onLoad() {
this._collider = this.node.getComponent(cc.PolygonCollider); this._collider = this.node.getComponent(cc.PolygonCollider);
console.log('this._collider', this._collider.points.length) this._physicsCollider = this.node.getComponent(cc.PhysicsPolygonCollider);
this._physicsCollider.points = this._collider.points;
this._physicsCollider.enabled = false;
this._rg = this.node.getComponent(cc.RigidBody);
eventTarget.on(CHECK_TOUCH, this.checkTouch, this); eventTarget.on(CHECK_TOUCH, this.checkTouch, this);
} }
private checkTouch(point: cc.Vec2) { private checkTouch(point: cc.Vec2) {
// console.log('point', point.toString(), this.node.position.toString());
const pos = point.clone().subtract(cc.v2(this.node.position.x, this.node.position.y)); const pos = point.clone().subtract(cc.v2(this.node.position.x, this.node.position.y));
let isInside = cc.Intersection.pointInPolygon(pos, this._collider.points); let isInside = cc.Intersection.pointInPolygon(pos, this._collider.points);
// console.log('this._collider.points', this._collider.points.map(i => i.toString()));
if (isInside) { if (isInside) {
console.log('isInside', isInside, pos.toString(), this.node.name) this.dropFragment();
} }
} }
private dropFragment() {
const pos = this.node.position;
this.node.setSiblingIndex(-1);
cc.tween(this.node)
.to(0.2, { scale: 0.95 })
.to(0.2, { scale: 1.05, position: pos.clone().multiplyScalar(1.2) })
.call(() => {
this._physicsCollider.enabled = true;
this._rg.type = cc.RigidBodyType.Dynamic;
this._rg.gravityScale = 2;
})
.start();
}
} }
...@@ -10,6 +10,8 @@ export default class BreakingBottleScreen extends cc.Component { ...@@ -10,6 +10,8 @@ export default class BreakingBottleScreen extends cc.Component {
private spriteNode: cc.Node = null; private spriteNode: cc.Node = null;
onLoad() { onLoad() {
cc.director.getPhysicsManager().enabled = true;
this.node.position = cc.v3(0, 0); this.node.position = cc.v3(0, 0);
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment