Advertisement
asseater64

Untitled

Mar 24th, 2021
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. onready var detectionZone = $DetectionZone
  4. onready var sprite = $AnimatedSprite
  5. onready var hitBox = $HitBoxPivot/HitBox/CollisionShape2D
  6. onready var bodies = []
  7.  
  8. var vector_to_enemy = Vector2.ZERO
  9. var velocity = Vector2.ZERO
  10. var state = IDLE
  11. var attack_range = 50
  12. var is_attacking = false
  13. const MAX_SPEED = 20
  14.  
  15. enum {IDLE, RUN, ATTACK, DEATH}
  16.  
  17. func _ready():
  18.     state = IDLE
  19.    
  20. func _physics_process(_delta):
  21.     match state:
  22.         IDLE:
  23.             bodies = detectionZone.get_overlapping_bodies()
  24.             bodies.erase(self)
  25.             sprite.play("Idle")
  26.             if bodies.size() > 0:
  27.                 state = RUN
  28.            
  29.         RUN:
  30.             var closest_enemy = find_closest_enemy(bodies)
  31.             if closest_enemy == null:
  32.                 state = IDLE
  33.             if self.position.x <= closest_enemy.position.x:
  34.                 velocity = self.position.direction_to(closest_enemy.position - Vector2(30,0))
  35.                 sprite.flip_h = false
  36.             elif self.position.x > closest_enemy.position.x:
  37.                 velocity = self.position.direction_to(closest_enemy.position + Vector2(30,0))
  38.                 sprite.flip_h = true
  39.             velocity = velocity.normalized()
  40.             velocity = move_and_slide(velocity * MAX_SPEED)
  41.             sprite.play("Run")
  42.             var d_self_y = self.position.y
  43.             var d_enemy_y = closest_enemy.position.y
  44.             if abs(d_enemy_y - d_self_y) < rand_range(5, 15):
  45.                 state = ATTACK
  46.            
  47.         ATTACK:
  48.             if bodies.size() == 0:
  49.                 state = IDLE
  50.             sprite.play("Attack1")
  51.             hitBox.disabled = false
  52.             if sprite.flip_h == true:
  53.                 get_node("HitBoxPivot").set_scale(Vector2(-1, 1))
  54.                
  55.         DEATH:
  56.             sprite.play("Death")
  57.             queue_free()
  58.            
  59. func find_closest_enemy(_bodies):
  60.     var min_dist = 99999
  61.     var closest_enemy
  62.     for body in _bodies:
  63.         if body != null:
  64.             var dist = self.position.distance_to(body.position)
  65.             if (dist < min_dist):
  66.                 min_dist = dist
  67.                 closest_enemy = body
  68.     return closest_enemy
  69.  
  70. func _on_AnimatedSprite_animation_finished():
  71.     if sprite.animation == "Death":
  72.         queue_free()
  73.     if sprite.animation == "Attack1":
  74.         hitBox.disabled = true
  75.  
  76. func _on_HurtBox_area_entered(_area):
  77.     state = DEATH
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement