cocos2dx游戏任务系统开发

cocos2dx游戏任务系统开发,第1张

概述UI.HPP #ifndef __QUESTUI_HEADER__#define __QUESTUI_HEADER__#include <typedef.hpp>#include <cocos-base.h>#include <TableView.h>#include "ScrollView.h"#include "WidgetProtocol.h"#include "cocos-

UI.HPP

#ifndef __QUESTUI_header__#define __QUESTUI_header__#include <typedef.hpp>#include <cocos-base.h>#include <tableVIEw.h>#include "ScrollVIEw.h"#include "WidgetProtocol.h"#include "cocos-ext.h"#include "cocos-Widget.h"#include "QuestProxy.hpp"#include <List>#include <vector>CLIENT_NS_BEGINUSING_NS_CC;enum QuestUiBtn{    tagCloseQuest,//退出按钮枚举;    tagQuestGreyBtn,//添加灰阶枚举;    tagQuestTestBtn             //任务添加单元格按钮枚举;};class QuestUi :    public CSceneExtension{public:    CREATE_SCENE_FUNC(QuestUi)    QuestUi();    virtual ~QuestUi();    virtual voID onLoadResources();    virtual voID onLoadResourcesCompleted();    virtual voID onLoadScene();    virtual voID onEnterScene();    voID onClickQuest(Ref* pSender);    voID onSeverQuest(int RID);    CWidgetWindow* m_pWindow;    //将任务过来的信息ID容器拷贝出来;    std::vector<i32>::iterator beg;    std::vector<string>QuestnameVe;    std::vector<string>::iterator nameBegin;private:    // 任务背景层;    CC_SYNTHESIZE_Readonly(CCSprite*,m_pQuestBg,QuestBg);    // 任务退出button;    CC_SYNTHESIZE_Readonly(cocosWidget::Cbutton*,m_pCloseQuest,CloseQuest);    // 任务标题显示;    CC_SYNTHESIZE_Readonly(CCSprite*,m_pQuestTitle,QuestTitle);    // 添加灰阶背景;    CC_SYNTHESIZE_Readonly(cocosWidget::Cbutton*,m_pQuestGreyBgBtn,QuestGreyBgBtn);    // 循环任务button;    CC_SYNTHESIZE_Readonly(cocosWidget::CtableVIEw*,m_pQuestVIEw,QuestVIEw);    CC_SYNTHESIZE_Readonly(cocosWidget::Cbutton*,m_pQuesbutton,QuestCellbutton);    // 添加任务名字信息;    CC_SYNTHESIZE_Readonly(Label*,m_pTitlename,Titlename);    QuestProxy* m_pQuestPro;private:    Ref* grIDpagevIEwDataSource(Ref* pConvertCell,unsigned int IDx);};#define g_missionDalog QuestUi::getInstance();APP_NS_END#endif __QUESTUI_header__

CPP

