
上一篇,做了数据库结构设计,这篇,简单说说在游戏中依靠数据库,调用产生一个场景。
很简单,加入一个LoadScene(),在Cocos载入一个Scene的时候,调用LoadScene()来载入场景所需的一切即可。
voID GhostBox::LoadScene(){ //根据scene_ID查询场景数据,并初始化场景 map<string,string> m; vector<map<string,string>> vect = DBHelper::Gettable("select * from scene where scene_ID=1"); //log("sqlite-->Gettable returns %d row",vect.size()); m = vect[0]; //设定场景模型 if (m["scene_3dm"] != ""&&m["scene_3dt"] != ""&&m["scene_3ds"] != "") { std::string filename = m["scene_3dm"].c_str(); auto sprite = Sprite3D::create(filename); sprite->setTexture(m["scene_3dt"].c_str()); sprite->setScale(atof(m["scene_3ds"].c_str())); _layer3D->addChild(sprite); } //设定场景光照 if (m["lighta_r"] != ""&&m["lighta_g"] != ""&&m["lighta_b"] != "") { //散射光,颜色 _ambIEntlight = AmbIEntlight::create(color3B(atoi(m["lighta_r"].c_str()),atoi(m["lighta_g"].c_str()),atoi(m["lighta_b"].c_str()))); _ambIEntlight->retain(); _ambIEntlight->setEnabled(true); addChild(_ambIEntlight); _ambIEntlight->setCameraMask(2); } if (m["lightd_r"] != ""&&m["lightd_g"] != ""&&m["lightd_b"] != "") { //方向光源,方向/颜色 _directionallight = Directionlight::create(Vec3(0.0f,0.0f,-1.0f),color3B(atoi(m["lightd_r"].c_str()),atoi(m["lightd_g"].c_str()),atoi(m["lightd_b"].c_str()))); _directionallight->retain(); _directionallight->setEnabled(true); addChild(_directionallight); _directionallight->setCameraMask(2); } //背景音乐 if (m["scene_bgm"] != ""){ // preload background music and effect //SimpleAudioEngine::getInstance()->preloadBackgroundMusic(m["scene_bgm"].c_str()); //SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.1); //SimpleAudioEngine::getInstance()->playBackgroundMusic(m["scene_bgm"].c_str(),true); }}
在这里,使用的数据库是sqlite,数据库的查询用到网上随处可见的DBHelper类。自己去下载吧。
对应上面的代码,贴一下Scene数据库相关的记录,一目了然,呵呵
LoadScene之外,当然还要LoadBB,LoadBoss,Load一堆这个场景需要的东西,方法掌握了,其实也就很简单了,从数据库去查,在程序里面画出来就是了呗,对吧。
也许有人说——为什么要用数据库呢?
当然,你可以把一个游戏的数据全部写死在程序里面,但是那样,当你要增加更多的关卡、boss、资源。。。。。。。的时候,你就要改程序。而用数据库的实现,当你需要增加一个关卡的时候,你需要做的,仅仅是在scene里面增加一条记录,说明一下这个关卡,以及这个关卡需要用到的boss、资源等等信息,要升级游戏,开发公司甚至都不用干活,把添加数据的接口给运营公司,运营公司就可以一直配置数据,把游戏不断地丰富内容运营下去。
从长远来看,程序和数据的分离,带来的好处,不需要再多说什么吧?呵呵 总结
以上是内存溢出为你收集整理的cocos2dx 3D战斗类游戏制作:【一】——数据库之二全部内容,希望文章能够帮你解决cocos2dx 3D战斗类游戏制作:【一】——数据库之二所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)