教學:製作附加包/實體

出自Minecraft基岩版开发Wiki
實體
系列教學
所屬系列
難易度
中級
前置知識
適用版本
國際版
實踐裝置
WindowsAndroidIOSMacOS
所需軟體

引言[編輯]

與物品不同,實體的自訂要由兩部分完成:

  • 伺服器端(紋理、名稱、動畫、聲音)
  • 用戶端(行為、屬性)

除此之外,我們還需要準備實體的模型及動畫,關於這部分內容後面的章節將詳細講述[1]

加入實體[編輯]

伺服器端[編輯]

實體的伺服器端檔案與物品非常相似,我們只需要在行為包/entities資料夾下新建一個名為custom_entity.json的檔案並插入以下內容即可:

{
	"format_version": "1.16.0",
	"minecraft:entity": {
		"description": {
			"identifier": "wiki:custom_entity",
			"is_summonable": true,
			"is_spawnable": true,
			"is_experimental": false
		},
		"components": {
			"minecraft:type_family": {
				"family": ["monster"]
			},
			"minecraft:health": {
				"value": 20,
				"max": 20
			},
			"minecraft:attack": {
				"damage": 3
			},
			"minecraft:movement": {
				"value": 0.2
			},
			"minecraft:collision_box": {
				"width": 1,
				"height": 2
			},
			"minecraft:loot": {
				"table": "loot_tables/entities/custom_entity.json"
			},

			"minecraft:movement.basic": {},
			"minecraft:navigation.walk": {
				"can_walk": true,
				"avoid_sun": true,
				"can_pass_doors": true,
				"can_open_doors": true
			},

			"minecraft:behavior.random_stroll": {
				"priority": 6,
				"speed_multiplier": 1
			},
			"minecraft:behavior.random_look_around": {
				"priority": 7
			},
			"minecraft:behavior.look_at_player": {
				"priority": 7,
				"look_distance": 6,
				"probability": 0.02
			},
			"minecraft:behavior.hurt_by_target": {
				"priority": 1
			},
			"minecraft:behavior.nearest_attackable_target": {
				"priority": 2,
				"within_radius": 25,
				"reselect_targets": true,
				"entity_types": [
					{
						"filters": {
							"any_of": [
								{
									"test": "is_family",
									"subject": "other",
									"value": "player"
								}
							]
						},
						"max_dist": 35
					}
				]
			},
			"minecraft:behavior.delayed_attack": {
				"priority": 0,
				"attack_once": false,
				"track_target": true,
				"require_complete_path": false,
				"random_stop_interval": 0,
				"reach_multiplier": 1.5,
				"speed_multiplier": 1,
				"attack_duration": 0.75,
				"hit_delay_pct": 0.5
			}
		}
	}
}

實體描述[編輯]

"description": {
	"identifier": "wiki:custom_entity",
	"is_summonable": true,
	"is_spawnable": true,
	"is_experimental": false
}

描述儲存了此實體的最基礎的一些屬性:

  • identifier:實體的賦命名空間識別碼,此處不再贅述
  • is_summonable:實體是否可以使用/summon指令召喚
  • is_spawnable:實體是否可以使用生怪蛋或生成規則在世界中生成
  • is_experimental:實體實體是否只能加入到開啟了實驗性玩法的世界

實體元件[編輯]

元件定義了實體更高級的屬性,其包含基礎元件、AI意向、觸發器等多種型別,關於元件的更多資訊,參見Manual:資料驅動/實體

基礎元件[編輯]
"minecraft:type_family": {
	"family": ["monster"]
},
"minecraft:health": {
	"value": 20,
	"max": 20
},
"minecraft:attack": {
	"damage": 3
},
"minecraft:movement": {
	"value": 0.2
},
"minecraft:collision_box": {
	"width": 1,
	"height": 2
},
"minecraft:loot": {
	"table": "loot_tables/entities/custom_entity.json"
}

基礎元件設定實體的生命值、移動速度、戰利品、碰撞箱等屬性,在上面的範例中:

運動機制[編輯]
"minecraft:physics": {},
"minecraft:jump.static": {},
"minecraft:movement.basic": {},
"minecraft:navigation.walk": {
	"can_walk": true,
	"avoid_sun": true,
	"can_pass_doors": true,
	"can_open_doors": true
}

運動機制由一些特殊的元件定義:

  • minecraft:physics將重力和碰撞應用於實體
  • minecraft:jump.static使實體可以跳躍
  • minecraft:movement.basic允許實體在方塊行走
  • minecraft:navigation.walk定義了實體遵循的路徑
