From ad5c9d6d7877a1b187fa9403cfbaaf0c9b8e3330 Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Sat, 11 Jun 2022 22:32:53 +0200 Subject: [PATCH] Turret anim transition into search & firing rate --- src/Actors/Enemies/Machines/Timer.gd | 16 +++++++++ src/Actors/Enemies/Machines/Turret.gd | 29 +++++++-------- src/Actors/Enemies/Machines/Turret.tscn | 36 +++++++++++++++---- .../Enemies/Machines/TurretStateMachine.gd | 22 +++++++----- src/Contraptions/Platform/Track.tscn | 9 ++--- src/HarmfulObjects/Bullet.gd | 2 +- src/Levels/Plattforms Level.tscn | 5 +-- 7 files changed, 81 insertions(+), 38 deletions(-) create mode 100644 src/Actors/Enemies/Machines/Timer.gd diff --git a/src/Actors/Enemies/Machines/Timer.gd b/src/Actors/Enemies/Machines/Timer.gd new file mode 100644 index 0000000..4dd555e --- /dev/null +++ b/src/Actors/Enemies/Machines/Timer.gd @@ -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 diff --git a/src/Actors/Enemies/Machines/Turret.gd b/src/Actors/Enemies/Machines/Turret.gd index 4648997..6ab8c68 100644 --- a/src/Actors/Enemies/Machines/Turret.gd +++ b/src/Actors/Enemies/Machines/Turret.gd @@ -7,12 +7,13 @@ onready var sightline: RayCast2D = $Sightline 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 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") # Derives the freedom from the number of bald head eagles onready var angle_of_freedom = deg2rad( - turret_animation.track_get_key_value( - 0, turret_animation.track_get_key_count(0) - 1 + turret_search_animation.track_get_key_value( + 0, turret_search_animation.track_get_key_count(0) - 1 ) ) onready var original_rotation = rotation @@ -24,16 +25,10 @@ var prey_ref = weakref(null) # Ray that goes from the turret to the target var target_ray - -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - $AnimationPlayer.play("Turret Rotation") - func prey(): return prey_ref.get_ref() func searching(): - $AnimationPlayer.play("Turret Rotation") if sightline.is_colliding(): # The collider returns not the area or body it hit, but the parent of them var collider = sightline.get_collider() @@ -51,8 +46,6 @@ func start_locking(): target_ray.exclude_parent = true if(prey() != null): target_ray.set_cast_to(to_local(prey().position)) - $AnimationPlayer.stop(false) - lock_on_timer.start() @@ -62,8 +55,7 @@ func is_locked_on_target(): func is_tracking_prey(): return true if(prey() != null) else false -# TODO simplify/split this method -func shooting(): +func aiming(): 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) @@ -75,6 +67,10 @@ func shooting(): rotation = clamp( rotation, original_rotation, original_rotation + angle_of_freedom ) + +# TODO simplify/split this method +func shooting(): + aiming() if sightline.is_colliding(): # 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 @@ -95,11 +91,12 @@ func shooting(): elif(prey() != null): 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 + + +func _on_FiringRateTimer_timeout(): + spawn_projectile() diff --git a/src/Actors/Enemies/Machines/Turret.tscn b/src/Actors/Enemies/Machines/Turret.tscn index 84c8a76..221d049 100644 --- a/src/Actors/Enemies/Machines/Turret.tscn +++ b/src/Actors/Enemies/Machines/Turret.tscn @@ -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/meta/new_dynamicfont.tres" type="DynamicFont" id=2] @@ -8,6 +8,22 @@ [sub_resource type="RectangleShape2D" id=1] 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] resource_name = "Turret Rotation" length = 6.66 @@ -20,7 +36,7 @@ tracks/0/imported = false tracks/0/enabled = true tracks/0/keys = { "times": PoolRealArray( 0, 3.2 ), -"transitions": PoolRealArray( 1, 1 ), +"transitions": PoolRealArray( 0.9, 1.1 ), "update": 0, "values": [ 0.0, 90.0 ] } @@ -44,11 +60,6 @@ enabled = true cast_to = Vector2( 0, 1e+07 ) 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="."] script = ExtResource( 4 ) @@ -73,3 +84,14 @@ position = Vector2( 0, 39 ) [node name="SightLostTimer" type="Timer" parent="."] wait_time = 3.33 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"] diff --git a/src/Actors/Enemies/Machines/TurretStateMachine.gd b/src/Actors/Enemies/Machines/TurretStateMachine.gd index 010949d..19b7db3 100644 --- a/src/Actors/Enemies/Machines/TurretStateMachine.gd +++ b/src/Actors/Enemies/Machines/TurretStateMachine.gd @@ -1,7 +1,6 @@ extends StateMachine -signal got_grounded - +onready var anim_player = parent.get_node("AnimationPlayer") # Adds the intial states func _ready(): @@ -12,6 +11,7 @@ func _ready(): add_state("shooting") state = states.searching set_state(states.searching) + anim_player.play("Turret Rotation") # Calls the parent behaviours according to state @@ -53,7 +53,6 @@ func _get_transition(_delta): parent.get_node("StateLabel").text = self.state var new_state if parent.is_tracking_prey() && self.state == "searching": - parent.start_locking() new_state = "locking" # TODO Helper function with null check and reference check if !parent.is_tracking_prey() && self.state == "shooting": @@ -67,11 +66,18 @@ func _get_transition(_delta): func _enter_state(new_state, old_state): - if old_state == "idle" && (new_state == "walk" || new_state == "run"): - 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") + 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 func _exit_state(old_state, new_state): diff --git a/src/Contraptions/Platform/Track.tscn b/src/Contraptions/Platform/Track.tscn index 66751aa..11b3f4f 100644 --- a/src/Contraptions/Platform/Track.tscn +++ b/src/Contraptions/Platform/Track.tscn @@ -8,17 +8,17 @@ extents = Vector2( 19.0405, 7.97217 ) [sub_resource type="Animation" id=2] resource_name = "Horizontal" -length = 2.5 +length = 6.0 loop = true tracks/0/type = "value" tracks/0/path = NodePath("KinematicBody2D:position") -tracks/0/interp = 1 +tracks/0/interp = 2 tracks/0/loop_wrap = true tracks/0/imported = false tracks/0/enabled = true tracks/0/keys = { -"times": PoolRealArray( 0, 1.3 ), -"transitions": PoolRealArray( 1, 1 ), +"times": PoolRealArray( 0, 3 ), +"transitions": PoolRealArray( 1.33, 1.33 ), "update": 0, "values": [ Vector2( 0, 0 ), Vector2( 30, 0 ) ] } @@ -27,6 +27,7 @@ tracks/0/keys = { script = ExtResource( 2 ) [node name="KinematicBody2D" type="KinematicBody2D" parent="."] +position = Vector2( 25.7293, 0 ) collision_layer = 8 collision_mask = 0 motion/sync_to_physics = true diff --git a/src/HarmfulObjects/Bullet.gd b/src/HarmfulObjects/Bullet.gd index 0b8ba21..33694ba 100644 --- a/src/HarmfulObjects/Bullet.gd +++ b/src/HarmfulObjects/Bullet.gd @@ -11,7 +11,7 @@ func _ready() -> 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: diff --git a/src/Levels/Plattforms Level.tscn b/src/Levels/Plattforms Level.tscn index 58b51f6..49d4c8c 100644 --- a/src/Levels/Plattforms Level.tscn +++ b/src/Levels/Plattforms Level.tscn @@ -76,11 +76,12 @@ position = Vector2( 291.104, 535.161 ) scale = Vector2( 2.83999, 0.56 ) [node name="KinematicBody2D" parent="Track" index="0"] -position = Vector2( 8, 0 ) +position = Vector2( 25.0812, 0 ) [node name="Turret" parent="." instance=ExtResource( 6 )] -position = Vector2( 479, 520 ) +position = Vector2( 479, 466 ) scale = Vector2( 0.4, 0.4 ) [editable path="Spring4"] [editable path="Track"] +[editable path="Turret"]