cocos2dx游戏只关卡开发

前端之家收集整理的这篇文章主要介绍了cocos2dx游戏只关卡开发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

小伙伴们今天晚上我要给大家讲讲cocos2dx之关卡开发,这里自己先看代码我里面写的很清楚了不懂的可以留言问我 我后续还有说明时间紧迫我暂时就不多写了,下次我在重点讲解

#ifndef __MISSIONUI_HEADER__
#define __MISSIONUI_HEADER__

#include <typedef.hpp>
#include <cocos-base.h>
#include <cocos-widget.h>
#include <ScreenUtil.hpp>
#include <CMisc.hpp>
#include <Facade.hpp>
#include <Language.hpp>
#include <TextButton.hpp>
#include <MissionDialogUi.hpp>


CLIENT_NS_BEGIN;
USING_NS_CC;
using namespace cocos2d::cocoswidget;
//关卡UI

enum MissionUiBtn
{
    tagTurnBack,tagStageNormal,tagStageElite,tagStageTeam,tagTurnLeft,tagTurnRight,tagShowDialog
};//Button tag


//todo
struct MissionData
{
    int id;
    int star;
    int result;
};
// enum MissionType
// {
// MISSION_TYPE_NORMAL,
// MISSION_TYPE_HARD,
// MISSION_TYPE_GUILD,
// 
// MISSION_TYPE_MAX
// };//关卡类型枚举

class CustomsPassUi : public CSceneExtension 
{
public:
    CREATE_SCENE_FUNC(CustomsPassUi)

    CustomsPassUi();

    virtual ~CustomsPassUi();

    virtual void onLoadResources();

    virtual void onLoadResourcesCompleted();

    virtual void onLoadScene();

    virtual void onEnterScene();

    virtual void onExitScene();

    void onLoadChild();
    void caseClick(); 
    void arrowClick(int i_arrowType);

protected:

private:
    CC_SYNTHESIZE(int,n_xMissionType,MissionType);
    //pageIndex
    int curtIndex;

    // PageView Contener
    CWidgetWindow* m_pWindow;

    //ScollView background 's textrue
    Texture2D *scollTexture;

    CC_SYNTHESIZE_READONLY(cocoswidget::CPageView*,m_pMissionScrView,MissionPage);

    //关卡SollView背景图
    CC_SYNTHESIZE_READONLY(CCSprite*,m_pScollViewBg,ScollViewBg);

    //返回
    CC_SYNTHESIZE_READONLY(cocoswidget::CButton*,m_pMissionBackBtn,MissionBack);

    // 精英
    CC_SYNTHESIZE_READONLY(TextButton*,m_pEliteBtn,MissionElite);

    //普通
    CC_SYNTHESIZE_READONLY(TextButton*,m_pNormalBtn,MissionNormal);

    //TeamButton
    CC_SYNTHESIZE_READONLY(TextButton*,m_pMissionTeam,MissionTeam);

    //TurnLeft
    CC_SYNTHESIZE_READONLY(cocoswidget::CButton*,m_pTurnLeft,MissionLeft);

    //Turn Right
    CC_SYNTHESIZE_READONLY(cocoswidget::CButton*,m_pTurnRight,MissionRight); 



private:
    Ref* pageviewDataSource(Ref* pConvertCell,unsigned int nPageIdx);

    void onPageChanged(Ref* pObject,unsigned int nPageIndx);

    void showDialogBack(CCObject* pSender);

};

APP_NS_END

#endif __MISSIONUI_HEADER__
#include "MissionUi.hpp"
#include <MissionMediator.hpp>
#include <Scale9Sprite.h>

CLIENT_NS_BEGIN

CustomsPassUi::CustomsPassUi() :
        m_pScollViewBg(nullptr),m_pMissionScrView(nullptr),m_pMissionBackBtn(nullptr),m_pEliteBtn(nullptr),m_pNormalBtn(nullptr),n_xMissionType(1),curtIndex(0)
{
    setCachable(true);
    setAutoRemoveUnusedTexture(true);
}
CustomsPassUi::~CustomsPassUi()
{

}


void CustomsPassUi::onLoadResources()
{
    addImageAsync("Common/bg.jpg");
    addImageAsync("MissionUi/stage-map-elite-frame.jpg");
    addImageAsync("MissionUi/stage-map-elite-frame_alpha_mask.png");
    addImageAsync("MissionUi/stage-map-frame.jpg");
    addImageAsync("MissionUi/stage-map-frame_alpha_mask.png");
    addImageAsync("MissionUi/stageselect_map_bg_1.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_2.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_3.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_4.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_5.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_6.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_7.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_8.jpg");
    addImageAsync("MissionUi/stageselect_map_bg_9.jpg");
    addImageAsync("MissionUi/back_normal.png");
    addImageAsync("MissionUi/back_seleted.png");
    addImageAsync("MissionUi/button_normal.png");
    addImageAsync("MissionUi/button_selected.png");
    addImageAsync("MissionUi/nextbtn.jpg");
    addImageAsync("MissionUi/nextbtn_alpha_mask.png");
}

void CustomsPassUi::onLoadResourcesCompleted()
{

}

