Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.54 KB | Source Code | 0 0
  1. extends Node2D
  2.  
  3. # Allow instancing of projectiles.
  4. export(PackedScene) var projectile
  5.  
  6. # Allow access to parent node path.
  7. export(NodePath) var parent_path
  8.  
  9. # Cooldown value & count.
  10. var cooldown_value : int
  11. var cooldown_count : int
  12.  
  13. # Gun status.
  14. var is_gun_ready : bool
  15.  
  16. # Ready.
  17. func _ready():
  18.     # Hide animation.
  19.     $GunfireAnimation.hide()
  20.    
  21.     # Set cooldown values.
  22.     cooldown_value = 32
  23.     cooldown_count = 0
  24.    
  25.     # Set gun status.
  26.     is_gun_ready = true
  27.    
  28. # Process.
  29. func _process(delta):
  30.     # Increment cooldown.
  31.     if not is_gun_ready:
  32.         cooldown_count += 1
  33.        
  34.     # Check cooldown & reset.
  35.     cooldown_count %= cooldown_value
  36.     if cooldown_count == 0:
  37.         is_gun_ready = true
  38.    
  39. # Fire the projectile.
  40. # Default speed set to 500.
  41. func fire(projectile_speed = 500):
  42.     # Check & reset gun status.
  43.     if not is_gun_ready:
  44.         return
  45.     is_gun_ready = false
  46.    
  47.     # Create projectile instance.
  48.     var projectile_instance = projectile.instance()
  49.    
  50.     # Set spawn position to gun barrel.
  51.     projectile_instance.position = $BarrelEnd.position
  52.    
  53.     # Set the direction of the projectile.
  54.     projectile_instance.rotation = get_node(parent_path).rotation - (PI / 2)
  55.    
  56.     # Set the velocity of the projectile.
  57.     projectile_instance.linear_velocity = Vector2(projectile_speed, 0.0).rotated(projectile_instance.rotation)
  58.    
  59.     # Play animation.
  60.     $GunfireAnimation.show()
  61.     $GunfireAnimation.play()
  62.    
  63.     # Spawn the projectile.
  64.     add_child(projectile_instance)
  65.  
  66.  
  67. func _on_GunfireAnimation_animation_finished():
  68.     $GunfireAnimation.stop()
  69.     $GunfireAnimation.hide()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement