[06]エネミー、スコア、HPを追加
エネミー
Area2D、AnimationSprite、CollisionShape、RayCast(左右)

スクリプト
エネミーは壁のないところに配置するのでRayCastで地面があるか判定してから反転の動きです。
左右に壁がある場所に配置する場合は「!」を無しで
extends Area2D
const SPEED = 50.0
var direction = -1
@onready var game_manager: Node2D = %GameManager
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var ray_cast_right: RayCast2D = $RayCastRight
func _physics_process(delta: float) -> void:
if !ray_cast_left.is_colliding():
direction = 1
animated_sprite_2d.flip_h = true
if !ray_cast_right.is_colliding():
direction = -1
animated_sprite_2d.flip_h = false
if !ray_cast_left.is_colliding() and !ray_cast_right.is_colliding():
direction = -1
animated_sprite_2d.flip_h = false
position.x += direction * SPEED * delta
animated_sprite_2d.play("walk")
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("player"):
game_manager.take_damage()
スコアとハート(HP)はCanvasLayerに背景になるColorRectを入れてLabelとハートを横に並べるHBoxContainerにTextureRectを入れてハートを表示しています
Font:https://tinyworlds.itch.io/free-pixel-font-thaleah

game_manager.gd
エネミーにプレイヤーが触れたらエネミー側からtake_damageが呼ばれハートが減ります
extends Node2D
@onready var score_label: Label = %scoreLabel
@onready var heart_parent: HBoxContainer = %heart
var score = 0
var heart = 3
var heart_list : Array[TextureRect]
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
score_label.text = "Score : " + str(score)
for child in heart_parent.get_children():
heart_list.append(child)
func _process(delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func add_coin():
score += 1
score_label.text = "Score : " + str(score)
func take_damage():
if heart > 1:
heart -= 1
update_heart()
else:
get_tree().reload_current_scene()
func update_heart():
for i in range(heart_list.size()):
heart_list[i].visible = i < heart
prints("i:",i, " H:", heart)