void CustomsPassUi::onLoadScene()
{
    this->removeAllChildrenWithCleanup(true);
    onLoadChild();
}
void CustomsPassUi::caseClick()
{
    //scollView的背景图
    switch (n_xMissionType)
    {
    case 1:
    {
        // normal Textrue
        scollTexture = getNewTexture("MissionUi/stage-map-frame.jpg","MissionUi/stage-map-frame_alpha_mask.png");
    }
        break;
    case 2:
    {
        // elite Textrue        
        scollTexture = getNewTexture("MissionUi/stage-map-elite-frame.jpg","MissionUi/stage-map-elite-frame_alpha_mask.png");

    }
        break;
    case 3:
    {
       // Team Textrue  


    }
        break;
    default:
        break;
    }

}
void CustomsPassUi::onLoadChild()
{
    caseClick();
    //背景图
    CCSprite *bg = CCSprite::create("Common/bg.jpg");
    bg->setPosition(ScreenUtil::getCenter());
    addChild(bg);

    if (scollTexture!=nullptr)
    {
        m_pScollViewBg = Sprite::createWithTexture(scollTexture);
        m_pScollViewBg->setAnchorPoint(Vec2(0.5,0));
        m_pScollViewBg->setPosition(Vec2(480,20));
        addChild(m_pScollViewBg);
    }



    //返回按钮
    m_pMissionBackBtn = cocoswidget::CButton::create("MissionUi/back_normal.png","MissionUi/back_seleted.png");
    getMissionBack()->setAnchorPoint(ccp(0,0));
    //getBagBack()->setScale(0.8f);
    getMissionBack()->setPosition(ccp(60,570));
    getMissionBack()->setTag(tagTurnBack);
    this->addChild(getMissionBack());

    //普通按钮
    m_pNormalBtn = TextButton::create(LANGUAGE_VALUE("Mission_Normal"),"MissionUi/button_normal.png","MissionUi/button_selected.png");
    getMissionNormal()->setPosition(Vec2(480 - 100,550));
    getMissionNormal()->setTag(tagStageNormal);
    addChild(getMissionNormal());

    //精英按钮
    m_pEliteBtn = TextButton::create(LANGUAGE_VALUE("Mission_Elit"),"MissionUi/button_selected.png");
    getMissionElite()->setPosition(Vec2(480 + 130,550));
    getMissionElite()->setTag(tagStageElite);
    addChild(getMissionElite());

    //TeamButton
    m_pMissionTeam = TextButton::create(LANGUAGE_VALUE("Mission_Team"),"MissionUi/button_selected.png");
    getMissionTeam()->setPosition(Vec2(480 + 390,550));
    getMissionTeam()->setTag(tagStageTeam);
    addChild(getMissionTeam());

    //Turn left
    m_pTurnLeft = cocoswidget::CButton::create("MissionUi/nextbtn.jpg","MissionUi/nextbtn_alpha_mask.png");
    getMissionLeft()->setPosition(Vec2(20,320));
    getMissionLeft()->setTag(tagTurnLeft);
    addChild(getMissionLeft(),1);

    //Turn Right
    m_pTurnRight = cocoswidget::CButton::create("MissionUi/nextbtn.jpg","MissionUi/nextbtn_alpha_mask.png");
    getMissionRight()->setPosition(Vec2(960 - 30,320));
    getMissionRight()->setTag(tagTurnRight);
    addChild(getMissionRight(),1);

    m_pWindow = CWidgetWindow::create();
    m_pWindow->setMultiTouchEnabled(true);
    m_pWindow->setAnchorPoint(ccp(0,0));
    m_pWindow->setPosition(Vec2(10,30));
    addChild(m_pWindow,1);
    //ScollView
    m_pMissionScrView = CPageView::create(
        Size(910,470),9,this,ccw_datasource_adapter_selector(CustomsPassUi::pageviewDataSource));
    m_pMissionScrView->setOnPageChangedListener(this,ccw_pagechanged_selector(CustomsPassUi::onPageChanged));
    m_pMissionScrView->setAnchorPoint(ccp(0,0));
    m_pMissionScrView->setPosition(Vec2(20,30));
    m_pMissionScrView->setEnabled(true);
    m_pMissionScrView->setDirection(eScrollViewDirectionHorizontal);
    m_pWindow->addChild(m_pMissionScrView);
    m_pMissionScrView->reloadData();


}
void CustomsPassUi::onEnterScene()
{
    g_facade.sendNotification(FRAME_MESSAGE_ENTER_MISSION_UI);
}


void CustomsPassUi::onExitScene()
{
//  g_facade.sendNotification(FRAME_MESSAGE_EXIT_MISSION_UI);
}
Ref* CustomsPassUi::pageviewDataSource(Ref* pConvertCell,unsigned int nPageIdx)
{
    CPageViewCell* pPageCell = (CPageViewCell*)pConvertCell;
    char pictrueName[40];
    sprintf(pictrueName,"MissionUi/stageselect_map_bg_%d.jpg",nPageIdx + 1);

    CButton* m_pShowDialog = nullptr;
    Sprite*  pageViewBg = nullptr;
    if (!pPageCell)
    {
        pPageCell = new CPageViewCell();
        pPageCell->autorelease();
        m_pShowDialog = CButton::create("MissionUi/nextbtn.jpg","MissionUi/nextbtn_alpha_mask.png");
        m_pShowDialog->setOnClickListener(this,ccw_click_selector(MissionMediator::onClickBtn));
        m_pShowDialog->setPosition(200,250);
        m_pShowDialog->setTag(tagShowDialog);
        pPageCell->addChild(m_pShowDialog);

        pageViewBg = Sprite::create();
        pageViewBg->setPosition(Vec2(455,235));
        pageViewBg->setTag(100001);
        pPageCell->addChild(pageViewBg);

    }
    else
    {
        m_pShowDialog = (CButton*)pPageCell->getChildByTag(tagShowDialog);
        pageViewBg = (Sprite*)pPageCell->getChildByTag(100001);
    }

    pageViewBg->setTexture(pictrueName);


    return pPageCell;
}
void CustomsPassUi::onPageChanged(Ref* pObject,unsigned int nPageIndx)
{
    //pageIndex = nPageIndx;
}

