diff --git a/prefab/Gun.tscn b/prefab/Gun.tscn index ef22feb..c04ae84 100644 --- a/prefab/Gun.tscn +++ b/prefab/Gun.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=2] [ext_resource path="res://resource/sprite/gun/gun1.png" type="Texture" id=1] -[ext_resource path="res://src/weapon/gun/Gun.cs" type="Script" id=2] +[ext_resource path="res://src/weapon/gun/OrdinaryGun.cs" type="Script" id=2] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 9.5, 3.5 ) @@ -9,11 +9,10 @@ [node name="Gun" type="Node2D"] script = ExtResource( 2 ) -[node name="Sprite" type="Sprite" parent="."] -position = Vector2( 1.49012e-08, -1.49012e-08 ) +[node name="GunSprite" type="Sprite" parent="."] +position = Vector2( 4, -3 ) scale = Vector2( 0.8, 0.8 ) texture = ExtResource( 1 ) -offset = Vector2( 4, -3 ) [node name="OriginPoint" type="Position2D" parent="."] position = Vector2( 0, -1.5 ) diff --git a/resource/map/dungeon_test.tmx b/resource/map/dungeon_test.tmx index 304a2b9..fb1ba2f 100644 --- a/resource/map/dungeon_test.tmx +++ b/resource/map/dungeon_test.tmx @@ -92,7 +92,7 @@ 51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51 - + diff --git a/src/Cursor.cs b/src/Cursor.cs index 498c4d6..4450f54 100644 --- a/src/Cursor.cs +++ b/src/Cursor.cs @@ -34,6 +34,9 @@ SetCursorPos(); } + /// + /// 设置光标半径范围 + /// private void SetScope(float scope) { if (TargetGun != null) @@ -41,7 +44,7 @@ var len = GlobalPosition.DistanceTo(TargetGun.GlobalPosition); if (TargetGun.Attribute != null) { - len = Mathf.Max(0, len - TargetGun.Attribute.BarrelLength); + len = Mathf.Max(0, len - TargetGun.Attribute.FirePosition.x); } scope = len / GameConfig.ScatteringDistance * scope; } diff --git a/src/role/Player.cs b/src/role/Player.cs index 8a5eeae..455f6e6 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -31,18 +31,18 @@ var attr = new GunAttribute(); attr.FiringSpeed = 600; attr.StartScatteringRange = 0; - attr.FinalScatteringRange = 30; + attr.FinalScatteringRange = 0; attr.ScatteringRangeAddValue = 5; attr.ScatteringRangeBackSpeed = 30; //连发 - attr.ContinuousShoot = false; + attr.ContinuousShoot = true; //扳机检测间隔 - attr.TriggerInterval = 0.35f; + attr.TriggerInterval = 0f; //连发数量 attr.MinContinuousCount = 3; attr.MaxContinuousCount = 3; //开火前延时 - attr.DelayedTime = 0f; + attr.DelayedTime = 0.5f; //攻击距离 attr.MinDistance = 500; attr.MaxDistance = 600; @@ -52,13 +52,12 @@ //抬起角度 attr.UpliftAngle = 10; //枪身长度 - attr.BarrelLength = 15; + attr.FirePosition = new Vector2(15, 1.5f); attr.Sprite = "res://resource/sprite/gun/gun4.png"; gun.Init(attr); gun.FireEvent += FireEvent_Func; RoomManager.Current.Cursor.TargetGun = gun; - //VisualServer.SetDefaultClearColor(new Color("1C1117")); } public override void _Process(float delta) diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index 9aa32e0..3900e80 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -4,7 +4,7 @@ /// /// 枪的基类 /// -public class Gun : Node2D +public abstract class Gun : Node2D { /// /// 开火回调事件 @@ -31,18 +31,18 @@ /// /// 枪的图片 /// - public Sprite Sprite { get; private set; } + public Sprite GunSprite { get; private set; } /// /// 枪攻击的目标阵营 /// public CampEnum TargetCamp { get; set; } /// - /// 开火点 + /// 枪管的开火点 /// public Position2D FirePoint { get; private set; } /// - /// 原点 + /// 枪管的原点 /// public Position2D OriginPoint { get; private set; } /// @@ -71,12 +71,10 @@ //连发次数 private float continuousCount = 0; private bool continuousShootFlag = false; - //子弹 - private PackedScene bulletPacked; public override void _EnterTree() { - Sprite = GetNode("Sprite"); + GunSprite = GetNode("GunSprite"); FirePoint = GetNode("FirePoint"); OriginPoint = GetNode("OriginPoint"); } @@ -112,7 +110,7 @@ attackTimer = 0; } } - else if (delayedTime > 0) + else if (delayedTime > 0) //攻击延时 { delayedTime -= delta; if (attackTimer < 0) @@ -149,14 +147,22 @@ public void Init(GunAttribute attribute) { + if (_attribute != null) + { + throw new Exception("当前武器已经初始化过了!"); + } + Attribute = attribute; //更新图片 var texture = ResourceLoader.Load(attribute.Sprite); - Sprite.Texture = texture; - //子弹 - bulletPacked = ResourceLoader.Load(attribute.Bullet); + GunSprite.Texture = texture; //开火位置 - FirePoint.Position = new Vector2(attribute.BarrelLength, FirePoint.Position.y); + FirePoint.Position = new Vector2(attribute.FirePosition.x, -attribute.FirePosition.y); + OriginPoint.Position = new Vector2(0, -attribute.FirePosition.y); + //握把位置 + GunSprite.Position = attribute.HoldPosition; + + Init(); } /// @@ -234,14 +240,38 @@ private void UpTriggern() { continuousShootFlag = false; + if (delayedTime > 0) + { + continuousCount = 0; + } } + /// + /// 触发开火 + /// private void TriggernFire() { continuousCount = continuousCount > 0 ? continuousCount - 1 : 0; fireInterval = 60 / Attribute.FiringSpeed; attackTimer += fireInterval; + + //触发开火函数 Fire(); + + //开火发射的子弹数量 + var bulletCount = MathUtils.RandRangeInt(Attribute.MaxFireBulletCount, Attribute.MinFireBulletCount); + //枪口角度 + var angle = new Vector2(GameConfig.ScatteringDistance, CurrScatteringRange).Angle(); + + //创建子弹 + for (int i = 0; i < bulletCount; i++) + { + //先算枪口方向 + Rotation = (float)GD.RandRange(-angle, angle); + //发射子弹 + ShootBullet(); + } + //当前的散射半径 CurrScatteringRange = Mathf.Min(CurrScatteringRange + Attribute.ScatteringRangeAddValue, Attribute.FinalScatteringRange); //枪的旋转角度 @@ -257,39 +287,18 @@ } /// - /// 开火 + /// 初始化时调用 /// - protected virtual void Fire() - { - //开火发射的子弹数量 - var bulletCount = MathUtils.RandRangeInt(Attribute.MaxFireBulletCount, Attribute.MinFireBulletCount); - //枪口角度 - var angle = new Vector2(GameConfig.ScatteringDistance, CurrScatteringRange).Angle(); + protected abstract void Init(); - //创建子弹 - for (int i = 0; i < bulletCount; i++) - { - //先算枪口方向 - Rotation = (float)GD.RandRange(-angle, angle); + /// + /// 单次开火时调用的函数 + /// + protected abstract void Fire(); - //创建子弹 - var bullet = CreateBullet(bulletPacked); - //位置 - bullet.GlobalPosition = FirePoint.GlobalPosition; - //角度 - bullet.Rotation = (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle(); - GetTree().CurrentScene.AddChild(bullet); - //飞行距离 - var distance = MathUtils.RandRange(Attribute.MinDistance, Attribute.MaxDistance); - //初始化子弹数据 - bullet.InitData(distance, Colors.White); - } - } - - public T CreateBullet(PackedScene bulletPack) where T : Bullet - { - T bullet = bulletPack.Instance(); - bullet.Init(TargetCamp, this, null); - return bullet; - } + /// + /// 发射子弹时调用的函数, 每发射一枚子弹调用一次, + /// 如果做霰弹枪效果, 一次开火发射5枚子弹, 则该函数调用5次 + /// + protected abstract void ShootBullet(); } \ No newline at end of file diff --git a/src/weapon/gun/GunAttribute.cs b/src/weapon/gun/GunAttribute.cs index df608a5..f3ebc4e 100644 --- a/src/weapon/gun/GunAttribute.cs +++ b/src/weapon/gun/GunAttribute.cs @@ -6,10 +6,6 @@ public class GunAttribute { /// - /// 子弹对象 - /// - public string Bullet = "res://prefab/Bullet.tscn"; - /// /// 枪的图片 /// public string Sprite = "res://resource/sprite/gun/gun1.png"; @@ -70,9 +66,13 @@ /// public float MinDistance = 800; /// - /// 枪管长度 + /// 开火位置 /// - public float BarrelLength = 11; + public Vector2 FirePosition = new Vector2(11, 0); + /// + /// 握把位置 + /// + public Vector2 HoldPosition = new Vector2(4, -3); /// /// 重量 /// @@ -89,6 +89,10 @@ /// 开火后枪口上抬角度 /// public float UpliftAngle = 30; + /// + /// 开火后枪口角度恢复速度倍数 + /// + public float UpliftAngleRestore = 1; public GunAttribute() { diff --git a/src/weapon/gun/OrdinaryGun.cs b/src/weapon/gun/OrdinaryGun.cs new file mode 100644 index 0000000..a5960f0 --- /dev/null +++ b/src/weapon/gun/OrdinaryGun.cs @@ -0,0 +1,44 @@ +using Godot; + +/// +/// 普通的枪 +/// +public class OrdinaryGun : Gun +{ + //子弹 + private PackedScene bulletPacked; + + + protected override void Init() + { + //子弹 + bulletPacked = ResourceLoader.Load("res://prefab/Bullet.tscn"); + } + + protected override void Fire() + { + + } + + protected override void ShootBullet() + { + //创建子弹 + var bullet = CreateBullet(bulletPacked); + //位置 + bullet.GlobalPosition = FirePoint.GlobalPosition; + //角度 + bullet.Rotation = (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle(); + GetTree().CurrentScene.AddChild(bullet); + //飞行距离 + var distance = MathUtils.RandRange(Attribute.MinDistance, Attribute.MaxDistance); + //初始化子弹数据 + bullet.InitData(distance, Colors.White); + } + + protected T CreateBullet(PackedScene bulletPack) where T : Bullet + { + T bullet = bulletPack.Instance(); + bullet.Init(TargetCamp, this, null); + return bullet; + } +} \ No newline at end of file