111 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-10-24 16:12:44 +04:00
// 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
2025-10-29 13:27:41 +04:00
import GameData from "./GameData";
2025-11-03 15:27:17 +04:00
import SoundManager from "./SoundsManager";
2025-10-29 13:27:41 +04:00
2025-10-24 16:12:44 +04:00
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
2025-10-31 16:20:20 +04:00
@property(cc.Label)
scoreLabel: cc.Label = null;
2025-10-28 16:32:18 +04:00
moveAmountX : number = 0;
RTL: boolean = false;
2025-10-27 14:50:15 +04:00
2025-10-30 15:03:57 +04:00
specialItem: string = null;
2025-10-24 16:12:44 +04:00
moveEnemy : cc.ActionInterval;
@property(cc.Prefab)
yellowBullet: cc.Prefab = null;
2025-10-30 15:03:57 +04:00
@property(cc.Prefab)
rocket: cc.Prefab = null;
@property(cc.Prefab)
health: cc.Prefab = null;
2025-10-29 13:27:41 +04:00
enemyLife: number = GameData.instance.enemyHealth;
2025-10-24 16:12:44 +04:00
playAnimation : Boolean = true;
setMovements() {
2025-10-29 13:27:41 +04:00
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));
2025-10-28 16:32:18 +04:00
this.node.runAction(cc.repeatForever(this.RTL ? cc.sequence(moveRight, moveLeft) : cc.sequence(moveLeft, moveRight)));
2025-10-24 16:12:44 +04:00
}
shootBullets() {
var bullet = cc.instantiate(this.yellowBullet);
bullet.setPosition(this.node.position.x, this.node.position.y - 60);
this.node.parent.addChild(bullet);
2025-11-03 15:27:17 +04:00
SoundManager.instance.playEffect('gun2');
2025-10-24 16:12:44 +04:00
}
// LIFE-CYCLE CALLBACKS:
onLoad () {
2025-10-28 10:41:02 +04:00
this.scheduleOnce(this.setMovements, 0.1);
2025-10-24 16:12:44 +04:00
2025-10-30 15:03:57 +04:00
this.schedule(this.shootBullets, GameData.instance.playerShootingInterval * Math.max(0.25, Math.random()), cc.macro.REPEAT_FOREVER, 0);
2025-10-31 16:20:20 +04:00
if (!this.scoreLabel) {
this.scoreLabel = cc.find("Canvas/ScoreBox/ScoreLabel").getComponent(cc.Label);
}
2025-10-24 16:12:44 +04:00
}
onCollisionEnter(otherCollider, selfCollider) {
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
this.enemyLife--;
if(this.enemyLife <= 0 && this.playAnimation) {
2025-11-03 15:27:17 +04:00
SoundManager.instance.playEffect('explosion');
2025-10-28 10:41:02 +04:00
this.node.getComponent(cc.Collider).enabled = false;
2025-10-24 16:12:44 +04:00
this.node.stopAllActions();
this.playAnimation = false;
this.node.getComponent(cc.Animation).play();
2025-11-06 10:59:34 +04:00
GameData.instance.setScore(GameData.instance.getScore() + GameData.instance.selectedLevel * 5);
this.scoreLabel.string = GameData.instance.getScore().toString();
2025-10-24 16:12:44 +04:00
}
}
if(otherCollider.name === 'player<PolygonCollider>') {
2025-11-03 15:27:17 +04:00
SoundManager.instance.stopMusic();
2025-10-24 16:12:44 +04:00
this.node.destroy();
2025-11-06 10:59:34 +04:00
cc.director.loadScene('Score');
2025-10-24 16:12:44 +04:00
}
}
removeExplosion() {
this.node.destroy();
2025-10-30 15:03:57 +04:00
if(this.specialItem === 'rocket') {
var rocket = cc.instantiate(this.rocket);
rocket.setPosition(this.node.position.x, this.node.position.y);
this.node.parent.addChild(rocket);
}
if(this.specialItem === 'health') {
var health = cc.instantiate(this.health);
health.setPosition(this.node.position.x, this.node.position.y);
this.node.parent.addChild(health);
}
2025-10-24 16:12:44 +04:00
}
start () {
}
update (dt) {
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
2025-11-03 15:27:17 +04:00
SoundManager.instance.stopMusic();
2025-10-24 16:12:44 +04:00
this.node.destroy();
2025-11-06 10:59:34 +04:00
cc.director.loadScene('Score');
2025-10-24 16:12:44 +04:00
}
}
}