Files
Sept26Game-tim-fork/scripts/slime.gd
T
2026-10-27 17:36:48 -04:00

83 lines
2.3 KiB
GDScript

class_name Slime extends Area2D
@onready var enemy_graphic: AnimatedSprite2D = $EnemyGraphic
@onready var right_cast: RayCast2D = $RightCast
@onready var right_down_cast: RayCast2D = $RightDownCast
@onready var left_down_cast: RayCast2D = $LeftDownCast
@onready var left_cast: RayCast2D = $LeftCast
signal enemyAttackSignal(body, enemy)
var speed = 100
enum FaceDirection {RIGHT,LEFT}
var facing:FaceDirection = FaceDirection.RIGHT
var direction = 0
enum EnemyStates {RUN, ATTACK, SPAWN}
var currentEnemyState:EnemyStates = EnemyStates.SPAWN
# 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:
handleMovement(delta)
handleStates()
handleAnimation()
handleCollisions()
func _on_body_entered(body: Node2D) -> void:
if body is Player:
print("Attack the player")
currentEnemyState = EnemyStates.ATTACK
direction = 0
enemyAttackSignal.emit(body, self)
func handleMovement(delta)->void:
position.x += direction*speed*delta
match facing:
FaceDirection.LEFT:
enemy_graphic.flip_h = true
FaceDirection.RIGHT:
enemy_graphic.flip_h = false
func handleStates()->void:
match currentEnemyState:
EnemyStates.SPAWN when not direction ==0 :
print("setting state to run")
currentEnemyState = EnemyStates.RUN
func handleAnimation()->void:
match currentEnemyState:
EnemyStates.SPAWN:
enemy_graphic.play("spawn")
EnemyStates.RUN:
enemy_graphic.play("idle")
EnemyStates.ATTACK:
enemy_graphic.play("attack")
func handleCollisions()->void:
if not right_down_cast.is_colliding():
direction =-1
facing = FaceDirection.LEFT
if not left_down_cast.is_colliding():
direction = 1
facing = FaceDirection.RIGHT
if right_cast.is_colliding() && not right_cast.get_collider() is Player:
direction = -1
facing=FaceDirection.LEFT
if left_cast.is_colliding() && not left_cast.get_collider() is Player:
direction = 1
facing=FaceDirection.RIGHT
func _on_animation_finished() -> void:
match currentEnemyState:
EnemyStates.SPAWN:
currentEnemyState = EnemyStates.RUN
direction = 1
EnemyStates.ATTACK:
currentEnemyState = EnemyStates.RUN
direction = (randi() % 2) * 2 - 1