AI意向[編輯]
"minecraft:behavior.random_stroll": {
	"priority": 6,
	"speed_multiplier": 1
},
"minecraft:behavior.random_look_around": {
	"priority": 7
},
"minecraft:behavior.look_at_player": {
	"priority": 7,
	"look_distance": 6,
	"probability": 0.02
},
"minecraft:behavior.hurt_by_target": {
	"priority": 1
},
"minecraft:behavior.nearest_attackable_target": {
	"priority": 2,
	"within_radius": 25,
	"reselect_targets": true,
	"entity_types": [
		{
			"filters": {
				"any_of": [
					{
						"test": "is_family",
						"subject": "other",
						"value": "player"
					}
				]
			},
			"max_dist": 35
		}
	]
},
"minecraft:behavior.delayed_attack": {
	"priority": 0,
	"attack_once": false,
	"track_target": true,
	"require_complete_path": false,
	"random_stop_interval": 0,
	"reach_multiplier": 1.5,
	"speed_multiplier": 1,
	"attack_duration": 0.75,
	"hit_delay_pct": 0.5
}

AI意向定義了實體將在何時做何事。

所有AI意向都包含一個名為priority(優先級)的欄位,其將會確定在可以在何時執行意向。

當實體選擇要執行的意向時,它會從最低優先級到最高優先級搜尋並排列其所有行為,並執行選擇選中的意向,因此,建議將一些重要的意向的優先級設定為01[1]

至此,實體的伺服器端檔案便設定好了,接下來我們將學習實體的用戶端檔案。

這時如果開啟世界並嘗試使用指令召喚實體,它的行為應該像我們預期的那樣。但地面上只會有一個影子,且實體名稱是一個在地化鍵名,這是因為我們還沒有設定用戶端檔案

實體的用戶端檔案與物品或實體伺服器端檔案區別較大,且包含動畫、動畫控制器、繪製控制器等多部分,下節我們就將學習實體的用戶端組態。

用戶端[編輯]

模型[編輯]

{
	"format_version": "1.12.0",
	"minecraft:geometry": [
		{
			"description": {
				"identifier": "geometry.custom_entity",
				"texture_width": 16,
				"texture_height": 16,
				"visible_bounds_width": 3,
				"visible_bounds_height": 3,
				"visible_bounds_offset": [0, 0.5, 0]
			},
			"bones": [
				{
					"name": "bb_main",
					"pivot": [0, 0, 0],
					"cubes": [
						{"origin": [-8, 0, -8], "size": [16, 16, 16], "uv": [-14, -14]}
					]
				}
			]
		}
	]
}

模型,又稱幾何,決定了實體的形狀。實體的模型按照JSON格式儲存在資源包/models/entity資料夾中,得益於Blockbench等工具,我們不必詳細學習其語法。

上面的程式碼範例就是在Blockbench內自動生成的一個類似於原版史萊姆的模型,請注意,此範例不可以直接拿來使用。

我們唯一需要注意的是字串identifier,這是模型的賦命名空間識別碼,一般格式為geometry.<模型名稱>,我們後面需要利用它來呼叫模型。

紋理[編輯]

紋理,決定了實體的外觀,實體的紋理一般是.png檔案。為了保證模型與紋理完美契合,我們通常會在Blockbench中生成一個模板紋理,然後對這個模板紋理進行繪製,繪製完成後我們將其放入資源包/textures/entity中即可。

動畫[編輯]

{
	"format_version": "1.8.0",
	"animations": {
		"animation.custom_entity.move": {},
		"animation.custom_entity.idle": {}
	}
}

動畫讓實體更加栩栩如生,我們可以根據需要為實體提供任意數量的動畫。

在上面的範例中,animation.custom_entity.move是動畫的識別碼。除此之外,筆者省去了動畫的執行過程而只保留動畫的識別碼,這在實際中是不允許的。

與模型類似,動畫也是在Blockbench中自動生成的,因此我們不必學習其語法。

我們將動畫檔案放入資源包/animations資料夾中,但此時我們還不能正常觸發它,因為這是是動畫控制器的職責。

動畫控制器[編輯]

{
    "format_version": "1.12.0",
    "animation_controllers": {
        "controller.animation.custom_entity.walk": {
            "initial_state": "standing",
            "states": {
                "standing": {
                    "blend_transition": 0.2,
                    "animations": [
                        "idle"
                    ],
                    "transitions": [
                        {
                            "moving": "q.modified_move_speed > 0.1"
                        }
                    ]
                },
                "moving": {
                    "blend_transition": 0.2,
                    "animations": [
                        "move"
                    ],
                    "transitions": [
                        {
                            "standing": "q.modified_move_speed < 0.1"
                        }
                    ]
                }
            }
        }
    }
}

動畫控制器控制動畫的播放方式,其由狀態和狀態之間的過渡組成。這使我們能夠在實體處於某些狀態時播放某些動畫,當滿足某些條件時,我們可以在它們之間轉換。

