Files
2026-10-07 17:58:02 +08:00

86 lines
2.1 KiB
GDScript

extends CharacterBody2D
var HP = 30
var target : Node2D
var target_two : Node2D
var Player_inside: bool = false
var slow_timer: float = 0.0
var slow_factor: float = 0.5
var is_hurt = false
var is_dead: bool = false
const SPEED = 150.0
const JUMP_VELOCITY = -250.0
@onready var TIMEJUMP = $timeJUMP
@onready var animated_sprite = $AnimatedSprite2D
func _ready() -> void:
add_to_group("enemy")
func die():
HP = max(0,HP)
if HP <= 0:
queue_free()
is_dead = true
func take_damage(amount:int,slow_during:float = 0.0,factor:float = 0.5):
if is_dead:
return
HP -= amount #伤害
animated_sprite.play("HIT")
is_hurt = true
if slow_during > 0.0:
slow_timer = max(slow_during,slow_timer)
slow_factor = factor
animated_sprite.play("HIT")
if not is_dead:
die()
await get_tree().create_timer(0.3).timeout
is_hurt = false
func _physics_process(delta: float) -> void:
var current_SPEED = SPEED
if not is_on_floor():
velocity += get_gravity() * delta
if Player_inside and is_instance_valid(target_two):
if target_two.has_method("take_damage"):
target_two.take_damage(1)
Player_inside = false
if slow_timer > 0.0:
slow_timer -= delta
if slow_timer < 0.0:
slow_timer = 0.0
if slow_timer > 0.0:
current_SPEED = SPEED * slow_factor
if target:
var direction := signf(target.global_position.x - global_position.x)
velocity.x = direction * SPEED
if direction < 0:
animated_sprite.flip_h = true
elif direction > 0:
animated_sprite.flip_h = false
if direction != 0 :
velocity.x = current_SPEED * direction
if animated_sprite.animation != "RUN" and not is_hurt:
animated_sprite.play("RUN")
if is_on_floor() and TIMEJUMP.is_stopped():
velocity.y = JUMP_VELOCITY
TIMEJUMP.start()
elif is_on_floor():
velocity.x = 0
if not is_hurt:
animated_sprite.play("IDLE")
move_and_slide()
func _on_jiance_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
target = body
func _on_damage_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
target_two = body
Player_inside = true