/
Weapon.gd
95 lines (78 loc) · 2.26 KB
/
Weapon.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extends Node3D
class_name Weapon
var cooldown: bool = false
var burst_cooldown: bool = false
var burst_counter: int = 0
var iff: IffProfile
var type: String
@onready var parent = get_node("../../")
@export var projectile_scene: PackedScene
@export var burst_count = 0
@export var dupe_count = 1
@export var spread: float = 0
@export var world_projectile: bool = true # Disable for beams or other things that should follow the player
@export var vary_pitch = 0
@export var ammo_item: String
@export var primary = true
@export var weapon_name: String = "Plasma"
@export var projectile_velocity: float
@export var projectile_lifetime: float
# @export var dmg_factor: float = 1
@export var damage: int
func _ready():
Data.weapons[type].apply_to_node(self)
iff = IffProfile.new(
parent,
parent.faction,
false
)
func try_shoot():
if not cooldown and not burst_cooldown:
if _try_consume_ammo():
# TODO: Consume ammo
_shoot()
return true
return false
func _shoot():
if burst_count:
burst_counter += 1
if burst_counter >= burst_count:
burst_cooldown = true
$BurstCooldown.start()
for i in range(dupe_count):
_create_projectile()
cooldown = true
$Cooldown.start()
_effects()
func _create_projectile():
var projectile = projectile_scene.instantiate()
# projectile.init()
#projectile.damage *= dmg_factor
#projectile.splash_damage *= dmg_factor
# TODO: Also scale splash damage
projectile.global_transform = global_transform
projectile.damage = damage
projectile.initial_velocity = projectile_velocity
projectile.linear_velocity = parent.linear_velocity
projectile.rotate_x(randf_range(-spread/2, spread/2))
projectile.rotate_y(randf_range(-spread/2, spread/2))
projectile.iff = iff
projectile.set_lifetime(projectile_lifetime)
if world_projectile:
Client.get_world().get_node("projectiles").add_child(projectile)
else:
get_node("../").add_child(projectile)
func _effects():
#$Emerge/MuzzleFlash.restart()
#$Emerge/MuzzleFlash.emitting = true
$AudioStreamPlayer3D.play()
parent.flash_weapon()
func _on_Cooldown_timeout():
cooldown = false
func _on_BurstCooldown_timeout():
burst_cooldown = false
burst_counter = 0
func _try_consume_ammo():
if ammo_item == "":
return true
return parent.get_node("Inventory").deduct_ingredients({ammo_item: 1})