-
Notifications
You must be signed in to change notification settings - Fork 4
/
weapon.gd
111 lines (80 loc) · 2.07 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
extends Node
var ye = load("res://yframework.gd").new()
var bullet = load("res://bullet_factory.gd").new()
var weapon_db = load("res://weapon_db.gd").new()
var that
var weapon_name ="normal"
var shot_timer = 0.2
var shot_counter = 0
var dmg = 10
var bullet_speed = 150
var bullet_life = 2
var team = 1
var is_ai
#if the weapon uses ammo
var uses_ammo
var max_ammo = 50
var ammo = 50
var recharge_rate = 0.5
var recharge_counter = 0
func _ready():
set_weapon_stats_by_name(weapon_name)
pass
func yinit(that2,fire_rate=0.2,is_ai2 = false):
that = that2
shot_timer = fire_rate
team = that.team
is_ai = is_ai2
pass
func can_shot_ai(delta):
shot_counter+=delta
if shot_timer<=shot_counter:
shot_counter=0
return true
pass
#end can_shot_ai
func can_shot(delta,button):
shot_counter+=delta
if shot_timer <= shot_counter and Input.is_action_pressed(button) :
shot_counter = 0 #reset firate timer
return true
pass
#end can_shot
func make_bullet(gun):
#no ammo? exit
if ammo<=0:return
ammo-=1 #reduce 1 ammo per bullet made/shot
#help ai aim at target
if is_ai:aim_gun(gun)
var b
#create bullet
if(weapon_name =="normal"): b = bullet.normal(self,gun)
if(weapon_name =="missile"): b = bullet.guided_missile(self,gun)
if(weapon_name =="dumb_missile"): b = bullet.dumb_missile(self,gun)
if(weapon_name =="flare"): b = bullet.flare(self,gun)
return b
pass
#end make_bullet
func aim_gun(gun):
if !that.targ:return
#get weapon owner target
var targ = that.targ
#aim bullet at the target plus its volocity (to track the target)
gun.look_at(targ.global_transform.origin+targ.velocity,Vector3.UP)
func reload():ammo = max_ammo
func recharge(delta):
#if uses ammo or has max ammo exit
if uses_ammo || ammo==max_ammo:return
#when recharge rate timer is done
recharge_counter += delta
if recharge_counter>recharge_rate:
recharge_counter=0
ammo+=1 #add 1 ammo
pass
#end recharge
func get_ammo_hud(): return float(ammo)/max_ammo *100
func update(delta):
recharge(delta)
func set_weapon_stats_by_name(name):
weapon_db.set_weapon(name,self)
pass