106 lines
3.9 KiB
TypeScript

// Learn TypeScript:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
private isDragging: boolean = false;
private touchOffset: cc.Vec2 = cc.v2(0, 0);
private shootingInterval: number = 1;
@property(cc.Prefab)
greenBullet: cc.Prefab = null;
playerLifes: number = 5;
onCollisionEnter(otherCollider, selfCollider) {
if(otherCollider.name === 'Bullet<PolygonCollider>') {
this.playerLifes--;
otherCollider.node.destroy();
if(this.playerLifes <= 0) {
this.node.destroy();
cc.director.loadScene('Menu');
}
}
else if(otherCollider.name === 'health<PolygonCollider>') {
this.playerLifes = 5;
otherCollider.node.destroy();
}
else if(otherCollider.name === 'rocket<PolygonCollider>') {
this.shootingInterval = Math.min(this.shootingInterval + 1, 3);
otherCollider.node.destroy();
}
}
spawnBullet(offsetX: number, offsetY: number) {
var bullet = cc.instantiate(this.greenBullet);
bullet.setPosition(this.node.position.x + offsetX, this.node.position.y + offsetY);
this.node.parent.addChild(bullet);
}
shootBullets() {
switch (this.shootingInterval) {
case 1:
this.spawnBullet(0, 40);
break;
case 2:
this.spawnBullet(-30, 20);
this.spawnBullet(30, 20);
break;
case 3:
this.spawnBullet(0, 40);
this.spawnBullet(-30, 20);
this.spawnBullet(30, 20);
break;
}
}
onLoad() {
// Enable touch on this node directly
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
this.schedule(this.shootBullets, 0.25, cc.macro.REPEAT_FOREVER, 0);
}
onTouchStart(event: cc.Event.EventTouch) {
this.isDragging = true;
// Get touch location in parent coordinates
const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
// Calculate offset between jet center and touch point
this.touchOffset = this.node.position.sub(cc.v2(touchPos.x, touchPos.y));
}
onTouchMove(event: cc.Event.EventTouch) {
if (!this.isDragging) return;
// Convert touch position to parent space
const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
// Keep the offset so dragging feels natural (finger stays on the same point)
this.node.setPosition(cc.v2(touchPos.x, touchPos.y).add(this.touchOffset));
}
onTouchEnd(event: cc.Event.EventTouch) {
this.isDragging = false;
}
onDestroy() {
this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
}
}