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

add polygon in fragment jar

parent f8cdb5e3
This diff is collapsed.
import { CHECK_FRAGMENT, CHECK_TOUCH, DROP_FRAGMENT, eventTarget, HAMMER, PLAY_DROP_SOUND, RESET_FRAGMENT } from "../Events";
const {ccclass, property} = cc._decorator;
@ccclass
export default class FragmentJarController extends cc.Component {
private _collider: cc.PolygonCollider = null;
private _physicsCollider: cc.PhysicsPolygonCollider = null;
private _rg: cc.RigidBody = null;
private _siblingIndex: number = 0;
private _isDropped: boolean = false;
onLoad() {
this._collider = this.node.getComponent(cc.PolygonCollider);
this._physicsCollider = this.node.getComponent(cc.PhysicsPolygonCollider);
this._physicsCollider.points = this._collider.points;
this._rg = this.node.getComponent(cc.RigidBody);
this._siblingIndex = this.node.getSiblingIndex();
this.reset();
eventTarget.on(CHECK_TOUCH, this.checkTouch, this);
eventTarget.on(RESET_FRAGMENT, this.reset, this);
eventTarget.on(DROP_FRAGMENT, () => this.node.name.includes('nap') && this.dropFragment(), this);
}
public reset() {
this._physicsCollider.enabled = false;
this._rg.type = cc.RigidBodyType.Static;
this.node.angle = 0;
this.node.setSiblingIndex(this._siblingIndex);
this._isDropped = false;
}
private checkTouch(point: cc.Vec2) {
if (this._isDropped) {
return;
}
const pos = point.clone().subtract(cc.v2(this.node.position.x, this.node.position.y));
let isInside = cc.Intersection.pointInPolygon(pos, this._collider.points);
if (isInside) {
eventTarget.emit(HAMMER, point);
this.dropFragment();
}
}
private dropFragment() {
this._isDropped = true;
const pos = this.node.position;
this.node.setSiblingIndex(-1);
eventTarget.emit(CHECK_FRAGMENT, this.node.name);
cc.tween(this.node)
.delay(0.5)
.call(() => {
eventTarget.emit(PLAY_DROP_SOUND);
})
.to(0.1, { scale: 0.9 })
.to(0.1, { scale: 1.05 })
.to(0.2, { position: pos.clone().multiplyScalar(1.2) })
.call(() => {
this._physicsCollider.enabled = true;
this._rg.type = cc.RigidBodyType.Dynamic;
this._rg.gravityScale = 10;
})
.delay(1)
.to(0, { scale: 1 })
.start();
}
}
{
"ver": "1.1.0",
"uuid": "6c74056e-3e34-4e34-9605-e6acbcef252c",
"importer": "typescript",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
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