Basic weapon direction

This commit is contained in:
Marco 2025-05-06 15:07:22 +02:00
commit 0146de8ab5
4 changed files with 92 additions and 6 deletions

View file

@ -410,9 +410,18 @@ sprite_frames = ExtResource("14_p5rkw")
animation = &"idle"
autoplay = "idle"
[node name="WeaponProvider" type="Node2D" parent="."]
[node name="WeaponProvider" type="Node2D" parent="." node_paths=PackedStringArray("StorageModule", "WeaponRightOffset", "WeaponLeftOffset")]
script = ExtResource("5_gp3hw")
StorageModule = NodePath("../Storage")
WeaponTemplate = ExtResource("6_8dcio")
WeaponRightOffset = NodePath("RightWeapon")
WeaponLeftOffset = NodePath("LeftWeapon")
[node name="RightWeapon" type="Marker2D" parent="WeaponProvider"]
position = Vector2(4, 3)
[node name="LeftWeapon" type="Marker2D" parent="WeaponProvider"]
position = Vector2(-14, 3)
[node name="AnimationProvider" type="Node2D" parent="." node_paths=PackedStringArray("_animatedSprite", "_shieldParticles")]
script = ExtResource("7_pmkfo")

View file

@ -1,20 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://crry0rgk7a8sm"]
[gd_scene load_steps=4 format=3 uid="uid://crry0rgk7a8sm"]
[ext_resource type="Script" uid="uid://2ji3nxqbq577" path="res://Scripts/Weapon.cs" id="1_f5iec"]
[ext_resource type="Script" uid="uid://dw7nwfbws3op4" path="res://Scripts/Weapons/WeaponSoundModule.cs" id="2_uwnyl"]
[ext_resource type="Texture2D" uid="uid://duwiasewxvcb5" path="res://Sprites/Items/Icicle_Gun.png" id="3_dbepg"]
[node name="Weapon" type="Sprite2D" node_paths=PackedStringArray("Muzzle")]
[node name="Weapon" type="Node2D" node_paths=PackedStringArray("Muzzle", "Pivot", "Sprite")]
script = ExtResource("1_f5iec")
Muzzle = NodePath("Muzzle")
Pivot = NodePath("Pivot")
Sprite = NodePath("Sprite2D")
[node name="Muzzle" type="Marker2D" parent="."]
[node name="Pivot" type="Marker2D" parent="."]
position = Vector2(-4.685, 3.52)
[node name="ShootTimer" type="Timer" parent="."]
one_shot = true
[node name="SoundModule" type="Node2D" parent="." node_paths=PackedStringArray("Weapon", "ShootSound", "ReloadSound", "EmptySound")]
script = ExtResource("2_uwnyl")
Weapon = NodePath("..")
Weapon = NodePath("")
ShootSound = NodePath("ShootSound")
ReloadSound = NodePath("ReloadSound")
EmptySound = NodePath("EmptySound")
@ -27,6 +33,9 @@ area_mask = 2
[node name="EmptySound" type="AudioStreamPlayer2D" parent="SoundModule"]
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("3_dbepg")
[connection signal="Empty" from="." to="SoundModule" method="PlayEmptySound"]
[connection signal="Reloading" from="." to="SoundModule" method="PlayReloadSound"]
[connection signal="Shooting" from="." to="SoundModule" method="PlayShootSound"]

View file

@ -14,6 +14,10 @@ public partial class PlayerWeaponProvider : Node2D
[Export] public PackedScene WeaponTemplate { get; private set; }
[Export] public double WeaponSwitchCooldown { get; private set; } = 0.5d;
[Export] public Marker2D WeaponRightOffset { get; private set; } // local offset when facing right
[Export] public Marker2D WeaponLeftOffset { get; private set; } // local offset when facing left
public Array<Weapon> EquippedWeapons { get; set; } = [];
private int _currentWeaponIndex = 0;
@ -64,6 +68,8 @@ public partial class PlayerWeaponProvider : Node2D
public void Update(double delta)
{
RotateWeapon();
if (!_switching) return;
_switchCooldown += delta;
if (_switchCooldown >= WeaponSwitchCooldown)
@ -72,6 +78,24 @@ public partial class PlayerWeaponProvider : Node2D
_switchCooldown = 0d;
}
}
private void RotateWeapon()
{
if (EquippedWeapon is null) return;
EquippedWeapon.RotateWeapon(StorageModule.FacingDirection, WeaponLeftOffset.Position, WeaponRightOffset.Position);
// EquippedWeapon.SetRotation(angle + Mathf.Pi / 2.0f);
//
//
//
// EquippedWeapon.FlipH = facingLeft;
//
// // 3. Position on correct side (assuming EquippedWeapon is a child of the Player node)
// EquippedWeapon.Position = facingLeft ? WeaponLeftOffset : WeaponRightOffset;
}
private void OnInventoryWeaponEquipped(string itemKey)
{
@ -192,7 +216,7 @@ public partial class PlayerWeaponProvider : Node2D
var weapon = this.CreateSibling<Weapon>(WeaponTemplate);
weapon.WeaponData = startingItem.WeaponData;
weapon.Texture = startingItem.InventorySprite;
weapon.Sprite.Texture = startingItem.InventorySprite;
this.AddWeapon(weapon);
return weapon;

View file

@ -6,7 +6,7 @@ using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
public partial class Weapon : Sprite2D
public partial class Weapon : Node2D
{
[Export]
@ -18,6 +18,12 @@ public partial class Weapon : Sprite2D
[Export]
public Marker2D Muzzle { get; set; }
[Export]
public Marker2D Pivot { get; set; }
[Export]
public Sprite2D Sprite { get; private set; }
[Signal]
public delegate void ShootingEventHandler();
@ -189,4 +195,42 @@ public partial class Weapon : Sprite2D
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
}
public void RotateWeapon(Vector2 facingDirection, Vector2 leftPosition, Vector2 rightPosition)
{
bool facingLeft = facingDirection.X < 0;
if (facingLeft)
{
Sprite.Position = leftPosition - Pivot.Position;
Sprite.FlipH = true;
}
else
{
Sprite.Position = rightPosition - Pivot.Position;
Sprite.FlipH = false;
}
// var angle = Mathf.Atan2(StorageModule.FacingDirection.X, StorageModule.FacingDirection.Y);
//
// // Weapon is drawn pointing right, but we want it to point "up" along aim vector.
// // So we apply a 90° offset (π/2), but...
// // If facing left, we also add 180° (π) to "un-mirror" it visually.
// float rotationOffset = (Mathf.Pi / 2f);
//
// if (facingLeft)
// {
// EquippedWeapon.Sprite.Rotation = -(angle + rotationOffset);// + rotationOffset + Mathf.Pi;
// EquippedWeapon.Sprite.Position = WeaponLeftOffset;
// EquippedWeapon.Sprite.FlipH = true; // disable FlipH to avoid messing with rotation
// EquippedWeapon.Sprite.FlipV = false;
// }
// else
// {
// EquippedWeapon.Sprite.Rotation = angle;// + rotationOffset;
// EquippedWeapon.Sprite.Position = WeaponRightOffset;
// EquippedWeapon.Sprite.FlipH = false;
// EquippedWeapon.Sprite.FlipV = true;
// }
}
}