Turret anim transition into search & firing rate

This commit is contained in:
Jakob Feldmann 2022-06-11 22:32:53 +02:00
parent 5359d5dd75
commit ad5c9d6d78
7 changed files with 81 additions and 38 deletions

View File

@ -0,0 +1,16 @@
extends Timer
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -7,12 +7,13 @@ onready var sightline: RayCast2D = $Sightline
onready var turret_state_machine = $TurretStateMachine onready var turret_state_machine = $TurretStateMachine
onready var lock_on_timer = $LockOnTimer onready var lock_on_timer = $LockOnTimer
onready var sight_lost_timer = $SightLostTimer onready var sight_lost_timer = $SightLostTimer
onready var turret_animation = $AnimationPlayer.get_animation("Turret Rotation") onready var turret_search_animation = $AnimationPlayer.get_animation("Turret Rotation")
onready var turret_return_animation = $AnimationPlayer.get_animation("Turret Returning")
onready var Bullet = preload("res://src/HarmfulObjects/Bullet.tscn") onready var Bullet = preload("res://src/HarmfulObjects/Bullet.tscn")
# Derives the freedom from the number of bald head eagles # Derives the freedom from the number of bald head eagles
onready var angle_of_freedom = deg2rad( onready var angle_of_freedom = deg2rad(
turret_animation.track_get_key_value( turret_search_animation.track_get_key_value(
0, turret_animation.track_get_key_count(0) - 1 0, turret_search_animation.track_get_key_count(0) - 1
) )
) )
onready var original_rotation = rotation onready var original_rotation = rotation
@ -24,16 +25,10 @@ var prey_ref = weakref(null)
# Ray that goes from the turret to the target # Ray that goes from the turret to the target
var target_ray var target_ray
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
$AnimationPlayer.play("Turret Rotation")
func prey(): func prey():
return prey_ref.get_ref() return prey_ref.get_ref()
func searching(): func searching():
$AnimationPlayer.play("Turret Rotation")
if sightline.is_colliding(): if sightline.is_colliding():
# The collider returns not the area or body it hit, but the parent of them # The collider returns not the area or body it hit, but the parent of them
var collider = sightline.get_collider() var collider = sightline.get_collider()
@ -51,8 +46,6 @@ func start_locking():
target_ray.exclude_parent = true target_ray.exclude_parent = true
if(prey() != null): if(prey() != null):
target_ray.set_cast_to(to_local(prey().position)) target_ray.set_cast_to(to_local(prey().position))
$AnimationPlayer.stop(false)
lock_on_timer.start() lock_on_timer.start()
@ -62,8 +55,7 @@ func is_locked_on_target():
func is_tracking_prey(): func is_tracking_prey():
return true if(prey() != null) else false return true if(prey() != null) else false
# TODO simplify/split this method func aiming():
func shooting():
var target_angle = target_ray.cast_to.angle_to(sightline.cast_to) var target_angle = target_ray.cast_to.angle_to(sightline.cast_to)
var rotation_speed = max( var rotation_speed = max(
min_rotation_speed, abs(target_angle / rotation_speed_divider) min_rotation_speed, abs(target_angle / rotation_speed_divider)
@ -75,6 +67,10 @@ func shooting():
rotation = clamp( rotation = clamp(
rotation, original_rotation, original_rotation + angle_of_freedom rotation, original_rotation, original_rotation + angle_of_freedom
) )
# TODO simplify/split this method
func shooting():
aiming()
if sightline.is_colliding(): if sightline.is_colliding():
# The collider returns not the area or body it hit, but the parent of them # The collider returns not the area or body it hit, but the parent of them
# TODO Sight could be a cone instead of a ray # TODO Sight could be a cone instead of a ray
@ -95,11 +91,12 @@ func shooting():
elif(prey() != null): elif(prey() != null):
target_ray.cast_to = to_local(prey().position) target_ray.cast_to = to_local(prey().position)
spawn_projectile()
func spawn_projectile(): func spawn_projectile():
var b = Bullet.instance() var b = Bullet.instance()
owner.add_child(b) owner.add_child(b)
b.set_collision_mask_bit(1, false) b.set_collision_mask_bit(1, false)
b.transform = $Muzzle.global_transform b.transform = $Muzzle.global_transform
func _on_FiringRateTimer_timeout():
spawn_projectile()

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/contraption/bumper.png" type="Texture" id=1] [ext_resource path="res://assets/contraption/bumper.png" type="Texture" id=1]
[ext_resource path="res://assets/meta/new_dynamicfont.tres" type="DynamicFont" id=2] [ext_resource path="res://assets/meta/new_dynamicfont.tres" type="DynamicFont" id=2]
@ -8,6 +8,22 @@
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 108.869, 37.8566 ) extents = Vector2( 108.869, 37.8566 )
[sub_resource type="Animation" id=3]
resource_name = "Turret Returning"
length = 3.3
tracks/0/type = "value"
tracks/0/path = NodePath(".:rotation_degrees")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/keys = {
"times": PoolRealArray( 3.3 ),
"transitions": PoolRealArray( 1.1 ),
"update": 3,
"values": [ 0.0 ]
}
[sub_resource type="Animation" id=2] [sub_resource type="Animation" id=2]
resource_name = "Turret Rotation" resource_name = "Turret Rotation"
length = 6.66 length = 6.66
@ -20,7 +36,7 @@ tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/keys = { tracks/0/keys = {
"times": PoolRealArray( 0, 3.2 ), "times": PoolRealArray( 0, 3.2 ),
"transitions": PoolRealArray( 1, 1 ), "transitions": PoolRealArray( 0.9, 1.1 ),
"update": 0, "update": 0,
"values": [ 0.0, 90.0 ] "values": [ 0.0, 90.0 ]
} }
@ -44,11 +60,6 @@ enabled = true
cast_to = Vector2( 0, 1e+07 ) cast_to = Vector2( 0, 1e+07 )
collision_mask = 59 collision_mask = 59
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
pause_mode = 2
playback_process_mode = 0
"anims/Turret Rotation" = SubResource( 2 )
[node name="TurretStateMachine" type="Node" parent="."] [node name="TurretStateMachine" type="Node" parent="."]
script = ExtResource( 4 ) script = ExtResource( 4 )
@ -73,3 +84,14 @@ position = Vector2( 0, 39 )
[node name="SightLostTimer" type="Timer" parent="."] [node name="SightLostTimer" type="Timer" parent="."]
wait_time = 3.33 wait_time = 3.33
one_shot = true one_shot = true
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
pause_mode = 1
playback_process_mode = 0
"anims/Turret Returning" = SubResource( 3 )
"anims/Turret Rotation" = SubResource( 2 )
[node name="FiringRateTimer" type="Timer" parent="."]
wait_time = 0.1
[connection signal="timeout" from="FiringRateTimer" to="." method="_on_FiringRateTimer_timeout"]

View File

@ -1,7 +1,6 @@
extends StateMachine extends StateMachine
signal got_grounded onready var anim_player = parent.get_node("AnimationPlayer")
# Adds the intial states # Adds the intial states
func _ready(): func _ready():
@ -12,6 +11,7 @@ func _ready():
add_state("shooting") add_state("shooting")
state = states.searching state = states.searching
set_state(states.searching) set_state(states.searching)
anim_player.play("Turret Rotation")
# Calls the parent behaviours according to state # Calls the parent behaviours according to state
@ -53,7 +53,6 @@ func _get_transition(_delta):
parent.get_node("StateLabel").text = self.state parent.get_node("StateLabel").text = self.state
var new_state var new_state
if parent.is_tracking_prey() && self.state == "searching": if parent.is_tracking_prey() && self.state == "searching":
parent.start_locking()
new_state = "locking" new_state = "locking"
# TODO Helper function with null check and reference check # TODO Helper function with null check and reference check
if !parent.is_tracking_prey() && self.state == "shooting": if !parent.is_tracking_prey() && self.state == "shooting":
@ -67,11 +66,18 @@ func _get_transition(_delta):
func _enter_state(new_state, old_state): func _enter_state(new_state, old_state):
if old_state == "idle" && (new_state == "walk" || new_state == "run"): if new_state == "shooting" && old_state == "locking":
parent.get_node("FiringRateTimer").start()
if new_state == "locking" && old_state == "searching":
parent.start_locking()
anim_player.stop()
if new_state == "searching" && old_state == "shooting":
# TODO Time for returning to 0 degrees is always the same
# Implement this using a tween
parent.get_node("FiringRateTimer").stop()
anim_player.play("Turret Returning")
anim_player.queue("Turret Rotation")
pass pass
# TODO This may be hard to keep track of if many states are added
if !["run", "walk", "idle"].has(old_state) && parent.is_on_floor():
emit_signal("got_grounded")
func _exit_state(old_state, new_state): func _exit_state(old_state, new_state):

View File

@ -8,17 +8,17 @@ extents = Vector2( 19.0405, 7.97217 )
[sub_resource type="Animation" id=2] [sub_resource type="Animation" id=2]
resource_name = "Horizontal" resource_name = "Horizontal"
length = 2.5 length = 6.0
loop = true loop = true
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/path = NodePath("KinematicBody2D:position") tracks/0/path = NodePath("KinematicBody2D:position")
tracks/0/interp = 1 tracks/0/interp = 2
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/keys = { tracks/0/keys = {
"times": PoolRealArray( 0, 1.3 ), "times": PoolRealArray( 0, 3 ),
"transitions": PoolRealArray( 1, 1 ), "transitions": PoolRealArray( 1.33, 1.33 ),
"update": 0, "update": 0,
"values": [ Vector2( 0, 0 ), Vector2( 30, 0 ) ] "values": [ Vector2( 0, 0 ), Vector2( 30, 0 ) ]
} }
@ -27,6 +27,7 @@ tracks/0/keys = {
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="KinematicBody2D" type="KinematicBody2D" parent="."] [node name="KinematicBody2D" type="KinematicBody2D" parent="."]
position = Vector2( 25.7293, 0 )
collision_layer = 8 collision_layer = 8
collision_mask = 0 collision_mask = 0
motion/sync_to_physics = true motion/sync_to_physics = true

View File

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

View File

@ -76,11 +76,12 @@ position = Vector2( 291.104, 535.161 )
scale = Vector2( 2.83999, 0.56 ) scale = Vector2( 2.83999, 0.56 )
[node name="KinematicBody2D" parent="Track" index="0"] [node name="KinematicBody2D" parent="Track" index="0"]
position = Vector2( 8, 0 ) position = Vector2( 25.0812, 0 )
[node name="Turret" parent="." instance=ExtResource( 6 )] [node name="Turret" parent="." instance=ExtResource( 6 )]
position = Vector2( 479, 520 ) position = Vector2( 479, 466 )
scale = Vector2( 0.4, 0.4 ) scale = Vector2( 0.4, 0.4 )
[editable path="Spring4"] [editable path="Spring4"]
[editable path="Track"] [editable path="Track"]
[editable path="Turret"]