# 弓箭戰遊戲資料包 v1.0.0
## 遊戲概述
玩家分紅藍兩隊,出生於一高空平台上,玩家用弓箭將敵方玩家擊落來獲得分數,達到指定分數的隊伍獲勝
## 資料包內容
### 資料包結構
```
arrow_fight_datapack/
├── pack.mcmeta
├── pack.png
└── data/
├── minecraft/
│ └── tags/
│ └── function/
│ └── tick.json
└── death_exp/
└── function/
├── start_game.mcfunction
├── init.mcfunction
├── tick.mcfunction
├── player_death.mcfunction
├── give_rewards_red.mcfunction
├── give_reward_blue.mcfunction
├── check_victor.mcfunction
├── red_victor.mcfunction
├── blue_victor.mcfunction
├── red.mcfunction
└── blue.mcfunction
```
### tick.json
```json
{
"values": [
"arrow_fight:tick"
]
}
```
- 遊戲每刻執行的功能
### init.mcfunction
```mcfunction=
scoreboard objectives add deaths deathCount
scoreboard objectives add game_state dummy
scoreboard players set game game_state 0
spawnpoint @a 16 95 60
tp @a 16.00 95.00 60.00
tag @a remove red
tag @a remove blue
xp set @a 0 levels
gamemode adventure @a
```
- 初始化遊戲
### start_game.mcfunction
```mcfunction=
tp @a[tag=red] 34.50 91 45.50
tp @a[tag=blue] -2.50 91 34.50
spawnpoint @a[tag=red] 34 91 45
spawnpoint @a[tag=blue] -2 91 34
xp set @a 0 levels
gamemode adventure @a[tag=red]
gamemode adventure @a[tag=blue]
clear @a[tag=red]
clear @a[tag=blue]
give @a[tag=red] minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}] 1
give @a[tag=blue] minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}] 1
give @a[tag=red] minecraft:arrow 1
give @a[tag=blue] minecraft:arrow 1
scoreboard players set game game_state 1
tellraw @a {"text":"遊戲開始!","color":"green"}
```
- 移動玩家到遊戲場地,設定玩家狀態,給予武器,然後開始遊戲
### tick.mcfunction
```mcfunction=
execute if score game game_state matches 1 run function arrow_fight:player_death
```
- 如果遊戲正在進行中,運行`player_death.mcfunction`
### player_death.mcfunction
```mcfunction=
execute if score game game_state matches 1 run execute as @a[tag=red, scores={deaths=1..}] run function arrow_fight:give_rewards_blue
execute if score game game_state matches 1 run execute as @a[tag=blue, scores={deaths=1..}] run function arrow_fight:give_rewards_red
scoreboard players set @a[scores={deaths=1..}] deaths 0
function arrow_fight:check_victor
```
- 偵測玩家死亡,並觸發獎勵機制,最後在觸發`check_victor.mcfunction`
### give_rewards_red.mcfunction
```mcfunction=
xp add @a[tag=red] 1 levels
give @a[tag=red] minecraft:decorated_pot 1
function arrow_fight:check_victory
```
- 紅隊獎勵
### give_reward_blue.mcfunction
```mcfunction=
xp add @a[tag=blue] 1 levels
give @a[tag=blue] minecraft:decorated_pot 1
function arrow_fight:check_victory
```
- 藍隊獎勵
### check_victor.mcfunction
```mcfunction=
execute as @a[tag=red,limit=1,level=1..] run function arrow_fight:red_victory
execute as @a[tag=blue,limit=1,level=1..] run function arrow_fight:blue_victory
```
- 檢查是否有隊伍獲勝,若有則觸發獲勝
### red_victor.mcfunction
```mcfunction=
tellraw @a "紅隊獲勝!"
function arrow_fight:init
```
- 宣布紅隊獲勝,並初始化遊戲
### blue_victor.mcfunction
```mcfunction=
tellraw @a "藍隊獲勝!"
function arrow_fight:init
```
- 宣布藍隊獲勝,並初始化遊戲
### red.mcfunction
```mcfunction=
tag @s remove blue
tag @s add red
say 加入紅隊
```
- 分隊系統,寫於遊戲世界中指令方塊
### blue.mcfunction
```mcfunction=
tag @s remove red
tag @s add blue
say 加入藍隊
```
- 分隊系統,寫於遊戲世界中指令方塊
## 指令碼解釋
### init.mcfunction
```mcfunction=
scoreboard objectives add deaths deathCount
scoreboard objectives add game_state dummy
scoreboard players set game game_state 0
spawnpoint @a 16 95 60
tp @a 16.00 95.00 60.00
tag @a remove red
tag @a remove blue
xp set @a 0 levels
gamemode adventure @a
```
#### 第一行
```mcfunction
scoreboard objectives add deaths deathCount
```
- 功能:
建立一個與玩家死亡相關的記分板`deaths`
- 語法結構:
```mcfunction
scoreboard objectives <動作> <記分板名稱> <類型> [顯示名稱]
```
- 類型(`deathCount`):指定此計分板為由系統根據系統記分板`deathCount`自動更新
- 顯示名稱(可選):可為此計分板設定玩家可見的名稱(不設則默認使用原名稱),格式須為json格式(例如`["ABC"]`,名稱為ABC)
#### 第二行
```mcfunction
scoreboard objectives add game_state dummy
```
- 功能:
建立一個負責表示遊戲狀態的記分板`game_state`
- 語法結構:
與**第一行**相同,差別在於
- 類型(`dummy`):指定此計分板為手動更新
#### 第四行
```mcfunction
scoreboard players set game game_state 0
```
- 功能:
在`game_state`加入一位虛擬玩家`game`並將該玩家的值設為`0`
- 語法結構:
與**第二行**大致相同,差別在於將**建立(`add`)改為設定(`set`)以及需要新增一位虛擬玩家**
#### 第六行
```mcfunction
spawnpoint @a 16 95 60
```
- 功能:
設定所有玩家的重生點
- 語法結構:
```mcfunction
spawnpoint <目標> <座標>
```
- 目標(`@a`):指定是所有玩家
- 座標(`16 95 60`):要設定的座標
#### 第七行
```mcfunction
tp @a 16.00 95.00 60.00
```
- 功能:
移動玩家到指定位置
- 語法結構:
與第六行類似
#### 第九和第十行
```mcfunction
tag @a remove red
tag @a remove blue
```
- 功能:
移除所有玩家身上的標籤
- 語法結構:
```mcfunction
tag <目標> <動作> <標籤>
```
#### 第十二行
```mcfunction
xp set @a 0 levels
```
- 功能:
將所有玩家的經驗等級設為`0`
- 語法結構:
```mcfunction
xp <動作> <目標> <數值> <類型>
```
#### 第十四行
```mcfunction
gamemode adventure @a
```
- 功能:
將所有玩家的遊戲模式切換成冒險模式
- 語法結構:
```mcfunction
gamemode <遊戲模式> <目標>
```
### start_game.mcfunction
```mcfunction=
tp @a[tag=red] 34.50 91 45.50
tp @a[tag=blue] -2.50 91 34.50
spawnpoint @a[tag=red] 34 91 45
spawnpoint @a[tag=blue] -2 91 34
xp set @a 0 levels
gamemode adventure @a[tag=red]
gamemode adventure @a[tag=blue]
clear @a[tag=red]
clear @a[tag=blue]
give @a[tag=red] minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}] 1
give @a[tag=blue] minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}] 1
give @a[tag=red] minecraft:arrow 1
give @a[tag=blue] minecraft:arrow 1
scoreboard players set game game_state 1
tellraw @a {"text":"遊戲開始!","color":"green"}
```
#### 目標篩選
```mcfnction
@a[tag=red]
@a[tag=blue]
```
- 功能:
從所有玩家中選出有特定標籤的玩家
#### 第十五和第十六行,特殊物品給予
```mcfunction
minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}]
```
- 功能:
給予玩家無限耐久且有附魔的弓
- 語法結構:
```mcfunction
minecraft:bow[unbreakable={show_in_tooltip:true},enchantments={levels:{'minecraft:punch':1,'minecraft:infinity':1,'minecraft:power':1},show_in_tooltip:true}]
<物品ID>[<特性>={屬性:參數}]
```
#### 第二十二行
```mcfunction
tellraw @a {"text":"遊戲開始!","color":"green"}
```
- 功能:
向所有玩家宣布遊戲已開始
- 語法結構:
```mcfunction
tellraw <目標> <訊息 JSON>
```
- 目標(`@a`):傳送訊息給的玩家,這裡是所有玩家
- 訊息 JSON:訊息的內容與格式,使用 JSON 格式來定制顯示效果
- `text`:訊息文字
- `color`:文字顏色
### tick.mcfunction
```mcfunction=
execute if score game game_state matches 1 run function arrow_fight:player_death
```
- 功能:
根據`game_state`的數值條件來執行`player_death.mcfunction`
- 語法結構:
```mcfunction
execute if score <目標玩家> <記分板> <條件> run <函數>
```
- 目標玩家(`game`):就是我們前面創建的虛擬玩家
- 記分板(`game_state`):儲存該玩家分數的記分板
- 條件(`matches 1`):要判斷的條件
- 函數(`arrow_fight:player_death`):條件成立時要執行的函數
### player_death.mcfunction
```mcfunction=
execute if score game game_state matches 1 run execute as @a[tag=red, scores={deaths=1..}] run function arrow_fight:give_rewards_blue
execute if score game game_state matches 1 run execute as @a[tag=blue, scores={deaths=1..}] run function arrow_fight:give_rewards_red
scoreboard players set @a[scores={deaths=1..}] deaths 0
function arrow_fight:check_victor
```
#### 目標篩選
```mcfunction
@a[scores={deaths=1..}]
```
- 功能:
找出所有玩家中死亡次數大於等於`1`的玩家
### give_rewards_red.mcfunction
```mcfunction=
xp add @a[tag=red] 1 levels
give @a[tag=red] minecraft:decorated_pot 1
function arrow_fight:check_victory
```
#### 第一行
```mcfunctuon
xp add @a[tag=red] 1 levels
```
- 功能:
給予玩家等級
### give_reward_blue.mcfunction
```mcfunction=
xp add @a[tag=blue] 1 levels
give @a[tag=blue] minecraft:decorated_pot 1
function arrow_fight:check_victory
```
### check_victor.mcfunction
```mcfunction=
execute as @a[tag=red,limit=1,level=1..] run function arrow_fight:red_victory
execute as @a[tag=blue,limit=1,level=1..] run function arrow_fight:blue_victory
```
#### 目標篩選,但只選一個
```mcfunction
@a[tag=red,limit=1,level=1..]
```
- 功能:
找出所有府和條件的玩家,但因為`limit=1`的關係只能選擇一位玩家
### red_victor.mcfunction
```mcfunction=
tellraw @a "紅隊獲勝!"
function arrow_fight:init
```
### blue_victor.mcfunction
```mcfunction=
tellraw @a "藍隊獲勝!"
function arrow_fight:init
```
### red.mcfunction
```mcfunction=
tag @s remove blue
tag @s add red
say 加入紅隊
```
### blue.mcfunction
```mcfunction=
tag @s remove red
tag @s add blue
say 加入藍隊
```
## [回到主頁](https://hackmd.io/@Huanyu763/home)