Replica Island 学习笔记 05 - 关卡数据

关卡数据

Replica Island的关卡数据记录在以下两个文件里:

  • res/xml/level_tree.xml
  • res/xml/linear_level_tree.xml

其文件结构都是一样的:

  • 文件含有一个levelTree标签,levelTree标签囊括所有的关卡信息;
  • levelTree标签下包含一个或多个group标签;
  • group标签下包含一个或多个level标签;
  • level标签下不包含或者只包含一个dialog标签,level标签指定某个关卡的信息;
  • dialog标签下包含以下标签中的任何组合,每个标签最多出现一次:diary、character1、character2,dialog标签指定关卡内的NPC对话信息。

各个标签支持的属性:

标签 属性
resource time title restartable waitmessage past
levelTree            
group            
level 关卡布局数据 通关时间记录
in res/values/strings.xml:
level_0_1_time ~ level_final_boss_time
关卡标题/存档:
in res/values/strings.xml:
level_0_1_sewer ~ level_final_boss_lab
关卡是否可以重玩

默认值为true
可设置为false
关卡开始前是否显示等待对话框

默认值为false
可设置为true
inThePast

默认值为false
可设置为true
dialog            
diary 日记数据:
in res/values/strings.xml:
Diary1 ~ Diary15
         
character1 NPC对话信息          
character2 NPC对话信息          

NPC对话信息

以下文件记录了各关卡里的NPC对话信息:

xml/level_0_1_dialog_wanda.xml
xml/level_0_2_dialog_kabocha_2.xml
......
xml/level_4_9_dialog_wanda.xml
xml/level_final_boss_dialog.xml

其文件结构都是一样的:

  • 文件含有一个dialog标签,dialog标签囊括关卡内的所有NPC对话信息;
  • dialog标签下包含一个或多个conversation标签,conversation标签指定某处NPC的对话信息。有几个conversation标签,该关卡内就有几处NPC要对话。
  • conversation标签下包含一个或多个page标签,page标签指定某个NPC的一段对话。有几个page标签,该NPC就有几段话要说。

除了通关对话level_final_boss_dialog.xml,其他的对话文件,都是每个文件里只有一个NPC角色。page标签支持以下三个属性:

  • image:NPC头像,保存在res/drawable目录下:
    • res/drawable/kabocha_closeup_concern.png
    • res/drawable/kabocha_closeup_lunatic.png
    • res/drawable/kabocha_closeup_lunatic_02.png
    • res/drawable/kabocha_closeup_normal.png
    • res/drawable/kyle_closeup_angry.png
    • res/drawable/kyle_closeup_neutral.png
    • res/drawable/kyle_closeup_noglasses.png
    • res/drawable/rokudou_closeup_mask.png
    • res/drawable/rokudou_closeup_normal.png
    • res/drawable/wanda_happy.png
    • res/drawable/wanda_sad.png
    • res/drawable/wanda_smile.png
    • res/drawable/wanda_surprised.png
  • text:NPC对话字符串,字符串内容保存在以下文件里,每个NPC一个文件:
    • res/values/kabocha.xml
    • res/values/kyle.xml
    • res/values/rokudou.xml
    • res/values/wanda.xml
  • title:NPC名字,保存在res/values/strings.xml里:
    <!-- names -->
    <string name="Wanda">Wanda</string>
    <string name="Kyle">Kyle</string>
    <string name="Kabocha">Dr. Kabochanomizu</string>
    <string name="Rokudou">Mr. Rokudou</string>
    <string name="Android">Android</string>

例如如下的NPC对话情景:

XML代码 字符串资源 游戏画面

res/xml/linear_level_tree.xml

<group>
    <level
        resource="@raw/level_0_3_lab"
        time="@string/level_0_3_time"
        title="@string/level_0_3_lab" >
        <dialog>
            <character1 resource="@xml/level_0_3_dialog_kabocha" />
        </dialog>
    </level>
</group>

res/values/strings.xml

<string name="level_0_3_lab">Memory #002</string>
 

res/xml/level_0_3_dialog_kabocha.xml

<conversation>
	<page
		image="@drawable/kabocha_closeup_normal"
		text="@string/Kabocha_0_3_1_1"
		title="@string/Kabocha" />
	<page
		image="@drawable/kabocha_closeup_normal"
		text="@string/Kabocha_0_3_1_2"
		title="@string/Kabocha" />
</conversation>

res/values/strings.xml

<string name="Kabocha">Dr. Kabochanomizu</string>

res/values/kabocha.xml

<string name="Kabocha_0_3_1_1">Jolly ho, my little creation!  You are almost ready to venture out by yourself.  I need to tell you about your mission, which is of critical importance to the future of this planet.  But before that, let me show you a trick.</string>
<string name="Kabocha_0_3_1_2">First, make your way through this maze.  Make sure you pick up the red gem on the way.</string>


res/xml/level_0_3_dialog_kabocha.xml

<conversation>
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_2_1"
        title="@string/Kabocha" />
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_2_2"
        title="@string/Kabocha" />
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_2_3"
        title="@string/Kabocha" />