void CustomsPassUi::arrowClick(int i_arrowType)
{   

    CCLog("=============click LEft=======");
    if (i_arrowType == 1)
    {

        //向左
        if (curtIndex == 0)
        {
            this->removeChildByTag(tagTurnLeft);
        }
        else
        {   
            curtIndex -= 1;
            m_pTurnRight = cocoswidget::CButton::create("MissionUi/nextbtn.jpg","MissionUi/nextbtn_alpha_mask.png");
            getMissionRight()->setPosition(Vec2(960 - 30,320));
            getMissionRight()->setTag(tagTurnRight);
            addChild(getMissionRight(),1);
            m_pMissionScrView->setContentOffsetEaseIn(Vec2(curtIndex * 910,0),0.05,0.5);
        }
    } 
    else if (i_arrowType == 2)
    {
        //向右
        CCLog("=============Click right======");
        if (curtIndex == 8)
        {
            this->removeChildByTag(tagTurnRight);

        }
        else
        {
            curtIndex += 1;
            m_pTurnLeft = cocoswidget::CButton::create("MissionUi/nextbtn.jpg","MissionUi/nextbtn_alpha_mask.png");
            getMissionLeft()->setPosition(Vec2(20,320));
            getMissionLeft()->setTag(tagTurnLeft);
            addChild(getMissionLeft(),1);
            getMissionPage()->setContentOffsetEaseIn(Vec2(-(curtIndex * 910),0.5);

        }
    }
}





APP_NS_END
#include <ScreenUtil.hpp>
#include <Language.hpp>
#include <CMisc.hpp>
#include <Facade.hpp>
USING_NS_CC;
CLIENT_NS_BEGIN
using namespace cocoswidget;

enum DialogBtn
{
    tagStageBack,tagStageFight
};
 class CustomsPassDialogUi : public CSceneExtension { public: CREATE_SCENE_FUNC(CustomsPassDialogUi); CustomsPassDialogUi(); ~CustomsPassDialogUi(); virtual void onLoadResources(); virtual void onLoadResourcesCompleted(); virtual void onLoadScene(); virtual void onEnterScene(); virtual void onExitScene(); private: void initDialog(); private: CC_SYNTHESIZE_READONLY(int,n_pMissionID,MissionID); //调用的关卡ID CC_SYNTHESIZE_READONLY(int,n_pMissionType,MissionDialogType);//调用关卡类型 CC_SYNTHESIZE_READONLY(Sprite*,m_pBackPictrue,BackPictrue); //地图图片 CC_SYNTHESIZE_READONLY(Texture2D*,m_pFrameTextrue,FrameTexture); //弹窗背景Texture CC_SYNTHESIZE_READONLY(Sprite*,m_pBg,Bg); //弹框背景 CC_SYNTHESIZE_READONLY(Sprite*,m_pStar,Star); //星星 CC_SYNTHESIZE_READONLY(cocoswidget::CButton*,m_pBack,CallBackBtn); //返回按钮 CC_SYNTHESIZE_READONLY(cocoswidget::CButton*,m_pFightBtn,GoFightBtn); //战斗按钮 CC_SYNTHESIZE_READONLY(cocoswidget::CLabel*,m_pTitle,LabelTitle); //该关卡名字 CC_SYNTHESIZE_READONLY(cocoswidget::CLabel*,m_pDes,LabelDes); //描述该关卡 CC_SYNTHESIZE_READONLY(cocoswidget::CLabel*,m_pConsume,LabelConsume);//体力消耗数值 // todo 敌方阵容....可能获得...图片显示 }; APP_NS_END #endif
#include "MissionDialogUi.hpp"
#include <XmlConfig.hpp>
CLIENT_NS_BEGIN

CustomsPassDialogUi::CustomsPassDialogUi()
{
    setCachable(true);
    //setAutoRemoveUnusedTexture(true);
}
CustomsPassDialogUi::~CustomsPassDialogUi()
{

}
void CustomsPassDialogUi::onLoadResources()
{
    addImageAsync("MissionUi/stage-map-frame.jpg");
    addImageAsync("MissionUi/stage-map-frame_alpha_mask.png");
    addImageAsync("MissionUi/star-disabled.jpg");
    addImageAsync("MissionUi/star-disabled_alpha_mask.png");
    addImageAsync("MissionUi/star.jpg");
    addImageAsync("MissionUi/star_alpha_mask.png"); 
    addImageAsync("MissionUi/back_normal.png");
    addImageAsync("MissionUi/back_seleted.png");
    addImageAsync("MissionUi/resume_battle.jpg");
    addImageAsync("MissionUi/resume_battle_alpha_mask.png");
    addImageAsync("MissionUi/resume_battle_press.jpg");
    addImageAsync("MissionUi/resume_battle_press_alpha_mask.png");

}
void CustomsPassDialogUi::onLoadResourcesCompleted()
{


}

void CustomsPassDialogUi::onLoadScene()
{

    initDialog();
}

// @param关卡ID,类型
void CustomsPassDialogUi::initDialog()
{   
    m_pBackPictrue = Sprite::create("MissionUi/stageselect_map_bg_1.jpg");
    m_pBackPictrue->setAnchorPoint(ccp(0,0));
    m_pBackPictrue->setPosition(ccp(20,20));
    m_pBackPictrue->setOpacity(40);
    addChild(m_pBackPictrue);

    m_pFrameTextrue = getNewTexture("MissionUi/stage-map-frame.jpg","MissionUi/stage-map-frame_alpha_mask.png");
    m_pBg = Sprite::createWithTexture(m_pFrameTextrue);
    m_pBg->setAnchorPoint(ccp(0,0));
 /* m_pBg->setOpacity(100);*/
    m_pBg->setPosition(ccp(20,20));
    addChild(m_pBg);


    //关卡名字
    m_pTitle = CLabel::create();
    getLabelTitle()->setString("");
    getLabelTitle()->setColor(Color3B(227,207,87));
    getLabelTitle()->setPosition(ccp(300,480));
    addChild(getLabelTitle());

    //关卡描述
    m_pDes = CLabel::create();
    getLabelDes()->setString("");
    getLabelDes()->setColor(Color3B(255,255,255));
    getLabelDes()->setPosition(100,450);
    addChild(getLabelDes());

    //星星
    auto disStarTexture = getNewTexture("MissionUi/star-disabled.jpg","MissionUi/star-disabled_alpha_mask.png");
    auto starTexture = getNewTexture("MissionUi/star.jpg","MissionUi/star_alpha_mask.png");

    for (int i = 0; i < 3; i++)
    {
        m_pStar = Sprite::createWithTexture(disStarTexture);
        getStar()->setAnchorPoint(ccp(0.5,0));
        getStar()->setPosition(ccp(450 + i * 100,500));
        addChild(getStar());
    }

    //体力消耗
    auto strengDes = CLabel::create();
    strengDes->setString(LANGUAGE_VALUE("MissionDialog_cosStr"));
    strengDes->setColor(Color3B(255,277,132));
    strengDes->setPosition(ccp(100,350));
    addChild(strengDes);

    //体力消耗数值
    m_pConsume = CLabel::create();
    getLabelConsume()->setString("");
    getLabelConsume()->setColor(Color3B(227,87));
    getLabelConsume()->setPosition(ccp(130,350));
    addChild(getLabelConsume());

    //体力图片



    //敌方阵容
    auto enmeyLable = CLabel::create();
    enmeyLable->setString(LANGUAGE_VALUE("MissionDialog_EnemyArr"));
    enmeyLable->setColor(Color3B(255,132));
    enmeyLable->setPosition(ccp(130,320));
    addChild(enmeyLable);

    //可能获得
    auto gainLable = CLabel::create();
    gainLable->setString(LANGUAGE_VALUE("MissionDialog_gain"));
    gainLable->setColor(Color3B(255,132));
    gainLable->setPosition(ccp(130,300));
    addChild(gainLable);

    //返回按钮
    m_pBack = CButton::create("MissionUi/back_normal.png","MissionUi/back_seleted.png");
    m_pBack->setPosition(ccp(100,540));
    getCallBackBtn()->setTag(tagStageBack);
    addChild(m_pBack);

    //去战斗按钮
    auto normalTextrue = getNewTexture("MissionUi/resume_battle.jpg","MissionUi/resume_battle_alpha_mask.png");
    auto secletTextrue = getNewTexture("MissionUi/resume_battle_press.jpg","MissionUi/resume_battle_press_alpha_mask.png");
    m_pFightBtn = CButton::create();
    m_pFightBtn->setNormalTexture(normalTextrue);
    m_pFightBtn->setSelectedTexture(secletTextrue);
    m_pFightBtn->setPosition(620,100);
    getGoFightBtn()->setTag(tagStageFight);
    addChild(m_pFightBtn);




}
void CustomsPassDialogUi::onEnterScene()
{
    g_facade.sendNotification(FRAME_MESSAGE_ENTER_MISSIONDIALOG_UI);

}

void CustomsPassDialogUi::onExitScene()
{

}


APP_NS_END
#ifndef __MISSION_MEDIATOR_HEADER__
#define __MISSION_MEDIATOR_HEADER__

#include <typedef.hpp>
#include <Mediator.hpp>
#include "MissionUi.hpp"


CLIENT_NS_BEGIN

USING_NS_CC;
struct MissionInfomation :
    public Ref
{

    int MissionInfoId;
    int MissionInfoType;
    int MissionInfoStar;
    MissionInfomation()
    {
        this->autorelease();
    }

};

class CustomsPassMediator : public Mediator 
{
public:
    CustomsPassMediator();
    virtual ~CustomsPassMediator();
    virtual void onRegisterNotification();

    virtual void handleNotification(Notification* pNotification);

    virtual void update();

    virtual std::string getMediatorName(){ return "MissionMediator"; }
    friend class MissionUi;
private:
    CC_SYNTHESIZE_READONLY(MissionUi*,m_pMissionUi,MissionUi);
    CC_SYNTHESIZE(int,n_CurMissionId,CurMissionId);
    CC_SYNTHESIZE(int,n_CurMissionType,CurMissionType);
    CC_SYNTHESIZE(int,n_CurMissionStar,CurMissionStar);
    void onClickBtn(CCObject* pObject);
};


APP_NS_END
#endif
include "MissionMediator.hpp"
#include <Facade.hpp>
CLIENT_NS_BEGIN


CustomsPassMediator::CustomsPassMediator() :m_pMissionUi(nullptr)
{
    REGISTER_SCENE_FUNC(MissionUi);
}

CustomsPassMediator::~CustomsPassMediator()
{

}

void CustomsPassMediator::onRegisterNotification()
{
    m_xFrameMessageVec.push_back(FRAME_MESSAGE_SHOW_MISSION_UI);

    m_xFrameMessageVec.push_back(FRAME_MESSAGE_ENTER_MISSION_UI);

    m_xFrameMessageVec.push_back(FRAME_MESSAGE_EXIT_MISSION_UI);
}

void CustomsPassMediator::handleNotification(Notification* pNotification)
{
    if (NULL == pNotification)
    {
        return;
    }

    switch (pNotification->m_xMessage)
    {
    case FRAME_MESSAGE_SHOW_MISSION_UI:
        {
            m_pMissionUi = (MissionUi*)LoadScene("MissionUi");

            CSceneManager::getInstance()->replaceScene(m_pMissionUi);
        } 
        break;
    case FRAME_MESSAGE_ENTER_MISSION_UI:
        {   
             //返回按钮
             getMissionUi()->getMissionBack()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));
             //普通按钮
             getMissionUi()->getMissionNormal()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));
             //精英按钮
             getMissionUi()->getMissionElite()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));
             //团队按钮
             getMissionUi()->getMissionTeam()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));
             //向左按钮
             getMissionUi()->getMissionLeft()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));
             //向右按钮
             getMissionUi()->getMissionRight()->setOnClickListener(this,ccw_click_selector(CustomsPassMediator::onClickBtn));

        }
        break;
    case FRAME_MESSAGE_EXIT_MISSION_UI:
        {
            g_facade.sendNotification(FRAME_MESSAGE_SHOW_MAIN_UI);
            /*CSceneManager::getInstance()->popScene(m_pMissionUi);*/
        }
        break;
    }
}

