-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathturret.gd
More file actions
42 lines (30 loc) · 796 Bytes
/
turret.gd
File metadata and controls
42 lines (30 loc) · 796 Bytes
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
extends Node2D
export (PackedScene) var Shell
export var rotation_speed = 1.5
export var fire_timeout = 2
var target = null
var can_shoot = true
func _ready():
$ShootTimer.wait_time = self.fire_timeout
func _process(delta):
aim(delta)
func aim(delta):
if target:
var target_rot = get_angle_to(target.global_position)
if abs(target_rot) > 0.01:
var max_rot = delta * self.rotation_speed
if max_rot > abs(target_rot):
rotation += target_rot
elif target_rot > 0:
rotation += max_rot
else:
rotation -= max_rot
func shoot():
if target and can_shoot:
var b = Shell.instance()
b.start(global_position, $muzzle.global_rotation)
get_tree().get_root().add_child(b)
can_shoot = false
$ShootTimer.start()
func _on_ShootTimer_timeout():
can_shoot = true