cocos2dx3.2开发 RPG《Flighting》(三)从Excel表中加载需要的数据

一、前言
在一个游戏里面,需要用到的数据一般都是由游戏策划提供的(这里的策划还是由我自己担任啦哈哈)。什么是需要用到的数据?例如我创建一个角色A,A有他自己的攻击力,防御力,速度等,再创建一个角色B,B也有自己的攻击力,防御力,速度等。每个角色都有一些基础属性,但是对应不同的角色,属性的值有可能不同。我们不可能在代码里面把这些数据写死。最好的办法是从一个文件(通常是Excel表格)中读入数据,这样就方便管理和修改。
二、正文
1.Excel
《Flighting》游戏里面用到了4个Excel表格(在Resource的excel文件夹可以找到)。
HeroMessage:对应不同英雄的各个属性
MonsterMessage:对应不同怪物的各个属性
SkillMessage:对应不同技能的各个属性
StageMessage:对应不同关卡的各个属性
其中一个Excel表基本格式如下图:

格式很简单,基本上一看就知道怎么回事了。

2.下面我们以HeroMessage为例子进行接下来的操作。
我们已经做好我们需要的Excel表格,现在我们的目标是将表格的一条条的信息用一个个对象(bean)保存起来。
这里我也是同样采用了cocostuio提供的数据编辑器。大致的思路是这样的:先用cocostudio的数据编辑器将excel表转成一串JSON字符串。
再在代码里面读取并解析这串字符串,将数据信息用一个对象保存,方便管理。
第一步:数据编辑器的使用
打开cocostudio1.6 -》选择数据管理器-》文件-》导入模板表-》Excel
导入完之后
文件-》导出JSON文件-》确定
成功的话,会得到一个json文件,可以用记事本打开看看。
接下来我们作如下修改,字符串的上一行加入:
{”json“:
在字符串的下一行加入:
}
不清楚的话可以看图:修改前:

修改后:

懂json的人可能知道这是为什么,不懂也没关系,先照着做就好。

第二步:在代码中构造相应的信息保存类(bean)
HeroMessage.h

#ifndef _HEROMESSAGE_H_  
#define _HEROMESSAGE_H_  
#include   
using namespace std;  
class HeroMessage{  
public:  
    int id;//唯一id  
    string name;//角色名  
    string r_name;//骨骼动画资源名  
    string r_png;//骨骼动画png  
    string r_plist;//骨骼动画plist  
    string r_ExportJson;//骨骼动画ExportJson  
    int offset_x;//x的偏移量  
    int offset_y;//y的偏移量  
    int atk_dis;//攻击距离  
    int hp;//生命值  
    int atk;//攻击力  
    int def;//防御力  
    int speed;//移动速度  
    double atk_speed;//攻击速度  
    bool naima;//是不是牧师  
    string bullet;//子弹资源名称  
    int skill;//对应的技能id  
};  
#endif  

```

第三步:信息转换工具HeroMessageUtil
HeroMessageUtil就是负责将Json文件读取并加载相应的信息。再用HeroMessage保存
#ifndef _HMUTIL_H_  
#define _HMUTIL_H_  
#include "HeroMessage.h"  
#include "json/rapidjson.h"  
#include "json/document.h"  
#include "cocos2d.h"  
#include   
using namespace cocos2d;  
  
class HeroMessageUtil{  
public:  
    static HeroMessageUtil* getInstance();  
    HeroMessage getMessageById(int id);  
    map getHeroMessageMap();  
    HeroMessageUtil();  
    ~HeroMessageUtil();  
private:  
    void initHeroMsgMap();  
private:  
    static HeroMessageUtil* m_instance;  
    map heroMsgMap;  
};  
#endif  

```

可以看出,除了单例方法,构造函数和析构函数外,有三个函数和一个成员变量需要注意
1. map heroMsgMap;     这是一个map,键是英雄的唯一id,值是对应的信息对象
2. HeroMessage getMessageById(int id);   通过一个英雄的id,获取其全部信息
map getHeroMessageMap();
获取英雄信息map
3. void initHeroMsgMap();
初始化英雄信息类,在构造函数中会调用
HeroMessageUtil::HeroMessageUtil(){  
    initHeroMsgMap();  
}  