"standing": {
                    "blend_transition": 0.2,
                    "animations": [
                        "idle"
                    ],
                    "transitions": [
                        {
                            "moving": "q.modified_move_speed > 0.1"
                        }
                    ]
                },
                "moving": {
                    "blend_transition": 0.2,
                    "animations": [
                        "move"
                    ],
                    "transitions": [
                        {
                            "standing": "q.modified_move_speed < 0.1"
                        }
                    ]
                }

可以看到,我們設定了兩個狀態:standing(站立)和moving(移動)。

     "transitions": [
                        {
                            "standing": "q.modified_move_speed < 0.1"
                        }
                    ]

透過設定transitions,我們可以讓動畫的狀態相互轉換。其中前一個值[注 1]是將要轉換的狀態名稱,後一個值[注 2]是透過Molang設定的轉換的條件。

現在動畫控制器編寫完成,我們將其放到资源包/animation_controllers中即可。

繪製控制器[編輯]

{
	"format_version": "1.10.0",
	"render_controllers": {
		"controller.render.custom_entity": {
			"geometry": "geometry.default",
			"materials": [
				{
					"*": "material.default"
				}
			],
			"textures": ["texture.default"]
		}
	}
}

繪製控制器幫助開發人員控制生物如何繪製在遊戲世界中的表現,其儲存在資源包/render_controllers資料夾中,與動畫控制器同為自訂實體最難的兩個部分。

原版實體中,不是所有生物都只有單一表現。例如村民根據群系擁有不同外觀,熱帶魚擁有數千種組合,狼在生氣時會紅眼等,這些都是由繪製控制器所控制。[2]

作為初學者,我們無需製作太複雜的繪製控制器,上面的範例即可滿足我們的需求,其將從實體用戶端檔案中取得並採用預設材質、紋理、模型。

用戶端實體[編輯]

{
	"format_version": "1.10.0",
	"minecraft:client_entity": {
		"description": {
			"identifier": "wiki:custom_entity",
			"materials": {
				"default": "entity_alphatest"
			},
			"textures": {
				"default": "textures/entity/custom_entity"
			},
			"geometry": {
				"default": "geometry.custom_entity"
			},
			"scripts": {
				"animate": ["walk_controller"]
			},
			"animations": {
				"walk_controller": "controller.animation.custom_entity.walk",
				"idle": "animation.custom_entity.idle",
				"move": "animation.custom_entity.move"
			},
			"spawn_egg": {
				"overlay_color": "#114514",
				"base_color": "#9f2333"
			},
			"render_controllers": ["controller.render.custom_entity"]
		}
	}
}

用戶端實體將動畫、紋理、模型整合到一起,儲存在资源包/entity資料夾下,其中:

  • identifier:實體的賦命名空間識別碼,值應與伺服器端檔案保持一致
  • materials:材質型別,這裡選用entity_alphatest以支援透明紋理
  • geometry:實體模型,填寫模型的賦命名空間識別碼
  • textures:實體紋理,填寫紋理路徑
動畫短名稱[編輯]
"animations": {
	"walk_controller": "controller.animation.custom_entity.walk",,
	"idle": "animation.custom_entity.idle",
	"move": "animation.custom_entity.move"
}

動畫控制器或動畫想要在實體中被引用就必須在用戶端檔案中的animations物件定義短名稱,其中前一個值[注 3]是短名稱,後一個值[注 4]是動畫的賦命名空間識別碼。

腳本[編輯]
"scripts": {
	"animate": [
		"walk_controller"
	]
}

腳本決定實體在哪些特定時間執行哪些某些操作,其中animate陣列將在每一刻執行陣列內的動畫或動畫控制器。

現在,我們的動畫應該可以正常工作了。

生怪蛋[編輯]
"spawn_egg": {
	"overlay_color": "#114514",
	"base_color": "#9f2333"
}
"spawn_egg": {
	"texture": "<物品纹理短名称>"
}

spawn_egg物件將為實體自動生成一個生怪蛋,其有兩種寫法:

  • 使用十六進位RGB程式碼為生怪蛋自動著色
  • 使用物品紋理短名稱自訂生怪蛋紋理

在地化檔案[編輯]

我們的實體還沒有自己的名稱,我們只需要進入或建立資源包/texts/zh_CN.lang檔案,插入以下內容:

entity.wiki:custom_entity.name=自定义实体

item.spawn_egg.entity.wiki:custom_entity.name=自定义实体

大功告成!現在你的實體已經可以正常顯示在Minecraft中了,恭喜!

注釋[編輯]

  1. 這裡是standing
  2. 這裡是q.modified_move_speed < 0.1
  3. 在這裡是move
  4. 在這裡是animation.custom_entity.move

參考[編輯]

< 戰利品表 | 動畫與繪製 >