ã¯ããã«
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ Godot 㧠2D ã²ãŒã ã«ããããã°ãªããããŒã¹ç§»åããå®è£ ããŠã¿ãŸããã§ããã ãã·ã³ãã«ã«ã
ããããã°ãªããããŒã¹ç§»åã£ãŠäœãªãïŒãšããæ¹ãããã£ãããããšãã°ãªããããŒã¹ç§»åãšã¯ãã°ãªããïŒæ Œåç¶ã®ç·ïŒã§çééã«åºåãããã²ãŒã ã®ç»é¢äžããã£ã©ã¯ã¿ãŒãªã©ã®ãªããžã§ã¯ãã 1 ã°ãªããïŒ1 ãã¹ïŒãã€ç§»åããåãã®ããšã§ãã
å°ãæã®ã²ãŒã ãäŸã«ãããŠæçž®ã§ãããããã¡ã€ã€ãŒãšã ãã¬ã ã·ãªãŒãºããã¿ã¯ãã£ã¯ã¹ãªãŠã¬ããªã©ãã·ãã¥ã¬ãŒã·ã§ã³ã²ãŒã ã§ããèŠãããŸããããšã¯ããã¡ã€ãã«ãã¡ã³ã¿ãžãŒããããã©ãŽã³ã¯ãšã¹ããã·ãªãŒãºãªã©ã® RPG ã 2D ã°ã©ãã£ãã¯æ代ã¯ããããäžã®ãã£ã©ã¯ã¿ãŒç§»åã芳å¯ãããšãã°ãªããããŒã¹ç§»åãè¡ã£ãŠããŸãããããªã¹ãªããã®ããºã«ã²ãŒã ã§ãããŒã¹ããããã¯ã®ç§»åãã°ãªããããŒã¹ã«ãªã£ãŠããããšãçµæ§ãããŸãã
ãã®ããã«ãã°ãªããããŒã¹ç§»åã¯ããŸããŸãªãžã£ã³ã«ã®ã²ãŒã ã§äœ¿çšãããŠããŠããã®æ±çšæ§ã¯ããªãé«ããšèšããŸãã
Environment
ãã®ãã¥ãŒããªã¢ã«ã¯ä»¥äžã®ç°å¢ã§äœæããŸããã
ã»Godot ã®ããŒãžã§ã³: 4.3
ã»ã³ã³ãã¥ãŒã¿ã®OS: macOS 14.6.1
ã¡ãªã¿ã«ãä»åäœã£ããããžã§ã¯ãã¯ãGitHubãªããžã㪠ã«çœ®ããŠããŸãã
ãŠã€ã³ããŠã®èšå®
Godot ã§æ°èŠãããžã§ã¯ããäœæãããããŸãã¯ãããžã§ã¯ãèšå®ã§ããããããžã§ã¯ã ã¡ãã¥ãŒ > ãããžã§ã¯ãèšå® ãéããGeneral ã¿ãã®ãµã€ãããŒãã Display > Window ãéžæããŠããŠã€ã³ããŠã®ãµã€ãºãšã¹ãã¬ãããèšå®ããŸãã
- Size:
- Width: 240
- Height: 176
- Windows Width Override: 960
- Windows Height Override: 704
- Test Width: 960
- Test Height: 704
- Stretch:
- Mode: viewport
- Aspect: keep
ã¢ã»ããã®ã€ã³ããŒã
ãããžã§ã¯ãã§äœ¿ã£ãã¢ã»ãã㯠KENNEY ã®ãµã€ãããããŠã³ããŒããããŠããã ãããã®ã§ããå©çšããã®ã¯ 1-Bit Pack ãšããã¢ã»ããããã¯ã§ãããã®çŽ æŽãããããç¡æã®çŽ æã«æè¬ããã«ã¯ããããŸããã
ããŠã³ããŒããããããã® Tilesheet ãã©ã«ãå ã® colored-transparent_packed.png ãã¡ã€ã«ããšãã£ã¿ã®ãã¡ã€ã«ã·ã¹ãã ããã¯ãžãã©ãã°&ããããããŠãããžã§ã¯ãã«ã€ã³ããŒãããŸãã
ãã¡ã€ã«ãã€ã³ããŒãããçŽåŸã¯ç»åããŒãããæãïŒãã©ãïŒã«ãªã£ãŠããã®ã§ããŸã ãããžã§ã¯ã ã¡ãã¥ãŒ > ãããžã§ã¯ãèšå® ãéããGeneral ã¿ãã®ãµã€ãã㌠Rendering > Textures ãéžæããŠãããã©ã«ãã®ãã¯ã¹ãã£ãã£ã«ã¿ã Nearest ã«èšå®ããŸãã
- Canvas Textures
- Default Texture Filter: Nearest
ããã§ãã¯ã»ã«ã¢ãŒãç¹æã®ãšããžã®å¹ããç»åã«ãªãã®ã§ãã
ã¿ã€ã«ãããã§ã²ãŒã ã®äžçãçšæãã
ã¿ã€ã«ãããã§èæ¯ãäœã£ãŠãããªããšããã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒãã°ãªããããŒã¹ç§»åã§ããŠããã®ããããã«ããã®ã§ããŸãæåã®ã·ãŒã³ãšããŠãã²ãŒã ã®äžçãã¿ã€ã«ãããã§äœããŸããã¡ãªã¿ã«ãã¿ã€ã«ãããã®äœãæ¹ã¯ä»¥äžã®èšäºãåèã«ãªãããšæããŸããä»åã®ãããžã§ã¯ãã§ã¯ TileMapLayer ããŒãã¯ã²ãšã€ã ãã«ããŠããŸãã
åèèšäºïŒ ð€ TileMapLayerã§ã¿ã€ã«ããããäœãã
ã·ãŒã³ããã¯ã¯ãããªæãã«ãªããŸãããã·ã³ãã«ã
ã¿ã€ã«ãããã®ãçµµæããããåã«ãTileSet ãšãã£ã¿ã§ Paint ã¿ããéããŠãPaint Properties > Physics > Physics Layer 0 ãéžæããæšãšãå¢ã®ã¿ã€ã«ã«è¡çªåœ¢ç¶ãèšå®ããŠãããŸããããã§ãã£ã©ã¯ã¿ãŒãéãæããããªãã¿ã€ã«ã«ãªãã¯ãã§ãã
ãããŠã TileMap ãšãã£ã¿ã§é©åœã«ãçµµæãããŸããæå ¥ãã®è¡ãå±ããå¢å°ã§ãã
ãã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒãäœã
ãã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒã®ã·ãŒã³ãäœæããŸããä»åãã°ãªããããŒã¹ç§»åããã察象ãã³ã¬ã§ããã
æ°èŠã·ãŒã³ã CharacterBody2D ã¯ã©ã¹ã®ããŒããã«ãŒãããŒãã«ããŠäœæããŸããã«ãŒãããŒãã®ååããPlayerãã«å€æŽããŸãããããšã¯ã«ãŒãããŒãã«ä»¥äžã®åããŒããã¡ãè¿œå ããŸãã
- Sprite2D
- CollisionShape2D
- RayCast2D
ãããã Player ã·ãŒã³ã®åããŒããç·šéããŠãããŸãã
Sprite2D ããŒã
Sprite2D ããŒãã®åããããã£ç·šéåŸã®ã€ã³ã¹ãã¯ã¿ãå ã«ãèŠãããŠãããŸãã
ç·šéããããããã£ã«ã€ããŠãããã説æããŸãã
ã€ã³ã¹ãã¯ã¿ãŒã® Texture ããããã£ã«æåã«çšæããã¢ã»ãããã¡ã€ã« res://colored-transparent_packed.png ããã©ãã°ããŠé©çšããŸããã
Offset > Centered ããããã£ããªãã«ããŸãããããã§ããã®ããŒãã®äœçœ®ïŒPosition ããããã£ïŒããã¯ã¹ãã£ã®äžå€®ã§ã¯ãªãå·Šäžè§ã«ãªã£ãŠãã¡ããã© 1 ã°ãªããã«ãã£ã©ã¯ã¿ãŒã®ã¹ãã©ã€ããåãŸãããã«ãªããŸãã
Region > Enabled ããªã³ã«ããŸããã
Edit Region ãã¿ã³ãã¯ãªãã¯ããŠãRegion Editor ãéããããã§å²ãåœãŠããã¹ãã©ã€ãã®ãã¯ã¹ãã£é åãæå®ããŠãããŸããäœæ¥ããããããã«ãå±éã¢ã€ã³ã³ãã¯ãªãã¯ããŠããã«ãåºããŠãããŸãã
ããã«äžéšã® snapã¢ãŒã 㧠ã°ãªããã¹ããã ãéžæããç¶ã㊠ã¹ããã ã 16px 16px ã«ããŸããããã§ã°ãªããã®ãµã€ãºãã¹ãã©ã€ãã·ãŒãã®ãã¯ã¹ã㣠1 ã€åãšåããµã€ãºã«ãªããŸããããã®ç¶æ ã§ãã¹ãã©ã€ãã·ãŒãäžã§ãã©ãã°æäœã«ãããçæ§ãã®ãã¯ã¹ãã£ãç¯å²éžæããŸããã
以äžã§ Sprite2D ããŒãã®ããããã£ç·šéã¯çµããã§ãã
CollisionShape2D ããŒã
ãã®ããŒã㧠CharactorBody2D ããŒãïŒã«ãŒãããŒãïŒã«å¯ŸããŠè¡çªåœ¢ç¶ãèšå®ããŸããç·šéåŸã®ã€ã³ã¹ãã¯ã¿ã¯ãããªæãã«ãªããŸããã
2Dã¯ãŒã¯ã¹ããŒã¹äžã¯ãããªæãã«ãªã£ãŠããŸãã
ã§ã¯ãç·šéããããããã£ã«ã€ããŠç¢ºèªããŠãããŸãã
Shape ããããã£ã« æ°èŠ RectangleShape2D ãªãœãŒã¹ãé©çšããŸãã
é©çšãã RectangleShape2D ã®ããããã£ã®ãã¡ Size ã (x: 16 px, y: 16 px) ã«èšå®ããŸãããã㯠2Dã¯ãŒã¯ã¹ããŒã¹ã§èŠèŠçã«èª¿æŽããŠãOKã§ããã
Sprite2D ããŒãã®ãã¯ã¹ãã£ã®äœçœ®ã«åãããããã Transform > Position ããããã£ã®å€ã (x: 8, y: 8) ã«ããŸãã
RayCast2D ããŒã
ãã®ããŒãã¯ã°ãªããããŒã¹ç§»åã§ã®è¡çªå€å®ã«ãšãŠã䟿å©ã§ãã2Dã¯ãŒã¯ã¹ããŒã¹äžã¯ã³ãªãžã§ã³åœ¢ç¶ãç¢å°åã§è¡šãããŸãããã®ç¢å°ãšãªããžã§ã¯ããéãªã£ãå Žåã«è¡çªãæ€ç¥ãããšããããã§ãããããå©çšããŠããã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒã®åæ¹ã«ãããªããžã§ã¯ããšã®è¡çªãæ€ç¥ããããã®å ã«ã¯é²ããªãããã«ãããªã©å¶åŸ¡ããããšãã§ããŸããå察ã«ããã®ããŒãããªããšãããšããã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒãšé害ç©ã®ã¿ã€ã«ã«ããããè¡çªåœ¢ç¶ãèšå®ãããŠããŠãããã£ã©ã¯ã¿ãŒãé害ç©ã®æ¹ãžã°ãªããããŒã¹ç§»åããããšãè¡çªããã«ééã§ããŠããŸããŸãã
ç·šéåŸã®ã€ã³ã¹ãã¯ã¿ã¯ãã®ããã«ãªããŸããã
2Dã¯ãŒã¯ã¹ããŒã¹äžã¯ãããªæãã§ãã
ããããã£ããããã確èªããŸããã
- Enabled ã OnïŒããã©ã«ãïŒã®ãŸãŸã«ããŠãããŸããããã§è¡çªå€å®ãå¯èœã«ãªããŸãã
- Transform > Position ããããã£ã (x: 8, y: 8) ã«ããŸãããSprite2D ã®ãã¯ã¹ãã£ã®äžå€®ã«äœçœ®ãåãããããã§ããã
- Target Position ã (x: 16, y: 0) ã«ããŸãããããã¯æ«å®çã«åæå€ãèšå®ããŠããã ãã§ãå®éã¯ã¹ã¯ãªããã§ãã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒã®ç§»åæäœã«åãããŠå€ãå€æŽããããšã«ãªããŸãã
- Collide With 㯠Areas 㯠OffãBodies 㯠OnïŒããã©ã«ãïŒã®ãŸãŸã«ããŠãããå ã«ã¿ã€ã«ã»ããã«èšå®ããæšãå¢ã®ã¿ã€ã«ã¯ç©çããã£ãªã®ã§ Bodies ã«ãã§ãã¯ãå ¥ã£ãŠããã°ãè¡çªãæ€ç¥ãããŸãã
Player ããŒãã«ã°ãªããããŒã¹ç§»åãå®è£ ãã
ã€ã³ãããããããèšå®ãã
å ã«ããŒããŒãã®ããŒå ¥åã§ãã¬ã€ã€ãŒãã£ã©ã¯ã¿ãŒãåãããããã«ããããžã§ã¯ãèšå®ã®ã€ã³ããããããã«ã¢ã¯ã·ã§ã³ãè¿œå ããŠãããŸãããããžã§ã¯ãã¡ãã¥ãŒ > ãããžã§ã¯ãèšå® ãéžæã㊠Input Map ã¿ããéãã以äžã®ïŒã€ã® Action ãè¿œå ããŸãã
- move_right: D ããŒ
- move_left: A ããŒ
- move_down: S ããŒ
- move_up: W ããŒ
Player ããŒãã«ã¹ã¯ãªãããã¢ã¿ããããŠç·šéãã
Player ïŒã«ãŒãïŒããŒãã«ã¹ã¯ãªãããã¢ã¿ããããŸããã§ãããã£ãã³ãŒãããã¡ãã§ããç°¡åã§ããã説æã®ã³ã¡ã³ããå ¥ããŠããŸãã
extends CharacterBody2D
# ã€ã³ããããããã®ã¢ã¯ã·ã§ã³ã«çžå¿ããããŒã«æ¹åãã¯ãã«ã®å€ãèšå®ããèŸæž
const inputs = {
"move_right": Vector2.RIGHT,
"move_left": Vector2.LEFT,
"move_down": Vector2.DOWN,
"move_up": Vector2.UP
}
# ã¿ã€ã«äžã€åã®çžŠæšªãµã€ãºãšåã 16 ãæ ŒçŽ
var grid_size = 16
# RayCast2D ãåç
§
@onready var ray_cast_2d: RayCast2D = $RayCast2D
# ã€ã³ããããããã®ã¢ã¯ã·ã§ã³ã®ãããããå
¥åãããããåŒæ°ã« inputs ã®ããŒãæž¡ã㊠move é¢æ°ãåŒã³åºã
func _unhandled_input(event):
for action in inputs.keys():
if event.is_action_pressed(action):
move(action)
# å
¥åãããããŒã«åãã㊠RayCast2D ã®æ¹åãæŽæ°ããè¡çªå€å®ããªããã° 1 ã°ãªãã移å
func move(action):
var destination = inputs[action] * grid_size
ray_cast_2d.target_position = destination
ray_cast_2d.force_raycast_update()
if not ray_cast_2d.is_colliding():
position += destination
ããã§ã°ãªããããŒã¹ç§»åãå¶åŸ¡ã§ããããã«ãªã£ãã¯ãã§ããã
World ã·ãŒã³ã« Player ã·ãŒã³ã®ã€ã³ã¹ã¿ã³ã¹ãè¿œå ãã
Player ã·ãŒã³ãå®æããã®ã§ããã®ã€ã³ã¹ã¿ã³ã¹ããŒãã World ã·ãŒã³ã«è¿œå ããŸãã
- World.tscn ã·ãŒã³ãéããŠãã«ãŒãã® World ããŒãã« Player.tscn ã·ãŒã³ã®ã€ã³ã¹ã¿ã³ã¹ããŒããè¿œå ããŸãã
- 2Dã¯ãŒã¯ã¹ããŒã¹ã§ãç»é¢ã®ã ãããäžå€®ãããïŒé©åœïŒã« Player ããŒããé 眮ããŸãã
ã°ãªããããŒã¹ç§»åã®åäœç¢ºèªããã
äœæ¥ãå®äºããã®ã§ãå®éã«ã°ãªããããŒã¹ç§»åãåé¡ãªãã§ãããã©ãã確èªããŠã¿ãããšæããŸãã
ç¹ã« RayCast2D ããŒãã®ç¢å°åã³ãªãžã§ã³åœ¢ç¶ã確èªãããã®ã§ãå ã« ãããã°(Debug) ã¡ãã¥ãŒ > Visible Collision Shapes ãæå¹ã«ããŠãããŸãã
Main Scene ã« World.tscn ãèšå®ããŠãããžã§ã¯ããå®è¡ããŸããçæ§ãã°ãªããããŒã¹ç§»åã§å¢å°ãããã€ããŠããŸãããšãã§ããªãå¥è¡ã§ããã
äžåã®ããŒå ¥åã§ãã¿ã€ã« 1 ã€åã ã移åããŠããã®ãããããããã ããŸãã§ããããããããšæšãå¢ã®ã¿ã€ã«ã®æ¹åã«ç§»åããããšããŠããè¡çªå€å®ã«ãã£ãŠç§»åã§ããªãããšã確èªã§ããŸããïŒãäžå¯§ã«ãç¢å°ãèµ€ã衚瀺ãããŸããïŒã
ãããã«
ä»å㯠2D ã²ãŒã ã«ãããã°ãªããããŒã¹ç§»åãå®è£ ããŠã¿ãŸãããæ¯èŒçç°¡åã«å®è£ ã§ããã®ã§ã¯ãªãããšæããŸããããã«çºå±ãããã°ããã¬ã€ã€ãŒãæµã®ãã£ã©ã¯ã¿ãŒãèªåã§ã°ãªããããŒã¹ç§»åããããªã©ã§ããããªæ°ãããŸãããããªãã®ã²ãŒã éçºã®ã圹ã«ç«ãŠããªã幞ãã§ãã
åç §
- KENNEY
- Godot Docs: Using TileSets
- Godot Docs: Using TileMaps
- Godot Docs: TileSet
- Godot Docs: RayCast2D
- KidsCanCode: Grid-based movement
- YouTube: GDQuest: Grid-based movement Godot 3 demo overview
- YouTube: GDQuest: Exploring grid-based movement
- YouTube: Emi: Make your first 2D grid-based game from scratch in Godot