cocos2dx 3D战斗类游戏制作:【一】——数据库之二

前端之家收集整理的这篇文章主要介绍了cocos2dx 3D战斗类游戏制作:【一】——数据库之二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇,做了数据库结构设计,这篇,简单说说在游戏中依靠数据库调用产生一个场景。


很简单,加入一个LoadScene(),在Cocos载入一个Scene的时候,调用LoadScene()来载入场景所需的一切即可。


  1. void GhostBox::LoadScene()
  2. {
  3. //根据scene_id查询场景数据,并初始化场景
  4. map<string,string> m;
  5. vector<map<string,string>> vect = DBHelper::GetTable("select * from scene where scene_id=1");
  6. //log("sqlite-->GetTable returns %d row",vect.size());
  7. m = vect[0];
  8. //设定场景模型
  9. if (m["scene_3dm"] != ""&&m["scene_3dt"] != ""&&m["scene_3ds"] != "")
  10. {
  11. std::string fileName = m["scene_3dm"].c_str();
  12. auto sprite = Sprite3D::create(fileName);
  13. sprite->setTexture(m["scene_3dt"].c_str());
  14. sprite->setScale(atof(m["scene_3ds"].c_str()));
  15. _layer3D->addChild(sprite);
  16. }
  17. //设定场景光照
  18. if (m["lighta_r"] != ""&&m["lighta_g"] != ""&&m["lighta_b"] != "")
  19. {
  20. //散射光,颜色
  21. _ambientLight = AmbientLight::create(Color3B(atoi(m["lighta_r"].c_str()),atoi(m["lighta_g"].c_str()),atoi(m["lighta_b"].c_str())));
  22. _ambientLight->retain();
  23. _ambientLight->setEnabled(true);
  24. addChild(_ambientLight);
  25. _ambientLight->setCameraMask(2);
  26. }
  27. if (m["lightd_r"] != ""&&m["lightd_g"] != ""&&m["lightd_b"] != "")
  28. {
  29. //方向光源,方向/颜色
  30. _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())));
  31. _directionalLight->retain();
  32. _directionalLight->setEnabled(true);
  33. addChild(_directionalLight);
  34. _directionalLight->setCameraMask(2);
  35. }
  36. //背景音乐
  37. if (m["scene_bgm"] != ""){
  38. // preload background music and effect
  39. //SimpleAudioEngine::getInstance()->preloadBackgroundMusic(m["scene_bgm"].c_str());
  40. //SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.1);
  41. //SimpleAudioEngine::getInstance()->playBackgroundMusic(m["scene_bgm"].c_str(),true);
  42. }
  43. }


在这里,使用的数据库sqlite,数据库查询用到网上随处可见的DBHelper类。自己去下载吧。


对应上面的代码,贴一下Scene数据库相关的记录,一目了然,呵呵




LoadScene之外,当然还要LoadBB,LoadBoss,Load一堆这个场景需要的东西,方法掌握了,其实也就很简单了,从数据库去查,在程序里面画出来就是了呗,对吧。



也许有人说——为什么要用数据库呢?

当然,你可以把一个游戏的数据全部写死在程序里面,但是那样,当你要增加更多的关卡、boss、资源。。。。。。。的时候,你就要改程序。

而用数据库的实现,当你需要增加一个关卡的时候,你需要做的,仅仅是在scene里面增加一条记录,说明一下这个关卡,以及这个关卡需要用到的boss、资源等等信息,要升级游戏,开发公司甚至都不用干活,把添加数据的接口给运营公司,运营公司就可以一直配置数据,把游戏不断地丰富内容运营下去。

从长远来看,程序和数据的分离,带来的好处,不需要再多说什么吧?呵呵

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