Turret shoots, can loose player

This commit is contained in:
Jakob Feldmann 2022-06-07 19:47:08 +02:00
parent 162a5b485e
commit bd6103a766
5 changed files with 39 additions and 4 deletions

View File

@ -6,7 +6,9 @@ extends KinematicBody2D
onready var sightline: RayCast2D = $RayCast2D
onready var turret_state_machine = $TurretStateMachine
onready var lock_on_timer = $LockOnTimer
onready var sight_lost_timer = $SightLostTimer
onready var turret_animation = $AnimationPlayer.get_animation("Turret Rotation")
onready var Bullet = preload("res://src/HarmfulObjects/Bullet.tscn")
# Derives the freedom from the number of bald head eagles
onready var angle_of_freedom = deg2rad(
turret_animation.track_get_key_value(
@ -36,6 +38,7 @@ func searching():
prey = collider
# TODO should this stand still?
func start_locking():
target_ray = RayCast2D.new()
add_child(target_ray)
@ -51,8 +54,8 @@ func is_locked_on_target():
return lock_on_timer.is_stopped()
# TODO simplify/split this method
func shooting():
target_ray.cast_to = to_local(prey.position)
var target_angle = target_ray.cast_to.angle_to(sightline.cast_to)
var rotation_speed = max(
min_rotation_speed, abs(target_angle / rotation_speed_divider)
@ -68,4 +71,26 @@ func shooting():
# The collider returns not the area or body it hit, but the parent of them
var collider = sightline.get_collider()
if collider.is_in_group("player"):
prey = collider
sight_lost_timer.stop()
target_ray.cast_to = to_local(prey.position)
else:
if (
sight_lost_timer.get_time_left() < 0.1
&& sight_lost_timer.get_time_left() > 0
):
prey = null
target_ray.queue_free()
return
if sight_lost_timer.is_stopped():
sight_lost_timer.start()
else:
target_ray.cast_to = to_local(prey.position)
spawn_projectile()
func spawn_projectile():
var b = Bullet.instance()
owner.add_child(b)
b.set_collision_mask_bit(1, false)
b.transform = $Muzzle.global_transform

View File

@ -42,6 +42,7 @@ shape = SubResource( 1 )
[node name="RayCast2D" type="RayCast2D" parent="."]
enabled = true
cast_to = Vector2( 0, 1e+07 )
collision_mask = 59
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
pause_mode = 2
@ -63,4 +64,12 @@ align = 1
valign = 1
[node name="LockOnTimer" type="Timer" parent="."]
wait_time = 0.633
one_shot = true
[node name="Muzzle" type="Position2D" parent="."]
position = Vector2( 0, 39 )
[node name="SightLostTimer" type="Timer" parent="."]
wait_time = 3.33
one_shot = true

View File

@ -55,6 +55,8 @@ func _get_transition(_delta):
if parent.prey != null && self.state == "searching":
parent.start_locking()
new_state = "locking"
if parent.prey == null && self.state == "shooting":
new_state = "searching"
if self.state == "locking" && parent.is_locked_on_target():
new_state = "shooting"
if new_state != self.state:

View File

@ -11,7 +11,7 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
position.x += 30 * delta
position += transform.y * 500 * delta
func _on_Bullet_body_entered(_body: Node) -> void:

View File

@ -7,7 +7,6 @@
extents = Vector2( 1.51498, 5.05697 )
[node name="Bullet" type="Area2D" groups=["harmful"]]
rotation = 1.5708
collision_layer = 64
collision_mask = 59
script = ExtResource( 2 )