GodotCourse/week6/original_project/scripts/badguy.gd

35 lines
936 B
GDScript3
Raw Permalink Normal View History

2024-08-18 23:47:45 +00:00
extends Area2D
const speed = 60
var direction = 1
@onready var cast_right = $CastRight
@onready var cast_left = $CastLeft
@onready var sprite = $AnimatedSprite2D
@onready var game = %GameManager
@onready var cast_floor_right = $CastFloorRight
@onready var cast_floor_left = $CastFloorLeft
# 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"):
game.playerDeath()