diff --git a/prefab/role/Role.tscn b/prefab/role/Role.tscn index 5c6d122..9dbcbfe 100644 --- a/prefab/role/Role.tscn +++ b/prefab/role/Role.tscn @@ -85,7 +85,7 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" -frame = 3 +frame = 1 playing = true [node name="HitArea" type="Area2D" parent="."] diff --git a/prefab/weapon/Gun.tscn b/prefab/weapon/Gun.tscn index b0d38ca..7b63e3f 100644 --- a/prefab/weapon/Gun.tscn +++ b/prefab/weapon/Gun.tscn @@ -1,25 +1,92 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://src/weapon/gun/OrdinaryGun.cs" type="Script" id=1] [ext_resource path="res://resource/materlal/Shadow.gdshader" type="Shader" id=2] -[ext_resource path="res://resource/sprite/gun/gun1.png" type="Texture" id=3] -[sub_resource type="ShaderMaterial" id=2] +[sub_resource type="ShaderMaterial" id=9] +resource_local_to_scene = true shader = ExtResource( 2 ) shader_param/shadowColor = Color( 1, 1, 1, 1 ) shader_param/schedule = 0.0 +[sub_resource type="StreamTexture" id=8] +load_path = "res://.import/gun1.png-f7bc3e27b4b477d47c7353ffb91687ea.stex" + +[sub_resource type="Animation" id=3] +resource_name = "Floodlight" +length = 3.0 +loop = true +step = 0.5 +tracks/0/type = "value" +tracks/0/path = NodePath("GunSprite:material:shader_param/shadowColor") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Color( 1, 1, 1, 1 ) ] +} +tracks/1/type = "value" +tracks/1/path = NodePath("GunSprite:material:shader_param/schedule") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/keys = { +"times": PoolRealArray( 0, 1.9, 2.4, 2.5, 3 ), +"transitions": PoolRealArray( 1, 1, 1, 1, 1 ), +"update": 0, +"values": [ 0.0, 0.0, 1.0, 1.0, 0.0 ] +} + +[sub_resource type="Animation" id=4] +length = 0.001 +tracks/0/type = "value" +tracks/0/path = NodePath("GunSprite:material:shader_param/shadowColor") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Color( 1, 1, 1, 1 ) ] +} +tracks/1/type = "value" +tracks/1/path = NodePath("GunSprite:material:shader_param/schedule") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} + [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 7.8, 3.5 ) [node name="Gun" type="Node2D"] +z_index = -1 script = ExtResource( 1 ) [node name="GunSprite" type="Sprite" parent="."] -material = SubResource( 2 ) +material = SubResource( 9 ) position = Vector2( 4, -3 ) scale = Vector2( 0.8, 0.8 ) -texture = ExtResource( 3 ) +texture = SubResource( 8 ) + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +autoplay = "Floodlight" +playback_process_mode = 0 +anims/Floodlight = SubResource( 3 ) +anims/RESET = SubResource( 4 ) [node name="OriginPoint" type="Position2D" parent="."] position = Vector2( 0, -1.5 ) diff --git a/project.godot b/project.godot index be00f17..f25ffdd 100644 --- a/project.godot +++ b/project.godot @@ -137,6 +137,16 @@ "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":71,"physical_scancode":0,"unicode":0,"echo":false,"script":null) ] } +mouse_roll_up={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":4,"pressed":false,"doubleclick":false,"script":null) + ] +} +mouse_roll_down={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":5,"pressed":false,"doubleclick":false,"script":null) + ] +} [layer_names] diff --git a/resource/materlal/Shadow.tres b/resource/materlal/Shadow.tres index c102048..df4a9bf 100644 --- a/resource/materlal/Shadow.tres +++ b/resource/materlal/Shadow.tres @@ -3,6 +3,7 @@ [ext_resource path="res://resource/materlal/Shadow.gdshader" type="Shader" id=1] [resource] +resource_local_to_scene = true shader = ExtResource( 1 ) shader_param/shadowColor = Color( 0, 0, 0, 0.8 ) shader_param/schedule = 1.0 diff --git a/scene/Room.tscn b/scene/Room.tscn index 23e17e9..64eb6c2 100644 --- a/scene/Room.tscn +++ b/scene/Room.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://prefab/role/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://resource/map/dungeon_test.tmx" type="PackedScene" id=2] [ext_resource path="res://src/room/RoomManager.cs" type="Script" id=3] [ext_resource path="res://prefab/ui/Cursor.tscn" type="PackedScene" id=4] -[ext_resource path="res://prefab/weapon/Gun.tscn" type="PackedScene" id=5] [node name="Room" type="Node2D"] script = ExtResource( 3 ) @@ -30,7 +29,3 @@ [node name="RemoteTransform2D" type="RemoteTransform2D" parent="YSort/Player"] remote_path = NodePath("../../../Camera2D") - -[node name="Gun" parent="YSort" instance=ExtResource( 5 )] -position = Vector2( 125, 132 ) -z_index = -1 diff --git a/src/manager/ResourceManager.cs b/src/manager/ResourceManager.cs index ef98c0a..ea38ce5 100644 --- a/src/manager/ResourceManager.cs +++ b/src/manager/ResourceManager.cs @@ -20,6 +20,22 @@ } private static ShaderMaterial _shadowMaterial; + /// + /// 2D阴影的Shader + /// + public static Shader ShadowShader + { + get + { + if (_shadowShader == null) + { + _shadowShader = ResourceLoader.Load("res://resource/materlal/Shadow.gdshader"); + } + return _shadowShader; + } + } + private static Shader _shadowShader; + private static readonly Dictionary CachePack = new Dictionary(); /// diff --git a/src/package/Holster.cs b/src/package/Holster.cs index e201dba..c9027ad 100644 --- a/src/package/Holster.cs +++ b/src/package/Holster.cs @@ -29,8 +29,15 @@ /// public Role Master { get; } + /// + /// 当前使用的武器对象 + /// public Gun ActiveGun { get; private set; } - public int ActiveIndex { get; private set; } + + /// + /// 当前使用的武器的索引 + /// + public int ActiveIndex { get; private set; } = 0; public GunSlot[] SlotList { get; } = new GunSlot[4]; @@ -68,6 +75,7 @@ if (item.Enable && gun.Attribute.WeightType == item.Type && item.Gun == null) { item.Gun = gun; + ExchangeByIndex(i); gun._PickUpGun(Master); return i; } @@ -77,20 +85,64 @@ } /// + /// 移除指定位置的武器, 并返回这个武器对象, 如果移除正在使用的这把武器, 则会自动切换到上一把武器 + /// + /// 所在枪套的位置索引 + public Gun RmoveGun(int index) + { + if (index < 0 || index >= SlotList.Length) + { + return null; + } + var slot = SlotList[index]; + if (slot.Gun == null) + { + return null; + } + var gun = slot.Gun; + gun.GetParent().RemoveChild(gun); + slot.Gun = null; + + //如果是当前手持的武器, 就需要调用切换武器操作 + if (index == ActiveIndex) + { + //没有其他武器了 + if (ExchangePrev() == index) + { + ActiveIndex = 0; + ActiveGun = null; + } + } + return gun; + } + + /// /// 切换到上一个武器 /// public int ExchangePrev() { - return 0; + var index = ActiveIndex - 1; + do + { + if (index < 0) + { + index = SlotList.Length - 1; + } + if (ExchangeByIndex(index)) + { + return index; + } + } while (index-- != ActiveIndex); + return -1; } /// - /// 切换到下一个武器 + /// 切换到下一个武器, /// public int ExchangeNext() { var index = ActiveIndex + 1; - while (index != ActiveIndex) + do { if (index >= SlotList.Length) { @@ -100,8 +152,8 @@ { return index; } - index++; } + while (index++ != ActiveIndex); return -1; } @@ -110,7 +162,7 @@ /// public bool ExchangeByIndex(int index) { - if (index == ActiveIndex && ActiveGun != null) return true; + if (index == ActiveIndex && ActiveGun != null) return true; if (index < 0 || index > SlotList.Length) return false; var slot = SlotList[index]; if (slot == null || slot.Gun == null) return false; @@ -118,33 +170,37 @@ //将上一把武器放到背后 if (ActiveGun != null) { - ActiveGun.GetParent().RemoveChild(ActiveGun); - Master.BackMountPoint.AddChild(ActiveGun); - if (ActiveIndex == 0) + var tempParent = ActiveGun.GetParentOrNull(); + if (tempParent != null) { - ActiveGun.Position = new Vector2(0, 5); - ActiveGun.RotationDegrees = 50; - ActiveGun.Scale = new Vector2(-1, 1); + tempParent.RemoveChild(ActiveGun); + Master.BackMountPoint.AddChild(ActiveGun); + if (ActiveIndex == 0) + { + ActiveGun.Position = new Vector2(0, 5); + ActiveGun.RotationDegrees = 50; + ActiveGun.Scale = new Vector2(-1, 1); + } + else if (ActiveIndex == 1) + { + ActiveGun.Position = new Vector2(0, 0); + ActiveGun.RotationDegrees = 120; + ActiveGun.Scale = new Vector2(1, -1); + } + else if (ActiveIndex == 2) + { + ActiveGun.Position = new Vector2(0, 5); + ActiveGun.RotationDegrees = 310; + ActiveGun.Scale = new Vector2(1, 1); + } + else if (ActiveIndex == 3) + { + ActiveGun.Position = new Vector2(0, 0); + ActiveGun.RotationDegrees = 60; + ActiveGun.Scale = new Vector2(1, 1); + } + ActiveGun._Conceal(); } - else if (ActiveIndex == 1) - { - ActiveGun.Position = new Vector2(0, 0); - ActiveGun.RotationDegrees = 120; - ActiveGun.Scale = new Vector2(1, -1); - } - else if (ActiveIndex == 2) - { - ActiveGun.Position = new Vector2(0, 5); - ActiveGun.RotationDegrees = 310; - ActiveGun.Scale = new Vector2(1, 1); - } - else if (ActiveIndex == 3) - { - ActiveGun.Position = new Vector2(0, 0); - ActiveGun.RotationDegrees = 60; - ActiveGun.Scale = new Vector2(1, 1); - } - ActiveGun._Conceal(); } //更改父节点 diff --git a/src/role/Player.cs b/src/role/Player.cs index e9ba08c..4a3a64b 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -31,8 +31,8 @@ Holster.SlotList[3].Enable = true; PickUpGun(GunManager.GetGun1()); //0 PickUpGun(GunManager.GetGun2()); //1 - PickUpGun(GunManager.GetGun1()); //2 - PickUpGun(GunManager.GetGun2()); //3 + PickUpGun(GunManager.GetGun3()); //2 + PickUpGun(GunManager.GetGun4()); //3 } public override void _Process(float delta) @@ -51,9 +51,16 @@ { Face = FaceDirection.Left; } - if (Input.IsActionJustPressed("exchange")) { + + if (Input.IsActionJustPressed("exchange")) //切换武器 + { ExchangeNext(); } + else if (Input.IsActionJustPressed("throw")) //扔掉武器 + { + ThrowGun(); + } + //攻击 Attack(); } diff --git a/src/role/Role.cs b/src/role/Role.cs index 4a6b36c..4bf5f92 100644 --- a/src/role/Role.cs +++ b/src/role/Role.cs @@ -82,15 +82,33 @@ /// 武器对象 public void PickUpGun(Gun gun) { - var index = Holster.PickupGun(gun); - Holster.ExchangeByIndex(index); + Holster.PickupGun(gun); } + /// + /// 切换到下一个武器 + /// public void ExchangeNext() { Holster.ExchangeNext(); } + /// + /// 切换到上一个武器 + /// + public void ExchangePrev() + { + Holster.ExchangePrev(); + } + + /// + /// 扔掉当前使用的武器, 切换到上一个武器 + /// + public void ThrowGun() + { + Holster.RmoveGun(Holster.ActiveIndex); + } + private void SetFace(FaceDirection face) { if (_face != face) diff --git a/src/room/RoomManager.cs b/src/room/RoomManager.cs index 6785fb8..30d1d59 100644 --- a/src/room/RoomManager.cs +++ b/src/room/RoomManager.cs @@ -34,6 +34,10 @@ Color color = (Color) node.GetMeta("ClearColor"); VisualServer.SetDefaultClearColor(color); + + var gun = GunManager.GetGun2(); + gun.Position = new Vector2(100, 100); + ItemRoot.AddChild(gun); } public override void _Process(float delta) diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index b00c87a..373b9ef 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -34,6 +34,12 @@ public Sprite GunSprite { get; private set; } /// + /// 动画播放器 + /// + /// + public AnimationPlayer AnimationPlayer { get; private set; } + + /// /// 枪攻击的目标阵营 /// public CampEnum TargetCamp { get; set; } @@ -82,28 +88,14 @@ private float continuousCount = 0; private bool continuousShootFlag = false; - private float floodlightTimer = -1; + //状态 0 在地上, 1 被拾起 + private int _state = 0; public override void _Process(float delta) { - if (Master == null) //这把武器被扔在地上 { - if (floodlightTimer < 0) - { - floodlightTimer = 3f; - } - else - { - if (floodlightTimer >= 2.5f) { - // Mathf.Lerp(); - } - else if (floodlightTimer >= 2f) { - - } - floodlightTimer -= delta; - } - + } else if (Master.Holster.ActiveGun != this) //当前武器没有被使用 { @@ -192,6 +184,7 @@ FirePoint = GetNode("FirePoint"); OriginPoint = GetNode("OriginPoint"); ShellPoint = GetNode("ShellPoint"); + AnimationPlayer = GetNode("AnimationPlayer"); Attribute = attribute; //更新图片 @@ -366,12 +359,16 @@ public void _PickUpGun(Role master) { Master = master; + _state = 1; + AnimationPlayer.Play("RESET"); OnPickUp(master); } public void _ThrowOutGun() { Master = null; + _state = 0; + AnimationPlayer.Play("Floodlight"); OnThrowOut(); } diff --git a/src/weapon/gun/GunManager.cs b/src/weapon/gun/GunManager.cs index 761b3b1..c0a19a1 100644 --- a/src/weapon/gun/GunManager.cs +++ b/src/weapon/gun/GunManager.cs @@ -82,4 +82,83 @@ gun.Init(attr); return gun; } + + public static Gun GetGun3() + { + //加载枪的 prefab + var gun = ResourceManager.LoadGunAndInstance("res://prefab/weapon/Gun.tscn"); + //设置基础属性 + var attr = new GunAttribute(); + attr.Id = "3"; + attr.Name = "Gun3"; + attr.StartFiringSpeed = 480; + attr.StartScatteringRange = 30; + attr.FinalScatteringRange = 90; + attr.ScatteringRangeAddValue = 2f; + attr.ScatteringRangeBackSpeed = 40; + //连发 + attr.ContinuousShoot = false; + //扳机检测间隔 + 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/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 Gun GetGun4() + { + //加载枪的 prefab + var gun = ResourceManager.LoadGunAndInstance("res://prefab/weapon/Gun.tscn"); + //设置基础属性 + var attr = new GunAttribute(); + attr.Id = "4"; + attr.Name = "Gun4"; + attr.WeightType = GunWeightType.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; + } }