diff --git a/prefab/role/Player.tscn b/prefab/role/Player.tscn index 41d150d..0389b0f 100644 --- a/prefab/role/Player.tscn +++ b/prefab/role/Player.tscn @@ -10,4 +10,4 @@ GunPrefab = ExtResource( 4 ) [node name="AnimatedSprite" parent="." index="0"] -frame = 3 +frame = 0 diff --git a/project.godot b/project.godot index eee72d2..65d7eec 100644 --- a/project.godot +++ b/project.godot @@ -152,6 +152,11 @@ "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"physical_scancode":0,"unicode":0,"echo":false,"script":null) ] } +reload={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":82,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} [layer_names] diff --git a/src/role/Player.cs b/src/role/Player.cs index 67fc1d7..d6ed468 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -54,19 +54,24 @@ if (Input.IsActionJustPressed("exchange")) //切换武器 { - ExchangeNext(); + TriggerExchangeNext(); } else if (Input.IsActionJustPressed("throw")) //扔掉武器 { - ThrowGun(); + TriggerThrowGun(); } else if (Input.IsActionJustPressed("interactive")) //互动物体 { TriggerTnteractive(); } - - //攻击 - Attack(); + else if (Input.IsActionJustPressed("reload")) //换弹 + { + TriggerReload(); + } + if (Input.IsActionPressed("fire")) //开火 + { + TriggerAttack(); + } } public override void _PhysicsProcess(float delta) @@ -93,17 +98,6 @@ Velocity = MoveAndSlide(Velocity); } - private void Attack() - { - if (Input.IsActionPressed("fire")) - { - if (Holster.ActiveGun != null) - { - Holster.ActiveGun.Trigger(); - } - } - } - // 播放动画 private void PlayAnim() { diff --git a/src/role/Role.cs b/src/role/Role.cs index 4b41ba9..8a4450c 100644 --- a/src/role/Role.cs +++ b/src/role/Role.cs @@ -89,7 +89,7 @@ /// /// 切换到下一个武器 /// - public void ExchangeNext() + public void TriggerExchangeNext() { Holster.ExchangeNext(); } @@ -105,7 +105,7 @@ /// /// 扔掉当前使用的武器, 切换到上一个武器 /// - public void ThrowGun() + public void TriggerThrowGun() { var gun = Holster.RmoveGun(Holster.ActiveIndex); //播放抛出效果 @@ -149,6 +149,28 @@ } /// + /// 触发换弹 + /// + public void TriggerReload() + { + if (Holster.ActiveGun != null) + { + Holster.ActiveGun._Reload(); + } + } + + /// + /// 触发攻击 + /// + public void TriggerAttack() + { + if (Holster.ActiveGun != null) + { + Holster.ActiveGun.Trigger(); + } + } + + /// /// 设置脸的朝向 /// private void SetFace(FaceDirection face) diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index c18a344..d29ee16 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -102,6 +102,10 @@ private float continuousCount = 0; //连发状态记录 private bool continuousShootFlag = false; + //是否在换弹中 + private bool reloading = false; + //换弹计时器 + private float reloadTimer = 0; /// @@ -112,13 +116,18 @@ /// /// 单次开火时调用的函数 /// - protected abstract void Fire(); + protected abstract void OnFire(); + + /// + /// 换弹时调用 + /// + protected abstract void OnReload(); /// /// 发射子弹时调用的函数, 每发射一枚子弹调用一次, /// 如果做霰弹枪效果, 一次开火发射5枚子弹, 则该函数调用5次 /// - protected abstract void ShootBullet(); + protected abstract void OnShootBullet(); /// /// 当武器被拾起时调用 @@ -145,6 +154,7 @@ { if (Master == null) //这把武器被扔在地上 { + reloading = false; triggerTimer = triggerTimer > 0 ? triggerTimer - delta : 0; triggerFlag = false; attackFlag = false; @@ -155,6 +165,7 @@ } else if (Master.Holster.ActiveGun != this) //当前武器没有被使用 { + reloading = false; triggerTimer = triggerTimer > 0 ? triggerTimer - delta : 0; triggerFlag = false; attackFlag = false; @@ -165,6 +176,17 @@ } else //正在使用中 { + + //换弹 + if (reloading) + { + reloadTimer -= delta; + if (reloadTimer <= 0) + { + ReloadSuccess(); + } + } + if (triggerFlag) { if (upTimer > 0) //第一帧按下扳机 @@ -206,6 +228,7 @@ //连发判断 if (continuousCount > 0 && delayedTime <= 0 && attackTimer <= 0) { + //开火 TriggernFire(); } @@ -254,6 +277,8 @@ //弹药量 CurrAmmo = attribute.CartridgeCapacity; + //剩余弹药量 + ResidueAmmo = attribute.MaxCartridgeCapacity - attribute.CartridgeCapacity; Init(); } @@ -298,23 +323,37 @@ if (flag) { - if (justDown) + if (reloading) { - //开火前延时 - delayedTime = Attribute.DelayedTime; - //扳机按下间隔 - triggerTimer = Attribute.TriggerInterval; - //连发数量 - if (!Attribute.ContinuousShoot) + //换弹中 + GD.Print("换弹中..."); + } + else if (CurrAmmo <= 0) + { + //子弹不够 + GD.Print("弹夹打空了, 按R换弹!"); + } + else + { + if (justDown) { - continuousCount = MathUtils.RandRangeInt(Attribute.MinContinuousCount, Attribute.MaxContinuousCount); + //开火前延时 + delayedTime = Attribute.DelayedTime; + //扳机按下间隔 + triggerTimer = Attribute.TriggerInterval; + //连发数量 + if (!Attribute.ContinuousShoot) + { + continuousCount = MathUtils.RandRangeInt(Attribute.MinContinuousCount, Attribute.MaxContinuousCount); + } } + if (delayedTime <= 0 && attackTimer <= 0) + { + TriggernFire(); + } + attackFlag = true; } - if (delayedTime <= 0 && attackTimer <= 0) - { - TriggernFire(); - } - attackFlag = true; + } triggerFlag = true; } @@ -345,11 +384,16 @@ private void TriggernFire() { continuousCount = continuousCount > 0 ? continuousCount - 1 : 0; + + //减子弹数量 + CurrAmmo--; + //开火间隙 fireInterval = 60 / Attribute.StartFiringSpeed; + //攻击冷却 attackTimer += fireInterval; //触发开火函数 - Fire(); + OnFire(); //开火发射的子弹数量 var bulletCount = MathUtils.RandRangeInt(Attribute.MaxFireBulletCount, Attribute.MinFireBulletCount); @@ -362,7 +406,7 @@ //先算枪口方向 Rotation = (float)GD.RandRange(-angle, angle); //发射子弹 - ShootBullet(); + OnShootBullet(); } //当前的散射半径 @@ -379,6 +423,44 @@ } } + /// + /// 触发换弹 + /// + public void _Reload() + { + if (CurrAmmo < Attribute.CartridgeCapacity && ResidueAmmo > 0 && !reloading) + { + reloading = true; + reloadTimer = Attribute.ReloadTime; + OnReload(); + } + } + + /// + /// 换弹计时器时间到, 执行换弹操作 + /// + private void ReloadSuccess() + { + if (Attribute.AloneReload) //单独装填 + { + + } + else //换弹结束 + { + reloading = false; + if (ResidueAmmo >= Attribute.CartridgeCapacity) + { + ResidueAmmo -= Attribute.CartridgeCapacity; + CurrAmmo = Attribute.CartridgeCapacity; + } + else + { + CurrAmmo = ResidueAmmo; + ResidueAmmo = 0; + } + } + } + public void Tnteractive(Role master) { var parent = GetParent(); diff --git a/src/weapon/gun/GunAttribute.cs b/src/weapon/gun/GunAttribute.cs index b03b528..db173dc 100644 --- a/src/weapon/gun/GunAttribute.cs +++ b/src/weapon/gun/GunAttribute.cs @@ -38,7 +38,7 @@ /// public int CartridgeCapacity = 30; /// - /// 弹夹容量上限 + /// 弹药容量上限 /// public int MaxCartridgeCapacity = 90; /// diff --git a/src/weapon/gun/OrdinaryGun.cs b/src/weapon/gun/OrdinaryGun.cs index f658b4b..ff11b4c 100644 --- a/src/weapon/gun/OrdinaryGun.cs +++ b/src/weapon/gun/OrdinaryGun.cs @@ -12,7 +12,7 @@ } - protected override void Fire() + protected override void OnFire() { //创建一个弹壳 var temp = new ThrowShell(); @@ -27,12 +27,17 @@ RoomManager.Current.SortRoot.AddChild(temp); } - protected override void ShootBullet() + protected override void OnShootBullet() { //创建子弹 CreateBullet(Attribute.BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle()); } + protected override void OnReload() + { + + } + protected override void OnPickUp(Role master) {