GodotCourse/week6/updated_project/scripts/badguy.gd

37 lines
1013 B
GDScript

class_name Enemy extends Area2D
const speed = 60
var direction = 1
@onready var cast_right = $CastRight
@onready var cast_left = $CastLeft
@onready var sprite = $AnimatedSprite2D
@onready var gameController = %GameManager
@onready var cast_floor_right = $CastFloorRight
@onready var cast_floor_left = $CastFloorLeft
signal playerDamage
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
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):
if body.is_in_group("player"):
GameManager.playerDamage()
playerDamage.emit()