void CustomsPassMediator::update()
{

}

void CustomsPassMediator::onClickBtn(CCObject* pObject)
{
    MissionInfomation* pMissionInfomation = nullptr;

    auto pButton = (CButton*)pObject;

    switch (pButton->getTag())
    {
    case tagTurnBack:
        {
            CCLog("tag Mission back");
            g_facade.sendNotification(FRAME_MESSAGE_EXIT_MISSION_UI);
            //g_facade.sendNotification(FRAME_MESSAGE_SHOW_MAIN_UI);
        }
        break;
    case tagStageNormal:
        {
            CCLog("tag Mission Normal");

            getMissionUi()->setMissionType(1);
            setCurMissionType(1);
            getMissionUi()->onLoadChild();
        }
        break;
    case tagStageElite:
        {
            CCLog("tag Mission Elite");
            getMissionUi()->setMissionType(2);
            setCurMissionType(2);
            getMissionUi()->onLoadChild();
        }
        break;
    case tagStageTeam:
        {
            CCLog("tag Mission Team");

            getMissionUi()->setMissionType(3);
            setCurMissionType(3);
            getMissionUi()->onLoadChild();
        }
        break;
    case tagTurnLeft:
        {
            CCLog("tag Turn left");
            getMissionUi()->arrowClick(1);
        }
        break;
    case tagTurnRight:
        {
            CCLog("tag Turn Left");
            getMissionUi()->arrowClick(2);

        }
        break;
    case tagShowDialog:
        {
            CCLog("show Mission Dialog ");
            /*  
                test,sendNotification to Dialog,pack this strcut,this ID should get from the  MissionUI,this is a test.
            */
            setCurMissionId(600001);
            pMissionInfomation = new MissionInfomation();
            pMissionInfomation->MissionInfoId = getCurMissionId();
            pMissionInfomation->MissionInfoType = 1;

            g_facade.sendNotification(FRAME_MESSAGE_SHOW_MISSIONDIALOG_UI);
        }
        break;
    }


}


