Translation notice
This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.
How to use the latest and hottest MC features to make vanilla chess (Part 2)
Is the title a little different?
Yes, because we have also made Chinese chess. You can watch this video: https://www.bilibili.com/video/BV1EhFWzJE9G
Preliminary summary
In the last video, we explained in detail how to spell out the patterns and words, and made the model part and operation interface part of chess. However, currently there is no response when we click on this dialog. Therefore, we need to add click events to these fonts to make the interface operable. How to correctly select and place the chess pieces by setting the click command and parsing the input results is what this tutorial will discuss.
Part0: Analyze the operation process
Before we officially start, we need to consider what the player's operation process is like: Because there are no rules for moving pieces, we only need to consider the player selecting and placing the pieces. Therefore, we need to maintain a selected state, and then respond differently to click events depending on whether it has a selected state:
- When no piece is selected:
- If you click on a space on the chessboard or a space in the capturing area, no operation will be performed;
- If you click on the square of the chess piece on the board/capture area, the chess piece will be selected.
- If a chess piece is selected:
- If you click on a space on the chessboard, move the chess piece there;
- If you click on the capture area, move the chess pieces off the board;
- If you click on itself, cancel the selection;
- If you click on your own chess piece, switch to select that chess piece;
- If you click on the enemy chess piece, you will capture the piece (move the chess piece and remove the enemy chess piece from the board).
Next, just follow this idea and design the logical system of the chessboard.
About binding
There is still an unresolved problem here, which is how to determine which chessboard the player is operating? In fact, the DC engine comes with its own uid system. When the player clicks on the chessboard, the uid of the chessboard can be bound to the player'spc_chess_bindOn the scoreboard, when searching for an entity, you can use this binding relationship to search in both directions. When exiting the dialog interface, monitoring will also be performed to unbind the player from the chessboard.
Part1: Monitor click operations
Fonts can be setclick_event, then the player can execute a command when clicking the font. However, due to Mojang's little ingenuity For security considerations, any command with permissions greater than or equal to 1 will pop up a secondary confirmation pop-up window here, which is disastrous for the player's interactive experience. Therefore, we have to use the only operable 0-privilege directive: trigger.
About trigger instructions
Data pack developers who have been involved since 1.13 or even in the 1.20 era may not be familiar with this ancient command. Specifically, it allows a player without permission to modify the value of a specific scoreboard a limited number of times through this command (the scoreboard needs to be a trigger criterion). For example, an administrator can executescoreboard players enable CR_019 Foo, so playerCR_019Just execute it oncetrigger foo set 1ortrigger foo add 1Come and modify yourself oncefooScores on this scoreboard. If you want to modify it later, you need to execute the enable command above again. Here, we need to allow the player to modify the value of the corresponding points item at any time, so we use high-frequency function to enable permissions for this scoreboard for all players. No further details will be given later.
Through the trigger instruction, we can pass an integer to the scoreboard. When it changes, we know that the player clicked.
Introducing the concept: click value
Since almost all subsequent click events use trigger to set the scoreboard, for the convenience of explanation, we will call the set value click value. Setting the click value means setting the click event of this character totrigger pc_chess_trigger set <点击值>, when the high-frequency function here detects a player click, it can obtain this value and analyze its meaning.
data/chess/function/tick.mcfunction:
scoreboard players enable @a pc_chess_trigger
#点击操作
execute as @a at @s if score @s pc_chess_trigger matches 1.. run function chess:dialog/_operation/click
execute as @a at @s if score @s pc_chess_trigger matches ..-1 run function chess:dialog/_operation/option
scoreboard players set @a pc_chess_trigger 0We use a high-frequency function to detect changes in this value, and then handle the subsequent logic in the click function.
data/chess/function/dialog/_operation/click.mcfunction
#点击事件
tag @s add pc_chess_player_temp
execute as @e[type=marker,tag=dc_pivot,distance=..10] if score @s dc_uid = @n[type=player,tag=pc_chess_player_temp] pc_chess_bind run function chess:dialog/_operation/click_
tag @s remove pc_chess_player_tempdata/chess/function/dialog/operation/click.mcfunction
#检查状态
execute if data entity @s data.chessboard.select run return run function chess:dialog/_operation/place
execute unless data entity @s data.chessboard.select run return run function chess:dialog/_operation/selectHere we check whether the chessboard data hasselecttag, if present, means that the chessboard is selected. In the selected state, the placement-related functions are executed, otherwise the selection-related functions are executed.
TIP
The structure of select is as follows:
id: represents the chess piece ID, which is consistent with the key in the chess game structure;
color: indicates the color of the chess pieces (black/white)
type: indicates whether the selected chess piece is on the chessboard or in the capturing area
x:The xcoordinate of the chess piece, if it is in the capture area, it is 0;
y:The ycoordinate of the chess piece, if it is in the capture area, it is 0;
Part2: Select operation
- When no piece is selected:
- If you click on a space on the chessboard or a space in the capturing area, no operation will be performed;
- If you click on the square of the chess piece on the board/capture area, the chess piece will be selected.
data/chess/function/dialog/_operation/select.mcfunction
#选择格子
scoreboard players operation @s pc_chess_position = @n[tag=pc_chess_player_temp] pc_chess_trigger
execute if score @s pc_chess_position matches 1..64 run function chess:dialog/_operation/select/chessboard
execute if score @s pc_chess_position matches 101.. run function chess:dialog/_operation/select/piece
#同步
tag @s add pc_chess_board_temp
tag @a remove pc_chess_player_temp
playsound ui.button.click block @a ~ ~ ~
execute as @a if score @s pc_chess_bind = @n[type=marker,tag=pc_chess_board_temp] dc_uid run function chess:dialog/chessboard/main
function chess:events/sync/execute
tag @s remove pc_chess_board_tempSelect action
At this point, we need to parse the specific coordinates of the player's click based on the click value. When splicing the dialog, I specified a separate trigger value for the coordinate of each checkerboard. Assign click values 1-64 from left to right and bottom to top. *Specially, for the sake of uniformity, when flipping the perspective, the click value of the dialog is actually rotated 180 degrees. When arranging the mapping of the chess pieces, just invert one x and one y, which is also very simple. *
In the capture area below, click values of 101-116 and 201-216 are assigned to the font of the chess piece according to the position of the chess piece. The click value of the empty character used to place the placeholder is set to 100, and no response is made to this value.
The chess pieces in the capture area are very simple. Since each click value corresponds to a specific chess piece, you can exhaustively set the corresponding chess piece color and ID.
data/chess/function/dialog/_operation/select/piece.mcfunction
#吃子区
#直接遍历
execute if score @s pc_chess_position matches 101 run data modify entity @s data.chessboard.select set value {x:0,y:0,type:"pieces",color:"white",id:"king"}
execute if score @s pc_chess_position matches 102 run data modify entity @s data.chessboard.select set value {x:1,y:0,type:"pieces",color:"white",id:"queen"}
execute if score @s pc_chess_position matches 103 run data modify entity @s data.chessboard.select set value {x:2,y:0,type:"pieces",color:"white",id:"bishop0"}
execute if score @s pc_chess_position matches 104 run data modify entity @s data.chessboard.select set value {x:3,y:0,type:"pieces",color:"white",id:"bishop1"}
execute if score @s pc_chess_position matches 105 run data modify entity @s data.chessboard.select set value {x:4,y:0,type:"pieces",color:"white",id:"knight0"}
execute if score @s pc_chess_position matches 106 run data modify entity @s data.chessboard.select set value {x:5,y:0,type:"pieces",color:"white",id:"knight1"}
execute if score @s pc_chess_position matches 107 run data modify entity @s data.chessboard.select set value {x:6,y:0,type:"pieces",color:"white",id:"rook0"}
execute if score @s pc_chess_position matches 108 run data modify entity @s data.chessboard.select set value {x:7,y:0,type:"pieces",color:"white",id:"rook1"}
execute if score @s pc_chess_position matches 109 run data modify entity @s data.chessboard.select set value {x:8,y:0,type:"pieces",color:"white",id:"pawn0"}
execute if score @s pc_chess_position matches 110 run data modify entity @s data.chessboard.select set value {x:9,y:0,type:"pieces",color:"white",id:"pawn1"}
execute if score @s pc_chess_position matches 111 run data modify entity @s data.chessboard.select set value {x:10,y:0,type:"pieces",color:"white",id:"pawn2"}
execute if score @s pc_chess_position matches 112 run data modify entity @s data.chessboard.select set value {x:11,y:0,type:"pieces",color:"white",id:"pawn3"}
execute if score @s pc_chess_position matches 113 run data modify entity @s data.chessboard.select set value {x:12,y:0,type:"pieces",color:"white",id:"pawn4"}
execute if score @s pc_chess_position matches 114 run data modify entity @s data.chessboard.select set value {x:13,y:0,type:"pieces",color:"white",id:"pawn5"}
execute if score @s pc_chess_position matches 115 run data modify entity @s data.chessboard.select set value {x:14,y:0,type:"pieces",color:"white",id:"pawn6"}
execute if score @s pc_chess_position matches 116 run data modify entity @s data.chessboard.select set value {x:15,y:0,type:"pieces",color:"white",id:"pawn7"}
execute if score @s pc_chess_position matches 201 run data modify entity @s data.chessboard.select set value {x:0,y:1,type:"pieces",color:"black",id:"king"}
execute if score @s pc_chess_position matches 202 run data modify entity @s data.chessboard.select set value {x:1,y:1,type:"pieces",color:"black",id:"queen"}
execute if score @s pc_chess_position matches 203 run data modify entity @s data.chessboard.select set value {x:2,y:1,type:"pieces",color:"black",id:"bishop0"}
execute if score @s pc_chess_position matches 204 run data modify entity @s data.chessboard.select set value {x:3,y:1,type:"pieces",color:"black",id:"bishop1"}
execute if score @s pc_chess_position matches 205 run data modify entity @s data.chessboard.select set value {x:4,y:1,type:"pieces",color:"black",id:"knight0"}
execute if score @s pc_chess_position matches 206 run data modify entity @s data.chessboard.select set value {x:5,y:1,type:"pieces",color:"black",id:"knight1"}
execute if score @s pc_chess_position matches 207 run data modify entity @s data.chessboard.select set value {x:6,y:1,type:"pieces",color:"black",id:"rook0"}
execute if score @s pc_chess_position matches 208 run data modify entity @s data.chessboard.select set value {x:7,y:1,type:"pieces",color:"black",id:"rook1"}
execute if score @s pc_chess_position matches 209 run data modify entity @s data.chessboard.select set value {x:8,y:1,type:"pieces",color:"black",id:"pawn0"}
execute if score @s pc_chess_position matches 210 run data modify entity @s data.chessboard.select set value {x:9,y:1,type:"pieces",color:"black",id:"pawn1"}
execute if score @s pc_chess_position matches 211 run data modify entity @s data.chessboard.select set value {x:10,y:1,type:"pieces",color:"black",id:"pawn2"}
execute if score @s pc_chess_position matches 212 run data modify entity @s data.chessboard.select set value {x:11,y:1,type:"pieces",color:"black",id:"pawn3"}
execute if score @s pc_chess_position matches 213 run data modify entity @s data.chessboard.select set value {x:12,y:1,type:"pieces",color:"black",id:"pawn4"}
execute if score @s pc_chess_position matches 214 run data modify entity @s data.chessboard.select set value {x:13,y:1,type:"pieces",color:"black",id:"pawn5"}
execute if score @s pc_chess_position matches 215 run data modify entity @s data.chessboard.select set value {x:14,y:1,type:"pieces",color:"black",id:"pawn6"}
execute if score @s pc_chess_position matches 216 run data modify entity @s data.chessboard.select set value {x:15,y:1,type:"pieces",color:"black",id:"pawn7"}On the chessboard it's a little more complicated. It is necessary to first decompose the incoming click value into xycoordinate, and then compare it with the data of the chess piece position one by one to confirm whether there is a chess piece at the clicked position; If there are no chess pieces, it will end directly. If there are chess pieces, in addition to setting the selected chess piece id and color, you also need to record the selected coordinate. There are three functions to handle here.
Checkerboard processing function
data/chess/function/dialog/_operation/select/chessboard.mcfunction:
#在棋盘上
scoreboard players remove @s pc_chess_position 1
#分解横纵坐标
scoreboard players operation $x pc_chess_position = @s pc_chess_position
scoreboard players operation $y pc_chess_position = @s pc_chess_position
scoreboard players operation $x pc_chess_position %= $8 pc_chess_position
scoreboard players operation $y pc_chess_position /= $8 pc_chess_position
#遍历选择
function chess:dialog/_operation/select/chessboard_piecedata/chess/function/dialog/_operation/select/chessboard_piece.mcfunction:
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn2.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn2.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn2"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn3.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn3.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn3"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn4.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn4.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn4"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn5.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn5.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn5"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn6.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn6.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn6"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn7.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn7.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn7"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"rook0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"rook1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"knight0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"knight1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"bishop0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"bishop1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.king.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.king.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"king"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.queen.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.queen.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"queen"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn2.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn2.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn2"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn3.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn3.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn3"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn4.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn4.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn4"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn5.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn5.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn5"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn6.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn6.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn6"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn7.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn7.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"pawn7"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"rook0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"rook1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"knight0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"knight1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"bishop0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"bishop1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.king.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.king.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"king"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.queen.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.queen.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"black",id:"queen"}data/chess/function/dialog/_operation/select/chessboard_selected.mcfunction:
$data modify entity @s data.chessboard.select set value {x:-1,y:-1,type:"chessboard",color:"$(color)",id:"$(id)"}
execute store result entity @s data.chessboard.select.x int 1 run scoreboard players get $x pc_chess_position
execute store result entity @s data.chessboard.select.y int 1 run scoreboard players get $y pc_chess_position
scoreboard players set @s pc_chess_switch 1Synchronous operation
Finally, we need to render the selection box to the player's dialog. In fact, we only need to perform a synchronization operation. The rendering of the selection box will be completed automatically when the dialog is refreshed. The same is true for the synchronization of the position of the chess pieces when placing the chess pieces later. A unified process is used here:
#同步
tag @s add pc_chess_board_temp
tag @a remove pc_chess_player_temp
playsound ui.button.click block @a ~ ~ ~
execute as @a if score @s pc_chess_bind = @n[type=marker,tag=pc_chess_board_temp] dc_uid run function chess:dialog/chessboard/main
function chess:events/sync/execute
tag @s remove pc_chess_board_tempRefresh and display the dialog once for the player, and refresh the model synchronously once, so that the data stored in the model can be synchronized to the display.
Part3: Placement operation
- If a chess piece is selected:
- If you click on a space on the chessboard, move the chess piece there;
- If you click on the capture area, move the chess pieces off the board;
- If you click on itself, cancel the selection;
- If you click on your own chess piece, switch to select that chess piece;
- If you click on the enemy chess piece, the piece will be captured (move the chess piece and remove the enemy chess piece from the board).
The part of placing chess pieces is a little more complicated, but it is generally divided into two categories: click on the grid on the chessboard or capture the piece area. Still use the click value for the first conditional judgment:
data/chess/function/dialog/_operation/place.mcfunction:
#放置棋子
scoreboard players operation @s pc_chess_position = @n[tag=pc_chess_player_temp] pc_chess_trigger
#在棋盘上
execute if score @s pc_chess_position matches 1..64 run function chess:dialog/_operation/place/chessboard
#在棋盘外
execute if score @s pc_chess_position matches 100.. run function chess:dialog/_operation/place/piece
#同步
tag @s add pc_chess_board_temp
tag @a remove pc_chess_player_temp
playsound block.decorated_pot.place block @a ~ ~ ~
execute as @a if score @s pc_chess_bind = @n[type=marker,tag=pc_chess_board_temp] dc_uid run function chess:dialog/chessboard/main
function chess:events/sync/execute
tag @s remove pc_chess_board_tempFirst conditional judgment
The logic of the capture area is very simple. If the selected piece is already in the capture area, just cancel the selection; if it is on the chessboard, move it out of the chessboard (set xy to -1).
data/chess/function/dialog/_operation/place/piece.mcfunction:
#如果本来就在吃子区,直接取消选择
execute if data entity @s {data:{chessboard:{select:{type:"pieces"}}}} run return run data remove entity @s data.chessboard.select
#如果在棋盘上,将其移出棋盘
execute unless data entity @s {data:{chessboard:{select:{type:"pieces"}}}} run function chess:dialog/_operation/place/piece_place with entity @s data.chessboard.selectdata/chess/function/dialog/_operation/place/piece_place.mcfunction
#移出棋子
$data modify entity @s data.chessboard.chess_pieces.$(color).$(id) set value {x:-1,y:-1}
data remove entity @s data.chessboard.selectThe logic of clicking on the chessboard is similar to that of selection. It still analyzes the coordinates first, and then determines the chess piece in the position. There are some changes in the part of judging chess pieces:
- First, a new self-check is added, just check the xycoordinate of the select directly;
- Then judge the relationship between the enemy and ourselves based on the color of the selected chess piece, and handle them separately:
Finally, if there is no matching piece, it is a space, move and clear the selection.
data/chess/function/dialog/_operation/place/chessboard.mcfunction:
#在棋盘上
scoreboard players remove @s pc_chess_position 1
#分解横纵坐标
scoreboard players operation $x pc_chess_position = @s pc_chess_position
scoreboard players operation $y pc_chess_position = @s pc_chess_position
scoreboard players operation $x pc_chess_position %= $8 pc_chess_position
scoreboard players operation $y pc_chess_position /= $8 pc_chess_position
#判断是否是自身
execute store result score $px pc_chess_position run data get entity @s data.chessboard.select.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.select.y
execute unless data entity @s {data:{chessboard:{select:{type:"pieces"}}}} if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run data remove entity @s data.chessboard.select
#判断是否是其他棋子的位置
scoreboard players set @s pc_chess_switch 0
execute if data entity @s {data:{chessboard:{select:{color:"white"}}}} run function chess:dialog/_operation/place/chessboard_piece_white
execute if data entity @s {data:{chessboard:{select:{color:"black"}}}} run function chess:dialog/_operation/place/chessboard_piece_black
#如果不是切换选择,将选定的棋子移动,并清除选择状态
execute unless score @s pc_chess_switch matches 1 run function chess:dialog/_operation/place/chessboard_place with entity @s data.chessboard.selectSwitch selection and capture
This part takes White as an example, and Black’s corresponding processing can be:
data/chess/function/dialog/_operation/place/chessboard_piece_white.mcfunction:
#己方棋子,切换选择
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn2.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn2.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn2"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn3.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn3.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn3"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn4.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn4.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn4"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn5.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn5.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn5"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn6.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn6.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn6"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn7.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.pawn7.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"pawn7"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"rook0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.rook1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"rook1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"knight0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.knight1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"knight1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"bishop0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.bishop1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"bishop1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.king.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.king.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"king"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.queen.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.white.queen.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/select/chessboard_selected {color:"white",id:"queen"}
#敌方棋子,吃子
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn2.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn2.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn2"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn3.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn3.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn3"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn4.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn4.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn4"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn5.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn5.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn5"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn6.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn6.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn6"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn7.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.pawn7.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"pawn7"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"rook0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.rook1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"rook1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"knight0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.knight1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"knight1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop0.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop0.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"bishop0"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop1.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.bishop1.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"bishop1"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.king.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.king.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"king"}
execute store result score $px pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.queen.x
execute store result score $py pc_chess_position run data get entity @s data.chessboard.chess_pieces.black.queen.y
execute if score $px pc_chess_position = $x pc_chess_position if score $py pc_chess_position = $y pc_chess_position run return run function chess:dialog/_operation/place/capture {color:"black",id:"queen"}This step is to traverse the data list and check whether there is a corresponding chess piece on the grid; If it is one's own chess piece, call the function of the select module to switch the selection; If it is an enemy chess piece, capture is triggered, and the corresponding color and chess piece id are passed into the capture function:
data/xiangqi/function/dialog/_operation/place/capture.mcfunction:
#吃子:将棋子移出棋盘
$data modify entity @s data.chessboard.chess_pieces.$(color).$(id) set value {x:-1,y:-1}The step of capturing only removes the enemy chess piece, because the part of moving the selected chess piece and the movement of clicking on the empty space are processed at the end.
synchronous
After moving the chess pieces, you need to synchronize them as usual. This part has been mentioned in the selection section, and the process is exactly the same.
PartEX: Switch perspective and default layout
At this point, we have completed the entire process of chess ornaments: selecting chess pieces and capturing them. Of course, you will also find three buttons below: switch perspective, clear the board, and resume the game.
This part is relatively simple, so I’ll walk you through it here:
Switching the perspective will trigger a click value of -2, modify the player's perspective parameters and refresh the dialog. The dialog rendering function will set the chessboard click value and chess piece rendering coordinate processing algorithm based on this parameter;
Exiting the dialog will trigger a click value of -1 and unbind the player from the board.
When setting the two buttons of the preset layout, I did not set the click value but directly bound the function, which just gave me a chance for a second confirmation. The implementation is also very simple, which is to forcefully modify the position of each chess piece and then synchronize the model once.
Conclusion
The above is the complete analysis of data pack chess. The feature of this work is mainly in the display of the first part. It uses item model mapping and the splicing characteristics of fonts, creating a lot of possibilities with a relatively limited number of models and textures.