Advertisement
Thatoneham

godot nav2d ai

Oct 2nd, 2024
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.41 KB | Source Code | 0 0
  1. extends KinematicBody2D
  2.  
  3. export(int) var Speed: int = 40
  4. export(int) var reposition_delay: float = 1.0
  5. export(int) var idle_step_distance: int = 20
  6. export(int) var attack_radius: int = 150
  7. export(int) var knockback_threshold: int = 5
  8.  
  9. var velocity: Vector2 = Vector2.ZERO
  10. var path: Array = []
  11. var levelnav: Navigation2D = null
  12. var player = null
  13. onready var soft_collision = $SoftCollision
  14. var knockbackforce = Vector2()
  15. var knockback_decay = 20
  16. var rng
  17. var wait_timer = false
  18. var made_a_path = false
  19. var waituntilldone = true
  20. var CurrentPath = Vector2.ZERO
  21. onready var raycast = $RayCast2D
  22. var hit_count = 0
  23. var is_running_away = false
  24.  
  25. enum {
  26.     IDLE,
  27.     ATTACK
  28. }
  29. var State = ATTACK
  30. var randompos = Vector2.ZERO
  31.  
  32. func _ready():
  33.     rng = RandomNumberGenerator.new()
  34.     yield(get_tree(),"idle_frame")
  35.     var tree = get_tree()
  36.     if tree.has_group("LevelNavigation"):
  37.         levelnav = tree.get_nodes_in_group("LevelNavigation")[0]
  38.         player = tree.get_nodes_in_group("Player")[0]
  39.  
  40. func _physics_process(delta):
  41.     $Line2D.global_position = Vector2.ZERO
  42.     $Line2D.points = path
  43.     if knockbackforce.length() > 7:
  44.         velocity = knockbackforce
  45.         knockbackforce += Vector2(0, 0) - knockbackforce / 3
  46.         if not knockbackforce.length() > 14:
  47.             $Sprite.self_modulate = Color(1, 1, 1)
  48.     else:
  49.         if wait_timer and waituntilldone:
  50.             waituntilldone = false
  51.             $Timer.start(reposition_delay)
  52.             wait_timer = false
  53.             made_a_path = false
  54.             print("starting timer")
  55.         else:
  56.             _navigate(delta)
  57.    
  58.     if soft_collision.is_colliding():
  59.         velocity += soft_collision.get_push_vector() * delta * 400
  60.         if soft_collision.touching_wall().size() > 0:
  61.             wait_timer = true
  62.    
  63.     velocity = move_and_slide(velocity)
  64.  
  65. func _navigate(delta):
  66.     if path.size() > 1 and made_a_path and not wait_timer:
  67.         velocity = global_position.direction_to(path[1]) * Speed + knockbackforce
  68.         if global_position.distance_to(path[1]) < 3:
  69.             path.pop_front()
  70.     elif soft_collision.touching_wall().size() > 0 and not wait_timer:
  71.         wait_timer = true
  72.     elif path.size() > 1 and path.size() <= 3 and global_position.distance_to(path[0]) < 12 and not wait_timer:
  73.         wait_timer = true
  74.     else:
  75.         velocity = lerp(velocity, Vector2.ZERO, 10 * delta)
  76.  
  77. func _generate_path(randopos):
  78.     if levelnav != null and player != null:
  79.         path = levelnav.get_simple_path(global_position, randopos, false)
  80.         $Line2D.points = path
  81.         if path.size() > 0:
  82.             CurrentPath = path[path.size() - 1]
  83.         made_a_path = true
  84.         wait_timer = false
  85.  
  86. func _on_SoftCollision_area_entered(area):
  87.     if "Bullet" in area.name:
  88.         _knockback(-area.rotation, area.knockback)
  89.  
  90. func _knockback(bulletrot, knockback):
  91.     path = levelnav.get_simple_path(global_position, CurrentPath, false)
  92.     $Line2D.points = path
  93.     knockbackforce = Vector2(sin(bulletrot), cos(bulletrot)).normalized() * -knockback
  94.     $Sprite.self_modulate = Color(100, 100, 100)
  95.  
  96. func _on_Timer_timeout():
  97.     waituntilldone = true
  98.     rng.randomize()
  99.    
  100.     if player and levelnav:
  101.         match State:
  102.             IDLE:
  103.                 randompos = global_position + Vector2(rng.randi_range(-idle_step_distance, idle_step_distance), rng.randi_range(-idle_step_distance, idle_step_distance))
  104.                 randompos = levelnav.get_closest_point(randompos)
  105.             ATTACK:
  106.                 randompos = player.global_position + Vector2(rng.randi_range(-attack_radius, attack_radius), rng.randi_range(-attack_radius, attack_radius))
  107.                 randompos = levelnav.get_closest_point(randompos)
  108.        
  109.         _generate_path(randompos)
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement