2024-08-20 01:18:41 +00:00
|
|
|
class_name badguy extends Area2D
|
2024-08-13 01:21:31 +00:00
|
|
|
|
|
|
|
const speed = 60
|
|
|
|
var direction = -1
|
|
|
|
@onready var right_cast = $RightCast
|
|
|
|
@onready var left_cast = $LeftCast
|
|
|
|
@onready var right_floor_cast = $RightFloorCast
|
|
|
|
@onready var left_floor_cast = $LeftFloorCast
|
|
|
|
@onready var player = $"../CharacterBody2D"
|
|
|
|
|
|
|
|
|
2024-08-20 01:18:41 +00:00
|
|
|
signal playerDamage
|
|
|
|
|
2024-08-13 01:21:31 +00:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
func _process(delta):
|
|
|
|
if right_cast.is_colliding():
|
|
|
|
if not right_cast.get_collider().is_in_group("player"):
|
|
|
|
direction = -1
|
|
|
|
|
|
|
|
if left_cast.is_colliding():
|
|
|
|
if not left_cast.get_collider().is_in_group("player"):
|
|
|
|
direction = 1
|
|
|
|
|
|
|
|
if not right_floor_cast.is_colliding() or not left_floor_cast.is_colliding():
|
|
|
|
direction *= -1
|
|
|
|
|
|
|
|
position.x += direction * speed * delta
|
|
|
|
|
|
|
|
|
|
|
|
func _on_body_entered(body):
|
2024-08-20 01:18:41 +00:00
|
|
|
if body.is_in_group("player"):
|
|
|
|
playerDamage.emit()
|