Facebook
From Tudor, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 108
  1. func _ready ():
  2.  
  3.  # set the timer wait time
  4.  timer.wait_time = attackRate
  5.  timer.start()
  6.  
  7. # called 60 times a second
  8. func _physics_process (_delta):
  9.  
  10.  # get the distance from us to the player
  11.  var dist = position.distance_to(player.position)
  12.  
  13.  # if we're outside of the attack distance, chase after the player
  14.  if dist > attackDist:
  15.   # calculate the direction between us and the player
  16.   var dir = (player.position - position).normalized()
  17.  
  18.   velocity.x = dir.x
  19.   velocity.y = 0
  20.   velocity.z = dir.z
  21.  
  22.   # move towards the player
  23.   move_and_slide()
  24.  
  25.   look_at(transform.origin - velocity, Vector3.UP)
  26.  
  27. # called every "attackRate" seconds
  28. func on_timeout():
  29.  # if we're within the attack distance - attack the player
  30.  if position.distance_to(player.position) <= attackDist:
  31.   weapon.try_attack("Player", damage)
  32.  # player.take_damage(damage)
  33.  
  34. # called when the player deals damage to us
  35. func take_damage (damageToTake):
  36.  curHp -= damageToTake
  37.  # if our health reaches 0 - die
  38.  if curHp <= 0:
  39.   die()
  40.  
  41. # called when our health reaches 0
  42. func die ():
  43.  # destroy the node
  44.  queue_free()