#include "QuestUi.hpp"#include <ScreenUtil.hpp>#include <CMisc.hpp>#include <Facade.hpp>#include <Language.hpp>#include "QuestMediator.hpp"#include <XmlConfig.hpp>CLIENT_NS_BEGINusing namespace cocosWidget;QuestUi::QuestUi()// ://m_pQuestPro(nullptr){    setCachable(true);    setautoRemoveUnusedTexture(true);    m_pQuestPro = (QuestProxy*)g_facade.retrIEveProxy("QuestProxy");    // 拷贝容器;    //ButSize = m_pQuestPro->ClIEntQuestInfo;    beg = m_pQuestPro->butsize.begin();    //在构造里面不能用,这个样会导致先进入这个然后在获取所对应的值;    //nameBegin = QuestnameVe.begin();}QuestUi::~QuestUi(){}//  UI资源添加;voID QuestUi::onLoadResources(){    addImageAsync("QuestUi/DailyQuestBg.jpg");    addImageAsync("QuestUi/DailyQuestBg_Alpha_mask.png");    addImageAsync("QuestUi/task_window_Title_bg.jpg");    addImageAsync("QuestUi/task_window_Title_bg_Alpha_mask.png");    addImageAsync("QuestUi/close.jpg");    addImageAsync("QuestUi/close_Alpha_mask.png");    addImageAsync("QuestUi/greyBg_1.png");}voID QuestUi::onLoadResourcesCompleted(){}//  各种UI按钮界面显示展示;voID QuestUi::onLoadScene(){    //  添加灰阶背景;    m_pQuestGreyBgBtn = cocosWidget::Cbutton::create();    getQuestGreyBgBtn()->setnormalimage("QuestUi/greyBg_2.png");    getQuestGreyBgBtn()->setAnchorPoint(ccp(0,0));    getQuestGreyBgBtn()->setposition(ccp(0,0));    this->addChild(getQuestGreyBgBtn());    this->setTag(tagQuestGreyBtn);    //  添加背景图层;    m_pQuestBg = CCSprite::create("QuestUi/QuestBg_1.png");    getQuestBg()->setAnchorPoint(ccp(0,0));    getQuestBg()->setposition(ccp(177,90));    this->addChild(getQuestBg());    //  任务退出按钮的添加;    Texture2D* pClosenormal = getNewTexture("QuestUi/close.jpg","QuestUi/close_Alpha_mask.png");    m_pCloseQuest = cocosWidget::Cbutton::create();    getCloseQuest()->setnormalTexture(pClosenormal);    getCloseQuest()->setAnchorPoint(ccp(0,0));    getCloseQuest()->setposition(ccp(810,490));    getCloseQuest()->setTag(tagCloseQuest);    this->addChild(getCloseQuest());    //  任务标题显示;    Texture2D* pQuestTitle = getNewTexture("QuestUi/task_window_Title_bg.jpg","QuestUi/task_window_Title_bg_Alpha_mask.png");    m_pQuestTitle = CCSprite::createWithTexture(pQuestTitle);    getQuestTitle()->setAnchorPoint(ccp(0,0));    getQuestTitle()->setposition(ccp(137,520));    this->addChild(getQuestTitle());    //添加WINDOW 按键;    m_pWindow = CWidgetWindow::create();    m_pWindow->setMultitouchEnabled(true);    m_pWindow->setAnchorPoint(ccp(0,0));    m_pWindow->setposition(ccp(0,0));    this->addChild(m_pWindow);    //任务滑动层    m_pQuestVIEw = CtableVIEw::create(        Size(729,415),Size(600,135),0,//shu        this,ccw_datasource_adapter_selector(QuestUi::grIDpagevIEwDataSource));    m_pQuestVIEw->setDirection(eScrollVIEwDirectionVertical);    m_pQuestVIEw->setautoRelocate(true);    m_pQuestVIEw->setAnchorPoint(ccp(0,0));    m_pQuestVIEw->setposition(Vec2(205,115));    m_pWindow->addChild(m_pQuestVIEw);    beg = m_pQuestPro->butsize.begin();    nameBegin = QuestnameVe.begin();}voID QuestUi::onEnterScene(){    g_facade.sendNotification(FRAME_MESSAGE_ENTER_QUEST_UI);}//  任务按钮循环创建;   Ref* QuestUi::grIDpagevIEwDataSource(Ref* pConvertVIEw,unsigned int IDx){    CtableVIEwCell* pCell = (CtableVIEwCell*)pConvertVIEw;    Cbutton* pbutton = nullptr;    if (!pCell)    {        pCell = new CGrIDPageVIEwCell();        pCell->autorelease();        //  添加        pbutton = Cbutton::createWith9Sprite(Size(600,125),"QuestUi/questFrameBg.png","QuestUi/task_window_Title_bg_Alpha_mask.png");        pbutton->setonClickListener(this,ccw_click_selector(QuestMediator::onClickBtn));        pbutton->setAnchorPoint(ccp(0,0));        pbutton->setTag(tagQuestTestBtn);        pbutton->setposition(ccp(0,0));        pCell->addChild(pbutton);           pbutton->setUserTag(*beg);        //名字显示在 单元格上;        m_pTitlename = CLabel::create();        getTitlename()->setString(*nameBegin);        getTitlename()->setcolor(color3B(0,0));        getTitlename()->setposition(Vec2(205,115));        pbutton->addChild(getTitlename());        if (beg != m_pQuestPro->butsize.end())        {            beg++;            nameBegin++;        }    }    else    {        pbutton = (Cbutton*)pCell->getChildByTag(tagQuestTestBtn);    }    return pCell;} voID QuestUi::onClickQuest(Ref* pSender) { }APP_NS_END

QuestMediator.hpp
上面的两个是UI展示部分也就是说能让玩家看到效果的演示而已

QuestMediator.hpp这里是UI部分的控制相当于说这里是大脑而UI是你的肢体

