giants-godot/entities/weapon.gd

107 lines
3.1 KiB
GDScript3
Raw Normal View History

2021-09-08 18:36:41 +02:00
extends Spatial
class_name Weapon
2021-09-22 18:04:03 +02:00
enum WeaponType {
Activated,
OneShot
}
enum WeaponHeld {
Left,
Right
}
export var projectile: PackedScene
2021-09-08 18:36:41 +02:00
var holder: Spatial
2021-09-22 18:04:03 +02:00
export(WeaponHeld) var held_hand = WeaponHeld.Right
export(WeaponType) var activation_type = WeaponType.OneShot
export var fire_sound: AudioStreamSample
export var switch_sound: AudioStreamSample
export var max_ammo: int = 99
export var current_ammo: int = 5
export var weapon_icon: Texture
var ui_icon: Control = preload("res://ui/rp_weapon_icon.tscn").instance()
# Type = Activated
export var activate_efx: PackedScene = null # preload("res://effects/anim_bow_charging.tscn")
export var activate_sound: AudioStreamSample = null # preload("res://assets/audio/Sounds/SR_SBWPL.xx_reaperlev1.gzp.wav")
var activate_efx_activated: Spatial
2021-09-08 18:36:41 +02:00
var current_activate_animation_value: Vector2 = Vector2.ZERO
var wanted_activate_animation_value: float = 0
func setup(_holder: Spatial):
holder = _holder
2021-09-22 18:04:03 +02:00
func has_ammo():
return current_ammo > 0 or max_ammo == 0
func can_shoot():
pass
2021-09-08 18:36:41 +02:00
func activate(_to: Vector3):
2021-09-22 18:04:03 +02:00
if not has_ammo():
return
if activation_type == WeaponType.OneShot:
if fire_sound:
Globals.play_sound(fire_sound, Globals.Bus.Sounds, self)
fire(_to)
elif activation_type == WeaponType.Activated:
if activate_sound:
Globals.play_sound(activate_sound, Globals.Bus.Sounds, self)
if activate_efx:
activate_efx_activated = preload("res://effects/anim_bow_charging.tscn").instance()
add_child(activate_efx_activated)
activate_efx_activated.global_transform.origin = global_transform.origin
activate_efx_activated.get_node("AudioStreamPlayer3D").stream = activate_sound
wanted_activate_animation_value = 1
2021-09-08 18:36:41 +02:00
func deactivate(to: Vector3):
2021-10-01 15:50:16 +02:00
if not has_ammo():
return
2021-09-22 18:04:03 +02:00
if activation_type == WeaponType.OneShot:
return
elif activation_type == WeaponType.Activated:
wanted_activate_animation_value = 0
if fire_sound:
Globals.play_sound(fire_sound, Globals.Bus.Sounds, self)
fire(to)
2021-09-08 18:36:41 +02:00
func _process(delta):
2021-09-22 18:04:03 +02:00
# allow blending between current animation (idle, running, etc) and activation
2021-09-08 18:36:41 +02:00
if is_visible_in_tree():
current_activate_animation_value = current_activate_animation_value.linear_interpolate(Vector2(wanted_activate_animation_value, 0), delta * 10)
2021-09-23 21:19:11 +02:00
holder.animation_tree.set("parameters/blend_draw/blend_amount", current_activate_animation_value.x)
2021-09-22 18:04:03 +02:00
func _ready():
ui_icon.get_node("WeaponIcon").texture = weapon_icon
2021-09-23 21:19:11 +02:00
update_ammo(current_ammo)
func update_ammo(new_ammo: int):
current_ammo = new_ammo
if max_ammo == 0:
ui_icon.get_node("Ammo").text = ""
else:
ui_icon.get_node("Ammo").text = str(current_ammo)
func play_switch_sound():
2021-09-22 18:04:03 +02:00
if switch_sound:
Globals.play_sound(switch_sound, Globals.Bus.Sounds, self)
2021-09-08 18:36:41 +02:00
func fire(to: Vector3):
2021-09-22 18:04:03 +02:00
if max_ammo != 0:
2021-09-23 21:19:11 +02:00
update_ammo(current_ammo-1)
2021-09-08 18:36:41 +02:00
# deactivate
2021-09-22 18:04:03 +02:00
if activate_efx_activated:
activate_efx_activated.queue_free()
2021-09-08 18:36:41 +02:00
# create projectile
if projectile:
var proj = projectile.instance()
get_tree().root.add_child(proj)
var transform = holder.transform
transform.origin = global_transform.origin
proj.create(transform, to, [holder])