| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- import Monster from "./role/Monster";
- import Player from "./role/Player";
- import Role from "./role/Role";
- import Timer from "./Timer";
- import TopBar from "./ui/TopBar";
- import Login from "./ui/Login";
- import Account from "./ui/Account";
- import Coach from "./role/Coach";
- import Tool from "./Tool";
- const {ccclass, property} = cc._decorator;
- /**游戏管理类 */
- @ccclass
- export default class GameMgr extends cc.Component {
- @property({
- type:cc.Prefab
- })
- private ui_login:cc.Prefab = null;
- @property({
- type:cc.AudioClip
- })
- public ad_bong:cc.AudioClip = null;
- @property({
- type:cc.AudioClip
- })
- public ad_btn:cc.AudioClip = null;
- @property({
- type:cc.AudioClip
- })
- public ad_bgm:cc.AudioClip = null;
- public gameViewSize:cc.Size;
- public camera:cc.Node;
- public background:cc.Node;
- public teachMode:TeachMode;
- private coach:Coach;
- private player:Player;
- private monster:Monster;
- private topBar:TopBar;
- private timer:Timer;
- private score:number = 0;
- public onLoad():void{
- window.gameMgr = this;
- this.camera = this.node.getChildByName('Main Camera');
- this.addLoginUi();
- }
- private addLoginUi():void{
- let ui = cc.instantiate(this.ui_login);
- ui.addComponent(Login);
- this.node.addChild(ui);
- //把登录界面作为背景节点
- this.background = ui;
- }
- public startGame():void{
- this.createMonster();
- this.createPlayer();
- this.player.setTarget(this.monster);
- this.monster.setTarget(this.player);
- this.createPlayerButton();
- this.createTopBarUi();
- this.setAccountScoreListener();
- this.setGameOverListener();
- this.timer.showStartCountDownAnimation(this.background,this.startMatch.bind(this));
- this.score = 0;
- }
- /**教学模式 */
- public startTeachMode():void{
- this.createCoach();
- this.createPlayer();
- this.coach.setTarget(this.player);
- this.player.setTarget(this.coach);
- this.createPlayerButton();
- this.createTopBarUi(true);
- this.player.pausing = false;
- this.coach.pausing = false;
- this.teachMode = new TeachMode(this.gameViewSize,this.background,this.coach,this.player);
- this.teachMode.start();
- }
- private createCoach():void{
- this.coach = new cc.Node().addComponent(Coach);
- this.coach.node.y = -this.gameViewSize.height*0.43;
- this.coach.node.scaleX *= this.gameViewSize.width/cc.view.getDesignResolutionSize().width;//适配游戏可视域的宽度
- this.background.addChild(this.coach.node);
- }
- private createMonster():void{
- this.monster = new cc.Node().addComponent(Monster);
- this.monster.node.y = -this.gameViewSize.height*0.43;
- this.monster.node.scaleX *= this.gameViewSize.width/cc.view.getDesignResolutionSize().width;//适配游戏可视域的宽度
- this.background.addChild(this.monster.node);
- }
- private createPlayer():void{
- this.player = new cc.Node().addComponent(Player);
- this.player.node.y = -this.gameViewSize.height/2;
- this.player.node.scaleX *= this.gameViewSize.width/cc.view.getDesignResolutionSize().width;//适配游戏可视域的宽度
- this.background.addChild(this.player.node);
- }
- /**摇杆 */
- private btn_rocker:cc.Node;
- private btn_rocker_offset:cc.Vec2;
- /**防守按钮 */
- private btn_defense:cc.Node;
- private createPlayerButton():void{
- this.btn_rocker = Tool.createButtonNode({
- width:this.gameViewSize.width,
- height:this.gameViewSize.height,
- });
- this.background.addChild(this.btn_rocker);
- this.btn_defense = Tool.createButtonNode({
- y:-550,
- backgroundImage:'texture/circle',
- transition:cc.Button.Transition.NONE,
- color:cc.Color.GRAY,
- opacity:155
- });
- this.background.addChild(this.btn_defense);
- //监听事件
- this.player.node.on(Role.EventType_NoAction,()=>{
- this.btn_rocker_offset = cc.v2(0,0);
- },this);
- this.btn_rocker.on(cc.Node.EventType.TOUCH_START,(event:cc.Event.EventTouch)=>{
- this.btn_rocker_offset = cc.v2(0,0);
- });
- this.btn_rocker.on(cc.Node.EventType.TOUCH_MOVE,(event:cc.Event.EventTouch)=>{
- this.btn_rocker_offset = this.btn_rocker_offset.add(event.getDelta());
- if(this.btn_rocker_offset.mag()>100){
- if(this.btn_rocker_offset.x<0&&this.btn_rocker_offset.y>=0){
- this.player.rightJay();
- return;
- }
- if(this.btn_rocker_offset.x>=0&&this.btn_rocker_offset.y>=0){
- this.player.leftJay();
- return;
- }
- if(this.btn_rocker_offset.x<0&&this.btn_rocker_offset.y<0){
- this.player.leftDodge();
- return;
- }
- if(this.btn_rocker_offset.x>0&&this.btn_rocker_offset.y<0){
- this.player.rightDodge();
- return;
- }
- }
- },this);
- this.btn_rocker.on(cc.Node.EventType.TOUCH_END,(event:cc.Event.EventTouch)=>{
- this.btn_rocker_offset = cc.v2(0,0);
- },this);
- this.btn_rocker.on(cc.Node.EventType.TOUCH_CANCEL,(event:cc.Event.EventTouch)=>{
- this.btn_rocker_offset = cc.v2(0,0);
- },this);
- this.btn_defense.on(cc.Node.EventType.TOUCH_START,()=>{
- this.player.defense();
- this.btn_defense.opacity = 90;
- },this);
- this.btn_defense.on(cc.Node.EventType.TOUCH_END,()=>{
- this.btn_defense.opacity = 155;
- },this);
- this.btn_defense.on(cc.Node.EventType.TOUCH_CANCEL,()=>{
- this.btn_defense.opacity = 155;
- },this);
- }
- /**
- * 创建战斗顶部栏
- * @param isCocah 对手是否为教练?
- */
- private createTopBarUi(isCoach?:boolean):void{
- let ui = cc.instantiate(cc.loader.getRes('prefab/ui_topBar',cc.Prefab)) as cc.Node;
- ui.scaleX *= this.gameViewSize.width/cc.view.getDesignResolutionSize().width;//适配游戏可视域的宽度
- this.background.addChild(ui);
- this.topBar = ui.addComponent(TopBar);
- this.topBar.listenRole(this.player,isCoach?this.coach:this.monster);
- this.timer = ui.addComponent(Timer);
- this.timer.init(60,this.topBar.timerLabel);
- }
- /**开始比赛 */
- private startMatch():void{
- this.player.pausing = false;
- this.monster.pausing = false;
- this.timer.startCountTime();
- }
- /**暂停比赛 */
- private pauseMatch():void{
- this.player.pausing = true;
- this.monster.pausing = true;
- this.timer.stopCountTime();
- }
- /**
- * 设置计分监听事件
- * 积分规则:被击中扣100,击中加100分,躲避成功加100分,防守成功加100分,分数下限为0
- */
- private setAccountScoreListener():void{
- this.player.node.on(Role.EventType_BeHit,()=>{
- let nextScore = this.score - 100;
- nextScore<0 ? this.score = 0 : this.score = nextScore;
- },this);
- this.player.node.on(Role.EventType_DodgeSuccess,()=>{
- this.score += 100;
- },this);
- this.player.node.on(Role.EventType_DefenseSuccess,()=>{
- this.score += 100;
- },this);
- this.monster.node.on(Role.EventType_BeHit,()=>{
- this.score += 100;
- },this);
- }
- /**设置游戏结束监听事件 */
- private setGameOverListener():void{
- //玩家死亡
- this.player.node.on(Role.EventType_Die,()=>{
- this.pauseMatch();
- this.player.node.runAction(cc.fadeOut(1));
- this.addAccountUi(false);
- },this);
- //怪物死亡
- this.monster.node.on(Role.EventType_Die,()=>{
- this.pauseMatch();
- this.monster.node.runAction(cc.fadeOut(1));
- this.addAccountUi(true);
- },this);
- //时间结束
- this.timer.node.on(Timer.EventType_ZERO,()=>{
- this.pauseMatch();
- if(this.player.property_hp>=this.monster.property_hp){
- this.addAccountUi(true);
- }else{
- this.addAccountUi(false);
- }
- },this);
- }
- /**展示结算面板 */
- private addAccountUi(win:boolean):void{
- //删除防御按钮
- this.btn_defense.destroy();
-
- if(win){
- //胜利时间加分:剩余时间*100分
- this.score += Math.floor(this.timer.time*100);
- }
- this.score += Math.floor(this.player.property_hp/this.player.property_maxHp*1000);
- let ui = cc.instantiate(cc.loader.getRes('prefab/ui_account',cc.Prefab));
- ui.scaleX *= this.gameViewSize.width/cc.view.getDesignResolutionSize().width;//适配游戏可视域的宽度
- let account:Account = ui.addComponent(Account);
- this.background.addChild(ui);
- account.showResult(win);
- account.showScore(this.score);
- }
- }
- /**教学模式类 */
- class TeachMode {
- private gameViewSize:cc.Size;
- private background:cc.Node;
- private coach:Coach;
- private player:Player;
- private fingerInitPos:cc.Vec2 = cc.v2(0,-250);
- private finger:cc.Node;
- private label:cc.Label;
- private circleMask:cc.Node;
- private blackScreen:cc.Node;
- constructor(gameViewSize:cc.Size,background:cc.Node,coach:Coach,player:Player){
- this.gameViewSize = gameViewSize;
- this.background = background;
- this.coach = coach;
- this.player = player;
- //创建圆形遮罩
- this.circleMask = cc.instantiate(cc.loader.getRes('prefab/circleMask',cc.Prefab));
- this.circleMask.zIndex = 1;
- this.circleMask.active = false;
- this.background.addChild(this.circleMask);
- //创建手指节点
- this.finger = new cc.Node();
- this.finger.zIndex = 2;
- this.finger.setAnchorPoint(0,1);
- this.finger.setPosition(this.fingerInitPos);
- this.finger.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/finger',cc.SpriteFrame);
- this.finger.active = false;
- this.background.addChild(this.finger);
- //创建文字标签
- this.label = new cc.Node().addComponent(cc.Label);
- this.label.node.zIndex = 3;
- this.label.node.setPosition(0,-400);
- this.label.lineHeight = 50;
- this.label.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
- this.label.node.active = false;
- this.background.addChild(this.label.node);
- //黑色屏幕
- this.blackScreen = Tool.createButtonNode({
- width:this.gameViewSize.width,
- height:this.gameViewSize.height,
- backgroundImage:'texture/white',
- transition:cc.Button.Transition.NONE,
- color:cc.Color.BLACK,
- opacity:0
- });
- this.blackScreen.zIndex = 4;
- this.blackScreen.active = false;
- this.background.addChild(this.blackScreen);
- }
- /**
- * 运动手指
- * @param isClick 是则为点击模式,否则为运动模式,运动模式运动偏移量
- * @param offset 运动偏移量
- */
- private runFinger(isClick:boolean,offset?:cc.Vec2):void{
- this.finger.active = true;
- this.finger.stopAllActions();
- if(isClick){
- this.finger.setPosition(-10,-530);
- this.finger.runAction(cc.repeatForever(cc.sequence(
- cc.fadeIn(0.3),
- cc.scaleTo(0.6,0.8),
- cc.scaleTo(0.6,1),
- cc.fadeOut(0.3),
- )));
- }else{
- this.finger.setPosition(this.fingerInitPos);
- this.finger.runAction(cc.repeatForever(cc.sequence(cc.fadeIn(0.3),
- cc.moveBy(0.6,cc.v2(offset)),
- cc.fadeOut(0.3),
- cc.callFunc(()=>{
- this.finger.setPosition(this.fingerInitPos);
- },this))));
- }
- }
-
- /**
- * 设置标签
- * @param text 文本
- * @param color 颜色
- */
- public setLabel(text:string,color?:cc.Color){
- this.label.string = text;
- this.label.node.color = color?color:cc.Color.WHITE;
- this.label.node.active = true;
- }
- /**开始教学 */
- public start():void{
- //创建标题
- let node = new cc.Node();
- node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_teach1',cc.SpriteFrame);
- node.setScale(1.5);
- node.setPosition(0,250);
- this.background.addChild(node);
- node.runAction(cc.sequence(cc.scaleBy(2,0.8).easing(cc.easeBackInOut()),cc.callFunc(()=>{
- node.destroy();
- this.teachRightJay();
- },this)));
- }
- /**左拳教学 */
- private teachRightJay():void{
- this.runFinger(false,cc.v2(-200,200))
- this.setLabel('向左上方滑动\n打出右勾拳');
- this.player.node.on(Role.EventType_DoAttack,this.rightJayFinish,this);
- }
- /**左拳完成 */
- private rightJayFinish():void{
- if(this.player.direction==-1){
- this.player.node.off(Role.EventType_DoAttack,this.rightJayFinish,this);
- this.finger.active = false;
- this.label.node.active = false;
- setTimeout(this.teachLeftJay.bind(this),500);
- }
- }
- /**右拳教学 */
- private teachLeftJay():void{
- this.runFinger(false,cc.v2(200,200));
- this.setLabel('向右上方滑动\n打出左勾拳');
- this.player.node.on(Role.EventType_DoAttack,this.leftJayFinish,this);
- }
- /**右拳完成 */
- private leftJayFinish():void{
- if(this.player.direction==1){
- this.player.node.off(Role.EventType_DoAttack,this.leftJayFinish,this);
- this.finger.active = false;
- this.label.node.active = false;
- setTimeout(this.createLeftAttackMark.bind(this),500);
- }
- }
- /**对手发出左攻击标志 */
- private createLeftAttackMark():void{
- this.coach.createMark(1,false,()=>{
- this.circleMask.setPosition(this.coach.mark.position);
- this.circleMask.active = true;
- this.teachRightDodge();
- });
- }
- /**右躲避教学 */
- private teachRightDodge():void{
- this.runFinger(false,cc.v2(200,-200));
- this.setLabel('向右下方滑动\n发起右躲避');
- this.player.node.on(Role.EventType_DoDodge,this.rightDodgeFinish,this);
- }
- /**右躲避完成 */
- public rightDodgeFinish():void{
- if(this.player.direction==1){
- this.player.node.off(Role.EventType_DoDodge,this.rightDodgeFinish,this);
- this.coach.mark.destroy();
- this.circleMask.active = false;
- this.finger.active = false;
- this.coach.leftJay();
- this.setLabel('记住:\n对方的攻击信号\n是预判的关键',cc.Color.YELLOW);
- setTimeout(this.createRightAttackMark.bind(this),3000);
- }
- }
- /**对手发出右攻击标志 */
- public createRightAttackMark():void{
- this.coach.createMark(-1,false,()=>{
- this.circleMask.setPosition(this.coach.mark.position);
- this.circleMask.active = true;
- this.teachLeftDodge();
- });
- }
- /**左躲避教学 */
- public teachLeftDodge():void{
- this.runFinger(false,cc.v2(-200,-200));
- this.setLabel('向左下方滑动\n发起左躲避');
- this.player.node.on(Role.EventType_DoDodge,this.leftDodgeFinish,this);
- }
- /**左躲避完成 */
- public leftDodgeFinish():void{
- if(this.player.direction==-1){
- this.player.node.off(Role.EventType_DoDodge,this.leftDodgeFinish,this);
- this.coach.mark.destroy();
- this.circleMask.active = false;
- this.finger.active = false;
- this.coach.rightJay();
- this.setLabel('记住:\n躲避成功可恢复大量的耐力\n是持续战斗的关键',cc.Color.YELLOW);
- setTimeout(this.teachDefense.bind(this),3000);
- }
- }
- /**防御教学 */
- public teachDefense():void{
- this.runFinger(true);
- this.setLabel('点击此处按钮\n发起防御');
- this.player.node.on(Role.EventType_DoDefense,this.defenseFinish,this);
- }
- /**防御完成 */
- public defenseFinish():void{
- this.player.node.off(Role.EventType_DoDefense,this.defenseFinish,this);
- this.finger.destroy();
- this.coach.rightJay();
- this.setLabel('记住:\n防御成功可以恢复少量血量\n是续航的关键',cc.Color.YELLOW);
- setTimeout(this.end.bind(this),3000);
- }
- /**结束教学 */
- public end():void{
- this.label.node.destroy();
- //创建标题
- let node = new cc.Node();
- node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_teach2',cc.SpriteFrame);
- node.setScale(1.5);
- node.setPosition(0,250);
- this.background.addChild(node);
- node.runAction(cc.sequence(cc.scaleBy(2,0.8).easing(cc.easeBackInOut()),cc.callFunc(()=>{
- node.destroy();
- this.blackScreen.active = true;
- //缓动
- let tween = new cc.Tween();
- tween.target(this.background);
- tween.to(1,{opacity:255},null);
- tween.then(cc.callFunc(()=>{
- //完成新手教程,缓存数据中记录为老玩家
- cc.sys.localStorage.setItem('isOldPlayer','true');
- window.gameMgr.teachMode = null;//回收教学模式
- //正式开始游戏
- this.background.removeAllChildren(true);
- window.gameMgr.startGame();
- },this));
- tween.start();
- },this)));
- }
- }
|