From 25a9bc1d78cf2bd0d56933cc76fa287af72f103b Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 8 Apr 2025 10:44:06 +0200 Subject: [PATCH] Face player towards aim direction --- Resources/Enemies/Thermathron.tres | 5 +- Resources/Enemies/Turret360.tres | 5 +- Resources/StartData/Pistol_Start.tres | 2 +- Scenes/test.tscn | 31 +++---- .../Actors/PlayerAnimationProvider.cs | 80 +++++++++++-------- .../Components/FSM/Enemy/AnimationModule.cs | 4 +- Scripts/Components/FSM/Player/Active.cs | 6 +- Scripts/Components/FSM/Player/Cutscene.cs | 3 +- .../FSM/Player/PlayerStorageModule.cs | 1 + 9 files changed, 77 insertions(+), 60 deletions(-) diff --git a/Resources/Enemies/Thermathron.tres b/Resources/Enemies/Thermathron.tres index cac16298..15a39755 100644 --- a/Resources/Enemies/Thermathron.tres +++ b/Resources/Enemies/Thermathron.tres @@ -1,6 +1,5 @@ -[gd_resource type="Resource" script_class="EnemyResource" load_steps=4 format=3 uid="uid://cfdvg162u65sr"] +[gd_resource type="Resource" script_class="EnemyResource" load_steps=3 format=3 uid="uid://cfdvg162u65sr"] -[ext_resource type="Script" uid="uid://cq65aed620ijo" path="res://Scripts/Resources/Loot/LootDrop.cs" id="1_f3huq"] [ext_resource type="Resource" uid="uid://cdfmedtgp2rcn" path="res://Resources/Weapons/EnemyWeapon.tres" id="7_filx8"] [ext_resource type="Script" uid="uid://cd5o0ceb50jki" path="res://Scripts/Resources/EnemyResource.cs" id="8_x8scf"] @@ -12,7 +11,7 @@ PrefabPath = &"res://Scenes/Actors/Thermathron.tscn" MaxHealth = 8.0 MovementSpeed = 38.0 Weapon = ExtResource("7_filx8") -LootDrops = Array[ExtResource("1_f3huq")]([]) +LootDrops = [] MotivationReward = 4.0 PlayerDetectionRange = 90.0 ViewRange = 120.0 diff --git a/Resources/Enemies/Turret360.tres b/Resources/Enemies/Turret360.tres index 99d247de..10c381e6 100644 --- a/Resources/Enemies/Turret360.tres +++ b/Resources/Enemies/Turret360.tres @@ -1,6 +1,5 @@ -[gd_resource type="Resource" script_class="EnemyResource" load_steps=4 format=3 uid="uid://ysd6wl2gmdhn"] +[gd_resource type="Resource" script_class="EnemyResource" load_steps=3 format=3 uid="uid://ysd6wl2gmdhn"] -[ext_resource type="Script" uid="uid://cq65aed620ijo" path="res://Scripts/Resources/Loot/LootDrop.cs" id="1_sma76"] [ext_resource type="Resource" uid="uid://dyaxm3st70sh6" path="res://Resources/Weapons/Enemy/TurretWeapon.tres" id="1_yap8t"] [ext_resource type="Script" uid="uid://cd5o0ceb50jki" path="res://Scripts/Resources/EnemyResource.cs" id="2_sma76"] @@ -12,7 +11,7 @@ PrefabPath = &"res://Scenes/Actors/Turret360.tscn" MaxHealth = 14.0 MovementSpeed = 0.0 Weapon = ExtResource("1_yap8t") -LootDrops = Array[ExtResource("1_sma76")]([]) +LootDrops = [] MotivationReward = 4.0 PlayerDetectionRange = 90.0 ViewRange = 120.0 diff --git a/Resources/StartData/Pistol_Start.tres b/Resources/StartData/Pistol_Start.tres index c28d047c..aa1efdc5 100644 --- a/Resources/StartData/Pistol_Start.tres +++ b/Resources/StartData/Pistol_Start.tres @@ -13,5 +13,5 @@ script = ExtResource("1_fmydh") EggIndex = 0 StartingEquipment = Array[ExtResource("1_xjbmv")]([ExtResource("1_juefn"), ExtResource("2_xjbmv")]) -RemoveEquipment = [ExtResource("1_wchkt"), ExtResource("2_2v4s6"), ExtResource("3_73lvc"), ExtResource("4_vnxsw")] +RemoveEquipment = Array[ExtResource("1_xjbmv")]([ExtResource("1_wchkt"), ExtResource("2_2v4s6"), ExtResource("3_73lvc"), ExtResource("4_vnxsw")]) metadata/_custom_type_script = "uid://mja0rk7n2kln" diff --git a/Scenes/test.tscn b/Scenes/test.tscn index c926a9e0..5b3dcb03 100644 --- a/Scenes/test.tscn +++ b/Scenes/test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=152 format=4 uid="uid://bv451a8wgty4u"] +[gd_scene load_steps=153 format=4 uid="uid://bv451a8wgty4u"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_8tmoj"] [ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_ksslq"] @@ -74,6 +74,7 @@ [ext_resource type="PackedScene" uid="uid://c5fiv1nioghfb" path="res://Scenes/Actors/Marisa.tscn" id="50_mp5ma"] [ext_resource type="Resource" uid="uid://37h0tjja4sns" path="res://Resources/Maps/RebelBase.tres" id="52_orkt3"] [ext_resource type="PackedScene" uid="uid://bqjcwxene73l2" path="res://Scenes/Actors/ActorEnemyTest.tscn" id="55_chha6"] +[ext_resource type="Script" uid="uid://b5fesrd4lv8t1" path="res://Scripts/Resources/Events/EventResource.cs" id="55_dferh"] [ext_resource type="PackedScene" uid="uid://bjskkeb3ppcs8" path="res://Scenes/Actors/Turret360.tscn" id="62_h0jm4"] [ext_resource type="Script" uid="uid://v57xjmhi28kw" path="res://Scripts/Resources/Events/ControlActorEvent.cs" id="64_rphi8"] [ext_resource type="PackedScene" uid="uid://b0gpbkxdfbnjh" path="res://Scenes/Actors/ForceField_Horizontal.tscn" id="66_nxn3h"] @@ -268,7 +269,7 @@ ActivationType = 0 Targets = Array[NodePath]([NodePath("../Rumia")]) WaitForCompletion = true -[sub_resource type="Resource" id="Resource_j86ly"] +[sub_resource type="Resource" id="Resource_fuaed"] resource_local_to_scene = true script = ExtResource("49_0si7g") Target = NodePath(".") @@ -717,7 +718,7 @@ Invisible = true metadata/_edit_group_ = true [node name="ScriptableAreaTrigger" parent="Parallax2D/Factory Tilemaps/LevelProps/DebugTeleporter" instance=ExtResource("43_kf3qc")] -Events = [SubResource("Resource_usuub")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_usuub")]) [node name="BlueKeycard2" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("35_rblsn")] position = Vector2(-1032, -317) @@ -914,7 +915,7 @@ position = Vector2(-1423, -188) [node name="LevelStartTrigger" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("43_kf3qc")] position = Vector2(-762, -129) scale = Vector2(1.455, 1.455) -Events = [SubResource("Resource_5er5x"), SubResource("Resource_b25hy")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_5er5x"), SubResource("Resource_b25hy")]) [node name="AreaTrigger2" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Target") instance=ExtResource("28_6au4t")] position = Vector2(-435, -162) @@ -957,7 +958,7 @@ metadata/_edit_group_ = true [node name="Script2" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer2"] position = Vector2(120, 25) script = ExtResource("42_wigyb") -Events = [SubResource("Resource_wma5c")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_wma5c")]) [node name="Computer6" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Targets") instance=ExtResource("31_243ec")] position = Vector2(-1113, -139) @@ -967,7 +968,7 @@ metadata/_edit_group_ = true [node name="Script2" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer6"] position = Vector2(0, 1) script = ExtResource("42_wigyb") -Events = [SubResource("Resource_cxj4w")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_cxj4w")]) [node name="Computer7" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Targets") instance=ExtResource("31_243ec")] position = Vector2(-1175.36, 213.708) @@ -978,7 +979,7 @@ metadata/_edit_group_ = true [node name="Computer7Script" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer7"] position = Vector2(0, 1) script = ExtResource("42_wigyb") -Events = [SubResource("Resource_tcc7d")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_tcc7d")]) [node name="Computer5" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Targets") instance=ExtResource("31_243ec")] position = Vector2(-1344, -251) @@ -986,7 +987,7 @@ Targets = [NodePath("Computer5Script")] [node name="Computer5Script" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer5"] script = ExtResource("42_wigyb") -Events = [SubResource("Resource_fasrq"), SubResource("Resource_mah4x"), SubResource("Resource_0bqta")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_fasrq"), SubResource("Resource_mah4x"), SubResource("Resource_0bqta")]) [node name="Computer3" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Target") instance=ExtResource("31_243ec")] position = Vector2(-612.57, -358.528) @@ -994,7 +995,7 @@ Target = NodePath("Script") [node name="Script" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer3"] script = ExtResource("42_wigyb") -Events = [SubResource("Resource_o2kpk")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_o2kpk")]) [node name="Computer4" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Target") instance=ExtResource("31_243ec")] position = Vector2(-791, -505) @@ -1002,7 +1003,7 @@ Target = NodePath("Node2D") [node name="Node2D" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer4"] script = ExtResource("42_wigyb") -Events = [SubResource("Resource_s3g2w")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_s3g2w")]) [node name="Label" type="Label" parent="Parallax2D/Factory Tilemaps/LevelProps/Computer4"] offset_left = -5.0 @@ -1031,11 +1032,11 @@ position = Vector2(-792, -407) [node name="BossBattleEndScript" type="Node2D" parent="Parallax2D/Factory Tilemaps/LevelProps"] position = Vector2(-1628.53, -477.628) script = ExtResource("42_wigyb") -Events = [SubResource("Resource_068l7"), SubResource("Resource_l3nop")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_068l7"), SubResource("Resource_l3nop")]) [node name="BossBattleStartScript" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("43_kf3qc")] position = Vector2(-1487, -396) -Events = [SubResource("Resource_4f4id"), SubResource("Resource_s2o7m"), SubResource("Resource_b1dht"), SubResource("Resource_xrgpy"), SubResource("Resource_j86ly")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_4f4id"), SubResource("Resource_s2o7m"), SubResource("Resource_b1dht"), SubResource("Resource_xrgpy"), SubResource("Resource_fuaed")]) [node name="Enemy13" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("47_u1ve6")] position = Vector2(-1657, -788) @@ -1162,7 +1163,7 @@ position = Vector2(-2000, -736) [node name="ControlPad8" parent="Parallax2D/Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Targets") instance=ExtResource("12_hfkf1")] position = Vector2(-2027, -735) Targets = [NodePath("../HorizontalForceField")] -Requirements = [ExtResource("84_ma1ta")] +Requirements = Array[ExtResource("6_8tdlb")]([ExtResource("84_ma1ta")]) [node name="Ammo6" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("34_17pjh")] position = Vector2(-872, -220) @@ -1324,7 +1325,7 @@ position = Vector2(-2072, -211) [node name="ScriptableAreaTrigger" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("43_kf3qc")] position = Vector2(-2063, -232) -Events = [SubResource("Resource_l476f"), SubResource("Resource_dskij")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_l476f"), SubResource("Resource_dskij")]) [node name="ShieldExtendPickup" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("103_0bqta")] position = Vector2(-1646, 38) @@ -1452,7 +1453,7 @@ position = Vector2(-1633, 391) [node name="MovementSensorLonger" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("112_j86ly")] position = Vector2(-1495, 516) -Events = [SubResource("Resource_uslvo")] +Events = Array[ExtResource("55_dferh")]([SubResource("Resource_uslvo")]) [node name="WallEmitter" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("113_dferh")] position = Vector2(-1480, 451) diff --git a/Scripts/Components/Actors/PlayerAnimationProvider.cs b/Scripts/Components/Actors/PlayerAnimationProvider.cs index 7c388cd9..df062b13 100644 --- a/Scripts/Components/Actors/PlayerAnimationProvider.cs +++ b/Scripts/Components/Actors/PlayerAnimationProvider.cs @@ -53,42 +53,56 @@ public partial class PlayerAnimationProvider : Node2D { _animatedSprite.Hide(); } - - public void SetAnimation(Vector2 velocity) - { - if (velocity.Length() == 0) - { - _animatedSprite.SpeedScale = 0; - } - else - { - if (velocity.Length() > 40) - { - _animatedSprite.SpeedScale = 1; - } - else - { - _animatedSprite.SpeedScale = 0.8f; - } - - } - if (velocity.X > 0) + public void SetAnimationSpeed(Vector2 velocity) + { + if (velocity.Length() == 0) + { + _animatedSprite.SpeedScale = 0; + } + else + { + if (velocity.Length() > 40) + { + _animatedSprite.SpeedScale = 1; + } + else + { + _animatedSprite.SpeedScale = 0.8f; + } + + } + } + + public void SetAnimation(Vector2 direction) + { + if (direction == Vector2.Zero) + return; + + float angle = Mathf.RadToDeg(direction.Angle()); + + // Normalize to 0-360 + if (angle < 0) + angle += 360; + + string animToPlay; + + switch (angle) { - _animatedSprite.Play(WalkRightAnimationName); - } - else if (velocity.X < 0) - { - _animatedSprite.Play(WalkLeftAnimationName); - } - else if (velocity.Y > 0) - { - _animatedSprite.Play(WalkDownAnimationName); - } - else if (velocity.Y < 0) - { - _animatedSprite.Play(WalkUpAnimationName); + case >= 45 and < 135: + _animatedSprite.Play(WalkDownAnimationName); + break; + case >= 135 and < 225: + _animatedSprite.Play(WalkLeftAnimationName); + break; + case >= 225 and < 315: + _animatedSprite.Play(WalkUpAnimationName); + break; + default: + _animatedSprite.Play(WalkRightAnimationName); + break; } + } public void Blink() diff --git a/Scripts/Components/FSM/Enemy/AnimationModule.cs b/Scripts/Components/FSM/Enemy/AnimationModule.cs index d0ade049..7c15606f 100644 --- a/Scripts/Components/FSM/Enemy/AnimationModule.cs +++ b/Scripts/Components/FSM/Enemy/AnimationModule.cs @@ -13,7 +13,7 @@ public partial class AnimationModule : ModuleBase public override void EnterState(EnemyState state) { - AnimationProvider.SetAnimation(StorageModule.FacingDirection); + AnimationProvider.SetAnimation(StorageModule.AimingDirection); AnimationProvider.SetAnimation(Vector2.Zero); } @@ -29,7 +29,7 @@ public partial class AnimationModule : ModuleBase public override void Process(double delta) { - AnimationProvider.SetAnimation(StorageModule.FacingDirection); + AnimationProvider.SetAnimation(StorageModule.AimingDirection); if (_machine.MainObject.Velocity == Vector2.Zero) { diff --git a/Scripts/Components/FSM/Player/Active.cs b/Scripts/Components/FSM/Player/Active.cs index 8a9bbd95..3bffb18d 100644 --- a/Scripts/Components/FSM/Player/Active.cs +++ b/Scripts/Components/FSM/Player/Active.cs @@ -98,7 +98,8 @@ public partial class Active : PlayerStateBase public override void ExitState() { base.ExitState(); - _animationProvider.SetAnimation(Vector2.Zero); + _animationProvider.SetAnimationSpeed(Vector2.Zero); + //_animationProvider.SetAnimation(Vector2.Zero); _crosshairProvider.Hide(); _hitboxSpriteProvider.Hide(); @@ -147,7 +148,8 @@ public partial class Active : PlayerStateBase } // } - _animationProvider.SetAnimation(MainObject.Velocity); + _animationProvider.SetAnimationSpeed(MainObject.Velocity); + _animationProvider.SetAnimation(FacingDirection); _crosshairProvider.UpdatePosition(FacingDirection); diff --git a/Scripts/Components/FSM/Player/Cutscene.cs b/Scripts/Components/FSM/Player/Cutscene.cs index 938b47bb..6c649556 100644 --- a/Scripts/Components/FSM/Player/Cutscene.cs +++ b/Scripts/Components/FSM/Player/Cutscene.cs @@ -15,11 +15,12 @@ public partial class Cutscene : PlayerStateBase public override void ExitState() { - _animationProvider.SetAnimation(Vector2.Zero); + _animationProvider.SetAnimationSpeed(Vector2.Zero); } public override void ProcessState(double delta) { + _animationProvider.SetAnimationSpeed(MainObject.Velocity); _animationProvider.SetAnimation(MainObject.Velocity); } diff --git a/Scripts/Components/FSM/Player/PlayerStorageModule.cs b/Scripts/Components/FSM/Player/PlayerStorageModule.cs index bc81d20c..2f3f37bf 100644 --- a/Scripts/Components/FSM/Player/PlayerStorageModule.cs +++ b/Scripts/Components/FSM/Player/PlayerStorageModule.cs @@ -8,5 +8,6 @@ public partial class PlayerStorageModule : Node2D public PlayerFSMProxy Root { get; private set; } public Node2D RootAsNode => Root; public Vector2 FacingDirection { get; set; } + public Vector2 AimingDirection { get; set; } } \ No newline at end of file