40 lines
1.1 KiB
GDScript
40 lines
1.1 KiB
GDScript
class_name BadGuy extends Area2D
|
|
|
|
signal playerDamage
|
|
var direction = 1
|
|
const speed =60
|
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var cast_right: RayCast2D = $CastRight
|
|
@onready var cast_left: RayCast2D = $CastLeft
|
|
@onready var cast_floor_right: RayCast2D = $CastFloorRight
|
|
@onready var cast_floor_left: RayCast2D = $CastFloorLeft
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
pass # Replace with function body.
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
if cast_right.is_colliding():
|
|
if not cast_right.get_collider().is_in_group("player"):
|
|
direction = -1
|
|
sprite.flip_h = true
|
|
if cast_left.is_colliding():
|
|
if not cast_left.get_collider().is_in_group("player"):
|
|
direction = 1
|
|
sprite.flip_h = false
|
|
if not cast_floor_right.is_colliding():
|
|
direction =-1
|
|
sprite.flip_h = true
|
|
if not cast_floor_left.is_colliding():
|
|
direction = 1
|
|
sprite.flip_h = false
|
|
|
|
position.x += direction * speed * delta
|
|
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("player"):
|
|
playerDamage.emit()
|