aboutsummaryrefslogtreecommitdiff
path: root/scripts/bullet.gd
blob: 2a5df41f5f95a36666fe22b9510c6e69ca727ad1 (plain)
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
extends Spatial

var _enemies_holder
var shooter

var damage = 1

var timer = 0
var time_life = 3
var speed = 7
var hit_something = false
var bounce = false
var hitlist = []

func _ready():
	_enemies_holder = get_tree().root.get_node("world").find_node("enemies")
	var _err = $Area.connect("body_entered", self, "collided")

func _physics_process(delta):
	var forward_dir = -global_transform.basis.z.normalized()
	if bounce:
		var space: PhysicsDirectSpaceState = get_world().direct_space_state
		var from = transform.origin 
		var to = transform.origin + forward_dir * speed * delta
		var mask = 0b0101
		var result = space.intersect_ray(from, to, [], mask)
		if result.size() > 0:
			hitlist.clear()
			var bounced = forward_dir.bounce(result.normal).normalized()
			var z = -bounced;
			var x = bounced.cross(result.normal).normalized();
			var y = x.cross(z).normalized();
			var basis = Basis(x, y, z);
			global_transform.basis = basis
			forward_dir = -global_transform.basis.z.normalized()
			var dist = transform.origin.distance_to(result.position)
			transform.origin = result.position
			var amt = speed * delta - dist
			global_translate(forward_dir * amt)
		else:
			global_translate(forward_dir * speed * delta)
	else:
		global_translate(forward_dir * speed * delta)
		
	
	timer += delta
	if timer >= time_life:
		queue_free()

func collided(body):
	var parent = body.get_parent()
	if parent == shooter: return
	if parent in hitlist: return
	
	if hit_something == false:
		var groups = parent.get_groups()
		if "enemies" in groups:
			_enemies_holder.damage(parent.name, damage)
			

	if !bounce:
		hit_something = true
		queue_free()
	else:
		hitlist.append(parent)