diff --git a/prefab/role/Player.tscn b/prefab/role/Player.tscn index 41d150d..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 = 3 diff --git a/prefab/role/Role.tscn b/prefab/role/Role.tscn index 9dbcbfe..c2d47e0 100644 --- a/prefab/role/Role.tscn +++ b/prefab/role/Role.tscn @@ -4,22 +4,6 @@ [ext_resource path="res://resource/sprite/role/role2.png" type="Texture" id=2] [ext_resource path="res://resource/sprite/role/role1.png" type="Texture" id=3] -[sub_resource type="AtlasTexture" id=21] -atlas = ExtResource( 3 ) -region = Rect2( 0, 48, 16, 24 ) - -[sub_resource type="AtlasTexture" id=22] -atlas = ExtResource( 3 ) -region = Rect2( 16, 48, 16, 24 ) - -[sub_resource type="AtlasTexture" id=23] -atlas = ExtResource( 3 ) -region = Rect2( 32, 48, 16, 24 ) - -[sub_resource type="AtlasTexture" id=24] -atlas = ExtResource( 3 ) -region = Rect2( 48, 48, 16, 24 ) - [sub_resource type="AtlasTexture" id=25] atlas = ExtResource( 3 ) region = Rect2( 48, 48, 16, 24 ) @@ -36,6 +20,22 @@ atlas = ExtResource( 3 ) region = Rect2( 0, 48, 16, 24 ) +[sub_resource type="AtlasTexture" id=21] +atlas = ExtResource( 3 ) +region = Rect2( 0, 48, 16, 24 ) + +[sub_resource type="AtlasTexture" id=22] +atlas = ExtResource( 3 ) +region = Rect2( 16, 48, 16, 24 ) + +[sub_resource type="AtlasTexture" id=23] +atlas = ExtResource( 3 ) +region = Rect2( 32, 48, 16, 24 ) + +[sub_resource type="AtlasTexture" id=24] +atlas = ExtResource( 3 ) +region = Rect2( 48, 48, 16, 24 ) + [sub_resource type="AtlasTexture" id=17] atlas = ExtResource( 3 ) region = Rect2( 0, 24, 16, 24 ) @@ -54,16 +54,16 @@ [sub_resource type="SpriteFrames" id=6] animations = [ { -"frames": [ SubResource( 21 ), SubResource( 22 ), SubResource( 23 ), SubResource( 24 ) ], -"loop": true, -"name": "run", -"speed": 10.0 -}, { "frames": [ SubResource( 25 ), SubResource( 26 ), SubResource( 27 ), SubResource( 28 ) ], "loop": true, "name": "reverseRun", "speed": 10.0 }, { +"frames": [ SubResource( 21 ), SubResource( 22 ), SubResource( 23 ), SubResource( 24 ) ], +"loop": true, +"name": "run", +"speed": 10.0 +}, { "frames": [ SubResource( 17 ), SubResource( 18 ), SubResource( 19 ), SubResource( 20 ) ], "loop": true, "name": "idle", @@ -85,17 +85,28 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" -frame = 1 +frame = 3 playing = true [node name="HitArea" type="Area2D" parent="."] +visible = false [node name="CollisionShape2D" type="CollisionShape2D" parent="HitArea"] visible = false position = Vector2( 0, -7.5 ) shape = SubResource( 29 ) +[node name="InteractiveArea" type="Area2D" parent="."] +collision_layer = 0 +collision_mask = 4 +monitorable = false + +[node name="Collision" type="CollisionShape2D" parent="InteractiveArea"] +position = Vector2( 0, -4.5 ) +shape = SubResource( 16 ) + [node name="Collision" type="CollisionShape2D" parent="."] +visible = false position = Vector2( 0, -4.5 ) shape = SubResource( 16 ) @@ -106,3 +117,6 @@ [node name="BackMountPoint" type="Position2D" parent="."] position = Vector2( 0, -10 ) z_index = -1 + +[connection signal="area_entered" from="InteractiveArea" to="." method="_OnPropsEnter"] +[connection signal="area_exited" from="InteractiveArea" to="." method="_OnPropsExit"] diff --git a/prefab/weapon/Gun.tscn b/prefab/weapon/Gun.tscn index 2acc680..2c54084 100644 --- a/prefab/weapon/Gun.tscn +++ b/prefab/weapon/Gun.tscn @@ -72,7 +72,9 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 7.8, 3.5 ) -[node name="Gun" type="Node2D"] +[node name="Gun" type="Area2D"] +collision_layer = 4 +collision_mask = 0 script = ExtResource( 1 ) [node name="GunSprite" type="Sprite" parent="."] @@ -96,12 +98,6 @@ [node name="FirePoint" type="Position2D" parent="."] position = Vector2( 7.39999, -1.1 ) -[node name="Area" type="Area2D" parent="."] -position = Vector2( -3.60001, 0.399996 ) -collision_layer = 4 -collision_mask = 0 - -[node name="Collision" type="CollisionShape2D" parent="Area"] -position = Vector2( 4.2, -0.199999 ) +[node name="Collision" type="CollisionShape2D" parent="."] +position = Vector2( 0.59999, 0.199997 ) shape = SubResource( 1 ) -disabled = true diff --git a/project.godot b/project.godot index f25ffdd..eee72d2 100644 --- a/project.godot +++ b/project.godot @@ -147,6 +147,11 @@ "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) ] } +interactive={ +"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":69,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} [layer_names] diff --git a/src/effect/ThrowNode.cs b/src/effect/ThrowNode.cs index 99aba7c..882a718 100644 --- a/src/effect/ThrowNode.cs +++ b/src/effect/ThrowNode.cs @@ -91,6 +91,11 @@ /// 抛射的节点显示的纹理, 用于渲染阴影用 public void InitThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Node2D mount) { + if (CollisionShape != null) + { + CollisionShape.Disabled = false; + } + IsOver = false; Size = size; GlobalPosition = StartPosition = start; @@ -194,6 +199,7 @@ { Mount.Position = new Vector2(0, 0); IsOver = true; + CollisionShape.Disabled = true; OnOver(); } } diff --git a/src/role/Player.cs b/src/role/Player.cs index 4a3a64b..67fc1d7 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -60,6 +60,10 @@ { ThrowGun(); } + else if (Input.IsActionJustPressed("interactive")) //互动物体 + { + TriggerTnteractive(); + } //攻击 Attack(); diff --git a/src/role/Role.cs b/src/role/Role.cs index 689d42b..165986a 100644 --- a/src/role/Role.cs +++ b/src/role/Role.cs @@ -60,6 +60,7 @@ private FaceDirection _face; private Vector2 StartScele; + private Gun InteractiveItem = null; public override void _Ready() { @@ -110,7 +111,8 @@ //播放抛出效果 if (gun != null) { - if (Face == FaceDirection.Left) { + if (Face == FaceDirection.Left) + { gun.Scale *= new Vector2(1, -1); gun.RotationDegrees = 180; } @@ -122,11 +124,38 @@ var xf = 30; var yf = MathUtils.RandRangeInt(60, 120); var rotate = MathUtils.RandRangeInt(-180, 180); - temp.InitThrow(new Vector2(16, 7), startPos, startHeight, direction, xf, yf, rotate, gun, gun.GunSprite); - RoomManager.Current.ObjectRoot.AddChild(temp); + temp.InitThrow(new Vector2(20, 20), startPos, startHeight, direction, xf, yf, rotate, gun, gun.GunSprite); + RoomManager.Current.SortRoot.AddChild(temp); } } + /// + /// 返回是否存在可互动的物体 + /// + public bool HasTnteractive() + { + return InteractiveItem != null; + } + + /// + /// 触发与碰撞的物体互动 + /// + public void TriggerTnteractive() + { + if (HasTnteractive()) + { + var tempItem = InteractiveItem; + //临时处理 + var parent = tempItem.GetParent(); + parent.RemoveChild(tempItem); + Holster.PickupGun(tempItem); + parent.QueueFree(); + } + } + + /// + /// 设置脸的朝向 + /// private void SetFace(FaceDirection face) { if (_face != face) @@ -162,4 +191,28 @@ } } + /// + /// 连接信号: InteractiveArea.area_entered + /// + private void _OnPropsEnter(Area2D other) + { + if (other is Gun gun) + { + InteractiveItem = gun; + GD.Print("enter"); + } + } + + /// + /// 连接信号: InteractiveArea.area_exited + /// + private void _OnPropsExit(Area2D other) + { + if (other == InteractiveItem) + { + InteractiveItem = null; + GD.Print("exit"); + } + } + } \ No newline at end of file diff --git a/src/room/RoomManager.cs b/src/room/RoomManager.cs index c9ffbb4..0f28e4a 100644 --- a/src/room/RoomManager.cs +++ b/src/room/RoomManager.cs @@ -18,7 +18,7 @@ public Cursor Cursor { get; private set; } public Player Player { get; set; } public Node2D ObjectRoot { get; private set; } - public YSort ItemRoot { get; private set; } + public YSort SortRoot { get; private set; } public override void _EnterTree() { @@ -28,7 +28,7 @@ Cursor = MouseCursor.Instance(); AddChild(Cursor); - ItemRoot = GetNode("ItemRoot"); + SortRoot = GetNode("ItemRoot"); ObjectRoot = GetNode("ObjectRoot"); //初始化地图 diff --git a/src/weapon/gun/Gun.cs b/src/weapon/gun/Gun.cs index bca287e..3819255 100644 --- a/src/weapon/gun/Gun.cs +++ b/src/weapon/gun/Gun.cs @@ -4,7 +4,7 @@ /// /// 枪的基类 /// -public abstract class Gun : Node2D +public abstract class Gun : Area2D { /// /// 开火回调事件 @@ -62,6 +62,11 @@ /// public Position2D ShellPoint { get; private set; } /// + /// 碰撞器节点 + /// + /// + public CollisionShape2D CollisionShape2D { get; private set; } + /// /// 枪的当前散射半径 /// public float CurrScatteringRange { get; private set; } = 0; @@ -185,6 +190,7 @@ OriginPoint = GetNode("OriginPoint"); ShellPoint = GetNode("ShellPoint"); AnimationPlayer = GetNode("AnimationPlayer"); + CollisionShape2D = GetNode("Collision"); Attribute = attribute; //更新图片 @@ -355,6 +361,18 @@ /// protected abstract void OnConceal(); + /// + /// 触发落到地面 + /// + public void _FallToGround() + { + //启用碰撞 + CollisionShape2D.Disabled = false; + } + + /// + /// 触发拾起 + /// public void _PickUpGun(Role master) { Master = master; @@ -363,9 +381,14 @@ GunSprite.Position = Attribute.HoldPosition; AnimationPlayer.Play("RESET"); ZIndex = 0; + //禁用碰撞 + CollisionShape2D.Disabled = true; OnPickUp(master); } + /// + /// 触发抛出 + /// public void _ThrowOutGun() { Master = null; @@ -375,11 +398,17 @@ OnThrowOut(); } + /// + /// 触发启用武器 + /// public void _Active() { OnActive(); } + /// + /// 触发收起武器 + /// public void _Conceal() { OnConceal(); @@ -405,7 +434,7 @@ bullet.GlobalRotation = globalRotation; if (parent == null) { - RoomManager.Current.ItemRoot.AddChild(bullet); + RoomManager.Current.SortRoot.AddChild(bullet); } else { diff --git a/src/weapon/gun/OrdinaryGun.cs b/src/weapon/gun/OrdinaryGun.cs index 7423f0b..f658b4b 100644 --- a/src/weapon/gun/OrdinaryGun.cs +++ b/src/weapon/gun/OrdinaryGun.cs @@ -24,7 +24,7 @@ var rotate = MathUtils.RandRangeInt(-720, 720); var sprite = Attribute.ShellPack.Instance(); temp.InitThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite, sprite); - RoomManager.Current.ObjectRoot.AddChild(temp); + RoomManager.Current.SortRoot.AddChild(temp); } protected override void ShootBullet() diff --git a/src/weapon/gun/ThrowGun.cs b/src/weapon/gun/ThrowGun.cs index 64e4c35..1b8b071 100644 --- a/src/weapon/gun/ThrowGun.cs +++ b/src/weapon/gun/ThrowGun.cs @@ -3,21 +3,34 @@ public class ThrowGun : ThrowNode { + private bool fristOver = true; + public override void _Ready() { base._Ready(); ZIndex = 2; } - protected override void OnOver() { + if (fristOver) + { + fristOver = false; + if (Mount is Gun gun) + { + gun._FallToGround(); + } + } //如果落地高度不够低, 再抛一次 if (StartYSpeed > 1) { InitThrow(Size, GlobalPosition, 0, Direction, XSpeed * 0.8f, StartYSpeed * 0.5f, RotateSpeed * 0.5f, null); } + else + { + GetParent().RemoveChild(this); + RoomManager.Current.ObjectRoot.AddChild(this); + } } - protected override void OnMaxHeight(float height) { ZIndex = 0; diff --git a/src/weapon/shell/ThrowShell.cs b/src/weapon/shell/ThrowShell.cs index 2320cfe..26931b7 100644 --- a/src/weapon/shell/ThrowShell.cs +++ b/src/weapon/shell/ThrowShell.cs @@ -21,6 +21,8 @@ } else { + GetParent().RemoveChild(this); + RoomManager.Current.ObjectRoot.AddChild(this); //等待被销毁 AwaitDestroy(); }