enemy.gd 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. extends KinematicBody2D
  2. var player
  3. var motion = Vector2()
  4. export var speed = 150
  5. export var attackrange = 64
  6. export var reactiontime = 1
  7. var active = false
  8. export var detectionzone = 128
  9. export var exppt = 15
  10. func _ready():
  11. player = get_tree().get_nodes_in_group('player')[0]
  12. var array = []
  13. for x in $types.get_animation_list():
  14. array.append(x)
  15. array.shuffle()
  16. while array[0] == 'RESET':
  17. array.shuffle()
  18. $types.play(array[0])
  19. pass # Replace with function body.
  20. func _physics_process(delta):
  21. if player:
  22. for x in range(-1,1):
  23. for y in range(-1,1):
  24. if checkapproxpos(Vector2(x,y)):
  25. active = true
  26. if active:
  27. reactiontime -= 1
  28. if reactiontime == 0:
  29. motion = (player.global_position - global_position).normalized()
  30. reactiontime = 180
  31. $hitbox.position = motion * attackrange
  32. move_and_slide(motion * speed)
  33. $Sprite.rotation = atan2(motion.y * speed,motion.x * speed)
  34. func checkapproxpos(var offset = Vector2()):
  35. var pos1 = Vector2(int(player.global_position.x/detectionzone), int(player.global_position.y/detectionzone))
  36. var pos2 = Vector2(int(global_position.x/detectionzone), int(global_position.y/detectionzone))
  37. if pos1.y+offset.y == pos2.y+offset.y and pos1.x+offset.x == pos2.x+offset.x:
  38. return true