extends CharacterBody2D ## 小怪:只在平台上左右巡逻,**不发射任何东西**,也**打不死**。 ## 作用纯粹是挡路 + 碰到扣血,逼玩家走位。 ## 弹幕交给专门的「弹幕源」节点负责。 @export_category("巡逻") @export var speed: float = 28.0 ## 巡逻方向:-1 向左,1 向右 @export var start_direction: int = -1 ## 掉头后的冷却时间:防止贴着墙时每帧都掉头、结果原地抖动 @export var turn_cooldown: float = 0.25 ## 速度设成 0 就变成待在原地不动的障碍 @export var damage: int = 1 var direction: int = -1 var _turn_timer: float = 0.0 @onready var sprite: Sprite2D = $Sprite @onready var floor_probe: RayCast2D = $FloorProbe @onready var touch_area: Area2D = $TouchArea func _ready() -> void: add_to_group("enemy") direction = -1 if start_direction < 0 else 1 _apply_direction() touch_area.body_entered.connect(_on_touch) func _physics_process(delta: float) -> void: if _turn_timer > 0.0: _turn_timer -= delta if not is_on_floor(): velocity += get_gravity() * delta velocity.x = float(direction) * speed # 撞到墙 或 前方脚下没有地板 -> 掉头(带冷却,掉头后先走一段再说) if _turn_timer <= 0.0: if is_on_wall() or (is_on_floor() and not floor_probe.is_colliding()): direction = -direction _turn_timer = turn_cooldown _apply_direction() move_and_slide() func _apply_direction() -> void: if sprite != null: sprite.flip_h = direction > 0 if floor_probe != null: floor_probe.position.x = float(direction) * 6.0 func _on_touch(body: Node2D) -> void: # 打不死:踩上去也一样只是扣血 if body.has_method("hurt"): body.hurt(damage)