| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import Boy from "./Boy";
- import Bot from "./Bot";
- import MatchPanel from "../Panel/MatchPanel";
- import SettlePanel from "../Panel/SettlePanel";
- import Camera from "./Camera";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class Game extends cc.Component {
- @property({
- type: cc.Float,
- range: [0, 1, 0.1],
- slide: true,
- })
- botStrength = 1;
- @property({
- type: cc.Label,
- })
- text_countDown:cc.Label = null;
-
- boys:Boy[] = new Array();
- mainCamera:cc.Node;
- isGameOver:boolean = false;
- onLoad(){
- window.gm = this;
- cc.director.getCollisionManager().enabled = true;
- cc.director.getPhysicsManager().enabled = true;
- }
- /**一局游戏时间,单位:秒s */
- gameTotalTime:number = 120;
- /**游戏倒计时,单位:秒s */
- countDown :number = 120;
- start(){
- console.log("开始匹配");
- // this.node.addChild(cc.instantiate(window.rs.pf_matchPanel));
- // this.node.getChildByName("MatchPanel").on(MatchPanel.EventType_MatchFinish,()=>{
- console.log("匹配成功");
- //游戏开始开启倒计时
- this.countDown = this.gameTotalTime;
- this.schedule(this.timerGameTimeCountDown, 1);
- for(let i=0;i<4;i++){
- let box = window.gc.boxs[i];
- let boyPosition = box.position.add(cc.v2(0,box.height/2+10));
- if(i==0){
- this.addBoy(i,boyPosition,false);
- }else{
- this.addBoy(i,boyPosition,true);
- }
- }
- // });
- this.mainCamera = this.node.getChildByName('Main Camera');
- this.mainCamera.addComponent(Camera);
- }
- /**游戏倒计时 */
- timerGameTimeCountDown(){
- if(this.isGameOver){
- console.log("取消计时器1111");
- this.unschedule(this.timerGameTimeCountDown);
- // return;
- }
- if(this.countDown>0){
- this.countDown--;
- }else{
- console.log("取消计时器2222");
- //游戏倒计时为0,强制结束游戏
- this.unschedule(this.timerGameTimeCountDown);
- this.gameOver();
- }
- // console.log("游戏倒计时:",this.countDown);
- this.text_countDown.string = this.countDown+"'";
- }
- addBoy(index:number,position:cc.Vec2,isBot:boolean){
- let node = cc.instantiate(window.rs.pf_boy);
- node.setPosition(position);
- let boy = node.addComponent(Boy);
- boy.index = index;
- window.gc.boyGroup.addChild(node);
- if(isBot){
- console.log("添加机器人");
- node.addComponent(Bot);
- }else{
- console.log("添加boy");
- window.boy = boy;
- }
- this.boys.push(boy);
- }
- /**游戏结束 */
- gameOver(){
- this.isGameOver = true;
- this.scheduleOnce(()=>{
- console.log("胜利的人",this.boys[0].index);
- this.showSettlePanel(MatchPanel.palyerInfos[this.boys[0].index]);
- },1);
- }
- rankingTag:number = 4;
- removeBoy(boy:Boy){
- boy.rankingNum = this.rankingTag;
- this.rankingTag-=1;
- // console.log("removeBoy------------",boy.index);
- let newBoys = new Array();
- this.boys.forEach((elem)=>{
- if(elem!=boy){
- newBoys.push(elem);
- }
- });
- this.boys = newBoys;
- boy.node.destroy();
- if(this.boys.length==1){
- this.gameOver();
- }
- }
- showSettlePanel(playerInfo:PalyerInfo){
- let node = cc.instantiate(window.rs.pf_settlePanel);
- node.setPosition(this.mainCamera.getPosition());
- let settlePanel = node.getComponent(SettlePanel);
- settlePanel.setPlayerInfo(playerInfo);
- this.node.addChild(node);
- node.on(SettlePanel.EventType_Restart,()=>{
- this.node.runAction(
- cc.sequence(
- cc.fadeOut(1),
- cc.callFunc(()=>{
- // cc.director.loadScene("Game");
- cc.director.loadScene("Pairing");
- })
- )
- );
- });
- cc.audioEngine.playEffect(window.rs.ac_win,false);
- }
- }
|