class_name Enemy extends Actor enum State { WALKING, DEAD, } var _state = State.WALKING onready var platform_detector = $PlatformDetector onready var floor_detector_left = $FloorDetectorLeft onready var floor_detector_right = $FloorDetectorRight onready var sprite = $Sprite onready var animation_player = $AnimationPlayer func _ready(): _velocity.x = speed.x func _physics_process(_delta): # If the enemy encounters a wall or an edge, the horizontal velocity is flipped. if not floor_detector_left.is_colliding(): _velocity.x = speed.x elif not floor_detector_right.is_colliding(): _velocity.x = -speed.x if is_on_wall(): _velocity.x *= -1 _velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y # Flip the Sprite depending on which way the enemy is moving. if _velocity.x > 0: sprite.scale.x = 1 else: sprite.scale.x = -1 var animation = get_new_animation() if animation != animation_player.current_animation: animation_player.play(animation) func destroy(): _state = State.DEAD _velocity = Vector2.ZERO func get_new_animation(): var animation_new = "" if _state == State.WALKING: if _velocity.x == 0: animation_new = "idle" else: animation_new = "walk" else: animation_new = "destroy" return animation_new