```


因此,我们重点关注void initHeroMsgMap()
void HeroMessageUtil::initHeroMsgMap(){  
    std::string  jsonStr = cocos2d::FileUtils::getInstance()->getStringFromFile("Json/HeroMessage.json");  
    rapidjson::Document _mDoc;  
  
    std::string mstr = jsonStr;  
    RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""));  
    _mDoc.Parse<0>(mstr.c_str());  
    RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject());  
    const rapidjson::Value &pArr = _mDoc"json"];  
  
    CCLOG("Size = %d",pArr.Capacity());  
  
    for(int i=0;i<pArr.Capacity();++i){  
        HeroMessage h;  
        const rapidjson::Value &temp = pArr*;  
        int id = temp"id"].GetInt();  
        h.id = temp"id"].GetInt();  
        h.name = temp"name"].GetString();  
        h.r_name = temp"r_name"].GetString();  
        h.r_png = temp"r_png"].GetString();  
        h.r_plist = temp"r_plist"].GetString();  
        h.r_ExportJson = temp"r_ExportJson"].GetString();  
        h.offset_x = temp"offset_x"].GetInt();  
        h.offset_y = temp"offset_y"].GetInt();  
        h.atk_dis = temp"atk_dis"].GetInt();  
        h.hp = temp"hp"].GetInt();  
        h.atk = temp"atk"].GetInt();  
        h.def = temp"def"].GetInt();  
        h.speed = temp"speed"].GetInt();  
        h.atk_speed = temp"atk_speed"].GetDouble();  
        int b = temp"naima"].GetInt();  
        h.naima = b==0?false:true;  
        h.bullet = temp"bullet_img"].GetString();  
        h.skill = temp"skill"].GetInt();  
        heroMsgMap.insert(make_pair(id,h));  
  
        CCLOG("h : %s",h.r_png.c_str());  
    }  
    CCLOG("MapSize = %d",heroMsgMap.size());  
    return;  
}  

```

这里我们用到的是rapidJson这个api,cocos2dx的附加库里面已经有的。所以我们只需要引入以下两个头文件就好
#include "json/rapidjson.h"  
#include "json/document.h"  

```


std::string  jsonStr = cocos2d::FileUtils::getInstance()->getStringFromFile("Json/HeroMessage.json");  

```

读取"Json/HeroMessage.json"这个文件,并将这个文件转成一串string
rapidjson::Document _mDoc;  

```

_mDoc是一个Document对象,可以先暂时理解为一个容器
_mDoc.Parse<0>(mstr.c_str());  

```

将从json文件读取的那串字符串转成一个Document对象
const rapidjson::Value &pArr = _mDoc"json"];  

```

读取_mDoc对应的那个json对象中,键为”json“的那个值,保存的pArr对象中(还记得刚刚对json文件的修改吗,现在得到的pArr对象其实才是真正包含重要数据的)

接下来的for循环就是用来遍历一行一行数据的。(pArr.Capacity可以获取pArr对象中又包含了多少个对象)
HeroMessage h;  
const rapidjson::Value &temp = pArr*;//当前行也是一个Value对象,用temp保存  
int id = temp"id"].GetInt();//temp"id"]表示从value对象中搜索键值为"id",并且GetInt方法转换成int  
h.id = temp"id"].GetInt();//将解析出来的int保存到我们的目的地——HeroMessage  

```

最后构造好HeroMessage对象之后,记得把它放到map里面
heroMsgMap.insert(make_pair(id,h));  

```


这样我们将excel表里面一行行的数据放到了一个map里面了。

最后贴上HeroMessageUtil两个常用的方法:
map HeroMessageUtil::getHeroMessageMap(){  
    return heroMsgMap;  
}  
  
HeroMessage HeroMessageUtil::getMessageById(int id){  
    auto it =  heroMsgMap.find(id);  
    CCASSERT(it!=heroMsgMap.end(),"can't get hero msg of the id");  
    return (*it).second;  
}  

```

本节结束。

我的csdn地址:http://blog.csdn.net/hezijian22*
邮箱地址:578690286@qq.com
如有问题或指教,欢迎与我交流,谢谢。*