APP_NS_END
#ifndef __MISSIONDIALOG_MEDIATOR_HEADER__
#define __MISSIONDIALOG_MEDIATOR_HEADER__

#include <typedef.hpp>
#include <Mediator.hpp>
#include "MissionDialogUi.hpp"
#include <MissionProxy.hpp>

CLIENT_NS_BEGIN

USING_NS_CC;

struct Data
{   
    int dataId;
    int dataType;

};
class CustomsPassDialogMediator : public Mediator
{
public:
    CustomsPassDialogMediator();
    virtual ~CustomsPassDialogMediator();
    virtual void onRegisterNotification();

    virtual void handleNotification(Notification* pNotification);

    virtual void update();

    virtual std::string getMediatorName(){ return "MissionDialogMediator"; }
    friend class CustomsPassUi;
private:
    CC_SYNTHESIZE_READONLY(MissionDialogUi*,m_pMissionDialogUi,MissionDialogUi);
    void onClickBtn(CCObject* pObject);
    MissionProxy *pMissionProxy;
};


APP_NS_END
#endif
#include "MissionDialogMediator.hpp"
#include "MissionMediator.hpp"

CLIENT_NS_BEGIN
CustomsPassDialogMediator::CustomsPassDialogMediator()
{
    REGISTER_SCENE_FUNC(MissionDialogUi);
    pMissionProxy = (MissionProxy*)g_facade.retrieveProxy("MissionProxy");
}

CustomsPassDialogMediator::~CustomsPassDialogMediator()
{

}
void CustomsPassDialogMediator::onRegisterNotification()
{
    m_xFrameMessageVec.push_back(FRAME_MESSAGE_SHOW_MISSIONDIALOG_UI);

    m_xFrameMessageVec.push_back(FRAME_MESSAGE_ENTER_MISSIONDIALOG_UI);

    m_xFrameMessageVec.push_back(FRAME_MESSAGE_EXIT_MISSIONDIALOG_UI);

}

