diff --git a/prefab/role/Player.tscn b/prefab/role/Player.tscn index 9089f4d..abab257 100644 --- a/prefab/role/Player.tscn +++ b/prefab/role/Player.tscn @@ -8,6 +8,3 @@ collision_layer = 8 script = ExtResource( 2 ) GunPrefab = ExtResource( 4 ) - -[node name="AnimatedSprite" parent="." index="0"] -frame = 1 diff --git a/prefab/role/Role.tscn b/prefab/role/Role.tscn index 1c7e387..a7d86e4 100644 --- a/prefab/role/Role.tscn +++ b/prefab/role/Role.tscn @@ -101,3 +101,8 @@ [node name="MountPoint" type="Position2D" parent="."] position = Vector2( 2, -4 ) +z_index = 1 + +[node name="BackMountPoint" type="Position2D" parent="."] +position = Vector2( 0, -5 ) +z_index = -1 diff --git a/prefab/weapon/Gun.tscn b/prefab/weapon/Gun.tscn index 0a3f551..9e1161b 100644 --- a/prefab/weapon/Gun.tscn +++ b/prefab/weapon/Gun.tscn @@ -7,7 +7,6 @@ extents = Vector2( 9.5, 3.5 ) [node name="Gun" type="Node2D"] -z_index = 5 script = ExtResource( 1 ) [node name="GunSprite" type="Sprite" parent="."] diff --git a/src/package/Holster.cs b/src/package/Holster.cs index 0251c81..5dcbc66 100644 --- a/src/package/Holster.cs +++ b/src/package/Holster.cs @@ -68,8 +68,7 @@ if (item.Enable && gun.Attribute.WeightType == item.Type && item.Gun == null) { item.Gun = gun; - ActiveGun = gun; - ActiveIndex = i; + gun._PickUpGun(Master); return i; } } @@ -97,23 +96,7 @@ { index = 0; } - var slot = SlotList[index]; - if (slot != null && slot.Gun != null) - { - //更改父节点 - var parent = slot.Gun.GetParentOrNull(); - if (parent == null) - { - Master.MountPoint.AddChild(slot.Gun); - } - else if (parent != Master.MountPoint) - { - parent.RemoveChild(slot.Gun); - Master.MountPoint.AddChild(slot.Gun); - } - - ActiveGun = slot.Gun; - ActiveIndex = index; + if (ExchangeByIndex(index)) { return index; } index++; @@ -129,6 +112,16 @@ if (index > SlotList.Length) return false; var slot = SlotList[index]; if (slot == null || slot.Gun == null) return false; + + //将上一把武器放到背后 + if (ActiveGun != null) + { + ActiveGun.GetParent().RemoveChild(ActiveGun); + Master.BackMountPoint.AddChild(ActiveGun); + ActiveGun.RotationDegrees = 60; + ActiveGun.Scale = new Vector2(-1, 1); + } + //更改父节点 var parent = slot.Gun.GetParentOrNull(); if (parent == null) @@ -141,6 +134,8 @@ Master.MountPoint.AddChild(slot.Gun); } + slot.Gun.Scale = Vector2.One; + slot.Gun.RotationDegrees = 0; ActiveGun = slot.Gun; ActiveIndex = index; return true; diff --git a/src/role/Player.cs b/src/role/Player.cs index 4acdb47..d11bb27 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -28,8 +28,8 @@ { base._Ready(); - PickUpGun(GunManager.GetGun1()); - PickUpGun(GunManager.GetGun2()); + PickUpGun(GunManager.GetGun1()); //0 + PickUpGun(GunManager.GetGun2()); //1 } public override void _Process(float delta) diff --git a/src/role/Role.cs b/src/role/Role.cs index 6a5e4d0..b845f07 100644 --- a/src/role/Role.cs +++ b/src/role/Role.cs @@ -48,6 +48,10 @@ /// 武器挂载点 /// public Position2D MountPoint { get; private set; } + /// + /// 背后武器的挂载点 + /// + public Position2D BackMountPoint { get; private set; } /// /// 脸的朝向 @@ -62,6 +66,7 @@ StartScele = Scale; AnimatedSprite = GetNode("AnimatedSprite"); MountPoint = GetNode("MountPoint"); + BackMountPoint = GetNode("BackMountPoint"); // 更改纹理 ChangeFrameTexture(AnimatorNames.Idle, AnimatedSprite, Texture); ChangeFrameTexture(AnimatorNames.Run, AnimatedSprite, Texture); @@ -83,21 +88,12 @@ public void ExchangeNext() { - var index = Holster.ExchangeNext(); - if (index != -1) - { - SetActiveGun(index); - } + Holster.ExchangeNext(); } private void SetActiveGun(int index) { - if (Holster.ActiveGun != null) - { - Holster.ActiveGun.Visible = false; - } Holster.ExchangeByIndex(index); - Holster.ActiveGun.Visible = true; } private void SetFace(FaceDirection face) diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index 15e154e..ad5529a 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -41,7 +41,7 @@ /// /// 该武器的拥有者 /// - public Node2D Master { get; } + public Role Master { get; private set; } /// /// 枪管的开火点 @@ -85,6 +85,10 @@ public override void _Process(float delta) { + if (Master == null || Master.Holster.ActiveGun != this) + { + return; + } if (triggerFlag) { if (upTimer > 0) //第一帧按下扳机 @@ -321,13 +325,25 @@ /// protected abstract void OnThrowOut(); + public void _PickUpGun(Role master) + { + Master = master; + OnPickUp(master); + } + + public void _ThrowOutGun() + { + Master = null; + OnThrowOut(); + } + /// /// 实例化并返回子弹对象 /// /// 子弹的预制体 protected T CreateBullet(PackedScene bulletPack, Vector2 globalPostion, float globalRotation, Node parent = null) where T : Node2D, IBullet { - return (T) CreateBullet(bulletPack, globalPostion, globalRotation, parent); + return (T)CreateBullet(bulletPack, globalPostion, globalRotation, parent); }