giants-godot/entities/weapon.gd
2021-10-01 15:50:16 +02:00

107 lines
3.1 KiB
GDScript

extends Spatial
class_name Weapon
enum WeaponType {
Activated,
OneShot
}
enum WeaponHeld {
Left,
Right
}
export var projectile: PackedScene
var holder: Spatial
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
var current_activate_animation_value: Vector2 = Vector2.ZERO
var wanted_activate_animation_value: float = 0
func setup(_holder: Spatial):
holder = _holder
func has_ammo():
return current_ammo > 0 or max_ammo == 0
func can_shoot():
pass
func activate(_to: Vector3):
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
func deactivate(to: Vector3):
if not has_ammo():
return
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)
func _process(delta):
# allow blending between current animation (idle, running, etc) and activation
if is_visible_in_tree():
current_activate_animation_value = current_activate_animation_value.linear_interpolate(Vector2(wanted_activate_animation_value, 0), delta * 10)
holder.animation_tree.set("parameters/blend_draw/blend_amount", current_activate_animation_value.x)
func _ready():
ui_icon.get_node("WeaponIcon").texture = weapon_icon
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():
if switch_sound:
Globals.play_sound(switch_sound, Globals.Bus.Sounds, self)
func fire(to: Vector3):
if max_ammo != 0:
update_ammo(current_ammo-1)
# deactivate
if activate_efx_activated:
activate_efx_activated.queue_free()
# 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])