void CustomsPassDialogMediator::handleNotification(Notification* pNotification)
{
    if (NULL == pNotification)
    {
        return;
    }

    switch (pNotification->m_xMessage)
    {
        case FRAME_MESSAGE_SHOW_MISSIONDIALOG_UI:
            {
                //int id = ((MissionInfomation*)pNotification->m_pBody)->MissionInfoId;

                m_pMissionDialogUi = (MissionDialogUi*)LoadScene("MissionDialogUi");

                CSceneManager::getInstance()->pushScene(m_pMissionDialogUi);
            }
            break;
        case FRAME_MESSAGE_ENTER_MISSIONDIALOG_UI:
            {
                if (pMissionProxy == nullptr)
                    {
                        return;
                    }
                else
                    {
                        //test information 
                    MissionTrunkVo *_CurMapInfo = pMissionProxy->getMapInfo(41010101);

                        std::string s = _CurMapInfo->getMapName();

                        getMissionDialogUi()->getLabelDes()->setString(s);
                    }

                getMissionDialogUi()->getCallBackBtn()->setOnClickListener(this,ccw_click_selector(CustomsPassDialogMediator::onClickBtn));
                getMissionDialogUi()->getGoFightBtn()->setOnClickListener(this,ccw_click_selector(CustomsPassDialogMediator::onClickBtn));
            }
        break;
        case FRAME_MESSAGE_EXIT_MISSIONDIALOG_UI:
            {
                CSceneManager::getInstance()->popScene(m_pMissionDialogUi);

            }
        break;
    }

}

void CustomsPassDialogMediator::update()
{

}

void CustomsPassDialogMediator::onClickBtn(CCObject* pObject)
{
    int nTag = ((cocoswidget::CButton*)pObject)->getTag();
    switch (nTag)
    {
    case tagStageBack:
        {
             CCLog(" tag MissionDialog Back");
             g_facade.sendNotification(FRAME_MESSAGE_EXIT_MISSIONDIALOG_UI);
        }
        break;
    case tagStageFight:
        {

            CCLog("tag MissionDialog Fight");
            if (nullptr == pMissionProxy)
            {
                return;
            }
            pMissionProxy->onMission(201110243,2,1);
        }
        break;

    }

}
APP_NS_END

接下来这部分就是客户端向服务端发送消息,意思就是当我通过关卡以后我要去告诉服务端那边玩家是否打过 打过了该怎么怎么样,没打过该怎么这么样

#ifndef __MISSION_PROXY_HEADER__
#define __MISSION_PROXY_HEADER__

#include <typedef.hpp>
#include <Proxy.hpp>
#include <MissionVo.hpp>
#include <Character.hpp>


CLIENT_NS_BEGIN

USING_NS_CC;


class CustomsPassProxy :
    public Proxy
{
public:
    CustomsPassProxy();
    ~CustomsPassProxy();

    virtual void onInitilize();

    virtual void onRegisterMessage();

    virtual std::string getProxyName(){ return "MissionProxy"; }

    void onMission(int id,int mission_type,int star);

    void handleMissionAgentTokenAck(Message::Ptr& pMessage);

    // Get this mapId's information
    MissionTrunkVo* getMapInfo(int mapId);

    //get this node's information
    MissionBranchVo* getNodeInfo(int nodeId);

    //get this NodeId ' enmey information
    MissionArrayVo* getCurEnemyInfo(int nodeId);

private:
    void loadTrunkInfo();

    void loadBranchInfo();

    void loadArrayInfo();

private:
    CC_SYNTHESIZE(CCDictionary*,m_pTrunkInfo,TrunkInfo);
    CC_SYNTHESIZE(CCDictionary*,m_pBranchInfo,BranchInfo);
    CC_SYNTHESIZE(CCDictionary*,m_pEnmeyArrInfo,EnmeyArrInfo);
};

APP_NS_END

#endif
#include <MissionProxy.hpp>
#include <tinyxml2/tinyxml2.h>
#include <AgentServer/Mission.hpp>
#include <Alert.hpp>
#include <Facade.hpp>
#include <NetDelegate.hpp>


CLIENT_NS_BEGIN

CustomsPassProxy::CustomsPassProxy()
{
    m_pTrunkInfo = CCDictionary::create();
    m_pTrunkInfo->retain();

    m_pBranchInfo = CCDictionary::create();
    m_pBranchInfo->retain();

    m_pEnmeyArrInfo = CCDictionary::create();
    m_pEnmeyArrInfo->retain();
}

CustomsPassProxy::~CustomsPassProxy()
{

}

void CustomsPassProxy::onInitilize()
{
    loadTrunkInfo();

    loadBranchInfo();

    loadArrayInfo();
}

void CustomsPassProxy::onRegisterMessage()
{
    DEFREG_MESSAGE(ID_GS2C_EnterMissionAck,CustomsPassProxy,handleMissionAgentTokenAck)
}

void CustomsPassProxy::handleMissionAgentTokenAck(Message::Ptr & pMessage)
{
    GS2C_EnterMissionAck missionAgentTokenAck;
    pMessage->unpack(missionAgentTokenAck);

    if (missionAgentTokenAck.Result == 1 )
    {
        MissionInfoVec xMissionInfoVec;

        StageInfoVec::iterator iter = missionAgentTokenAck.StageInfo.begin();

        StageInfoVec::iterator end  = missionAgentTokenAck.StageInfo.end();

        for (; iter != end;++iter)
        {
            MissionInfo xMissionInfo;

            xMissionInfo.MissionId = iter->MissionsqlId;
            xMissionInfo.MissionType = iter->MissionsqlType;
            xMissionInfo.MissionStar = iter->MissionsqlStar;
            xMissionInfo.MissionCount = iter->MissionsqlCount;
            xMissionInfo.MissionResetCount = iter->MissionsqlResetCount;
            xMissionInfo.MissionDilayChallenge = iter->MissionsqlDilayChallenge;
            xMissionInfoVec.push_back(xMissionInfo);
        }

        g_character.setMissionInfoVec(xMissionInfoVec);

    }
    else
    {

        return;
    }

}

