Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- # Allow instancing of projectiles.
- export(PackedScene) var projectile
- # Allow access to parent node path.
- export(NodePath) var parent_path
- # Cooldown value & count.
- var cooldown_value : int
- var cooldown_count : int
- # Gun status.
- var is_gun_ready : bool
- # Ready.
- func _ready():
- # Hide animation.
- $GunfireAnimation.hide()
- # Set cooldown values.
- cooldown_value = 32
- cooldown_count = 0
- # Set gun status.
- is_gun_ready = true
- # Process.
- func _process(delta):
- # Increment cooldown.
- if not is_gun_ready:
- cooldown_count += 1
- # Check cooldown & reset.
- cooldown_count %= cooldown_value
- if cooldown_count == 0:
- is_gun_ready = true
- # Fire the projectile.
- # Default speed set to 500.
- func fire(projectile_speed = 500):
- # Check & reset gun status.
- if not is_gun_ready:
- return
- is_gun_ready = false
- # Create projectile instance.
- var projectile_instance = projectile.instance()
- # Set spawn position to gun barrel.
- projectile_instance.position = $BarrelEnd.position
- # Set the direction of the projectile.
- projectile_instance.rotation = get_node(parent_path).rotation - (PI / 2)
- # Set the velocity of the projectile.
- projectile_instance.linear_velocity = Vector2(projectile_speed, 0.0).rotated(projectile_instance.rotation)
- # Play animation.
- $GunfireAnimation.show()
- $GunfireAnimation.play()
- # Spawn the projectile.
- add_child(projectile_instance)
- func _on_GunfireAnimation_animation_finished():
- $GunfireAnimation.stop()
- $GunfireAnimation.hide()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement