Game.js 9.47 KB
//游戏探索界面
var Common = require('Common');
var Network = require('Network');
var TVFocus = require('TVFocus');
var FocusInfo = require('FocusInfo');
var TVCanvas = require('TVCanvas');
var Utils = require('Utils');
var BusinessParameter = require('BusinessParameter');
var ListView = require('ListView');
var ListCell = require('ListCell');
var GameLobbyCategoryJson = require('GameLobbyCategoryJson');

cc.Class({
    extends: TVCanvas,

    properties: {
        activitySprite:cc.Sprite,
        roleSprite:cc.Sprite,
        taskSprite:cc.Sprite,
        achievementSprite:cc.Sprite,
        shopSprite:cc.Sprite,
        closeTaskSprite:cc.Sprite,
        roleAnim:dragonBones.ArmatureDisplay,
        PFB_GAME_TASKITEM: {
            default: null,
            type: cc.Prefab
        },
    },

    onLoad:function(){
        this._super();

        this._iSceneStatus=1;   //焦点层级
        this.initFocus();

        //右分类框框
        this._nodeCategoryList = cc.find("task/task_panel/CategoryList", this.node);
        let lvCategoryList = this._nodeCategoryList.addComponent(ListView);
        lvCategoryList.init(this, 3, 1, 3, 1, 'pfbGameTaskItemCell',
            0, 100,   //起始位置
            0, 0, 0, 0,  //margin
            1, 1,
            function () {
                this.getCategoryList();
            }
        );
    },

    getCategoryList: function () {
        var self = this;
        let lvCategoryList = this._nodeCategoryList.getComponent(ListView);
        let oCategoryRequestParameters = {
            "view": "json",
            "collectionId": this._iDefaultCollectionId,
            // "appId": this._aTypeAppId[this._oSceneContext._iCurrentTypeIndex],
            "sortByCollection": 1,
            "sortField": "update_time",
            "sortDirection": "desc"
        };
        let iStart1 = 0;
        if (null != this._oSceneContext.categoryRecordIndexOfFirstCell) {
            iStart1 = this._oSceneContext.categoryRecordIndexOfFirstCell;
        }

        lvCategoryList.setDataSource(
            "GET",
            Common.TOPDRAW_API_SERVER_EDU + "User/ListMediaByCollection",
            oCategoryRequestParameters,
            iStart1, (lvCategoryList.getShowCellRows() + lvCategoryList.getAlphaCellRows()) * lvCategoryList.getCellCountEachRow(),
            "", ""
        );

        lvCategoryList.loadData(
            function (strResponse) {
                // var oJSONResult = JSON.parse(strResponse);
                // if (oJSONResult.resultSet.length <= 0) {    //没有数据就显示占位图
                //     cc.find('BlankIcon', this.node).opacity = 255
                // } else {
                //     cc.find('BlankIcon', this.node).opacity = 0
                // }
                strResponse = GameLobbyCategoryJson.gameCategory;     //暂时采用假数据
                lvCategoryList.renderInitData(JSON.stringify(strResponse)); //将假数据转成字符串在给过去
                this._bIsCategoryListDataInit = true;
                this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
                // this.checkDataReadyAndInitFocus();

                // lvCategoryList.renderInitData(strResponse, function () {
                //     this._bIsCategoryListDataInit = true;
                //     this._oSceneContext.categoryRecordIndexOfFirstCell = lvCategoryList.getRecordIndexOfFirstCellInPage();
                //     this.checkDataReadyAndInitFocus();
                //     // this.changeEditorStatus();
                // });
            },
            null,
            this
        );

        var self = this;
        this._iShowCellRows = 2;
        this._iCellCountEachRow = 3;
    },

    initFocus: function () {
        this._aFocusTargets[1] = [];
        var fiActivitySprite = this.activitySprite.addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_activity'] = this.activitySprite;
        fiActivitySprite.init(
            null, true
        );

        var fiRoleSprite = this.roleSprite.addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_role'] = this.roleSprite;
        fiRoleSprite.init(
            null, true
        );

        var fiTaskSprite = this.taskSprite.addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_task'] = this.taskSprite;
        fiTaskSprite.init(
            null, true
        );

        var fiAchievementSprite = this.achievementSprite.addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_achievement'] = this.achievementSprite;
        fiAchievementSprite.init(
            null, true
        );

        var fiShopSprite = this.shopSprite.addComponent(FocusInfo);//
        this._aFocusTargets[1]['to_shop'] = this.shopSprite;
        fiShopSprite.init(
            null, true
        );

        var fiCloseTaskSprite = this.closeTaskSprite.addComponent(FocusInfo);//
        this._aFocusTargets[0]['to_close_task'] = this.closeTaskSprite;
        fiCloseTaskSprite.init(
            null, true
        );
       

        var nodeFocus = new cc.Node('nodeFocus');
        this.node.addChild(nodeFocus, 10);
        this._cFocus = this.node.getChildByName('nodeFocus').addComponent(TVFocus);
        this._cFocus.init('focusContainer', this,
            this.activitySprite.getComponent(FocusInfo),
            Common.SCREEN_WIDTH, Common.SCREEN_HEIGHT, 6, 4, 1.0);
        // this._cFocus.hide();        //所有焦点都是隐藏的
    },

    keyDownDirection: function (Direct) {
        var fiFocusTarget = null;
        var fiCurrentFocus = this._fiCurrentFocus;
        var oScrollParameter = null;

        fiFocusTarget = this._cFocus.findTarget(fiCurrentFocus, this._aFocusTargets, this._iSceneStatus, Direct);
        fiFocusTarget = this.checkFocusTarget(fiFocusTarget);
        if (!fiFocusTarget) { return; }
        this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Direct, null, oScrollParameter);
    },

    checkFocusTarget: function (fiFocusTarget) {
        return fiFocusTarget;
    },

    onKeyDown: function (event) {
        this._super(event);     

        switch (event.keyCode) {
            case cc.macro.KEY.up:
            case Common.ANDROID_KEY.up:
                this.keyDownDirection(Common.MOVE_DIRECTION_UP);
                break;
            case cc.macro.KEY.right:
            case Common.ANDROID_KEY.right:
                this.keyDownDirection(Common.MOVE_DIRECTION_RIGHT);
                break;
            case cc.macro.KEY.down:
            case Common.ANDROID_KEY.down:
                this.keyDownDirection(Common.MOVE_DIRECTION_DOWN);            
                break;
            case cc.macro.KEY.left:
            case Common.ANDROID_KEY.left:
                this.keyDownDirection(Common.MOVE_DIRECTION_LEFT);
                break;
            case cc.macro.KEY.enter:
            case cc.macro.KEY.space:
            case Common.ANDROID_KEY.enter:
                if(0 == this._fiCurrentFocus.node.getName().indexOf('spriteTask')){ //任务
                    cc.find("task",this.node).active=true;
                    this._iSceneStatus=0;
                    var fiFocusTarget = this._cFocus.findTarget(this._fiCurrentFocus, this._aFocusTargets, this._iSceneStatus, Common.MOVE_DIRECTION_LEFT);
                    this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_LEFT, null, null);

                    cc.find("task",this.node).zIndex=10;
                    this.activitySprite.node.zIndex=0;  //这几个总是另类凸显出来
                    this.roleSprite.node.zIndex=0;
                    this.taskSprite.node.zIndex=0;
                    this.achievementSprite.node.zIndex=0;
                    this.shopSprite.node.zIndex=0;                   
                }else if(0 == this._fiCurrentFocus.node.getName().indexOf('close')){
                    cc.find("task",this.node).active=false;
                    this._iSceneStatus=1;
                    var fiFocusTarget = this.taskSprite.getComponent(FocusInfo);
                    this._cFocus.flyFocus(this._fiCurrentFocus, fiFocusTarget, Common.MOVE_DIRECTION_UP, null, null);
                }
                this.doCurrentFocusTVLinkAction(Common.TV_LINK_ACTION_CLICK);
                break;
            case cc.macro.KEY.backspace:
            case Common.ANDROID_KEY.back:
                this.backAScene();
                break;
        }
    },

    
    onBeforeFocusChange: function (event) {
        this._super(event);
        var self = this;
        let fiFrom = event.detail.from;
        if (0 == fiFrom.node.name.indexOf('sprite')) {
            fiFrom.node.getChildByName("active").active=false;
        }
    },

    onAfterFocusChange: function (event) {
        this._super(event);
        var self = this;
        let fiTo = event.detail.to;
        if (0 == fiTo.node.name.indexOf('sprite')) {          
            fiTo.node.getChildByName("active").active=true;
        }

    },

    doCurrentFocusTVLinkAction: function (strAction) {
        let strTVLink = this._fiCurrentFocus.getTVLink();
        var joTVLink = null;
        try {
            joTVLink = JSON.parse(strTVLink);
            let jaOperationList = joTVLink.click;
            for (let i = 0; i < jaOperationList.length; i++) {
                switch (jaOperationList[i].action) {
                    default:
                        this.doTVLinkAction(jaOperationList[i]);
                        break;
                }
            }
        } catch (error) {
            cc.log("runTVLinkAction Exception..." + error);
        }
    },


});