diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index b67a1c6..f89032d 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -8,3 +8,6 @@ collision_layer = 8 script = ExtResource( 2 ) GunPrefab = ExtResource( 4 ) + +[node name="AnimatedSprite" parent="." index="1"] +frame = 1 diff --git a/DungeonShooting_Godot/prefab/weapon/Weapon.tscn b/DungeonShooting_Godot/prefab/weapon/Weapon.tscn index 18e8050..d730340 100644 --- a/DungeonShooting_Godot/prefab/weapon/Weapon.tscn +++ b/DungeonShooting_Godot/prefab/weapon/Weapon.tscn @@ -1,6 +1,5 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=7 format=2] -[ext_resource path="res://src/weapon/gun/Gun.cs" type="Script" id=1] [ext_resource path="res://resource/materlal/Shadow.gdshader" type="Shader" id=2] [sub_resource type="ShaderMaterial" id=9] @@ -72,32 +71,33 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 7.8, 3.5 ) -[node name="Weapon" type="Area2D"] +[node name="Weapon" type="Node2D"] + +[node name="WeaponBody" type="Area2D" parent="."] collision_layer = 4 collision_mask = 0 -script = ExtResource( 1 ) -[node name="WeaponSprite" type="Sprite" parent="."] +[node name="WeaponSprite" type="Sprite" parent="WeaponBody"] material = SubResource( 9 ) position = Vector2( 0.4, -2.6 ) scale = Vector2( 0.8, 0.8 ) texture = SubResource( 8 ) -[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +[node name="AnimationPlayer" type="AnimationPlayer" parent="WeaponBody"] autoplay = "Floodlight" playback_process_mode = 0 anims/Floodlight = SubResource( 3 ) anims/RESET = SubResource( 4 ) -[node name="OriginPoint" type="Position2D" parent="."] +[node name="OriginPoint" type="Position2D" parent="WeaponBody"] position = Vector2( -3.60001, -1.1 ) -[node name="ShellPoint" type="Position2D" parent="."] +[node name="ShellPoint" type="Position2D" parent="WeaponBody"] position = Vector2( -2.60001, -2.60001 ) -[node name="FirePoint" type="Position2D" parent="."] +[node name="FirePoint" type="Position2D" parent="WeaponBody"] position = Vector2( 7.39999, -1.1 ) -[node name="Collision" type="CollisionShape2D" parent="."] +[node name="Collision" type="CollisionShape2D" parent="WeaponBody"] position = Vector2( 0.59999, 0.199997 ) shape = SubResource( 1 ) diff --git a/DungeonShooting_Godot/prefab/weapon/gun/Shotgun.tscn b/DungeonShooting_Godot/prefab/weapon/gun/Shotgun.tscn deleted file mode 100644 index c1c65dc..0000000 --- a/DungeonShooting_Godot/prefab/weapon/gun/Shotgun.tscn +++ /dev/null @@ -1,7 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://prefab/weapon/Weapon.tscn" type="PackedScene" id=1] -[ext_resource path="res://src/weapon/gun/Shotgun.cs" type="Script" id=2] - -[node name="Shotgun" instance=ExtResource( 1 )] -script = ExtResource( 2 ) diff --git a/DungeonShooting_Godot/src/common/NodeExtend.cs b/DungeonShooting_Godot/src/common/NodeExtend.cs index f408367..6b0d817 100644 --- a/DungeonShooting_Godot/src/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/common/NodeExtend.cs @@ -72,4 +72,18 @@ weapon.Position = Vector2.Zero; return weapon.StartThrow(new Vector2(20, 20), startPos, startHeight, direction, xf, yf, rotate, weapon.WeaponSprite); } + + public static IProp AsProp(this Node2D node2d) + { + if (node2d is IProp p) + { + return p; + } + var parent = node2d.GetParent(); + if (parent != null && parent is IProp p2) + { + return p2; + } + return null; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/manager/GameManager.cs b/DungeonShooting_Godot/src/manager/GameManager.cs index 5631110..0216d77 100644 --- a/DungeonShooting_Godot/src/manager/GameManager.cs +++ b/DungeonShooting_Godot/src/manager/GameManager.cs @@ -11,6 +11,9 @@ public GameManager() { GameManager.Instance = this; + + //扫描并注册当前程序集下的武器 + WeaponManager.RegisterWeaponFromAssembly(GetType().Assembly); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/manager/ResourceManager.cs b/DungeonShooting_Godot/src/manager/ResourceManager.cs index 4eaacac..5421af6 100644 --- a/DungeonShooting_Godot/src/manager/ResourceManager.cs +++ b/DungeonShooting_Godot/src/manager/ResourceManager.cs @@ -46,7 +46,7 @@ { if (CachePack.TryGetValue(path, out var pack)) { - return (T)pack; + return pack as T; } else { @@ -54,24 +54,9 @@ if (pack != null) { CachePack.Add(path, pack); - return (T)pack; + return pack as T; } } return default(T); } - - /// - /// 加载并实例化一个武器对象 - /// - /// 资源路径 - public static Weapon LoadWeaponAndInstance(string path) - { - var pack = Load(path); - if (pack != null) - { - return pack.Instance(); - } - return null; - } - } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/package/Holster.cs b/DungeonShooting_Godot/src/package/Holster.cs index 6de869c..bbe7c9b 100644 --- a/DungeonShooting_Godot/src/package/Holster.cs +++ b/DungeonShooting_Godot/src/package/Holster.cs @@ -83,7 +83,7 @@ for (int i = 0; i < SlotList.Length; i++) { var item = SlotList[i]; - if (item.Weapon != null && item.Weapon.Attribute.Id == id) + if (item.Weapon != null && item.Weapon.Id == id) { return i; } diff --git a/DungeonShooting_Godot/src/role/Player.cs b/DungeonShooting_Godot/src/role/Player.cs index 8735cde..71fa511 100644 --- a/DungeonShooting_Godot/src/role/Player.cs +++ b/DungeonShooting_Godot/src/role/Player.cs @@ -60,10 +60,10 @@ base._Ready(); Holster.SlotList[2].Enable = true; Holster.SlotList[3].Enable = true; - PickUpWeapon(WeaponManager.GetGun1()); //0 - PickUpWeapon(WeaponManager.GetGun2()); //1 - PickUpWeapon(WeaponManager.GetGun3()); //2 - PickUpWeapon(WeaponManager.GetGun4()); //3 + // PickUpWeapon(WeaponManager.GetGun1()); //0 + // PickUpWeapon(WeaponManager.GetGun2()); //1 + // PickUpWeapon(WeaponManager.GetGun3()); //2 + // PickUpWeapon(WeaponManager.GetGun4()); //3 RefreshGunTexture(); MaxHp = 50; @@ -181,7 +181,7 @@ var gun = Holster.ActiveWeapon; if (gun != null) { - RoomUI.Current.SetGunTexture(gun.Attribute.Sprite); + RoomUI.Current.SetGunTexture(gun.WeaponSprite.Texture); } else { diff --git a/DungeonShooting_Godot/src/role/Role.cs b/DungeonShooting_Godot/src/role/Role.cs index a8e715a..362981b 100644 --- a/DungeonShooting_Godot/src/role/Role.cs +++ b/DungeonShooting_Godot/src/role/Role.cs @@ -303,7 +303,8 @@ /// private void _OnPropsEnter(Area2D other) { - if (other is IProp prop) //道具类型 + IProp prop = other.AsProp(); + if (prop != null) //道具类型 { if (!InteractiveItemList.Contains(prop)) { @@ -318,7 +319,8 @@ /// private void _OnPropsExit(Area2D other) { - if (other is IProp prop) //道具类型 + IProp prop = other.AsProp(); + if (prop != null) //道具类型 { if (InteractiveItemList.Contains(prop)) { @@ -331,5 +333,4 @@ } } } - } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/room/RoomManager.cs b/DungeonShooting_Godot/src/room/RoomManager.cs index 77e4f24..c3a1711 100644 --- a/DungeonShooting_Godot/src/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/room/RoomManager.cs @@ -43,21 +43,21 @@ { //播放bgm SoundManager.PlayeMusic("intro.ogg", this, -17f); - var gun1 = WeaponManager.GetGun1(); + var gun1 = WeaponManager.GetGun("1001"); gun1.Position = new Vector2(80, 80); gun1.PutDown(gun1.WeaponSprite); - var gun2 = WeaponManager.GetGun2(); - gun2.Position = new Vector2(80, 120); - gun2.PutDown(gun2.WeaponSprite); - var gun3 = WeaponManager.GetGun3(); - gun3.Position = new Vector2(120, 80); - gun3.PutDown(gun3.WeaponSprite); - var gun4 = WeaponManager.GetGun4(); - gun4.Position = new Vector2(120, 120); - gun4.PutDown(gun4.WeaponSprite); - var gun5 = WeaponManager.GetGun5(); - gun5.Position = new Vector2(160, 160); - gun5.PutDown(gun5.WeaponSprite); + // var gun2 = WeaponManager.GetGun2(); + // gun2.Position = new Vector2(80, 120); + // gun2.PutDown(gun2.WeaponSprite); + // var gun3 = WeaponManager.GetGun3(); + // gun3.Position = new Vector2(120, 80); + // gun3.PutDown(gun3.WeaponSprite); + // var gun4 = WeaponManager.GetGun4(); + // gun4.Position = new Vector2(120, 120); + // gun4.PutDown(gun4.WeaponSprite); + // var gun5 = WeaponManager.GetGun5(); + // gun5.Position = new Vector2(160, 160); + // gun5.PutDown(gun5.WeaponSprite); } public override void _Process(float delta) diff --git a/DungeonShooting_Godot/src/weapon/RegisterWeapon.cs b/DungeonShooting_Godot/src/weapon/RegisterWeapon.cs index 8399b7e..3cbcd57 100644 --- a/DungeonShooting_Godot/src/weapon/RegisterWeapon.cs +++ b/DungeonShooting_Godot/src/weapon/RegisterWeapon.cs @@ -1,10 +1,23 @@ using System; -[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] +/// +/// 用作 Weapon 子类上, 用于注册武器, 允许同时存在多个 RegisterWeapon 特性 +/// +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class RegisterWeapon : Attribute { + + public string Id { get; private set; } + public Type AttributeType { get; private set; } + public RegisterWeapon(string id) { + Id = id; + } + public RegisterWeapon(string id, Type attributeType) + { + Id = id; + AttributeType = attributeType; } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/weapon/RegisterWeaponFunction.cs b/DungeonShooting_Godot/src/weapon/RegisterWeaponFunction.cs new file mode 100644 index 0000000..1796a95 --- /dev/null +++ b/DungeonShooting_Godot/src/weapon/RegisterWeaponFunction.cs @@ -0,0 +1,16 @@ +using System; + +/// +/// 用作静态函数上, 用于注册武器, 函数必须返回一个 Weapon 对象, 且参数为 string id, +/// 那么它看起来应该像这样: static Weapon Method(string id); +/// +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +public class RegisterWeaponFunction : Attribute +{ + public string Id { get; private set; } + + public RegisterWeaponFunction(string id) + { + Id = id; + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/weapon/Weapon.cs b/DungeonShooting_Godot/src/weapon/Weapon.cs index febf1cb..01bda48 100644 --- a/DungeonShooting_Godot/src/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/weapon/Weapon.cs @@ -4,8 +4,14 @@ /// /// 武器的基类 /// -public abstract class Weapon : Area2D, IProp +public abstract class Weapon : Node2D, IProp { + + /// + /// 武器的唯一id + /// + public string Id { get; } + /// /// 开火回调事件 /// @@ -14,19 +20,7 @@ /// /// 属性数据 /// - public WeaponAttribute Attribute - { - get - { - if (_attribute == null) - { - throw new Exception("请先调用Init来初始化武器的属性"); - } - return _attribute; - } - private set => _attribute = value; - } - private WeaponAttribute _attribute; + public WeaponAttribute Attribute { get; private set; } /// /// 武器的图片 @@ -127,6 +121,22 @@ //连发状态记录 private bool continuousShootFlag = false; + public Weapon(string id, WeaponAttribute attribute) + { + Id = id; + Attribute = attribute; + //加载预制体 + var tempPrefab = ResourceManager.Load(Attribute.WeaponPrefab); + if (tempPrefab == null) + { + throw new Exception("WeaponAttribute中未设置'WeaponPrefab'属性!"); + } + var tempNode = tempPrefab.Instance(); + var body = tempNode.GetChild(0); + tempNode.RemoveChild(body); + AddChild(body); + } + /// /// 初始化时调用 /// @@ -169,6 +179,30 @@ /// protected abstract void OnConceal(); + public override void _Ready() + { + WeaponSprite = GetNode("WeaponBody/WeaponSprite"); + FirePoint = GetNode("WeaponBody/FirePoint"); + OriginPoint = GetNode("WeaponBody/OriginPoint"); + ShellPoint = GetNode("WeaponBody/ShellPoint"); + AnimationPlayer = GetNode("WeaponBody/AnimationPlayer"); + CollisionShape2D = GetNode("WeaponBody/Collision"); + + //更新图片 + WeaponSprite.Texture = ResourceLoader.Load(Attribute.Sprite); + WeaponSprite.Position = Attribute.CenterPosition; + //开火位置 + FirePoint.Position = new Vector2(Attribute.FirePosition.x, -Attribute.FirePosition.y); + OriginPoint.Position = new Vector2(0, -Attribute.FirePosition.y); + + //弹药量 + CurrAmmo = Attribute.AmmoCapacity; + //剩余弹药量 + ResidueAmmo = Attribute.MaxAmmoCapacity - Attribute.AmmoCapacity; + + Init(); + } + public override void _Process(float delta) { if (Master == null) //这把武器被扔在地上 @@ -274,36 +308,6 @@ } } - public void Init(WeaponAttribute attribute) - { - if (_attribute != null) - { - throw new Exception("当前武器已经初始化过了!"); - } - - WeaponSprite = GetNode("WeaponSprite"); - FirePoint = GetNode("FirePoint"); - OriginPoint = GetNode("OriginPoint"); - ShellPoint = GetNode("ShellPoint"); - AnimationPlayer = GetNode("AnimationPlayer"); - CollisionShape2D = GetNode("Collision"); - - Attribute = attribute; - //更新图片 - WeaponSprite.Texture = attribute.Sprite; - WeaponSprite.Position = Attribute.CenterPosition; - //开火位置 - FirePoint.Position = new Vector2(attribute.FirePosition.x, -attribute.FirePosition.y); - OriginPoint.Position = new Vector2(0, -attribute.FirePosition.y); - - //弹药量 - CurrAmmo = attribute.AmmoCapacity; - //剩余弹药量 - ResidueAmmo = attribute.MaxAmmoCapacity - attribute.AmmoCapacity; - - Init(); - } - /// /// 扳机函数, 调用即视为扣动扳机 /// @@ -363,8 +367,9 @@ //子弹不够 _Reload(); } - - if (fireFlag) { + + if (fireFlag) + { if (justDown) { //开火前延时 @@ -502,7 +507,7 @@ { if (ResidueAmmo >= Attribute.AloneReloadCount) //剩余子弹充足 { - if ( CurrAmmo + Attribute.AloneReloadCount <= Attribute.AmmoCapacity) + if (CurrAmmo + Attribute.AloneReloadCount <= Attribute.AmmoCapacity) { ResidueAmmo -= Attribute.AloneReloadCount; CurrAmmo += Attribute.AloneReloadCount; @@ -563,7 +568,7 @@ { var masterWeapon = master.Holster.ActiveWeapon; //查找是否有同类型武器 - var index = master.Holster.FindWeapon(Attribute.Id); + var index = master.Holster.FindWeapon(Id); if (index != -1) //如果有这个武器 { if (CurrAmmo + ResidueAmmo != 0) //子弹不为空 @@ -605,7 +610,7 @@ public void Interactive(Role master) { //查找是否有同类型武器 - var index = master.Holster.FindWeapon(Attribute.Id); + var index = master.Holster.FindWeapon(Id); if (index != -1) //如果有这个武器 { if (CurrAmmo + ResidueAmmo == 0) //没有子弹了 @@ -693,7 +698,7 @@ /// /// 触发抛出 - /// + /// a public void _ThrowOutWeapon() { Master = null; diff --git a/DungeonShooting_Godot/src/weapon/WeaponAttribute.cs b/DungeonShooting_Godot/src/weapon/WeaponAttribute.cs index eec1eed..27c46e5 100644 --- a/DungeonShooting_Godot/src/weapon/WeaponAttribute.cs +++ b/DungeonShooting_Godot/src/weapon/WeaponAttribute.cs @@ -6,29 +6,21 @@ public class WeaponAttribute { /// - /// 武器的唯一id - /// - public string Id = "1"; - /// /// 武器显示的名称 /// public string Name = "Gun1"; /// + /// 武器 Prefab, 必须继承场景 "res://prefab/weapon/Weapon.tscn" + /// + public string WeaponPrefab = "res://prefab/weapon/Weapon.tscn"; + /// /// 武器类型 /// public WeaponWeightType WeightType = WeaponWeightType.MainWeapon; /// /// 武器的图片 /// - public Texture Sprite; - /// - /// 子弹预制体 - /// - public PackedScene BulletPack; - /// - /// 弹壳预制体 - /// - public PackedScene ShellPack; + public string Sprite = "res://resource/sprite/gun/gun4.png"; /// /// 是否连续发射, 如果为false, 则每次发射都需要扣动扳机 /// diff --git a/DungeonShooting_Godot/src/weapon/WeaponManager.cs b/DungeonShooting_Godot/src/weapon/WeaponManager.cs index c732b44..5343aa2 100644 --- a/DungeonShooting_Godot/src/weapon/WeaponManager.cs +++ b/DungeonShooting_Godot/src/weapon/WeaponManager.cs @@ -1,241 +1,339 @@ +using System.Reflection; +using System; using System.Collections.Generic; using Godot; +/// +/// 武器管理类, 负责武器注册和创建 +/// public static class WeaponManager { - public static void RegisterWeapon() - { + private static Dictionary> registerData = new Dictionary>(); + /// + /// 从一个指定的程序集中扫描并注册武器对象 + /// + /// 查询集 + public static void RegisterWeaponFromAssembly(Assembly assembly) + { + var types = assembly.GetTypes(); + foreach (var type in types) + { + //注册类 + Attribute[] attribute = Attribute.GetCustomAttributes(type, typeof(RegisterWeapon), false); + if (attribute != null && attribute.Length > 0) + { + if (!typeof(Weapon).IsAssignableFrom(type)) + { + throw new Exception($"注册武器类'{type.FullName}'没有继承类'Weapon'!"); + } + var atts = (RegisterWeapon[])attribute; + foreach (var att in atts) + { + //注册类 + if (att.AttributeType == null) //没有指定属性类型 + { + RegisterWeapon(att.Id, () => + { + return Activator.CreateInstance(type, att.Id, new WeaponAttribute()) as Weapon; + }); + } + else + { + if (!typeof(WeaponAttribute).IsAssignableFrom(att.AttributeType)) + { + throw new Exception($"注册武器类'{type.FullName}'标注的特性中参数'AttributeType'类型没有继承'WeaponAttribute'!"); + } + RegisterWeapon(att.Id, () => + { + return Activator.CreateInstance(type, att.Id, Activator.CreateInstance(att.AttributeType)) as Weapon; + }); + } + } + } + + //注册函数 + MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + foreach (var method in methods) + { + Attribute mAttribute; + // + if ((mAttribute = Attribute.GetCustomAttribute(method, typeof(RegisterWeaponFunction), false)) != null) + { + if (!typeof(Weapon).IsAssignableFrom(method.ReturnType)) //返回值类型不是 Weapon + { + throw new Exception($"注册武器函数'{method.DeclaringType.FullName}.{method.Name}'返回值类型不为'Weapon'!"); + } + var args = method.GetParameters(); + if (args == null || args.Length != 1 || args[0].ParameterType != typeof(string)) //参数类型不正确 + { + throw new Exception($"注册武器函数'{method.DeclaringType.FullName}.{method.Name}'参数不满足(string id)类型"); + } + var att = (RegisterWeaponFunction)mAttribute; + //注册函数 + RegisterWeapon(att.Id, () => + { + return method.Invoke(null, new object[] { att.Id }) as Weapon; + }); + } + } + } } + /// + /// 注册当个武器对象 + /// + /// 武器唯一id, 该id不能重复 + /// 获取武器时的回调函数, 函数返回武器对象 + public static void RegisterWeapon(string id, Func callBack) + { + if (registerData.ContainsKey(id)) + { + throw new Exception($"武器id: '{id} 已经被注册!'"); + } + registerData.Add(id, callBack); + } + + /// + /// 根据武器唯一id获取 + /// + /// 武器id public static Weapon GetGun(string id) { - return null; + if (registerData.TryGetValue(id, out var callback)) + { + return callback(); + } + throw new Exception($"当前武器'{id}未被注册'"); } + /// + /// 根据武器唯一id获取 + /// + /// 武器id public static T GetGun(string id) where T : Weapon { return (T)GetGun(id); } - //----------------------------- 以下均为临时处理 ------------------------------- + /* + public static Weapon GetGun1() + { + //加载枪的 prefab + var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); + //设置基础属性 + var attr = new WeaponAttribute(); + attr.Id = "1"; + attr.Name = "Gun1"; + attr.Weight = 40; + attr.CenterPosition = new Vector2(0.4f, -2.6f); + attr.StartFiringSpeed = 480; + attr.StartScatteringRange = 30; + attr.FinalScatteringRange = 90; + attr.ScatteringRangeAddValue = 2f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = true; + //扳机检测间隔 + attr.TriggerInterval = 0f; + //连发数量 + attr.MinContinuousCount = 3; + attr.MaxContinuousCount = 3; + //开火前延时 + attr.DelayedTime = 0f; + //攻击距离 + attr.MinDistance = 500; + attr.MaxDistance = 600; + //发射子弹数量 + attr.MinFireBulletCount = 1; + attr.MaxFireBulletCount = 1; + //抬起角度 + attr.UpliftAngle = 10; + //枪身长度 + attr.FirePosition = new Vector2(16, 1.5f); + attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun4.png"); + attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); + attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + gun.Init(attr); + return gun; + } - public static Weapon GetGun1() - { - //加载枪的 prefab - var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); - //设置基础属性 - var attr = new WeaponAttribute(); - attr.Id = "1"; - attr.Name = "Gun1"; - attr.Weight = 40; - attr.CenterPosition = new Vector2(0.4f, -2.6f); - attr.StartFiringSpeed = 480; - attr.StartScatteringRange = 30; - attr.FinalScatteringRange = 90; - attr.ScatteringRangeAddValue = 2f; - attr.ScatteringRangeBackSpeed = 40; - //连发 - attr.ContinuousShoot = true; - //扳机检测间隔 - attr.TriggerInterval = 0f; - //连发数量 - attr.MinContinuousCount = 3; - attr.MaxContinuousCount = 3; - //开火前延时 - attr.DelayedTime = 0f; - //攻击距离 - attr.MinDistance = 500; - attr.MaxDistance = 600; - //发射子弹数量 - attr.MinFireBulletCount = 1; - attr.MaxFireBulletCount = 1; - //抬起角度 - attr.UpliftAngle = 10; - //枪身长度 - attr.FirePosition = new Vector2(16, 1.5f); - attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun4.png"); - attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); - attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); - gun.Init(attr); - return gun; - } + public static Weapon GetGun2() + { + //加载枪的 prefab + var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); + //设置基础属性 + var attr = new WeaponAttribute(); + attr.Id = "2"; + attr.Name = "Gun2"; + attr.Weight = 20; + attr.CenterPosition = new Vector2(0.4f, -2.6f); + attr.WeightType = WeaponWeightType.DeputyWeapon; + attr.StartFiringSpeed = 600; + attr.StartScatteringRange = 5; + attr.FinalScatteringRange = 60; + attr.ScatteringRangeAddValue = 8f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = false; + //扳机检测间隔 + attr.TriggerInterval = 0.4f; + //连发数量 + attr.MinContinuousCount = 3; + attr.MaxContinuousCount = 3; + //开火前延时 + attr.DelayedTime = 0f; + //攻击距离 + attr.MinDistance = 500; + attr.MaxDistance = 600; + //发射子弹数量 + attr.MinFireBulletCount = 1; + attr.MaxFireBulletCount = 1; + //抬起角度 + attr.UpliftAngle = 30; + //枪身长度 + attr.FirePosition = new Vector2(10, 1.5f); + attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun3.png"); + attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); + attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + gun.Init(attr); + return gun; + } - public static Weapon GetGun2() - { - //加载枪的 prefab - var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); - //设置基础属性 - var attr = new WeaponAttribute(); - attr.Id = "2"; - attr.Name = "Gun2"; - attr.Weight = 20; - attr.CenterPosition = new Vector2(0.4f, -2.6f); - attr.WeightType = WeaponWeightType.DeputyWeapon; - attr.StartFiringSpeed = 600; - attr.StartScatteringRange = 5; - attr.FinalScatteringRange = 60; - attr.ScatteringRangeAddValue = 8f; - attr.ScatteringRangeBackSpeed = 40; - //连发 - attr.ContinuousShoot = false; - //扳机检测间隔 - attr.TriggerInterval = 0.4f; - //连发数量 - attr.MinContinuousCount = 3; - attr.MaxContinuousCount = 3; - //开火前延时 - attr.DelayedTime = 0f; - //攻击距离 - attr.MinDistance = 500; - attr.MaxDistance = 600; - //发射子弹数量 - attr.MinFireBulletCount = 1; - attr.MaxFireBulletCount = 1; - //抬起角度 - attr.UpliftAngle = 30; - //枪身长度 - attr.FirePosition = new Vector2(10, 1.5f); - attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun3.png"); - attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); - attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); - gun.Init(attr); - return gun; - } + public static Weapon GetGun3() + { + //加载枪的 prefab + var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/gun/Shotgun.tscn"); + //设置基础属性 + var attr = new WeaponAttribute(); + attr.Id = "3"; + attr.Name = "Gun3"; + attr.Weight = 30; + attr.CenterPosition = new Vector2(0.4f, -2.6f); + attr.StartFiringSpeed = 180; + attr.StartScatteringRange = 30; + attr.FinalScatteringRange = 90; + attr.ScatteringRangeAddValue = 2f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = false; + //装弹时间 + attr.ReloadTime = 0.4f; + //单独装弹 + attr.AloneReload = true; + attr.AloneReloadCount = 1; + attr.AloneReloadCanShoot = true; + //扳机检测间隔 + attr.TriggerInterval = 0f; + //连发数量 + attr.MinContinuousCount = 1; + attr.MaxContinuousCount = 1; + //开火前延时 + attr.DelayedTime = 0f; + //攻击距离 + attr.MinDistance = 150; + attr.MaxDistance = 200; + //发射子弹数量 + attr.MinFireBulletCount = 1; + attr.MaxFireBulletCount = 1; + //抬起角度 + attr.UpliftAngle = 10; + //枪身长度 + attr.FirePosition = new Vector2(16, 1.5f); + attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun2.png"); + attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); + attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + gun.Init(attr); + return gun; + } - public static Weapon GetGun3() - { - //加载枪的 prefab - var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/gun/Shotgun.tscn"); - //设置基础属性 - var attr = new WeaponAttribute(); - attr.Id = "3"; - attr.Name = "Gun3"; - attr.Weight = 30; - attr.CenterPosition = new Vector2(0.4f, -2.6f); - attr.StartFiringSpeed = 180; - attr.StartScatteringRange = 30; - attr.FinalScatteringRange = 90; - attr.ScatteringRangeAddValue = 2f; - attr.ScatteringRangeBackSpeed = 40; - //连发 - attr.ContinuousShoot = false; - //装弹时间 - attr.ReloadTime = 0.4f; - //单独装弹 - attr.AloneReload = true; - attr.AloneReloadCount = 1; - attr.AloneReloadCanShoot = true; - //扳机检测间隔 - attr.TriggerInterval = 0f; - //连发数量 - attr.MinContinuousCount = 1; - attr.MaxContinuousCount = 1; - //开火前延时 - attr.DelayedTime = 0f; - //攻击距离 - attr.MinDistance = 150; - attr.MaxDistance = 200; - //发射子弹数量 - attr.MinFireBulletCount = 1; - attr.MaxFireBulletCount = 1; - //抬起角度 - attr.UpliftAngle = 10; - //枪身长度 - attr.FirePosition = new Vector2(16, 1.5f); - attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun2.png"); - attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); - attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); - gun.Init(attr); - return gun; - } + public static Weapon GetGun4() + { + //加载枪的 prefab + var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); + //设置基础属性 + var attr = new WeaponAttribute(); + attr.Id = "4"; + attr.Name = "Gun4"; + attr.Weight = 10; + attr.CenterPosition = new Vector2(0.4f, -2.6f); + attr.WeightType = WeaponWeightType.DeputyWeapon; + attr.StartFiringSpeed = 600; + attr.StartScatteringRange = 5; + attr.FinalScatteringRange = 60; + attr.ScatteringRangeAddValue = 8f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = false; + //扳机检测间隔 + attr.TriggerInterval = 0.4f; + //连发数量 + attr.MinContinuousCount = 3; + attr.MaxContinuousCount = 3; + //开火前延时 + attr.DelayedTime = 0f; + //攻击距离 + attr.MinDistance = 500; + attr.MaxDistance = 600; + //发射子弹数量 + attr.MinFireBulletCount = 1; + attr.MaxFireBulletCount = 1; + //抬起角度 + attr.UpliftAngle = 30; + //枪身长度 + attr.FirePosition = new Vector2(10, 1.5f); + attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun7.png"); + attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); + attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + gun.Init(attr); + return gun; + } - public static Weapon GetGun4() - { - //加载枪的 prefab - var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); - //设置基础属性 - var attr = new WeaponAttribute(); - attr.Id = "4"; - attr.Name = "Gun4"; - attr.Weight = 10; - attr.CenterPosition = new Vector2(0.4f, -2.6f); - attr.WeightType = WeaponWeightType.DeputyWeapon; - attr.StartFiringSpeed = 600; - attr.StartScatteringRange = 5; - attr.FinalScatteringRange = 60; - attr.ScatteringRangeAddValue = 8f; - attr.ScatteringRangeBackSpeed = 40; - //连发 - attr.ContinuousShoot = false; - //扳机检测间隔 - attr.TriggerInterval = 0.4f; - //连发数量 - attr.MinContinuousCount = 3; - attr.MaxContinuousCount = 3; - //开火前延时 - attr.DelayedTime = 0f; - //攻击距离 - attr.MinDistance = 500; - attr.MaxDistance = 600; - //发射子弹数量 - attr.MinFireBulletCount = 1; - attr.MaxFireBulletCount = 1; - //抬起角度 - attr.UpliftAngle = 30; - //枪身长度 - attr.FirePosition = new Vector2(10, 1.5f); - attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun7.png"); - attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); - attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); - gun.Init(attr); - return gun; - } - - public static Weapon GetGun5() - { - //加载枪的 prefab - var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); - //设置基础属性 - var attr = new WeaponAttribute(); - attr.Id = "5"; - attr.Name = "Gun5"; - attr.Weight = 10; - attr.CenterPosition = new Vector2(0.4f, -2.6f); - attr.WeightType = WeaponWeightType.DeputyWeapon; - attr.StartFiringSpeed = 480; - attr.StartScatteringRange = 5; - attr.FinalScatteringRange = 30; - attr.ScatteringRangeAddValue = 8f; - attr.ScatteringRangeBackSpeed = 40; - //连发 - attr.ContinuousShoot = true; - //扳机检测间隔 - attr.TriggerInterval = 0.4f; - //连发数量 - attr.MinContinuousCount = 1; - attr.MaxContinuousCount = 1; - //开火前延时 - attr.DelayedTime = 0f; - //攻击距离 - attr.MinDistance = 500; - attr.MaxDistance = 600; - //发射子弹数量 - attr.MinFireBulletCount = 1; - attr.MaxFireBulletCount = 1; - //弹夹容量 - attr.AmmoCapacity = 120; - attr.MaxAmmoCapacity = 360; - //抬起角度 - attr.UpliftAngle = 30; - //枪身长度 - attr.FirePosition = new Vector2(10, 1.5f); - attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun5.png"); - attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); - attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); - gun.Init(attr); - return gun; - } + public static Weapon GetGun5() + { + //加载枪的 prefab + var gun = ResourceManager.LoadWeaponAndInstance("res://prefab/weapon/Weapon.tscn"); + //设置基础属性 + var attr = new WeaponAttribute(); + attr.Id = "5"; + attr.Name = "Gun5"; + attr.Weight = 10; + attr.CenterPosition = new Vector2(0.4f, -2.6f); + attr.WeightType = WeaponWeightType.DeputyWeapon; + attr.StartFiringSpeed = 480; + attr.StartScatteringRange = 5; + attr.FinalScatteringRange = 30; + attr.ScatteringRangeAddValue = 8f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = true; + //扳机检测间隔 + attr.TriggerInterval = 0.4f; + //连发数量 + attr.MinContinuousCount = 1; + attr.MaxContinuousCount = 1; + //开火前延时 + attr.DelayedTime = 0f; + //攻击距离 + attr.MinDistance = 500; + attr.MaxDistance = 600; + //发射子弹数量 + attr.MinFireBulletCount = 1; + attr.MaxFireBulletCount = 1; + //弹夹容量 + attr.AmmoCapacity = 120; + attr.MaxAmmoCapacity = 360; + //抬起角度 + attr.UpliftAngle = 30; + //枪身长度 + attr.FirePosition = new Vector2(10, 1.5f); + attr.Sprite = ResourceManager.Load("res://resource/sprite/gun/gun5.png"); + attr.BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/HighSpeedBullet.tscn"); + attr.ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + gun.Init(attr); + return gun; + } + */ } diff --git a/DungeonShooting_Godot/src/weapon/gun/Gun.cs b/DungeonShooting_Godot/src/weapon/gun/Gun.cs index 094a07b..34f18be 100644 --- a/DungeonShooting_Godot/src/weapon/gun/Gun.cs +++ b/DungeonShooting_Godot/src/weapon/gun/Gun.cs @@ -1,12 +1,35 @@ using System; using Godot; - +[RegisterWeapon("1001", typeof(WeaponAttribute))] +[RegisterWeapon("1002")] /// /// 普通的枪 /// public class Gun : Weapon { + + /// + /// 子弹预制体 + /// + public PackedScene BulletPack; + /// + /// 弹壳预制体 + /// + public PackedScene ShellPack; + + [RegisterWeaponFunction("1003")] + private static Gun Test(string id) + { + return new Gun(id, new WeaponAttribute()); + } + + public Gun(string id, WeaponAttribute attribute): base(id, attribute) + { + BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); + ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + } + protected override void Init() { @@ -21,7 +44,7 @@ var xf = MathUtils.RandRangeInt(20, 60); var yf = MathUtils.RandRangeInt(60, 120); var rotate = MathUtils.RandRangeInt(-720, 720); - var sprite = Attribute.ShellPack.Instance(); + var sprite = ShellPack.Instance(); sprite.StartThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite); //创建抖动 MainCamera.Main.ProssesDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); @@ -30,7 +53,7 @@ protected override void OnShootBullet() { //创建子弹 - CreateBullet(Attribute.BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle()); + CreateBullet(BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle()); } protected override void OnReload() diff --git a/DungeonShooting_Godot/src/weapon/gun/Shotgun.cs b/DungeonShooting_Godot/src/weapon/gun/Shotgun.cs index 5b8f442..7825375 100644 --- a/DungeonShooting_Godot/src/weapon/gun/Shotgun.cs +++ b/DungeonShooting_Godot/src/weapon/gun/Shotgun.cs @@ -2,6 +2,22 @@ public class Shotgun : Weapon { + + /// + /// 子弹预制体 + /// + public PackedScene BulletPack; + /// + /// 弹壳预制体 + /// + public PackedScene ShellPack; + + public Shotgun(string id, WeaponAttribute attribute): base(id, attribute) + { + BulletPack = ResourceManager.Load("res://prefab/weapon/bullet/OrdinaryBullets.tscn"); + ShellPack = ResourceManager.Load("res://prefab/weapon/shell/ShellCase.tscn"); + } + protected override void Init() { @@ -16,7 +32,7 @@ var xf = MathUtils.RandRangeInt(20, 60); var yf = MathUtils.RandRangeInt(60, 120); var rotate = MathUtils.RandRangeInt(-720, 720); - var sprite = Attribute.ShellPack.Instance(); + var sprite = ShellPack.Instance(); sprite.StartThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite); //创建抖动 MainCamera.Main.ProssesDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); @@ -27,7 +43,7 @@ for (int i = 0; i < 5; i++) { //创建子弹 - CreateBullet(Attribute.BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle() + MathUtils.RandRange(-20 / 180f * Mathf.Pi, 20 / 180f * Mathf.Pi)); + CreateBullet(BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle() + MathUtils.RandRange(-20 / 180f * Mathf.Pi, 20 / 180f * Mathf.Pi)); } }