</conversation>

res/values/strings.xml

<string name="Kabocha">Dr. Kabochanomizu</string>

res/values/kabocha.xml

<string name="Kabocha_0_3_2_1">I’ve outfitted you with a special type of energy weapon that allows you to control machines.  While standing on the ground, <b>hold down the attack button</b> for a few seconds to charge up the <b>Possession Orb</b>.</string>
<string name="Kabocha_0_3_2_2">Once the Possession Orb is released, you can control its movement by tilting the phone.  Running it into a mechanical object will allow you to possess that object.  Pressing the attack button will destroy it and return control to your body.</string>
<string name="Kabocha_0_3_2_3">Try possessing that robot down there and using him to break through those blue blocks.  Here’s a hint: releasing a possessed robot causes it to explode.</string>



res/xml/level_0_3_dialog_kabocha.xml

<conversation>
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_3_1"
        title="@string/Kabocha" />
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_3_2"
        title="@string/Kabocha" />
    <page
        image="@drawable/kabocha_closeup_normal"
        text="@string/Kabocha_0_3_3_3"
        title="@string/Kabocha" />
</conversation>

res/values/strings.xml

<string name="Kabocha">Dr. Kabochanomizu</string>

res/values/kabocha.xml

<string name="Kabocha_0_3_3_1">Jolly good work!  You’re already asymptotically equal to my best neural net model.  </string>
<string name="Kabocha_0_3_3_2">One more thing about possession: those energy orbs require a lot of power to maintain, so you can only use them for a short time if you have not collected any gems.  Collecting gems will extend the amount of time the orb can survive before dissipating.</string>
<string name="Kabocha_0_3_3_3">Let’s practice possession a little more.  I’ll meet you up top.</string>




LevelTree.java

LevelTree负责管理所有的关卡信息,它提供方法loadLevelTree来加载所有的关卡信息:

public static final void loadLevelTree(int resource, Context context) {
    ...
}

方法get用于获取指定关卡信息:

public static final Level get(int row, int index) {
    return levels.get(row).levels.get(index);
}

方法get的参数row指定group标签的下标,index指定group标签内level标签的下标。

方法loadAllDialog用于加载所有的NPC对话信息:

public final static void loadAllDialog(Context context) {
    ...
}

方法loadLevelTree和loadAllDialog共有两处引用,且都是一同调用的:

// AndouKun.onCreate()
int levelTreeResource = R.xml.level_tree;
if (mLinearMode != 0) {
	levelTreeResource = R.xml.linear_level_tree;
}

if (!LevelTree.isLoaded(levelTreeResource)) {
	LevelTree.loadLevelTree(levelTreeResource, this);
	LevelTree.loadAllDialog(this);
}

// MainMenuActivity.onCreate()
SharedPreferences prefs =
		getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
final int row = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_ROW, 0);
final int index = prefs.getInt(PreferenceConstants.PREFERENCE_LEVEL_INDEX, 0);
int levelTreeResource = R.xml.level_tree;
if (row != 0 || index != 0) {
	final int linear = prefs.getInt(PreferenceConstants.PREFERENCE_LINEAR_MODE, 0);
	if (linear != 0) {
		levelTreeResource = R.xml.linear_level_tree;
	}
}

if (!LevelTree.isLoaded(levelTreeResource)) {
	LevelTree.loadLevelTree(levelTreeResource, this);
	LevelTree.loadAllDialog(this);
}

这两处调用都是将所有的关卡信息、NPC对话信息一次性都加载入内存。这并不是最优化的处理,其实对于NPC对话信息,应该只加载玩家当前要玩的关卡和下一关卡的对话信息即可。不知道为什么Chris Pruett没有这么做,也许因为Replica Island只是个教学产品?

关卡布局数据

关卡布局数据保存在以下文件中:

  • raw/level_0_1_sewer_kyle.bin
  • raw/level_0_1_sewer_wanda.bin
  • ......
  • raw/level_4_9_underground.bin
  • raw/level_final_boss_lab.bin

关卡布局数据通过方法LevelSystem.loadLevel()来解析。分析该方法的代码后,可以得知关卡布局数据文件的格式:

位移(字节) 长度(字节) 类型 说明
0 1 byte 关卡布局数据文件的标识,值必须为96。
1 1 byte 关卡地图的层数 layerCount
2 1 byte 关卡地图背景层的索引,下标从1开始。
3 layerSize1 1 byte 关卡地图层1 地图层类型
4 1 byte 地图砖块索引 tileIndex 作用?下标起始值?
5 4 float, little endian 地图层卷动速度 scrollSpeed 单位?
9 1 byte TiledWorld 地图层数据标识,值必须为42。
10 4 int, little endian 地图层的宽度(以砖块数量计)layerWidth
14 4 int, little endian 地图层的宽度(以砖块数量计)layerHeight
18 totalLayerTiles byte array 地图层的砖块索引数组
totalLayerTiles = layerWidth * layerHeight
  layerSize2   关卡地图层2
  ......   ......
  layerSizeN   关卡地图层N (N = layerCount)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章