#include <typedef.hpp>#include <Mediator.hpp>#include <QuestUi.hpp>#include <MainMediator.hpp>#include <FortuneMediator.hpp>#include <QuestProxy.hpp>#include <stdio.h>CLIENT_NS_BEGINUSING_NS_CC;class QuestMediator :    public Mediator{public:    QuestMediator();    ~QuestMediator();    virtual voID onRegisterNotification();    virtual voID handleNotification(Notification* pNotification);    virtual voID update();    virtual std::string getMediatorname(){ return "QuestMediator"; }    voID onClickQuestInof();private:    CC_SYNTHESIZE_Readonly(QuestUi*,m_pQuestUi,QuestUi);    CC_SYNTHESIZE_Readonly(QuestProxy*,m_pQuestProxy,QuestProxy);    CC_SYNTHESIZE_Readonly(cocosWidget::CLabel*,Titlename);    voID onClickBtn(CCObject* pObject);};APP_NS_END

CPP

#include "QuestMediator.hpp"#include <Facade.hpp>#include <MultiMediator.hpp>CLIENT_NS_BEGINQuestMediator::QuestMediator() :m_pQuestUi(nullptr),m_pQuestProxy(nullptr){    REGISTER_SCENE_FUNC(QuestUi);    m_pQuestProxy = (QuestProxy*)g_facade.retrIEveProxy("QuestProxy");}QuestMediator::~QuestMediator(){}voID QuestMediator::onRegisterNotification(){    m_xFrameMessageVec.push_back(FRAME_MESSAGE_SHOW_QUEST_UI);    m_xFrameMessageVec.push_back(FRAME_MESSAGE_ENTER_QUEST_UI);    m_xFrameMessageVec.push_back(FRAME_MESSAGE_EXIT_QUEST_UI);}voID QuestMediator::handleNotification(Notification* pNotification){    if (NulL == pNotification)    {        return;    }    switch (pNotification->m_xMessage)    {    //  点击以后进入任务界面;    case FRAME_MESSAGE_SHOW_QUEST_UI:    {        m_pQuestUi = (QuestUi*)LoadScene("QuestUi");        CSceneManager::getInstance()->runUIScene(m_pQuestUi);        getQuestProxy()->clIEntQuestInfo = g_character.getQuestsqlInfovc();        //拷贝上线时候的任务;        getQuestProxy()->butsize = getQuestProxy()->clIEntQuestInfo;        vector<int>::iterator itor = getQuestProxy()->butsize.begin();        vector<int>::iterator end = getQuestProxy()->butsize.end();        for (;itor!=end; itor++)        {            QuestXmlVo* sQ = getQuestProxy()->getQuestInof(*itor);            getQuestUi()->QuestnameVe.push_back(sQ->getQuestname());        }        //广播当前任务信息;        //g_facade.sendNotification()    }        break;    case FRAME_MESSAGE_ENTER_QUEST_UI:    {        if (getQuestProxy() == nullptr)        {            return;        }        // 上线是反馈回来的长度;        m_pQuestUi->getQuestVIEw()->setCountOfCell(m_pQuestProxy->butsize.size());        getQuestUi()->getQuestVIEw()->reloadData();        getQuestUi()->getCloseQuest()->setonClickListener(this,ccw_click_selector(QuestMediator::onClickBtn));    }        break;    case  FRAME_MESSAGE_EXIT_QUEST_UI:    {        CSceneManager::getInstance()->getRunningScene()->setModalable(false);        CSceneManager::getInstance()->popUIScene(m_pQuestUi);        FortuneMediator* pFortuneMediator = (FortuneMediator*)g_facade.retrIEveMediator("FortuneMediator");        if (nullptr == pFortuneMediator)        {            return;        }        pFortuneMediator->getFortuneUi()->setModalable(false);        //  返回主场景;        MultiMediator* pMultiMediator = (MultiMediator*)g_facade.retrIEveMediator("MultiMediator");        if (nullptr == pMultiMediator)        {            return;        }        pMultiMediator->getMultiui()->setModalable(false);    }        break;    }}//  状态的更新;voID QuestMediator::update(){    /*if (NulL == pNotification)    {        return;    }    switch (pNotification->m_xMessage)    {*/        /*        //任务数据更新以后;        case FRAME_MESSAGE_CHANGE_QUEST_UI:        {        //任务数据更新以后;        if (getQuestProxy() == nullptr)        {        return;        }        // 上线是反馈回来的长度;        m_pQuestUi->getQuestVIEw()->setCountOfCell(m_pQuestProxy->butsize.size());        getQuestUi()->getQuestVIEw()->reloadData();        getQuestUi()->getCloseQuest()->setonClickListener(this,ccw_click_selector(QuestMediator::onClickBtn));        }        break;*///  }}voID QuestMediator::onClickBtn(CCObject* pObject){    auto pbutton = (cocosWidget::Cbutton*)pObject;    if (nullptr == pbutton)    {        return;    }    switch (pbutton->getTag())    {    case tagQuestTestBtn:    {        //任务ID;        auto tag = pbutton->getUserTag();        QuestProxy* pQuestProxy = (QuestProxy*)g_facade.retrIEveProxy("QuestProxy");        if (nullptr == pQuestProxy)        {            return;        }        pQuestProxy->handleQuestcommpReq(tag);    }        break;    case tagCloseQuest:    {                          cclog("tagCloseQuest");                          g_facade.sendNotification(FRAME_MESSAGE_EXIT_QUEST_UI);    }        break;    default:        break;    }}APP_NS_END