void CustomsPassProxy::onMission(int id,int mission_type,int star)
{
    Message::Ptr sendMsg(new Message());

    C2GS_MissionReq missionAgentTokenReq;

    missionAgentTokenReq.MissionToID = id;
    missionAgentTokenReq.MissionToType = mission_type;
    missionAgentTokenReq.MissionToStar = star;
    missionAgentTokenReq.MissionToCount = 2;
    missionAgentTokenReq.MissionToResetCount = 1;
    missionAgentTokenReq.MissionToDilayChallenge = 3;
    sendMsg->pack(missionAgentTokenReq);

    g_netDelegate.send(sendMsg);

}
//地图读取
void CustomsPassProxy::loadTrunkInfo()
{
    ssize_t bufferSize = 0;

    unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData("MissionData.xml","r",&bufferSize);

    if (NULL == pBuffer)
    {
        return;
    }

    tinyxml2::XMLDocument* pXml = new tinyxml2::XMLDocument();

    pXml->Parse((const char*)pBuffer);

    tinyxml2::XMLElement* pNode = pXml->RootElement()->FirstChildElement();

    MissionTrunkVo* pMissionTrunkVo = nullptr;
    while (pNode)
    {
        pMissionTrunkVo = new MissionTrunkVo();

        pMissionTrunkVo->setMapId(pNode->IntAttribute("MapId"));            // 地图Id

        pMissionTrunkVo->setMapName(pNode->Attribute("MapName"));           //地图名字

        pMissionTrunkVo->setMapResource(pNode->Attribute("MapResource"));   //地图资源  

        pMissionTrunkVo->setMapType(pNode->IntAttribute("MapType"));        //地图类型

        pMissionTrunkVo->setLimitLevel(pNode->IntAttribute("LimitLevel"));  //等级限制

        pMissionTrunkVo->setLimitTime(pNode->IntAttribute("LimitTime"));    //时间限制

        pMissionTrunkVo->setLimitType(pNode->IntAttribute("LimitType"));    //限制条件

        pMissionTrunkVo->setLimitCon(pNode->IntAttribute("LimitCon"));      //限制条件参数

        pMissionTrunkVo->setLastId(pNode->IntAttribute("LastId"));          //前置要求

        pMissionTrunkVo->autorelease();

        m_pTrunkInfo->setObject(pMissionTrunkVo,pMissionTrunkVo->getMapId());

        pNode = pNode->NextSiblingElement();
    }
}
//节点读取
void CustomsPassProxy::loadBranchInfo()
{
    ssize_t bufferSize = 0;

    unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData("NodeData.xml",&bufferSize);

    if (NULL == pBuffer)
    {
        return;
    }

    tinyxml2::XMLDocument* pXml = new tinyxml2::XMLDocument();

    pXml->Parse((const char*)pBuffer);

    tinyxml2::XMLElement* pNode = pXml->RootElement()->FirstChildElement();

    MissionBranchVo* pMissionBranchVo = nullptr;
    while (pNode)
    {
        pMissionBranchVo = new MissionBranchVo();

        pMissionBranchVo->setBranchId(pNode->IntAttribute("NodeId"));                       // 节点Id

        pMissionBranchVo->setNodeName(pNode->Attribute("NodeName"));                    //节点名字

        pMissionBranchVo->setNodeResource(pNode->Attribute("NodeResource"));            //图片资源

        pMissionBranchVo->setNodeMap(pNode->Attribute("NodeMap"));                  //所属地图

        pMissionBranchVo->setNodeImage(pNode->Attribute("NodeImage"));              //背景地图

        pMissionBranchVo->setFrontId(pNode->IntAttribute("FrontId"));               //前置节点

        pMissionBranchVo->setNodeLevel(pNode->IntAttribute("NodeLevel"));           //需要等级

        pMissionBranchVo->setNeedStr(pNode->IntAttribute("NeedStr"));               //体力消耗

        pMissionBranchVo->setLimitCou(pNode->IntAttribute("LimitCou"));             //次数限制

        pMissionBranchVo->setNodeCion(pNode->IntAttribute("NodeCion"));             //金币产出

        pMissionBranchVo->setNodeExp(pNode->IntAttribute("NodeExp"));               //角色经验产出

        pMissionBranchVo->setNodeHeroExp(pNode->IntAttribute("NodeHeroExp"));           //英雄经验产出

        pMissionBranchVo->setDropType(pNode->IntAttribute("DropType"));             //掉落类型

        pMissionBranchVo->setDropId(pNode->IntAttribute("DropId"));                 //掉落ID

        pMissionBranchVo->setDropNum(pNode->IntAttribute("DropNum"));               //掉落数量

        pMissionBranchVo->setNodeDec(pNode->Attribute("NodeDec"));              //节点描述

        pMissionBranchVo->setNodeOrderId(pNode->IntAttribute("NodeOrderId"));           //节点阵容ID

        pMissionBranchVo->setTeamDiff1(pNode->IntAttribute("TeamDiff1"));               //阵容难度1

        pMissionBranchVo->setTeamDiff2(pNode->IntAttribute("TeamDiff2"));               //阵容难度2

        pMissionBranchVo->setTeamDiff3(pNode->IntAttribute("TeamDiff3"));               //阵容难度3

        pMissionBranchVo->setTeamDiff4(pNode->IntAttribute("TeamDiff4"));               //阵容难度4

        pMissionBranchVo->setTeamDiff5(pNode->IntAttribute("TeamDiff15"));              //阵容难度5

        pMissionBranchVo->setCopyDrop1(pNode->IntAttribute("CopyDrop1"));               //副本掉落1

        pMissionBranchVo->setCopyPro1(pNode->IntAttribute("CopyPro1"));             //概率1

        pMissionBranchVo->setCopyDrop2(pNode->IntAttribute("CopyDrop2"));               //副本掉落2

        pMissionBranchVo->setCopyPro2(pNode->IntAttribute("CopyPro2"));             //概率2

        pMissionBranchVo->setCopyDrop3(pNode->IntAttribute("CopyDrop3"));               //副本掉落3

        pMissionBranchVo->setCopyPro3(pNode->IntAttribute("CopyPro3"));             //概率3

        pMissionBranchVo->setCopyDrop4(pNode->IntAttribute("CopyDrop4"));               //副本掉落4

        pMissionBranchVo->setCopyPro4(pNode->IntAttribute("CopyPro4"));             //概率4

        pMissionBranchVo->setCopyDrop5(pNode->IntAttribute("CopyDrop5"));               //副本掉落5

        pMissionBranchVo->setCopyPro5(pNode->IntAttribute("CopyPro5"));             //概率5

        pMissionBranchVo->setWipeDrop1(pNode->IntAttribute("WipeDrop1"));               //扫荡掉落1

        pMissionBranchVo->setWipeNum1(pNode->IntAttribute("WipeNum1"));             //扫荡掉落数量1

        pMissionBranchVo->setWipeDrop2(pNode->IntAttribute("WipeDrop2"));               //扫荡掉落2

        pMissionBranchVo->setWipeNum2(pNode->IntAttribute("WipeNum2"));             //扫荡掉落数量2

        pMissionBranchVo->setAdjustRatio(pNode->IntAttribute("AdjustRatio"));       //调整系数

        pMissionBranchVo->autorelease();

        m_pBranchInfo->setObject(pMissionBranchVo,pMissionBranchVo->getBranchId());

        pNode = pNode->NextSiblingElement();
    }
}
//阵容读取
void CustomsPassProxy::loadArrayInfo()
{
    ssize_t bufferSize = 0;

    unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData("MissionEnemy.xml",&bufferSize);

    if (NULL == pBuffer)
    {
        return;
    }

    tinyxml2::XMLDocument* pXml = new tinyxml2::XMLDocument();

    pXml->Parse((const char*)pBuffer);

    tinyxml2::XMLElement* pNode = pXml->RootElement()->FirstChildElement();

    MissionArrayVo* pMissionArrayVo = nullptr;
    while (pNode)
    {
        pMissionArrayVo = new MissionArrayVo();

        pMissionArrayVo->setEnemyArrID(pNode->IntAttribute("EnemyArrID"));          // 阵容ID

        pMissionArrayVo->setEnemyOrder(pNode->IntAttribute("EnemyOrder"));          // 阵容序号

        pMissionArrayVo->setEnemyRatio(pNode->DoubleAttribute("EnemyRatio"));   // 阵容系数 

        pMissionArrayVo->setEnmey1(pNode->IntAttribute("Enmey1"));      // 出战角色1

        pMissionArrayVo->setLevel1(pNode->IntAttribute("Level1"));  // 等级1

        pMissionArrayVo->setEnmey2(pNode->IntAttribute("Enmey2"));  // 出战角色2

        pMissionArrayVo->setLevel2(pNode->IntAttribute("Level2"));  // 等级2

        pMissionArrayVo->setEnmey3(pNode->IntAttribute("Enmey3"));      // 出战角色3

        pMissionArrayVo->setLevel3(pNode->IntAttribute("Level3"));          // 等级3

        pMissionArrayVo->setEnmey4(pNode->IntAttribute("Enmey4"));      // 出战角色4

        pMissionArrayVo->setLevel4(pNode->IntAttribute("Level4"));          // 等级4

        pMissionArrayVo->setEnmey5(pNode->IntAttribute("Enmey5"));      // 出战角色5

        pMissionArrayVo->setLevel5(pNode->IntAttribute("Level5"));          // 等级5


        pMissionArrayVo->autorelease();

        m_pEnmeyArrInfo->setObject(pMissionArrayVo,pMissionArrayVo->getEnemyArrID());

        pNode = pNode->NextSiblingElement();
    }
}

