You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

72 lines
1.5 KiB

  1. extends KinematicBody
  2. # stats
  3. var curHp : int = 3
  4. var maxHp : int = 3
  5. # attacking
  6. var damage : int = 1
  7. var attackDist : float = 1.5
  8. var attackRate : float = 1.0
  9. # physics
  10. var moveSpeed : float = 2.5
  11. var gravity : float = 15.0
  12. # vectors
  13. var vel : Vector3 = Vector3()
  14. # components
  15. onready var timer = get_node("Timer")
  16. export var player_path : NodePath
  17. onready var player = get_node(player_path)
  18. onready var anim = get_node("Zombie_Male/AnimationPlayer")
  19. func _ready ():
  20. # set the timer wait time
  21. timer.wait_time = attackRate
  22. timer.start()
  23. # called every "attackRate" seconds
  24. func _on_Timer_timeout ():
  25. # if we're within the attack distance - attack the player
  26. if translation.distance_to(player.translation) <= attackDist:
  27. player.take_damage(damage)
  28. # called 60 times a second
  29. func _physics_process (delta):
  30. # get the distance from us to the player
  31. var dist = translation.distance_to(player.translation)
  32. # if we're outside of the attack distance, chase after the player
  33. if dist > attackDist:
  34. # calculate the direction between us and the player
  35. var dir = (player.translation - translation).normalized()
  36. vel.x = dir.x
  37. vel.z = dir.z
  38. # gravity
  39. vel.y -= gravity * delta
  40. # move towards the player
  41. vel = move_and_slide(vel, Vector3.UP)
  42. # called when the player deals damage to us
  43. func take_damage (damageToTake):
  44. curHp -= damageToTake
  45. anim.play("RecieveHit")
  46. # if our health reaches 0 - die
  47. if curHp <= 0:
  48. die()
  49. # called when our health reaches 0
  50. func die ():
  51. # destroy the node
  52. queue_free()