UI到服务端的通信也就是传到服务端的信息,比如我是什么任务请求提交 或者完成 或者是否有这个任务等;传到服务端那边的只能是ID
如果你加入string huo这char 这些那么你就会增加通信量减缓消息的接受速度等;

#ifndef __QUEST_PROXY_header__#define __QUEST_PROXY_header__#include <typedef.hpp>#include <Proxy.hpp>#include <QuestXmlVo.hpp>#include "Character.hpp"CLIENT_NS_BEGINenum MyEnum{};class QuestProxy : public Proxy{public:    QuestProxy();    ~QuestProxy();    virtual voID onInitilize();    virtual voID onRegisterMessage();    virtual std::string getProxyname(){ return "QuestProxy"; }    voID handleQuestcommpReq(int ID);    //接收请求结果;    voID SeverQuestAgentTokenAck(Message::Ptr &message);    // Xml信息加载;    QuestXmlVo* getQuestInof(int QuestID);    //服务端过来的信息ID容器;    typedef std::vector<i32> ClIEntQuestInfo;    ClIEntQuestInfo clIEntQuestInfo;    //将任务过来的信息ID ClIEntQusetVo 容器拷贝出来;    typedef std::vector<i32> ButSize;    ButSize butsize;    std::vector<QuestXmlVo*> XmlVo;    // 返回字典长度    int getDirLength();private:    // 加载信息;    voID loadQuestInfo();    CC_SYNTHESIZE_Readonly(Character*,m_pCharacter,Character);private:    CC_SYNTHESIZE(CCDictionary*,m_pQuestInof,QuestInof);};APP_NS_END#endif // !__QUEST_PROXY_header__

CPP

