80 lines
2.1 KiB
GDScript
80 lines
2.1 KiB
GDScript
class_name Slime extends Area2D
|
|||
|
|
|
||
|
|
@onready var right_cast: RayCast2D = $RightCast
|
||
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
||
|
|
@onready var right_down_cast: RayCast2D = $RightDownCast
|
||
|
|
@onready var left_down_cast: RayCast2D = $LeftDownCast
|
||
|
|
@onready var slime_graphic: AnimatedSprite2D = $SlimeGraphic
|
||
|
|
|
||
|
|
var speed = 100
|
||
|
|
enum FaceDirection {RIGHT, LEFT}
|
||
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
||
|
|
#direction 1 move to right, 0 no movement, -1 to the left
|
||
|
|
var direction = 1
|
||
|
|
|
||
|
|
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 handleMovement(delta)->void:
|
||
|
|
position.x += direction*speed*delta
|
||
|
|
match facing:
|
||
|
|
FaceDirection.LEFT:
|
||
|
|
slime_graphic.flip_h = true
|
||
|
|
FaceDirection.RIGHT:
|
||
|
|
slime_graphic.flip_h = false
|
||
|
|
|
||
|
|
func handleStates()->void:
|
||
|
|
match currentEnemyState:
|
||
|
|
EnemyStates.SPAWN when not direction ==0:
|
||
|
|
currentEnemyState = EnemyStates.RUN
|
||
|
|
|
||
|
|
func handleAnimation()->void:
|
||
|
|
match currentEnemyState:
|
||
|
|
EnemyStates.SPAWN:
|
||
|
|
slime_graphic.play("spawn")
|
||
|
|
EnemyStates.RUN:
|
||
|
|
slime_graphic.play("run")
|
||
|
|
EnemyStates.ATTACK:
|
||
|
|
slime_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() and not right_cast.get_collider() is Player:
|
||
|
|
direction = -1
|
||
|
|
facing = FaceDirection.LEFT
|
||
|
|
if left_cast.is_colliding() and not left_cast.get_collider() is Player:
|
||
|
|
direction = 1
|
||
|
|
facing= FaceDirection.RIGHT
|
||
|
|
|
||
|
|
|
||
|
|
pass
|
||
|
|
func _on_body_entered(body: Node2D) -> void:
|
||
|
|
print("Slime Attack!")
|
||
|
|
|
||
|
|
|
||
|
|
func _on_animation_finished() -> void:
|
||
|
|
match currentEnemyState:
|
||
|
|
EnemyStates.SPAWN:
|
||
|
|
currentEnemyState = EnemyStates.RUN
|
||
|
|
direction = 1
|
||
|
|
EnemyStates.ATTACK:
|
||
|
|
currentEnemyState = EnemyStates.RUN
|
||
|
|
direction = (randi() % 2) *2 -1
|