enemy.gd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. name = array[0]
  20. pass # Replace with function body.
  21. func _physics_process(delta):
  22. if player:
  23. for x in range(-1,1):
  24. for y in range(-1,1):
  25. if checkapproxpos(Vector2(x,y)):
  26. active = true
  27. if active:
  28. reactiontime -= 1
  29. if reactiontime == 0:
  30. motion = (player.global_position - global_position).normalized()
  31. reactiontime = 180
  32. $hitbox.position = motion * attackrange
  33. move_and_slide(motion * speed)
  34. $Sprite.rotation = atan2(motion.y * speed,motion.x * speed)
  35. func checkapproxpos(var offset = Vector2()):
  36. var pos1 = Vector2(int(player.global_position.x/detectionzone), int(player.global_position.y/detectionzone))
  37. var pos2 = Vector2(int(global_position.x/detectionzone), int(global_position.y/detectionzone))
  38. if pos1.y+offset.y == pos2.y+offset.y and pos1.x+offset.x == pos2.x+offset.x:
  39. return true