#include "QuestProxy.hpp"#include <tinyxml2/tinyxml2.h>#include "GameServer/Quest.hpp"#include <Alert.hpp>#include <Facade.hpp>#include "QuestMediator.hpp"#include <NetDelegate.hpp>CLIENT_NS_BEGINQuestProxy::QuestProxy() :m_pCharacter(nullptr){    m_pQuestInof = CCDictionary::create();    m_pQuestInof->retain();    m_pCharacter = (Character*)g_facade.retrIEveProxy("Character");}QuestProxy::~QuestProxy(){}voID QuestProxy::onInitilize(){    loadQuestInfo();// getDirLength();}voID QuestProxy::onRegisterMessage(){       DEFREG_MESSAGE(ID_GS2C_ListQuestCommitack,QuestProxy,SeverQuestAgentTokenAck)}//任务请求客户端向服务端发送消息;voID QuestProxy::handleQuestcommpReq(int ID){    Message::Ptr sendMsg(new Message());    C2GS_QuestsubmitReq questsubmitReq;    questsubmitReq.nQuestID = ID;    sendMsg->pack(questsubmitReq);    g_netDelegate.send(sendMsg);}//接收请求结果;voID QuestProxy::SeverQuestAgentTokenAck(Message::Ptr &message){    GS2C_ListQuestCommitack sListQuestCommitack;    message->unpack(sListQuestCommitack);    std::vector<int>::iterator ite = butsize.begin();    std::vector<int>::iterator ender = butsize.end();    for (; ite != ender; ite++)    {        if (*ite == sListQuestCommitack.nQuestID)        {            butsize.erase(ite);            break;        }    }        g_facade.sendNotification(FRAME_MESSAGE_ENTER_QUEST_UI);    //QuestMediator* pQuestMediator = (QuestMediator*)g_facade->retrIEveMediator(FRAME_MESSAGE_CHANGE_QUEST_UI);    //pQuestMediator->;//->setMailinfoMap(mailinfoMap); */    //g_facade.sendNotification(FRAME_MESSAGE_CHANGE_QUEST_UI);}QuestXmlVo* QuestProxy::getQuestInof(int QuestID){    DictElement* pQuestElement = nullptr;    CCDICT_FOREACH(m_pQuestInof,pQuestElement)    {        auto KeyQuestID = pQuestElement->getIntKey();        if (KeyQuestID == QuestID)        {            QuestXmlVo* QuestsInfo = (QuestXmlVo*)pQuestElement->getobject();            return QuestsInfo;        }    }    std::vector<int>::iterator ite = butsize.begin();    std::vector<int>::iterator ender = butsize.end();    for (; ite != ender; ite++)    {        QuestXmlVo* mVo = getQuestInof(*ite);        XmlVo.push_back(mVo);    }}//XML 客户端的配置读取voID QuestProxy::loadQuestInfo(){    ssize_t bufferSize = 0;    unsigned char* pBuffer = CCfileUtils::sharedfileUtils()->getfileData("QuestX.xml","r",&bufferSize);    if (NulL == pBuffer)    {         return;    }    tinyxml2::XMLdocument* pXml = new tinyxml2::XMLdocument();    pXml->Parse((const char*)pBuffer);    tinyxml2::XMLElement* pNode = pXml->RootElement()->FirstChildElement();    QuestXmlVo* pQuestXmlVo = nullptr;    while (pNode)    {        pQuestXmlVo = new QuestXmlVo();        // 任务ID;        pQuestXmlVo->setQuestID(pNode->IntAttribute("QuestID"));        // 任务名字;        pQuestXmlVo->setQuestname(pNode->Attribute("Questname"));        // 任务资源;        pQuestXmlVo->setQuestimageID(pNode->Attribute("QuestimageID"));        // 任务线;        pQuestXmlVo->setQuestGroup(pNode->IntAttribute("QuestGroup"));        // 前置任务;        pQuestXmlVo->setQuestRelate(pNode->IntAttribute("QuestRelate"));        // 任务解锁等级;        pQuestXmlVo->setQuestNlockGrade(pNode->IntAttribute("QuestNlockGrade"));        // 任务类型;        pQuestXmlVo->setQuestType(pNode->IntAttribute("QuestType"));        // 任务目标参数1;        pQuestXmlVo->setQuestTargetParam1(pNode->IntAttribute("QuestTargetParam1"));        // 任务目标参数2;        pQuestXmlVo->setQuestTargetParam2(pNode->IntAttribute("QuestTargetParam2"));        // 任务目标参数3;        pQuestXmlVo->setQuestTargetParam3(pNode->IntAttribute("QuestTargetParam3"));        // 任务加权;        pQuestXmlVo->setQuestWeight(pNode->IntAttribute("QuestWeight"));        // 任务任务描述;        pQuestXmlVo->setQuestDescription(pNode->Attribute("QuestDescription"));        // 任务金币奖励;        pQuestXmlVo->setQuestcoin(pNode->IntAttribute("QuestCoin"));        // 任务水晶奖励;        pQuestXmlVo->setQuestCrystal(pNode->Attribute("QuestCrystal"));        // 任务经验奖励;        pQuestXmlVo->setQuetsExp(pNode->IntAttribute("QuetsExp"));        // 任务其他奖励类型;        pQuestXmlVo->setQuestRewardType(pNode->Attribute("QuestRewardType"));        // 任务其他奖励;        pQuestXmlVo->setQuestReward(pNode->IntAttribute("QuestReward"));        // 任务说明;        pQuestXmlVo->setQuestRemark(pNode->Attribute("QuestRemark"));        // 任务动作;        pQuestXmlVo->setQuestACtion(pNode->Attribute("QuestACtion"));        // 任务目标;        pQuestXmlVo->setQuestDestination(pNode->IntAttribute("QuestDestination"));        pQuestXmlVo->autorelease();        m_pQuestInof->setobject(pQuestXmlVo,pQuestXmlVo->getQuestID());        pNode = pNode->NextSiblingElement();    }}APP_NS_END
总结

以上是内存溢出为你收集整理的cocos2dx游戏任务系统开发全部内容,希望文章能够帮你解决cocos2dx游戏任务系统开发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1052086.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存