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

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


很简单,加入一个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、资源等等信息,要升级游戏,开发公司甚至都不用干活,把添加数据的接口给运营公司,运营公司就可以一直配置数据,把游戏不断地丰富内容运营下去。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值