ä»åã®ãã¥ãŒããªã¢ã«ã§ã¯ããã3ããºã«ã²ãŒã ãäœã£ãŠãããããã3ïŒã¹ãªãŒïŒããºã«ã²ãŒã ãšã¯ãç€é¢ã®ã°ãªããã«æ²¿ã£ãŠåäžã«äžŠã¹ãããè€æ°ã®ã«ã©ãã«ãªããŒã¹ã®ãã¡ã®ïŒã€ãã1ãã¹åãããŠåãè²ã®ããŒã¹ãïŒã€ä»¥äžäžŠã¹ãŠæ¶ãã¿ã€ãã®ããºã«ã²ãŒã ã®ç·ç§°ã ãããŸãåãããšãäžåã®æäœã§é£ç¶çã«è€æ°ã®ããŒã¹ãæ¶ãããšãã§ãããªããšãæ°æã¡ã®è¯ããã¬ã€æèŠãå³ããããšãã§ãããç°¡åãªæäœã§æ°è»œã«æ¥œããããããã¢ãã€ã«ã²ãŒã ã§ç¹ã«äººæ°ã®ãããžã£ã³ã«ã ã
人æ°ã©ãããããã€ãäŸã«æãããšããã£ã³ãã£ãŒã¯ã©ãã·ã¥ããã¥ãŒã³ãã©ã¹ãããã€ã€ã«ããããªã©ãããã«ããããå°ãæäœæã¯ç°ãªãããä»ã«ããããºã«&ãã©ãŽã³ãºãããLINEãã ãã ããããŒã¹ã¯ããã3ã ãä»åã¯ãã£ã³ãã£ãŒã¯ã©ãã·ã¥ã®ãããªãäžåã®æäœã§ããŒã¹ã1ãã¹ã ãåãããŠè²ãæããã¿ã€ãã®ããºã«ãäœã£ãŠããã
Other Tutorials
ãããºã«ïŒãã©ãŽã³ãºãã®ãããªã²ãŒã ãäœã£ãŠã¿ããå ŽåïŒ
Godot ã§äœãé²å圢ããã 3 ããºã«ã²ãŒã
ãLINEïŒãã£ãºããŒãã ãã ãã®ãããªã²ãŒã ãäœã£ãŠèŠããå ŽåïŒ
Godot ã§äœãåãè²ãã€ãªããŠæ¶ãããºã«ã²ãŒã
ãã®ãã¥ãŒããªã¢ã«ã§æåŸã«ã§ãããããããžã§ã¯ãã®ãã¡ã€ã«ã¯ GitHubãªããžããª
ã«çœ®ããŠããã.zipãã¡ã€ã«ãããŠã³ããŒãããŠããã ãããEndããã©ã«ãå
ã®ãproject.godotããã¡ã€ã«ã Godot Engine ã§ã€ã³ããŒãããŠããã ããã°ãçŽæ¥ãããžã§ã¯ãã確èªããŠããã ãããšãå¯èœã ã
Environment
ãã®ãã¥ãŒããªã¢ã«ã¯ä»¥äžã®ç°å¢ã§äœæããŸããã
ã»Godot ã®ããŒãžã§ã³: 3.4.4
ã»ã³ã³ãã¥ãŒã¿ã®OS: macOS 11.6.5
Memo:
ã²ãŒã ãäœãå§ããã®ã«ä»¥äžã®èšäºãã圹ç«ãŠãã ããã
Godot ãããŠã³ããŒããã
Godot ã®ãããžã§ã¯ããããŒãžã£ãŒ
Godot ã®èšèªèšå®
æ°èŠãããžã§ã¯ããäœæãã
ããã§ã¯ Godot Engine ãç«ã¡äžããŠãæ°èŠãããžã§ã¯ããäœæãããããããžã§ã¯ãã®ååã¯ããªãã®ã奜ã¿ã§æ±ºããŠããã ããŠOKã ãããæãã€ããªããã°ãMatch3 StartããšããŠãããã
ãããžã§ã¯ãèšå®ãæŽæ°ãã
ãšãã£ã¿ã衚瀺ãããããå ã«ãããžã§ã¯ãå šäœã«é¢ããèšå®ãæŽæ°ããŠãããã
ãŸãã¯ã²ãŒã ã®ãã£ã¹ãã¬ã€ãµã€ãºãèšå®ãããä»åã¯ã¹ããã®çžŠåãã®ç»é¢ãæ³å®ããŠã瞊暪ã®æ¯çã 16 : 9 ãšããã
- ããããžã§ã¯ããã¡ãã¥ãŒïŒããããžã§ã¯ãèšå®ããéãã
- ãäžè¬ãã¿ãã§ãwindowãã§æ€çŽ¢ããŠããµã€ãããŒã®ãDisplayãïŒãWindowããéžæããã
- ãSizeãã»ã¯ã·ã§ã³ã§ä»¥äžã®é
ç®ã®å€ãå€æŽããã
- Width: 630
- Height: 1120
- Test Width: 315
- Test Height: 560
- ãStretchãã»ã¯ã·ã§ã³ã§ä»¥äžã®é
ç®ã®å€ãå€æŽããã
- Mode: 2d
- Aspect: keep
ãã®ãŸãŸããããžã§ã¯ãèšå®ããŠã€ã³ããŠãéããç¶æ ã§ããããã°ããã«ã§ã¹ããã®ã¿ããæäœãããŠã¹ã§ä»£çšããããã®èšå®ãããã
- ãäžè¬ãã¿ãã§ãmouseããšæ€çŽ¢ãããµã€ãããŒã®ãInput DevicesãïŒãPointingããéžæããã
- ãEmulate Touch From Mouseãã® On ã®ãã§ãã¯ãå
¥ããã
ããã«ããããžã§ã¯ãèšå®ããŠã€ã³ããŠãéããç¶æ ã§ãã€ã³ããããããã«ã¹ããã®ã¿ããæäœã®çžåœããã¢ã¯ã·ã§ã³ãè¿œå ãããã
- ãã€ã³ãããããããã¿ãã«åãæ¿ããã¢ã¯ã·ã§ã³ã«ãtouchããè¿œå ããã
- ãtouchãã®æäœã«ããŠã¹ã®å·Šã¯ãªãã¯ãè¿œå ããã
ã¢ã»ãããããŠã³ããŒãããŠã€ã³ããŒããã
次ã«ãKENNEYã®ãµã€ãããã¢ã»ãããããŠã³ããŒãããŠå©çšãããŠãããããä»åå©çšããã®ã¯ãPhysics Assets ã ãšããã¢ã»ããããã¯ã ããã®ã¢ã»ããã«å«ãŸãããªããšããããããšã€ãªã¢ã³ã®é¡ã®ç»åããã²ãŒã ã®ç€é¢ã«äžŠã¹ãããŒã¹ã®ãã¯ã¹ãã£ãšããŠäœ¿çšããããã®çŽ æŽãããããç¡æã®çŽ æã«æè¬ããã«ã¯ããããªãã
ããŠã³ããŒããããã/physicspack/PNG/Aliensããã©ã«ãã®äžã®ãã¡ã€ã«åãã~_round.pngãã®ç»åã ããæ®ããŠä»ã¯åé€ãããAliensããã©ã«ãããšãšãã£ã¿ã®ãã¡ã€ã«ã·ã¹ãã ããã¯ãžãã©ãã°ããŠãããžã§ã¯ãã«ã€ã³ããŒããããã
Grid ã·ãŒã³ãäœã
ãŸãã¯ããã3ããºã«ã²ãŒã ã§ããŒã¹ãé 眮ãããç€é¢ãšããŠããGridãã·ãŒã³ãäœæãããã
- ãã·ãŒã³ãã¡ãã¥ãŒïŒãæ°èŠã·ãŒã³ããéžæããã
- ãã«ãŒãããŒããçæãã«ãŠã2D ã·ãŒã³ããéžæããã
1.ãNode2Dãã¯ã©ã¹ã®ã«ãŒãããŒããçæããããããã®ååããGridãã«å€æŽããã - ã·ãŒã³ãä¿åããããã©ã«ããäœæããŠããã¡ã€ã«ãã¹ããres://Grid/Grid.tscnããšããŠã·ãŒã³ãä¿åããã
Grid ã·ãŒã³ã«ããŒããè¿œå ãã
ãGridãã«ãŒãããŒãã«ãããã«ãNode2Dãã¯ã©ã¹ã®åããŒããè¿œå ããååããPiecesContainerãã«å€æŽãããããã®ããŒãã¯ç€é¢ã«é 眮ãããããŒã¹ããŸãšããããã®ããŒãã ãã²ãŒã äžã¯ãã¹ã¯ãªããã§ããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ãçæããããå šãŠãã®ãPiecesContainerãããŒãã®åãšããŠè¿œå ããããšã«ãªãã
ã·ãŒã³ããªãŒããã¯ã®è¡šç€ºã¯ä»¥äžã®ããã«ãªã£ãã¯ãã ã
ãªãããGridãã·ãŒã³ã®ããŒãã®ããããã£ç·šéã¯ç¹ã«å¿ èŠãªãã
Piece ã·ãŒã³ãäœã
次ã«ãç€é¢ã«äžŠã¹ãããŒã¹ãšããŠãPieceãã·ãŒã³ãäœæããããã ãããã®ãPieceãã·ãŒã³ã¯ãããŸã§é圢ã§ãå®éã«ã²ãŒã äžã§å©çšããåè²ã®ããŒã¹ã¯ããã®ãPieceãã·ãŒã³ãç¶æ¿ãã圢ã§ã®ã¡ã»ã©çšæããã
- ãã·ãŒã³ãã¡ãã¥ãŒïŒãæ°èŠã·ãŒã³ããéžæããã
- ãã«ãŒãããŒããçæãã«ãŠã2D ã·ãŒã³ããéžæããã
- ãNode2Dãã¯ã©ã¹ã®ã«ãŒãããŒããçæããããããã®ååããPieceãã«å€æŽããã
- ã·ãŒã³ãä¿åããŠãããããã©ã«ããäœæããŠããã¡ã€ã«ãã¹ããres://Pieces/Piece.tscnããšããŠã·ãŒã³ãä¿åããã
Piece ããŒãã«åããŒããè¿œå ãã
ãPieceãã«ãŒãããŒãã«ãSpriteãã¯ã©ã¹ã®åããŒããè¿œå ããããã·ãŒã³ããªãŒããã¯ã®è¡šç€ºã¯ä»¥äžã®ããã«ãªã£ãã¯ãã ã
Sprite ããŒãã®ããããã£ãç·šéãã
ãSpriteãããŒãã®ããããã£ãå°ãç·šéããŠããããå è¿°ã®éãããPieceãã¯ãããŸã§ç¶æ¿å ïŒé圢ïŒãªã®ã§ããã®ã·ãŒã³ã§ã¯ãSpriteãããŒãã®ãTextureãããããã£ã«ã¯ãªãœãŒã¹ãæ¢ããŠé©çšãããã®ãŸãŸã«ããŠãããç¶æ¿å ã®ã·ãŒã³ã§ããããã®ããŒã¹ã®è²ã«ãã£ãç»åãé©çšããäºå®ã ã
ãOffsetã>ãOffsetãããããã£ã®å€ãã(x: 35, y: -35)ãã«å€æŽããŠãããããã®ã·ãŒã³ãç¶æ¿ããåè²ã®ããŒã¹ã®ã·ãŒã³ã§ã¯ãTextureãããããã£ã«å
ã«ã€ã³ããŒããã KENNEY ã®ç»åãé©çšãããããã®ç»åã®çžŠæšªã®ãµã€ãºã 70 px ãªã®ã§ããã®ç»åã®äžå¿ãå³äžã«ããããŠç»åã®å·Šäžã®è§ã(x: 0, y: 0)ã«åãããã
ããŒã¹ãé
眮ããç€é¢ã®ã°ãªãã㯠x 軞ã¯å·Šããå³ãžãy 軞ã¯äžããäžãžã«ãŠã³ãããä»æ§ã§ããã€ç€é¢ã® 1 ã°ãªããã®ãµã€ãºããã¯ã¹ãã£ã®ãµã€ãºã«åãã㊠70 px ãšãããããŒã¹ã®ãã¯ã¹ãã£ç»åã®å·Šäžã®è§ã (x: 0, y: 0) ã«åãããã°ããPieceãã«ãŒãããŒãã®äœçœ® (x: 0, y: 0) ãã°ãªããã«åãããŠé
眮ãããšãã«ãã¡ããã©ãSpriteãã®ç»åãã°ãªããã«æ²¿ã£ãŠé
眮ããããšããããã ã
Piece ããŒãã«ã¹ã¯ãªãããã¢ã¿ããããŠç·šéãã
ãPieceãã«ãŒãããŒãã«æ°èŠã¹ã¯ãªãããã¢ã¿ãããããããã¡ã€ã«ãã¹ããres://Pieces/Piece.gdããšããŠã¹ã¯ãªãããã¡ã€ã«ãäœæããã
ã²ãšãŸãã¹ã¯ãªããã以äžã®ããã«ç·šéããŠã»ããã
###Piece.gd###
extends Node2D
# ããŒã¹ã®è²ãæååããŒã¿ãšããŠèšå®ããããã®ããããã£
export (String) var color
# ãããããïŒ3ã€ä»¥äžåãè²ã䞊ãã ïŒç¶æ
ã瀺ãããããã£
var matched = false
# Sprite ããŒãã®åç
§
onready var sprite = $Sprite
# ããŒã¹ã移åãããã¡ãœãã
# åŒæ° target ã«æž¡ããäœçœ®ã« Piece ã€ã³ã¹ã¿ã³ã¹ã移åãã
func move(target):
position = target
# ãããããïŒ3ã€ä»¥äžåãè²ã䞊ãã ïŒãšãã«åŒã°ããã¡ãœãã
# matched ããããã£ã true ã«ããè²ãåéæã«ãã
func make_matched():
matched = true
sprite.modulate = Color(1,1,1,.5)
ããã§ãPiece.gdãã¹ã¯ãªããã®ç·šéã¯å®äºã ã
Piece ã·ãŒã³ãç¶æ¿ããåè²ã®ã·ãŒã³ãäœã
é圢ãšãªããPieceãã·ãŒã³ã¯å®æããã®ã§ããããç¶æ¿ããã·ãŒã³ãããŒã¹ã®è²ã®æ°ã ãäœæããŠããããããŒã¹ã®è²ã¯ãããŒãžã¥ãéãç·ããã³ã¯ãé»ã® 5 è²ã ããŸãã¯ãããŒãžã¥ãã®ãããããäŸã«æé ãé²ããŠã¿ããã
- ãã·ãŒã³ãã¡ãã¥ãŒïŒãæ°ããç¶æ¿ã·ãŒã³ããéžæããã
- ç¶æ¿å ã®ã·ãŒã³ãšããŠãPiece.tscnããéžæããã
- ç¶æ¿ã·ãŒã³ãçæãããããã«ãŒãããŒãã®ååããPieceBeigeãã«å€æŽããã
*ãã®ã«ãŒãããŒãã®ååã¯ããããã®ããããã®è²ã«åãããããšã - ã·ãŒã³ãäžæŠä¿åããŠããããã¡ã€ã«ãã¹ããres://Pieces/PieceBeige.tscnããšããŠä¿åããã
- ã·ãŒã³ããªãŒããã¯ã§ã«ãŒãããŒããPieceBeigeããéžæããç¶æ
ã§ãã€ã³ã¹ãã¯ã¿ãŒã§ãScript Variablesãã®ãColorãããããã£ã®å€ããbeigeããšããã
- ã·ãŒã³ããªãŒããã¯ã§ãSpriteãããŒããéžæãããTextureãããããã£ã«å
ã«ã€ã³ããŒãããŠããããªãœãŒã¹ãres://Aliens/alienBeige_round.pngããé©çšããïŒãã¡ã€ã«ã·ã¹ãã ããã¯ãããã©ãã°ããã°OKïŒã
2D ã¯ãŒã¯ã¹ããŒã¹äžã§ã¯ä»¥äžã®ã¹ã¯ãªãŒã³ã·ã§ããã®ããã«ãªã£ãã¯ãã ã
以äžã§ããPieceBeigeãã·ãŒã³ã¯å®æã ãæ®ãã® 4 è²ã®ããŒã¹ã«ã€ããŠããåæ§ã®æé ã§ã·ãŒã³ãäœæããŠã»ããããªããã·ãŒã³ããšã«ç°ãªãéšåã«ã€ããŠã¯ã以äžãåèã«ããŠã»ããã
- éã®ããŒã¹
- ã«ãŒãããŒãå: PieceBlue
- Color ããããã£: blue
- Sprite > Texture ããããã£: res://Aliens/alienBlue_round.png
- ã·ãŒã³ä¿åæã®ãã¡ã€ã«ãã¹: res://Pieces/PieceBlue.tscn
- ç·ã®ããŒã¹
- ã«ãŒãããŒãå: PieceGreen
- Color ããããã£: green
- Sprite > Texture ããããã£: res://Aliens/alienGreen_round.png
- ã·ãŒã³ä¿åæã®ãã¡ã€ã«ãã¹: res://Pieces/PieceGreen.tscn
- ãã³ã¯ã®ããŒã¹
- ã«ãŒãããŒãå: PiecePink
- Color ããããã£: green
- Sprite > Texture ããããã£: res://Aliens/alienPink_round.png
- ã·ãŒã³ä¿åæã®ãã¡ã€ã«ãã¹: res://Pieces/PiecePink.tscn
- é»ã®ããŒã¹
- ã«ãŒãããŒãå: PieceYellow
- Color ããããã£: yellow
- Sprite > Texture ããããã£: res://Aliens/alienYellow_round.png
- ã·ãŒã³ä¿åæã®ãã¡ã€ã«ãã¹: res://Pieces/PieceYellow.tscn
å šéšã§ 5 è²ã®ããŒã¹ã®ç¶æ¿ã·ãŒã³ãã§ãããäœæ¥å®äºã ã
Grid ã·ãŒã³ãã¹ã¯ãªããã§å¶åŸ¡ãã
åè²ã®ããŒã¹ã®ã·ãŒã³ãã§ãããã£ãã®ã§ãããããã¯ããã°ã©ãã³ã°ããŠã²ãŒã ãå¶åŸ¡ããŠãããä»åã¯ã³ãŒãéãããå€ããªã®ã§é 匵ããã
Godot ãšãã£ã¿ã§ãGrid.tscnãã·ãŒã³ã«åãæ¿ãããããGridãã«ãŒãããŒãã«æ°èŠã¹ã¯ãªãããã¢ã¿ãããããããã¡ã€ã«ãã¹ã¯ãres://Grid/Grid.gdããšããŠäœæããã
ãªããã¹ã¯ãªããå ã®ã³ã¡ã³ãã«ã¯ãæã觊ããããŸãã¯ãæãé¢ããããšèšèŒããŠããããGodot ãããã°ããã«äžã§ã¯ãããŠã¹å·Šãã¿ã³ãæŒããããŸãã¯ãããŠã¹å·Šãã¿ã³ãé¢ããããšçœ®ãæããŠã»ããã
ãŸããåãè²ãïŒã€ä»¥äžæã£ãç¶æ ã®ããšãããããããšè¡šçŸããŠããã®ã§ããã¡ãããçæããã ãããã
ã§ã¯ãã¹ã¯ãªãããšãã£ã¿ãéãããããŸãã¯å¿ èŠãªããããã£ãå®çŸ©ããŠãããã
###Grid.gd###
extends Node2D
# åè²ã®ããŒã¹ã®ã·ãŒã³ãã¡ã€ã«ãèŠçŽ ãšããé
å
const pieces_scn = [
preload("res://Pieces/PieceBeige.tscn"),
preload("res://Pieces/PieceBlue.tscn"),
preload("res://Pieces/PieceGreen.tscn"),
preload("res://Pieces/PiecePink.tscn"),
preload("res://Pieces/PieceYellow.tscn")
]
# x軞æ¹åã®ã°ãªããæ°
var width: = 7
# y軞æ¹åã®ã°ãªããæ°
var height: = 10
# x軞æ¹åã®ã°ãªããéå§äœçœ®ïŒpixelïŒ
var x_start: = 70
# y軞æ¹åã®ã°ãªããéå§äœçœ®ïŒpixelïŒ
var y_start: = 910
# 1ã°ãªããã®ãµã€ãºïŒPieceã®Spriteã®Textureãšåãã«ããïŒ
var grid_size: = 70
# ç€é¢ã®ããŒã¹ã®é
眮ãè¡šãé
åïŒäºæ¬¡å
é
åïŒ
var all_pieces = []
# ç»é¢ã«æã觊ããäœçœ®
var touched_pos = Vector2()
# ç»é¢ããæãé¢ããäœçœ®
var released_pos = Vector2()
# ç»é¢ã«æã觊ããŠããã¹ããŒãã觊ããŠãã: true / é¢ããŠãã: false
var is_touching = false
# ãããããããŒã¹ã®èªååŠçã¹ããŒããåŠçäž: true / åæ¢äž: false
var is_waiting = false
# PiecesContainer ããŒãã®åç
§
onready var pieces_container = $PiecesContainer
ç¶ããŠä»¥äžã®ã¡ãœãããè¿œå ãããããªãã以äžã®ã³ãŒãå ã«åºãŠãã äºæ¬¡å é å ãšã¯ãèŠçŽ ãšããŠé åãæ ŒçŽããé åãã€ãŸãé åã®é åã®ããšã ã
ä»åã®ã¹ã¯ãªããã§å©çšããŠããäºæ¬¡å é åã®å Žåãäžéå±€ç®ã®é åã§ã¯ãç€é¢ã® x 軞æ¹åã®ã°ãªããã®æ°ã ã空ã®é åãèŠçŽ ãšããäºéå±€ç®ãšããŠããããã®é åå ã«çžŠæ¹åã®ã°ãªããæ°ã ãèŠçŽ ãæ ŒçŽããããã®èŠçŽ ãšããŠãããŒã¹ãªããžã§ã¯ããæ ŒçŽããããšã§ãããããã®ããŒã¹ãç€é¢ã®ã©ãã«äœçœ®ããŠãããïŒx 軞æ¹åã«äœçªç®ã®ã°ãªããã§ãy 軞æ¹åã«äœçªç®ã®ã°ãªãããïŒã管çããããšãã§ãããšããããã ã
###Grid.gd###
# ã·ãŒã³ãèªã¿èŸŒãŸãããåŒã°ããé¢æ°
func _ready():
# ã©ã³ãã ãªæ°ãçæããé¢æ°ã®åºåçµæãæ¯åã©ã³ãã ã«ããããã®çµã¿èŸŒã¿é¢æ°ãåŒã¶
randomize()
# all_pieces ãç€é¢ã®ã°ãªãããæ§æããäºæ¬¡å
é
åã«ãã
all_pieces = make_2d_array() # ãã®ããšå®çŸ©
# ããŒã¹ãçæããŠç€é¢ã«é
眮ããŠç€é¢æ
å ±ã all_pieces ã«åæ ããã¡ãœãããåŒã¶
spawn_pieces() # ãã®ããšå®çŸ©
# ç€é¢ã®ã°ãªãããæ§æããäºæ¬¡å
é
åãçæããã¡ãœãã
func make_2d_array():
# array ãšããååã®é
åãçšæ
var array = []
# array ã« x 軞æ¹åã®ã°ãªããæ°ã ã空ã®é
åãå
¥ãã
for i in width:
array.append([])
# ããã«ããããã®é
åã« y 軞æ¹åã®ã°ãªããæ°ã ãæ«å®çã« null ãå
¥ãã
for j in height:
array[i].append(null)
# ã§ãããã£ãäºæ¬¡å
é
åãè¿ã
return array
# ããŒã¹ãçæããŠç€é¢ã«é
眮ããŠç€é¢æ
å ±ã board ã«åæ ããã¡ãœãã
func spawn_pieces():
# x軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for i in width:
# y軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for j in height:
# å
šããŒã¹ã®äºæ¬¡å
é
åäžã§è©²åœã°ãªããã«ããŒã¹ãååšããªãå Žå
#ïŒã²ãŒã éå§æã¯å
šéš nullïŒ
if all_pieces[i][j] == null:
# åè²ã®ããŒã¹ã®ã·ãŒã³ããã©ã³ãã ã§ïŒã€éžæããŠã€ã³ã¹ã¿ã³ã¹å
var index = floor(rand_range(0, pieces_scn.size()))
var piece = pieces_scn[index].instance()
# ãããããŠããŸã£ãå Žåã¯ãããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ãåé€ããŠããçŽã
while match_at(i, j, piece.color): # ãã®ããšå®çŸ©
piece.queue_free()
index = floor(rand_range(0, pieces_scn.size()))
piece = pieces_scn[index].instance()
# ããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ãPiecesContainerããŒãã®åã«ãã
pieces_container.add_child(piece)
# ã°ãªãããããã¯ã»ã«ã«å€æããäœçœ®ã«ããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ãé
眮
piece.position = grid_to_pixel(i, j) # ãã®ããšå®çŸ©
# å
šããŒã¹ã®äºæ¬¡å
é
åãæŽæ°
all_pieces[i][j] = piece
äžèšã³ãŒãã®äžã§æªå®çŸ©ã®match_at
ã¡ãœãããšgrid_to_pixel
ã¡ãœãããå®çŸ©ããŠãããã
###Grid.gd###
# æå®ããã°ãªããäœçœ®ã§åãè²ããŒã¹ãïŒã€ä»¥äžäžŠãã§ããã確èªããã¡ãœãã
# åŒæ°columnã¯x軞ã®ã°ãªããäœçœ®ãrowã¯y軞ã®ã°ãªããäœçœ®ãcolorã¯ããŒã¹ã®è²
func match_at(column, row, color):
# æå®ããã°ãªããã® x 軞æ¹åã®äœçœ®ã3以äžã®å Žå
if column >= 2:
# æå®ããã°ãªããäœçœ®ã®å·Šé£ãšããäžã€å·Šé£ã«ããŒã¹ãããå Žå
if all_pieces[column-1][row] != null \
and all_pieces[column-2][row] != null:
# å·Šé£ãšããäžã€å·Šé£ã®ããŒã¹ã®è²ãæå®ããããŒã¹ã®è²ãšåãå Žå
if all_pieces[column-1][row].color == color \
and all_pieces[column-2][row].color == color:
# true ãè¿ã
return true
# æå®ããã°ãªããã® y 軞æ¹åã®äœçœ®ã3以äžã®å Žå
if row >= 2:
# æå®ããã°ãªããäœçœ®ã®äžãšããäžã€äžã«ããŒã¹ãããå Žå
if all_pieces[column][row-1] != null \
and all_pieces[column][row-2] != null:
# äžãšããäžã€äžã®ããŒã¹ã®è²ãæå®ããããŒã¹ã®è²ãšåãå Žå
if all_pieces[column][row-1].color == color \
and all_pieces[column][row-2].color == color:
# true ãè¿ã
return true
# ã°ãªããã®äœçœ®ããã¯ã»ã«ã®äœçœ®ã«å€æããã¡ãœãã
func grid_to_pixel(column, row):
# å
ã«ãã¯ã»ã«äœçœ®åºåçšã« Vector2 åã®å€æ° pixel_pos ãå®çŸ©
var pixel_pos = Vector2()
# ãã¯ã»ã« x åº§æš = x 軞æ¹åã®ã°ãªããéå§äœçœ® + ã°ãªãããµã€ãº x ã°ãªãã x 座æš
pixel_pos.x = x_start + grid_size * column
# ãã¯ã»ã« y åº§æš = y 軞æ¹åã®ã°ãªããéå§äœçœ® - ã°ãªãããµã€ãº x ã°ãªãã y 座æš
pixel_pos.y = y_start - grid_size * row
# ãã¯ã»ã«åº§æšãè¿ã
return pixel_pos
ããã§ãã²ãŒã éå§æã«åè²ã®ããŒã¹ãç€é¢ã«ã©ã³ãã ã§äžŠã¹ãããã¯ãã ãäžåºŠãããžã§ã¯ããå®è¡ããŠç¢ºèªããŠã¿ããããªããåããŠãããžã§ã¯ããå®è¡ããå Žåã¯ãã¡ã€ã³ã·ãŒã³éžæã®ãã€ã¢ãã°ã衚瀺ãããã®ã§ããGrid.tscnããã¡ã€ã³ã·ãŒã³ãšããŠéžæãããã
ã¡ããã©grid_to_pixel
ã¡ãœãããå®çŸ©ããã®ã§ãã€ãã§ã«ãã®ããšäœ¿çšããpixel_to_grid
ã¡ãœãããå®çŸ©ããŠããããååã®éããå
ã«å®çŸ©ããgrid_to_pixel
ãšã¯éã§ããã¯ã»ã«ã®äœçœ®ãã°ãªããã®äœçœ®ã«å€æããã¡ãœããã ã
###Grid.gd###
# ãã¯ã»ã«ã®äœçœ®ãã°ãªããã®äœçœ®ã«å€æããã¡ãœãã
func pixel_to_grid(pixel_x, pixel_y) -> Vector2:
var grid_pos = Vector2()
grid_pos.x = floor((pixel_x - x_start) / grid_size)
grid_pos.y = floor((pixel_y - y_start) / -grid_size)
return grid_pos
ããã«ãããäžã€ãã®ããšäœ¿çšããis_in_grid
ã¡ãœãããå®çŸ©ããŠãããããã¯åŒæ°ã«æž¡ããäœçœ®ãç€é¢ã°ãªããã®ç¯å²å
ãã©ãããå€å®ããŠãã®çµæãè¿ãã¡ãœããã ã
###Grid.gd###
# æå®ããäœçœ®ãç€é¢ã°ãªããã®ç¯å²å
ãã©ãããè¿ãã¡ãœãã
func is_in_grid(grid_position: Vector2):
if grid_position.x >= 0 and grid_position.x < width \
and grid_position.y >= 0 and grid_position.y < height:
# ç€é¢ã°ãªããã®ç¯å²å
ã ã£ãã true ãè¿ã
return true
else:
# ç€é¢ã°ãªããã®ç¯å²å€ã ã£ãã false ãè¿ã
return false
ããã§ãã²ãŒã ã®ãã¬ã€ã€ãŒã®å ¥åïŒç»é¢ã®ã¿ããæäœïŒãåŠçããããã°ã©ã ãèšè¿°ããŠããã
###Grid.gd###
# ã²ãŒã ã®ã¡ã€ã³ã«ãŒãã§æ¯ãã¬ãŒã åŒã°ããé¢æ°
func _process(_delta):
# ãããããã³ã°ã®åŠçäžã§ãªããã°
if not is_waiting:
# ãã¬ã€ã€ãŒã®å
¥åãåŠçãã
touch_input() # ãã®ããšå®çŸ©
# ãã¬ã€ã€ãŒã®å
¥åãåŠçããã¡ãœãã
func touch_input():
# ããç»é¢ã«æã觊ããã
if Input.is_action_just_pressed("touch"):
# æã®äœçœ®ããã¯ã»ã«ããã°ãªããã«å€æ
var start_pos = get_global_mouse_position()
var start_grid = pixel_to_grid(start_pos.x, start_pos.y)
# æã®äœçœ®ãç€é¢ã°ãªããã®ç¯å²å
ã ã£ãã
if is_in_grid(start_grid):
# ç»é¢ã«æã觊ããäœçœ®ãä¿å
touched_pos = start_grid
# ã¹ããŒããç»é¢ã«æã觊ããŠããç¶æ
ã«ãã
is_touching = true
# ããç»é¢ããæãé¢ããã
if Input.is_action_just_released("touch"):
# æã®äœçœ®ããã¯ã»ã«ããã°ãªããã«å€æ
var end_pos = get_global_mouse_position()
var end_grid = pixel_to_grid(end_pos.x, end_pos.y)
# ããæã®äœçœ®ãç€é¢ã°ãªããã®ç¯å²å
...
# ãã€ã¹ããŒããç»é¢ã«æã觊ããŠããç¶æ
ã ã£ãã
if is_in_grid(end_grid) and is_touching:
# æãé¢ããäœçœ®æ
å ±ãšããŠä¿å
released_pos = end_grid
# æã觊ããäœçœ®ãšæãé¢ããäœçœ®ã§ããŒã¹ã®ç§»åãåŠçããã¡ãœãããåŒã¶
touch_and_release() # ãã®ããšå®çŸ©
# ã¹ããŒããç»é¢ããæãé¢ããŠããç¶æ
ã«ãã
is_touching = false
äžèšã³ãŒãã§ãç€é¢ã°ãªããå
ã§ç»é¢ã«æã觊ããäœçœ®ãšç»é¢ããæãé¢ããäœçœ®ãååŸãããããã®æ
å ±ãå©çšããŠããŒã¹ã®ç§»åãåŠçããtouch_and_release
ã¡ãœãããåŒã³åºããŠããããã®ã¡ãœãããšãã®äžã§ããã«åŒã³åºãswap_pieces
ãšãããã«ããŒã¡ãœãããå®çŸ©ããŠãããããã«ããŒã¡ãœãããšããã®ã¯ç°¡åã«èšããšãã¡ãœããã®äžã§åŒã³åºãããã¡ãœããã§ã芪ã®ã¡ãœãããã·ã³ãã«ã«ä¿ã€åœ¹å²ãããã
###Grid.gd###
# æã觊ããäœçœ®ãšæãé¢ããäœçœ®ãå©çšããŠããŒã¹ã®ç§»åãåŠçããã¡ãœãã
func touch_and_release():
# æã觊ããäœçœ®ãšæãé¢ããäœçœ®ãšã®å·®ãèšç®
var difference = released_pos - touched_pos
# x 軞æ¹åã®å·®ã®çµ¶å¯Ÿå€ã y 軞æ¹åã®å·®ã®çµ¶å¯Ÿå€ãã倧ããå Žå
if abs(difference.x) > abs(difference.y):
# x 軞æ¹åã®å·®ã 0 ãã倧ããå Žå
if difference.x > 0:
# ãã«ããŒã¡ãœãããåŒã³ã觊ããäœçœ®ã®ããŒã¹ãšå³ã«é£æ¥ããããŒã¹ãå
¥ãæ¿ãã
swap_pieces(touched_pos, Vector2.RIGHT) # ãã®ããšå®çŸ©
# x 軞æ¹åã®å·®ã 0 ããå°ããå Žå
elif difference.x < 0:
# ãã«ããŒã¡ãœãããåŒã³ã觊ããäœçœ®ã®ããŒã¹ãšå·Šã«é£æ¥ããããŒã¹ãå
¥ãæ¿ãã
swap_pieces(touched_pos, Vector2.LEFT) # ãã®ããšå®çŸ©
# x 軞æ¹åã®å·®ã®çµ¶å¯Ÿå€ã y 軞æ¹åã®å·®ã®çµ¶å¯Ÿå€ããå°ããå Žå
elif abs(difference.x) < abs(difference.y):
# y 軞æ¹åã®å·®ã 0 ãã倧ããå Žå
if difference.y > 0:
# ãã«ããŒã¡ãœãããåŒã³ã觊ããäœçœ®ã®ããŒã¹ãšäžã«é£æ¥ããããŒã¹ãå
¥ãæ¿ãã
swap_pieces(touched_pos, Vector2.DOWN) # ãã®ããšå®çŸ©
# y 軞æ¹åã®å·®ã 0 ããå°ããå Žå
elif difference.y < 0:
# ãã«ããŒã¡ãœãããåŒã³ã觊ããäœçœ®ã®ããŒã¹ãšäžã«é£æ¥ããããŒã¹ãå
¥ãæ¿ãã
swap_pieces(touched_pos, Vector2.UP) # ãã®ããšå®çŸ©
# ããŒã¹ãå
¥ãæ¿ãããã«ããŒã¡ãœãã
func swap_pieces(pos, dir):
# æã觊ããäœçœ®ã®ããŒã¹ãå
šããŒã¹ã®äºæ¬¡å
é
åããååŸ
var touched_piece = all_pieces[pos.x][pos.y]
# æãé¢ããæ¹åã«é£æ¥ããããŒã¹ãå
šããŒã¹ã®äºæ¬¡å
é
åããååŸ
var target_piece = all_pieces[pos.x + dir.x][pos.y + dir.y]
# å
šããŒã¹ã®äºæ¬¡å
é
åäžãã©ã¡ãã®ããŒã¹ãååšããŠããå Žå
if touched_piece != null and target_piece != null:
# å
šããŒã¹ã®äºæ¬¡å
é
åã®äžããæã觊ããäœçœ®ã®ããŒã¹ãæãé¢ããæ¹ã«é£æ¥ããããŒã¹ã§äžæžããã
all_pieces[pos.x][pos.y] = target_piece
# å
šããŒã¹ã®äºæ¬¡å
é
åäžãæãé¢ããæ¹ã«é£æ¥ããããŒã¹ãæã觊ããäœçœ®ã®ããŒã¹ã§äžæžããã
all_pieces[pos.x + dir.x][pos.y + dir.y] = touched_piece
# ç€é¢äžãæã觊ããäœçœ®ã®ããŒã¹ã€ã³ã¹ã¿ã³ã¹ãæãé¢ããæ¹ã«ã«1ã°ãªãã移åãã
touched_piece.move(grid_to_pixel(pos.x + dir.x, pos.y + dir.y))
# ç€é¢äžãæãé¢ããæ¹ã«é£æ¥ããããŒã¹ã€ã³ã¹ã¿ã³ã¹ãæã觊ããäœçœ®ãžç§»åãã
target_piece.move(grid_to_pixel(pos.x, pos.y))
# ãããããããããããŒã¹ã®åŠçãå§ãŸãã®ã§ãèªååŠçã¹ããŒããåŠçäžã«ãã
is_waiting = true
äžèšã³ãŒãã§ããã¬ã€ã€ãŒããã©ãã°ããããŒã¹ãé£æ¥ããããŒã¹ãšå
¥ãæ¿ããåŠçãå®è£
ã§ãããå
¥åæäœãæ£ããæ©èœãããããããžã§ã¯ããå®è¡ããŠç¢ºèªããŠãããã
ããããã¯ããŒã¹ãå ¥ãæ¿ããããšã«èªåçã«å®è¡ãããã¹ãåŠçãå®è£ ããŠããã倧ãŸãã«ã¯ä»¥äžã®æµãã ã
- èªååŠçã¹ããŒããåŠçäžã«å€æŽããã
- ãããããããŒã¹ã1çµã§ãããããã§ãã¯ããã1çµã§ãããå Žåã¯ä»¥äžã®åŠçãã«ãŒãããã
- å šãŠã®ããŒã¹ããã§ãã¯ããŠãããããããŒã¹ã«ãã©ã°ãç«ãŠãã
- ãã©ã°ã®ç«ã£ãŠããããŒã¹ãåé€ããã
- åé€ããŠç©ºã«ãªã£ãã¹ããŒã¹ãžåãåã®äžã®ã°ãªããããããŒã¹ã移åãããŠè©°ããã
- ããŒã¹ãäžãžè©°ããããæåŸã«ç©ºã®ã¹ããŒã¹ã«æ°ããããŒã¹ãçæããã
- ãããããããŒã¹ã1çµããªããªã£ããèªååŠçã¹ããŒããåæ¢äžã«ããã
äžèšã®å€§ãŸããªæµããã³ãŒãã£ã³ã°ãããã
ãŸãå
ã«å®çŸ©ããããŒã¹ãå
¥ãæ¿ããswap_pieces
ã¡ãœããã®æåŸã«is_waiting = true
ã®1è¡ãè¿œå ããããã€ãŸãããã¯ãèªååŠçã¹ããŒããåŠçäžã«å€æŽããŠããã
###Grid.gd###
func swap_pieces(pos, dir):
var touched_piece = all_pieces[pos.x][pos.y]
var target_piece = all_pieces[pos.x + dir.x][pos.y + dir.y]
if touched_piece != null and target_piece != null:
all_pieces[pos.x][pos.y] = target_piece
all_pieces[pos.x + dir.x][pos.y + dir.y] = touched_piece
touched_piece.move(grid_to_pixel(pos.x + dir.x, pos.y + dir.y))
target_piece.move(grid_to_pixel(pos.x, pos.y))
# 以äžãè¿œå
# ãããããããããããŒã¹ã®èªååŠçãéå§ããã®ã§èªååŠçã¹ããŒããåŠçäžã«ãã
is_waiting = true
touch_input
ã¡ãœããã«ãããèªååŠçå®è¡äžã®ã¹ããŒãã ã£ããããšããif
æ§æãèšè¿°ãããã®ãããã¯ã®äžã«èªåçã«å®è¡ããããåŠçãè¿œå ããŠããããè¿œå ããäœçœ®ã¯ã¡ããã©æã§ã®æäœãçµãã£ãããšã ãåŠçãå®äºããåŸã«ã¯is_waiting = false
ã®1è¡è¿œå ããŠãèªååŠçã¹ããŒããåæ¢äžã«ããŠãããã
###Grid.gd###
func touch_input():
if Input.is_action_just_pressed("touch"):
# çç¥
if Input.is_action_just_released("touch"):
# çç¥
# ããããè¿œå
# ããåŠçäžã ã£ãã
if is_waiting:
# ãããããããŒã¹ã1çµã§ãããããã§ã㯠> ããéãã«ãŒã
while check_matches(): # ãã®ããšå®çŸ©
pass
# ãããããããŒã¹ã1çµããªãåŠçãå®äºããããèªååŠçã¹ããŒããåæ¢äžã«ãã
is_waiting = false
while
ã«ãŒãã§ããããããããŒã¹ã1çµã§ãããã°å¿
èŠãªåŠçãç¹°ãè¿ãããã«ããŠããã
ãŸãã¯while
ã«ãŒãã®ã«ãŒãæ¡ä»¶ãšããªã£ãŠããcheck_matches()
ã以äžã®éãã«å®çŸ©ããã
###Grid.gd###
# ãããããããŒã¹ã1çµã§ãããããã§ãã¯ããŠçµæãè¿ãã¡ãœãã
func check_matches() -> bool:
# ç€é¢ã® x軞æ¹åã®ã°ãªããã§ã«ãŒã
for i in width:
# ç€é¢ã® y軞æ¹åã®ã°ãªããã§ã«ãŒã
for j in height:
# ãã® ã°ãªããã®åº§æšã«ããŒã¹ãååšããå Žå
if all_pieces[i][j] != null:
# ãã®ã°ãªãã座æšã®ããŒã¹ããããããŠããã° true ãè¿ããŠã¡ãœãããçµäº
if match_at(i, j, all_pieces[i][j].color):
return true
# å
šããŒã¹ããã§ãã¯ããŠãããããŠããããŒã¹ãäžã€ããªããã° false ãè¿ã
return false
ç¶ããŠãwhile
ã«ãŒãå
ã®æåã®åŠçãå
šãŠã®ããŒã¹ããã§ãã¯ããŠãããããããŒã¹ã«ã¯ãã©ã°ãç«ãŠãããè¡ãããã®find_matches
ã¡ãœããã以äžã®ããã«å®çŸ©ããããããã§ããããã©ã°ãç«ãŠãããšèšãã®ã¯ãã€ãŸããããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ã®matched
ããããã£ãtrue
ã«å€æŽããããšããããšã ã
###Grid.gd###
# ãããããŠããããŒã¹ãèŠã€ããŠãã©ã°ãç«ãŠãã¡ãœãã
func find_matches():
# ç€é¢ã® x 軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for i in width:
# ç€é¢ã® y 軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for j in height:
# ãã®ã°ãªããã®åº§æšã«ããŒã¹ãååšããå Žå
if all_pieces[i][j] != null:
# çŸåšã®è²ããã®ã°ãªãã座æšã®ããŒã¹ã®è²ãšå®çŸ©ãã
var current_color = all_pieces[i][j].color
# ãããã® x 軞座æšã x 軞æ¹åã®ã°ãªããæ° - 2 ããå°ãããã°
if i < width - 2:
# ãã®ããŒã¹ã®å³é£ãšããã«ãã®å³é£ã«ããŒã¹ãååšããå Žå
if all_pieces[i+1][j] != null \
and all_pieces[i+2][j] != null:
# ãããã®ããŒã¹ã®è²ãçŸåšã®è²ãšåãå Žå
if all_pieces[i+1][j].color == current_color \
and all_pieces[i+2][j].color == current_color:
# ãã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i][j].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i][j].make_matched()
# ãã®ããŒã¹ã®å³é£ã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i+1][j].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i+1][j].make_matched()
# ãã®ããŒã¹ã®ïŒã€å³é£ã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i+2][j].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i+2][j].make_matched()
# ãããã®ããŒã¹ã® y 座æšã y 軞æ¹åã®ã°ãªããæ° - 2 ããå°ãããã°
if j < height - 2:
# ãã®ããŒã¹ã®äžãšããã«ãã®äžã«ããŒã¹ãååšããå Žå
if all_pieces[i][j+1] != null \
and all_pieces[i][j+2] != null:
# ãããã®ããŒã¹ã®è²ãçŸåšã®è²ãšåãå Žå
if all_pieces[i][j+1].color == current_color \
and all_pieces[i][j+2].color == current_color:
# ãã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i][j].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i][j].make_matched()
# ãã®ããŒã¹ã®äžã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i][j+1].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i][j+1].make_matched()
# ãã®ããŒã¹ã®ïŒã€äžã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠãªããã°
if not all_pieces[i][j+2].matched:
# ãã®ããŒã¹ã® matched ããããã£ã true ã«ããŠãã©ã°ãç«ãŠã
# åæã«ãã®ããŒã¹ã®ãã¯ã¹ãã£ã®è²ãåéæã«ãã
all_pieces[i][j+2].make_matched()
ãã®find_matches
ã¡ãœãããtouch_input
ã¡ãœããå
ã®while
ã«ãŒãã®äžã§åŒã³åºãå¿
èŠãããã®ã§ã以äžã®ããã«æŽæ°ãããã
###Grid.gd###
func touch_input():
if Input.is_action_just_pressed("touch"):
# çç¥
if Input.is_action_just_released("touch"):
# çç¥
if is_waiting:
# ãããããããŒã¹ã1çµã§ãããããã§ã㯠> ããéãã«ãŒã
while check_matches():
# ãããããããŒã¹ãèŠã€ããŠãã©ã°ãç«ãŠã
find_matches()
# åŠçãèŠèŠçã«ãããããããããã0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
is_waiting = false
ããã§ããããããåããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ã®matched
ããããã£ã¯true
ã«ãªããããŒã¹ã®è²ãåéæã«ãªãã¯ãã ããããžã§ã¯ããå®è¡ããŠç¢ºèªããŠã¿ããã
ç¶ããŠãwhile
ã«ãŒãå
ã®2çªç®ã®åŠçããã©ã°ã®ç«ã£ãŠããããŒã¹ãåé€ããããå®è¡ããdelete_matches
ã¡ãœããã以äžã®ããã«å®çŸ©ããŠã»ããã
###Grid.gd###
# ãããããŠããããŒã¹ãåé€ããã¡ãœãã
func delete_matches():
# ç€é¢ã® x 軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for i in width:
# ç€é¢ã® y 軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for j in height:
# ãã®ã°ãªãã座æšã«ããŒã¹ãååšããå Žå
if all_pieces[i][j] != null:
# ãã®ã°ãªãã座æšã®ããŒã¹ã«ãã©ã°ãç«ã£ãŠããã
if all_pieces[i][j].matched:
# ãã®ã°ãªãã座æšã®ããŒã¹ãåé€ãã
all_pieces[i][j].queue_free()
# å
šããŒã¹ã®äºæ¬¡å
é
åãããã®ã°ãªãã座æšã®èŠçŽ ã空ã«ãã
all_pieces[i][j] = null
ãã®delete_matches
ã¡ãœãããtouch_input
ã¡ãœããã®while
ã«ãŒãå
ã«è¿œå ãããã
###Grid.gd###
func touch_input():
if Input.is_action_just_pressed("touch"):
# çç¥
if Input.is_action_just_released("touch"):
# çç¥
if is_waiting:
# ãããããããŒã¹ã1çµã§ãããããã§ã㯠> ããéãã«ãŒã
while check_matches():
# ãããããããŒã¹ãèŠã€ããŠãã©ã°ãç«ãŠã
find_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãã©ã°ãç«ãŠãããŒã¹ãåé€ãã
delete_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãããããããŒã¹ã1çµããªãåŠçãå®äºããããåŠçäžç¶æ
ãç¡å¹ã«ãã
is_waiting = false
ããã§ãããããããŒã¹ãåéæã«ãªã£ãåŸãåé€ãããã¯ãã ããããžã§ã¯ããå®è¡ããŠç¢ºèªããŠã¿ããã
次ã¯ãwhile
ã«ãŒãå
ã®3çªç®ã®åŠçãåé€ããŠç©ºã«ãªã£ãã¹ããŒã¹ãžåãåã®äžã®ã°ãªããããããŒã¹ã移åãããŠè©°ããããå®è¡ããcollapse_columns
ã¡ãœããã以äžã®ããã«æŽæ°ããŠã»ããã
###Grid.gd###
# åããšã«ããŒã¹ãååšããªãã¹ããŒã¹ã«ã¯äžã«ããããŒã¹ã移åããŠè©°ããã¡ãœãã
func collapse_columns():
# ç€é¢ã® xã軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for i in width:
# ç€é¢ã® yã軞æ¹åã®ã°ãªããæ°ã ãã«ãŒã
for j in height:
# ãã®ã°ãªãã座æšã«ããŒã¹ãååšããªãïŒnullïŒå Žå
if all_pieces[i][j] == null:
# ãã®ã°ãªããã® y 座æšããïŒã€äžã®è¡ããäžçªäžã®è¡ãŸã§ã®ã«ãŒã
for k in range(j + 1, height):
# 1ã€äžã®ã°ãªããã«ããŒã¹ãååšããŠããå Žå
if all_pieces[i][k] != null:
# ïŒã€äžã®ã°ãªããã®ããŒã¹ãäžã®ç©ºããŠããã°ãªããã«ç§»åãã
all_pieces[i][k].move(grid_to_pixel(i, j))
# å
šããŒã¹ã®äºæ¬¡å
é
åã®çŸåšã®ã°ãªãã座æšã«ïŒã€äžã®ããŒã¹ãå
¥ãã
all_pieces[i][j] = all_pieces[i][k]
# å
šããŒã¹ã®äºæ¬¡å
é
åã®ïŒã€äžã®ã°ãªãã座æšã空ã«ãã
all_pieces[i][k] = null
# ã«ãŒããæãã
break
ãã®collapse_columns
ã¡ãœãããtouch_input
ã¡ãœããã®while
ã«ãŒãå
ã§åŒã³åºããã
###Grid.gd###
func touch_input():
if Input.is_action_just_pressed("touch"):
# çç¥
if Input.is_action_just_released("touch"):
# çç¥
if is_waiting:
# ãããããããŒã¹ã1çµã§ãããããã§ã㯠> ããéãã«ãŒã
while check_matches():
# ãããããããŒã¹ãèŠã€ããŠãã©ã°ãç«ãŠã
find_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãã©ã°ãç«ãŠãããŒã¹ãåé€ãã
delete_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# 空ã®ã¹ããŒã¹ã«åãåã®äžã«ããããŒã¹ã移åããŠè©°ãã
collapse_columns()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãããããããŒã¹ã1çµããªãåŠçãå®äºããããåŠçäžç¶æ
ãç¡å¹ã«ãã
is_waiting = false
ããã§ããŒã¹ãåé€ãããŠç©ºããã¹ããŒã¹ã«äžã«ããããŒã¹ãè©°ããä»çµã¿ãã§ãããããã«while
ã«ãŒãã«ããã空ããã¹ããŒã¹ã«ããŒã¹ãè©°ãã ããšã« ãããããããŒã¹ãé£ç¶çã«åé€ãããã¯ãã ããããžã§ã¯ããå®è¡ããŠç¢ºèªããŠã¿ããã
æåŸã«ããã§ã«å®çŸ©æžã¿ã®spawn_pieces
ã¡ãœãããããŒã¹ãäžã«è©°ããããšã«åŒã³åºãããã«ããã°ã空ãã空éã«æ°ããããŒã¹ãé
眮ããç€é¢ãåãŸãã¯ãã ãtouch_input
ã¡ãœããã以äžã®ããã«æŽæ°ãããã
###Grid.gd###
func touch_input():
if Input.is_action_just_pressed("touch"):
# çç¥
if Input.is_action_just_released("touch"):
# çç¥
if is_waiting:
# ãããããããŒã¹ã1çµã§ãããããã§ã㯠> ããéãã«ãŒã
while check_matches():
# ãããããããŒã¹ãèŠã€ããŠãã©ã°ãç«ãŠã
find_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãã©ã°ãç«ãŠãããŒã¹ãåé€ãã
delete_matches()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# 空ã®ã¹ããŒã¹ã«åãåã®äžã«ããããŒã¹ã移åããŠè©°ãã
collapse_columns()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# 空ã®ã¹ããŒã¹ã«æ°ããããŒã¹ãçæããŠé
眮ãã
spawn_pieces()
# 0.3ç§åŸ
æ©
yield(get_tree().create_timer(0.3), "timeout")
# ãããããããŒã¹ã1çµããªãåŠçãå®äºããããåŠçäžç¶æ
ãç¡å¹ã«ãã
is_waiting = false
以äžã§èªååŠçéšåã®ã³ãŒãã£ã³ã°ã¯å®äºã ããã®ãã¥ãŒããªã¢ã«ã®æé ããããŸã§ãšãªããæåŸã«ãããžã§ã¯ããå®è¡ããŠåäœã確èªããŠãããã
ãµã³ãã«ã²ãŒã
ä»åã®ãã¥ãŒããªã¢ã«ã§äœæãããããžã§ã¯ããããã«ãã©ãã·ã¥ã¢ãããããµã³ãã«ã²ãŒã ãçšæãããã¡ãªã¿ã«ä»¥äžã®GIFç»åã¯3åéã§åçããŠããã®ã§å®éã¯ããå°ãç©ããã ã
ãµã³ãã«ã²ãŒã ã®ãããžã§ã¯ããã¡ã€ã«ã¯ãGitHubãªããžã㪠ã«çœ®ããŠããã®ã§ããããã .zip ãã¡ã€ã«ãããŠã³ããŒãããŠããã ãããSampleããã©ã«ãå ã®ãproject.godotããã¡ã€ã«ã Godot Engine ã§ã€ã³ããŒãããã°ç¢ºèªããŠããã ããã¯ãã ã
ãããã«
ä»åã¯ããã3ããºã«ã²ãŒã ãäœã£ããã·ã³ãã«ãªæäœã§äœåã§ã楜ãããã¢ãã€ã«ã²ãŒã ã«ã¯ãã£ãŠã€ãã®ã²ãŒã ãžã£ã³ã«ã ã
ä»åã®ãããªã·ã³ãã«ãªããã3ããºã«ã²ãŒã ãäœããšãã®ãã€ã³ãããŸãšããŠãããã
- æäœéå¿ èŠãªã·ãŒã³ã¯ç€é¢ãšããŒã¹ã®ïŒã€ã ãã
- é圢ã®ããŒã¹ã·ãŒã³ãäœã£ãŠããããããç¶æ¿ããŠåè²ã®ããŒã¹ã·ãŒã³ãäœãã
- äºæ¬¡å é åãå©çšããŠç€é¢ã°ãªããã«é 眮ããããŒã¹ã管çããã
- ããŒã¹ãå ¥ãæ¿ãããšãã¯ãç»é¢äžã®ããŒã¹ã®äœçœ®ã®å ¥ãæ¿ããšäºæ¬¡å é åã®èŠçŽ ã®å ¥ãæ¿ãã®äž¡æ¹ãå¿ èŠã
- ã¹ã¯ãªããã¯ä»¥äžããã€ã³ãã
- ãã¬ã€ã€ãŒãããŒã¹ãåããåŠç
- ç»é¢ã«æã觊ããäœçœ®ãšç»é¢ããæãé¢ããäœçœ®ãååŸããã
- ïŒã€ã®äœçœ®ãã°ãªããå ãïŒæå¹ãªæäœãïŒå€å®ããã
- ïŒã€ã®äœçœ®ã®å·®ããããŒã¹ãå ¥ãæ¿ããæ¹åãå€å®ããã
- ãããããæã®èªååŠçïŒã«ãŒãïŒã
- ãããããŠããããŒã¹ã1çµã§ãããããã§ãã¯ïŒã«ãŒãã®æ¡ä»¶ïŒã
- ãããããŠããããŒã¹ã«ãã©ã°ãç«ãŠãã
- ãã©ã°ã®ç«ã£ãŠããããŒã¹ãåé€ããã
- åé€ãããŠç©ºããã¹ããŒã¹ã«äžã®ããŒã¹ãè©°ããã
- è©°ããŠç©ºããã¹ããŒã¹ã«æ°ããããŒã¹ãçæããã
- ãã¬ã€ã€ãŒãããŒã¹ãåããåŠç
åç §
- ãCandy CrushïŒãã£ã³ãã£ãŒã¯ã©ãã·ã¥ïŒã
- ãRoyal MatchïŒãã€ã€ã«ãããïŒã
- ãToon BlastïŒãã¥ãŒã³ãã©ã¹ãïŒã
- ãPuzzleïŒDragonsïŒããºã«ïŒãã©ãŽã³ãºïŒé称ããºãã©ïŒïŒã
- YouTube: Part 0: Why Godot? - Make a Match 3 game like Candy Crush Using Godot.