import EventMgr from "../game/EventMgr";
import { GameEvent } from "../game/GameEvent";
import { GameMode } from "../game/GameMode";
import Main from "../game/Main";
import { SDKIpt } from "../game/SDKIpt";
import DateTime from "../help/DateTime";
import valBar from "./valBar";
const {ccclass, property} = cc._decorator;
@ccclass
export default class gameView extends cc.Component {
/**开始游戏 */
@property(cc.Button)
private btnPause: cc.Button = null;
@property(cc.Label)
private lbKillMonsterNum: cc.Label = null;
@property(cc.Label)
private lbBest: cc.Label = null;
@property(cc.Node)
private deductionText: cc.Node = null;
@property(cc.Node)
private endlessNode: cc.Node = null;
@property(cc.Label)
private lbRound: cc.Label = null;
@property(cc.RichText)
private endlessTime: cc.RichText = null;
@property(cc.Node)
private endlessWolfTitle: cc.Node = null;
//无尽模式这一波狼剩余数量
@property(cc.Label)
private endlessWolfCount: cc.Label = null;
// @property(cc.Label)
// private lbHp: cc.Label = null;
@property(valBar)
private hpBar: valBar = null;
@property(valBar)
private rageBar: valBar = null;
// @property(cc.Node)
// private spHpTmp: cc.Node = null;
// @property(cc.Node)
// private hpRoot: cc.Node = null;
// @property(cc.Node)
// private hpPanel: cc.Node = null;
// @property([cc.SpriteFrame])
// private sprites:cc.SpriteFrame[] = []
/**无尽模式计时器 */
private tiemrId_endless;
@property([cc.Node])
private spHeadStats: cc.Node[] = [];
@property(cc.Node)
private guildTitle: cc.Node = null;
// private initHp:number = 20;
// private hps:cc.Node[]=[];
onLoad () {
this.btnPause.node.on("click",this.onBtnPause,this);
this.guildTitle.active = Main.Ins.IsGuide;
EventMgr.Instance.add_event_listenner(GameEvent.GuideState,this,this.onGuideChange);
}
onGuideChange(GuideState: GameEvent, isOpen: boolean, onGuideChange: any) {
this.guildTitle.active = Main.Ins.IsGuide;
}
// protected start(): void {
// }
ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
if (!this.node.active) { return; }
// if (Ticker.isPause) { return; }
if (Main.Ins.MainView.active) { return; }
switch (ipt) {
case SDKIpt.leftFoot:
Main.Ins.PauseGameView();
break;
default:
break;
}
}
OnEndlessGameNewRound() {
// console.error("OnEndlessGameNewRound-------");
let me = this;
if (Main.Ins.Mode ==GameMode.endless) {
// console.log("第"+Main.Ins.Round+"波--"+DateTime.format_hh_mm_ss());
me.lbRound.string ="第"+Main.Ins.Round+"波";
let endlessTime = Main.Ins.EndlessTime;
me.endlessTime.string =me.timeShow(endlessTime-2);
me.endlessWolfCount.string = Main.Ins.EndlessEnemyCount.toString()+'只';
Ticker.unregistCount(this.tiemrId_endless);
me.tiemrId_endless = Ticker.registerCount(function (count) {
if (count>=2) {
me.endlessTime.string =me.timeShow(endlessTime - count);
// me.endlessTime.string = (endlessTime- count).toString();
}
}, endlessTime);
}
}
private timeShow(endlessTime) {
// console.error(endlessTime);
let richOutline = "{2}";// "x";
let richBold = "{0}";//"x"
let richColor = "{1}";//20
let str;
let count1;
let count2 = endlessTime - (Main.Ins.initEndlessTime - 2);
if (count2 > 0) {
count1 = endlessTime - count2;
// str = count1 + "+" + count2;
// str = richOutline.format("black", "2", count1);
// str = richBold.format(str);
// str = richColor.format("#FFE25D",str);
str = ""+count1+"秒+"+count2+"秒"
} else {
count1 = endlessTime;
str = endlessTime;
// str = richOutline.format("black", "2", count1);
// str = richBold.format(str);
// str = richColor.format("#FFE25D",str);
str = ""+count1+"秒"
}
// str = richOutline.format("black", "2", count1);
// str = richBold.format(str)
// 20+1
// if (endlessTime>Main.Ins.initEndlessTime-2) {
// let allCount = endlessTime;
// let count1 = Main.Ins.initEndlessTime-2;
// let count2 = allCount - count1;
// str = count1.toString();
// if (count2>0) {
// str +="+"+count2;
// }
// }else{
// str =endlessTime;
// }
return str;
}
OnHeroSuperSkill() {
this.rageBar.show(0);
}
OnUpdateBestKill() {
this.lbBest.string = `最高记录:${Main.Ins.BestScore}分`;
}
private onBtnPause() {
// Ticker.tooglePause();
// if ( cc.director.getScheduler().getTimeScale()==1) {
// //游戏开始
// cc.director.getScheduler().setTimeScale(0);
// } else {
// //游戏暂停
// cc.director.getScheduler().setTimeScale(1);
// }
// if (cc.director.isPaused()) {
// //游戏开始
// cc.director.resume();
// } else {
// //游戏暂停
// cc.director.pause();
// }
EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.leftFoot);
// Main.Ins.PauseGameView();
}
private tickId_pause_ipt;
public Show(){
let me= this;
this.node.active = true;
EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,me,me.ON_SDK_UI_IPT);
Ticker.unregisterDelay(this.tickId_pause_ipt);
this.tickId_pause_ipt = Ticker.registerDelay(function(){
EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,me,me.ON_SDK_UI_IPT);
// console.error("监听暂停");
},100);
EventMgr.Instance.add_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
EventMgr.Instance.add_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
EventMgr.Instance.add_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
EventMgr.Instance.add_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
// console.error("gameview监听");
EventMgr.Instance.add_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
EventMgr.Instance.add_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
}
on_update_endless_wolf_count(update_endless_wolf_count: GameEvent, count: number, on_update_endless_wolf_count: any) {
this.endlessWolfCount.string = count.toString()+'只';
}
private hasInit:boolean =false;
public Hide(){
this.node.active = false;
Ticker.unregisterDelay(this.tiemrId_endless);
EventMgr.Instance.remove_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
EventMgr.Instance.remove_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
EventMgr.Instance.remove_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
EventMgr.Instance.remove_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
EventMgr.Instance.remove_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
Ticker.unregisterDelay(this.tiemrId_endless);
}
public Init(hp:number){
// this.initHp = hp;
Ticker.unregisterDelay(this.tiemrId_endless);
this.hpBar.node.active = Main.Ins.Mode == GameMode.normal;
if (Main.Ins.Mode == GameMode.normal) {
this.rageBar.node.y = this.hpBar.node.y-44;
}else{
this.rageBar.node.y = this.hpBar.node.y;
}
// this.rageBar.node.active = Main.Ins.Mode == GameMode.normal;
this.endlessNode.active = Main.Ins.Mode == GameMode.endless;
this.lbRound.string = "";
if (Main.Ins.Mode == GameMode.normal) {
this.hpBar.show(hp);
}
this.rageBar.show(0);
this.OnUpdateBestKill();
this.lbKillMonsterNum.string = "0";
this.showHeroHead(hp);
}
public OnMonsterDie(killNum:number){
this.lbKillMonsterNum.string = killNum.toString();
}
public OnHeroHpChange(msg: string, nowHp: number) {
if (Main.Ins.Mode == GameMode.normal) {
this.hpBar.show(nowHp);
this.showHeroHead(nowHp);
}
}
public OnHeroRageChange(msg: string, now: number) {
this.rageBar.show(now);
}
public showDeductionText(){
const lbKillWidth = this.lbKillMonsterNum.node.width
const lbKillRight = this.lbKillMonsterNum.node.x
this.deductionText.x = Math.ceil(Math.abs(lbKillWidth)+ Math.abs(lbKillRight)+6)*-1
cc.tween(this.deductionText)
.stop()
.to(.3, {opacity: 200})
.delay(.2)
.to(.3, {opacity: 0})
.start();
}
private showHeroHead(nowHp: number) {
if (Main.Ins.Mode == GameMode.normal) {
this.spHeadStats[0].active = nowHp > 8;
this.spHeadStats[1].active = nowHp > 6;
this.spHeadStats[2].active = nowHp > 4;
this.spHeadStats[3].active = nowHp > 2;
this.spHeadStats[4].active = nowHp > 1;
this.spHeadStats[5].active = true;
}else{
this.spHeadStats[0].active = true;
}
}
protected onDestroy(): void {
EventMgr.Instance.remove_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
EventMgr.Instance.remove_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
EventMgr.Instance.remove_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
EventMgr.Instance.remove_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
EventMgr.Instance.remove_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
Ticker.unregisterDelay(this.tiemrId_endless);
}
}