46 lines
1.1 KiB
GDScript
46 lines
1.1 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var speed = 60
|
|
@export var hp = 2
|
|
|
|
var direction = 1
|
|
|
|
@onready var ray_cast_right: RayCast2D = $RayCastRight
|
|
@onready var ray_cast_left: RayCast2D = $RayCastLeft
|
|
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
|
@onready var hurt_sound: AudioStreamPlayer2D = $Hurt
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _physics_process(delta: float) -> void:
|
|
# Apply gravity
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
if ray_cast_right.is_colliding():
|
|
direction = -1
|
|
animated_sprite.flip_h = true
|
|
|
|
if ray_cast_left.is_colliding():
|
|
direction = 1
|
|
animated_sprite.flip_h = false
|
|
|
|
position.x += direction * speed * delta
|
|
|
|
move_and_slide()
|
|
|
|
|
|
func _on_hit_box_hit() -> void:
|
|
hp -= 1
|
|
|
|
if hp <= 0:
|
|
animated_sprite.play("die")
|
|
animation_player.play("die")
|
|
else:
|
|
hurt_sound.play()
|
|
animated_sprite.play("hurt")
|
|
|
|
|
|
func _on_animated_sprite_2d_animation_finished() -> void:
|
|
animated_sprite.play("default")
|