80 lines
2.7 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
import GameData from "./GameData";
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
moveAmountX : number = 0;
RTL: boolean = false;
moveEnemy : cc.ActionInterval;
@property(cc.Prefab)
yellowBullet: cc.Prefab = null;
enemyLife: number = GameData.instance.enemyHealth;
playAnimation : Boolean = true;
setMovements() {
var moveLeft = cc.moveBy(GameData.instance.enemyMoveTime, cc.v2(-this.moveAmountX, -this.node.parent.getContentSize().height * 0.32));
var moveRight = cc.moveBy(GameData.instance.enemyMoveTime, cc.v2(this.moveAmountX, -this.node.parent.getContentSize().height * 0.32));
this.node.runAction(cc.repeatForever(this.RTL ? cc.sequence(moveRight, moveLeft) : cc.sequence(moveLeft, moveRight)));
}
shootBullets() {
var bullet = cc.instantiate(this.yellowBullet);
bullet.setPosition(this.node.position.x, this.node.position.y - 60);
this.node.parent.addChild(bullet);
}
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.scheduleOnce(this.setMovements, 0.1);
this.schedule(this.shootBullets, GameData.instance.enemyShootingInterval, cc.macro.REPEAT_FOREVER, 0);
}
onCollisionEnter(otherCollider, selfCollider) {
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
this.enemyLife--;
if(this.enemyLife <= 0 && this.playAnimation) {
this.node.getComponent(cc.Collider).enabled = false;
this.node.stopAllActions();
this.playAnimation = false;
this.node.getComponent(cc.Animation).play();
}
}
if(otherCollider.name === 'player<PolygonCollider>') {
this.node.destroy();
cc.director.loadScene('Menu');
}
}
removeExplosion() {
this.node.destroy();
}
start () {
}
update (dt) {
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
this.node.destroy();
cc.director.loadScene('Menu');
}
}
}