//根据关卡ID获取此关卡ID的信息
MissionTrunkVo* CustomsPassProxy::getMapInfo(int mapId)
{
    DictElement* pElement = NULL;
    CCDICT_FOREACH(m_pTrunkInfo,pElement)
    {
        auto KeyMapId = pElement->getIntKey();
        if (KeyMapId == mapId)
        {
            MissionTrunkVo* MapInfo = (MissionTrunkVo*)pElement->getObject();
            return MapInfo;
        }
    }
}

//根据节点id去获取此节点id中的数据
MissionBranchVo* CustomsPassProxy::getNodeInfo(int nodeId)
{
    DictElement* pElement = NULL;
    CCDICT_FOREACH(m_pBranchInfo,pElement)
    {
        auto KeyNodeId = pElement->getIntKey();
        if (KeyNodeId == nodeId)
        {
            MissionBranchVo* NodeInfo = (MissionBranchVo*)pElement->getObject();
            return NodeInfo;
        }
    }

}

//根据节点id去获取阵容表中的数据
MissionArrayVo* CustomsPassProxy::getCurEnemyInfo(int nodeId)
{
    MissionBranchVo* Node = getNodeInfo(nodeId);

    int _ArrId = Node->getNodeOrderId();

    DictElement* pElement = NULL;
    CCDICT_FOREACH(m_pEnmeyArrInfo,pElement)
    {
        auto KeyEnemyId = pElement->getIntKey();
        if (KeyEnemyId == _ArrId)
        {
            MissionArrayVo* _EnemyArrInfo = (MissionArrayVo*)pElement->getObject();
            return _EnemyArrInfo;
        }
    }

}



APP_NS_END
原文链接:https://www.f2er.com/cocos2dx/343494.html

猜你在找的Cocos2d-x相关文章