forked from OddlyTimbot/Sept26Game
smarter slimes
This commit is contained in:
1 parent
c3ab17230b
commit
5240811f02
6 files changed
+266
No files matched your search
@@ -0,0 +1,82 @@
|
||||
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
|
||||
@@ -0,0 +1 @@
|
||||
uid://c83er3jdb7i8d
|
||||
Reference in new issue
Block a user