73 lines
2.0 KiB
GDScript
73 lines
2.0 KiB
GDScript
extends CharacterBody2D
|
|
|
|
var HP = 30
|
|
var Blink : Node2D = null
|
|
@onready var animated = $Block_damage_animation
|
|
var is_playing_blink: bool = false
|
|
var is_in_observe : bool = false
|
|
var is_playing_transition: bool = false
|
|
var down_SPEED = 1000.0
|
|
var down : bool = false
|
|
|
|
func _ready() -> void:
|
|
add_to_group("block")
|
|
if not animated.animation_finished.is_connected(_on_animation_finished):
|
|
animated.animation_finished.connect(_on_animation_finished)
|
|
animated.play("SLEEP")
|
|
|
|
# 玩家进入 observe 区域
|
|
func _on_area_2d_observe_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("Player"):
|
|
is_in_observe = true
|
|
# 如果没有在播过渡动画,就播 BLINK
|
|
if not is_playing_transition:
|
|
is_playing_transition = true
|
|
animated.play("BLINK")
|
|
# 如果你想强制打断 NOBLINK 直接播 BLINK,可以把上面换成:
|
|
# is_playing_transition = true
|
|
# animated.play("BLINK")
|
|
|
|
# 玩家离开 observe 区域
|
|
func _on_area_2d_observe_body_exited(body: Node2D) -> void:
|
|
if body.is_in_group("Player"):
|
|
is_in_observe = false
|
|
# 如果没有在播过渡动画,就播 NOBLINK
|
|
if not is_playing_transition:
|
|
is_playing_transition = true
|
|
animated.play("NOBLINK")
|
|
|
|
# 【统一回调】所有动画播放完毕时触发
|
|
func _on_animation_finished() -> void:
|
|
# 只要播放完 BLINK 或 NOBLINK,就解除过渡锁定
|
|
if animated.animation == "BLINK" or animated.animation == "NOBLINK":
|
|
is_playing_transition = false
|
|
|
|
# 根据玩家最终是否在区域内,决定稳定状态
|
|
if is_in_observe:
|
|
animated.play("IDLE")
|
|
else:
|
|
animated.play("SLEEP")
|
|
|
|
|
|
func _on_area_2d_damage_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("Player"):
|
|
animated.play("HIT")
|
|
down = true
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if down:
|
|
velocity.y += down_SPEED * delta
|
|
if is_on_floor():
|
|
down = false
|
|
velocity.y = -down_SPEED * 2
|
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
|
|
func _on_area_2d_take_damage_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("Player"):
|
|
if body.has_method("take_damage"):
|
|
body.take_damage(1)
|