(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/scripts/Game.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, 'd1e59/joM1CbaSfFC/sj8Uv', 'Game', __filename);
// scripts/Game.ts
// 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
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var NewClass = /** @class */ (function (_super) {
__extends(NewClass, _super);
function NewClass() {
var _this = _super !== null && _super.apply(this, arguments) || this;
//2个游戏背景
_this.bg0 = null;
_this.bg1 = null;
//3只小鸟来实现飞行效果
_this.birdNode = null;
_this.bird0 = null;
_this.bird1 = null;
_this.bird2 = null;
//3组水管
_this.pipeNode0 = null;
_this.pipeNode1 = null;
_this.pipeNode2 = null;
//成绩,使用字体实现
_this.lbScore = null;
//game over
_this.nGameOver = null;
//开始游戏按钮
_this.btnStart = null;
_this.time = 0; //两次切换小鸟的间隔时间
_this.speed = 0; //小鸟下坠速度
_this.score = 0; //成绩分数
_this.isStart = false; //是否开始
return _this;
}
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
NewClass.prototype.start = function () {
this.initPipe();
};
NewClass.prototype.update = function (dt) {
var timeTemp = this.time + dt;
this.time = timeTemp;
//如果离上次切换时间达到0.5S
if (this.time > 0.8) {
//切换显示状态
if (this.bird0.node.active) {
this.bird0.node.active = false;
this.bird1.node.active = true;
}
else if (this.bird1.node.active) {
this.bird1.node.active = false;
this.bird2.node.active = true;
}
if (this.bird2.node.active) {
this.bird2.node.active = false;
this.bird0.node.active = true;
}
this.time = 0;
}
//开始检测
if (this.isStart == false) {
return;
}
//小鸟向下坠落
// let birdY = this.birdNode.y;
// this.birdNode.y = birdY - 2;
this.speed = this.speed - 0.05;
this.birdNode.y = this.birdNode.y + this.speed;
//小鸟旋转
this.birdNode.rotation = -this.speed * 8;
//地图背景移动
this.moveMap(this.bg0);
this.moveMap(this.bg1);
//管子移动
this.movePipe(this.pipeNode0);
this.movePipe(this.pipeNode1);
this.movePipe(this.pipeNode2);
//碰撞检测
this.checkCollision(this.birdNode, this.pipeNode0);
this.checkCollision(this.birdNode, this.pipeNode1);
this.checkCollision(this.birdNode, this.pipeNode2);
};
NewClass.prototype.moveMap = function (bg) {
bg.x = bg.x - 1;
if (bg.x < -288) {
bg.x = bg.x + 288 * 2;
}
};
NewClass.prototype.movePipe = function (pipe) {
pipe.x = pipe.x - 3;
if (pipe.x < (-144 - 26)) {
pipe.x = pipe.x + 288 + 52;
//+-60的随机
pipe.y = 60 - (Math.random() * 60);
//成绩+1
this.score = this.score + 1;
this.lbScore.string = this.score.toString();
}
};
//碰撞检测
NewClass.prototype.checkCollision = function (bird, pipe) {
//小鸟右边X 小于 管子最左边
if (bird.x + 17 < pipe.x - 26) {
return;
}
if (bird.x - 17 > pipe.x + 26) {
return;
}
if ((bird.y + 12 < pipe.y + 50) && (bird.y - 12 > pipe.y - 50)) {
return;
}
console.log("撞到啦~~~~~~~");
this.gameOver();
};
//飞行按钮响应
NewClass.prototype.onBtnClickFly = function () {
console.log("fly~~~~~~~");
this.speed = 2.5;
};
//开始游戏按钮响应
NewClass.prototype.onBtnClickStart = function () {
console.log("game start~~~~~~~");
this.isStart = true;
this.initPipe();
this.resetGame();
};
//游戏结束
NewClass.prototype.gameOver = function () {
this.isStart = false;
this.nGameOver.active = true;
this.btnStart.node.active = true;
};
//重置游戏
NewClass.prototype.resetGame = function () {
this.score = 0;
this.birdNode.x = 0;
this.birdNode.y = 0;
this.speed = 0;
this.time = 0;
this.nGameOver.active = false;
this.btnStart.node.active = false;
};
//初始化管子
NewClass.prototype.initPipe = function () {
var pipeStartX = 200;
var spaceX = (288 + 52) / 3;
this.pipeNode0.x = spaceX * 0 + pipeStartX;
this.pipeNode1.x = spaceX * 1 + pipeStartX;
this.pipeNode2.x = spaceX * 2 + pipeStartX;
};
__decorate([
property(cc.Node)
], NewClass.prototype, "bg0", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "bg1", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "birdNode", void 0);
__decorate([
property(cc.Sprite)
], NewClass.prototype, "bird0", void 0);
__decorate([
property(cc.Sprite)
], NewClass.prototype, "bird1", void 0);
__decorate([
property(cc.Sprite)
], NewClass.prototype, "bird2", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "pipeNode0", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "pipeNode1", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "pipeNode2", void 0);
__decorate([
property(cc.Label)
], NewClass.prototype, "lbScore", void 0);
__decorate([
property(cc.Node)
], NewClass.prototype, "nGameOver", void 0);
__decorate([
property(cc.Button)
], NewClass.prototype, "btnStart", void 0);
NewClass = __decorate([
ccclass
], NewClass);
return NewClass;
}(cc.Component));
exports.default = NewClass;
cc._RF.pop();
}
if (CC_EDITOR) {
__define(__module.exports, __require, __module);
}
else {
cc.registerModuleFunc(__filename, function () {
__define(__module.exports, __require, __module);
});
}
})();
//# sourceMappingURL=Game.js.map