51 lines
1.1 KiB
GDScript
51 lines
1.1 KiB
GDScript
extends KinematicBody2D
|
|
|
|
const SPEED: int = 30
|
|
|
|
var player: KinematicBody2D = null
|
|
var velocity: Vector2 = Vector2.ZERO
|
|
|
|
var status = "walk"
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
velocity = Vector2.ZERO
|
|
|
|
if player:
|
|
velocity = position.direction_to(player.position).normalized() * SPEED
|
|
var angle = position.angle_to_point(player.position)
|
|
if abs(angle) > PI/2:
|
|
$AnimatedSprite1.scale.x = -0.563
|
|
else:
|
|
$AnimatedSprite1.scale.x = 0.563
|
|
|
|
velocity = move_and_slide(velocity)
|
|
return
|
|
|
|
|
|
func _on_player_detector_area_entered(area: Area2D) -> void:
|
|
if area.get_parent().name == 'Player':
|
|
player = area.get_parent()
|
|
$AnimatedSprite1.animation = "Walk"
|
|
return
|
|
|
|
|
|
func _on_player_detector_area_exited(_area: Area2D):
|
|
player = null
|
|
$AnimatedSprite1.animation = "Idle"
|
|
return
|
|
|
|
|
|
func _on_Player_Attack_area_entered(area: Area2D) -> void:
|
|
if area.get_parent().name == 'Player':
|
|
player = area.get_parent()
|
|
$AnimatedSprite1.animation = "Attack"
|
|
status = "attack"
|
|
return
|
|
|
|
|
|
func _on_Player_Attack_area_exited(area: Area2D) -> void:
|
|
player = null
|
|
if not status == "attack":
|
|
$AnimatedSprite1.animation = "Walk"
|
|
return
|