forked from OddlyTimbot/Sept26Game
95 lines
2.5 KiB
GDScript
95 lines
2.5 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
|
|
|
|
signal enemyAttackSignal(body,enemy)
|
|
|
|
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, HURT}
|
|
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")
|
|
EnemyStates.HURT:
|
|
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!")
|
|
if body is Player:
|
|
currentEnemyState = EnemyStates.ATTACK
|
|
direction = 0
|
|
enemyAttackSignal.emit(body,self)
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
match currentEnemyState:
|
|
EnemyStates.SPAWN:
|
|
currentEnemyState = EnemyStates.RUN
|
|
direction = 1
|
|
EnemyStates.ATTACK:
|
|
currentEnemyState = EnemyStates.RUN
|
|
direction = (randi() % 2) *2 -1
|
|
EnemyStates.HURT:
|
|
currentEnemyState = EnemyStates.RUN
|
|
|
|
func takeDamage()->void:
|
|
print("Slime taking damage!!")
|
|
currentEnemyState = EnemyStates.HURT
|