diff --git a/DungeonShooting_Godot/prefab/role/Role.tscn b/DungeonShooting_Godot/prefab/role/Role.tscn index 45897d9..7b06e6f 100644 --- a/DungeonShooting_Godot/prefab/role/Role.tscn +++ b/DungeonShooting_Godot/prefab/role/Role.tscn @@ -91,6 +91,7 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" +frame = 3 playing = true [node name="Collision" type="CollisionShape2D" parent="."] diff --git a/DungeonShooting_Godot/prefab/weapon/shell/ShellCase.tscn b/DungeonShooting_Godot/prefab/weapon/shell/ShellCase.tscn index 8741487..7c08e95 100644 --- a/DungeonShooting_Godot/prefab/weapon/shell/ShellCase.tscn +++ b/DungeonShooting_Godot/prefab/weapon/shell/ShellCase.tscn @@ -1,6 +1,24 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://resource/sprite/shell/shellCase.png" type="Texture" id=1] +[ext_resource path="res://resource/materlal/Shadow.tres" type="Material" id=2] +[ext_resource path="res://addons/dungeonShooting_plugin/ActivityObjectTemplate.cs" type="Script" id=3] -[node name="ShellCase" type="Sprite"] -texture = ExtResource( 1 ) +[sub_resource type="SpriteFrames" id=1] +animations = [ { +"frames": [ ExtResource( 1 ) ], +"loop": true, +"name": "default", +"speed": 5.0 +} ] + +[node name="ShellCase" type="Node"] +script = ExtResource( 3 ) + +[node name="ShadowSprite" type="Sprite" parent="."] +material = ExtResource( 2 ) + +[node name="AnimatedSprite" type="AnimatedSprite" parent="."] +frames = SubResource( 1 ) + +[node name="Collision" type="CollisionShape2D" parent="."] diff --git a/DungeonShooting_Godot/src/framework/ActivityObject.cs b/DungeonShooting_Godot/src/framework/ActivityObject.cs index 30022c1..0f23d5f 100644 --- a/DungeonShooting_Godot/src/framework/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/ActivityObject.cs @@ -29,6 +29,16 @@ /// public bool IsDestroyed { get; private set; } + /// + /// 是否正在投抛过程中 + /// + public bool IsThrowing => _throwData != null && !_throwData.IsOver; + + /// + /// 物体厚度, 影响阴影偏移 + /// + public int Thickness { get; protected set; } = 2; + private List> _components = new List>(); private bool initShadow; private string _prevAnimation; @@ -136,13 +146,19 @@ /// 返回是否能与其他ActivityObject互动 /// /// 触发者 - public abstract CheckInteractiveResult CheckInteractive(ActivityObject master); + public virtual CheckInteractiveResult CheckInteractive(ActivityObject master) + { + return new CheckInteractiveResult(this); + } /// /// 与其它ActivityObject互动时调用 /// /// 触发者 - public abstract void Interactive(ActivityObject master); + public virtual void Interactive(ActivityObject master) + { + + } /// /// 投抛该物体达到最高点时调用 @@ -308,13 +324,9 @@ var rotate = GlobalRotationDegrees + _throwData.RotateSpeed * delta; GlobalRotationDegrees = rotate; - //计算阴影位置 - var pos = AnimatedSprite.GlobalPosition + new Vector2(0, 2 + _throwData.Y); + var pos = AnimatedSprite.GlobalPosition; ShadowSprite.GlobalRotationDegrees = rotate; - ShadowSprite.GlobalPosition = pos; - //碰撞器位置 - Collision.GlobalPosition = pos; - + var ysp = _throwData.YSpeed; _throwData.Y += _throwData.YSpeed * delta; @@ -330,7 +342,9 @@ //落地判断 if (_throwData.Y <= 0) { - ShadowSprite.GlobalPosition = AnimatedSprite.GlobalPosition + new Vector2(0, 2); + ShadowSprite.GlobalPosition = pos + new Vector2(0, Thickness); + Collision.GlobalPosition = pos; + _throwData.IsOver = true; //第一次接触地面 @@ -362,6 +376,13 @@ ThrowOver(); } } + else + { + //计算阴影位置 + ShadowSprite.GlobalPosition = pos + new Vector2(0, Thickness + _throwData.Y); + //碰撞器位置 + Collision.GlobalPosition = pos + new Vector2(0, _throwData.Y); + } } //更新阴影贴图, 使其和动画一致 diff --git a/DungeonShooting_Godot/src/game/common/NodeExtend.cs b/DungeonShooting_Godot/src/game/common/NodeExtend.cs index 2d84709..b42605f 100644 --- a/DungeonShooting_Godot/src/game/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/game/common/NodeExtend.cs @@ -83,20 +83,4 @@ } return null; } - - public static T StartThrow(this ActivityObject node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate) where T : ThrowComponent - { - T throwNode = node.GetComponent(); - if (throwNode == null) - { - throwNode = Activator.CreateInstance(); - node.AddComponent(throwNode); - } - else - { - throwNode.StopThrow(); - } - throwNode.StartThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate); - return throwNode; - } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/item/throwObject/ThrowComponent.cs b/DungeonShooting_Godot/src/game/item/throwObject/ThrowComponent.cs deleted file mode 100644 index 3ee64b9..0000000 --- a/DungeonShooting_Godot/src/game/item/throwObject/ThrowComponent.cs +++ /dev/null @@ -1,196 +0,0 @@ - -using Godot; - -public class ThrowComponent : Component -{ - - /// - /// 是否已经结束 - /// - public bool IsOver { get; protected set; } = true; - - /// - /// 物体大小 - /// - public Vector2 Size { get; protected set; } = Vector2.One; - - /// - /// 起始坐标 - /// - public Vector2 StartPosition { get; protected set; } - - /// - /// 移动方向, 0 - 360 - /// - public float Direction { get; protected set; } - - /// - /// x速度, 也就是水平速度 - /// - public float XSpeed { get; protected set; } - - /// - /// y轴速度, 也就是竖直速度 - /// - public float YSpeed { get; protected set; } - - /// - /// 初始x轴组队 - /// - public float StartXSpeed { get; protected set; } - - /// - /// 初始y轴速度 - /// - public float StartYSpeed { get; protected set; } - - /// - /// 旋转速度 - /// - public float RotateSpeed { get; protected set; } - - /// - /// 绑定的kinematicBody2D节点 - /// - protected KinematicBody2D KinematicBody; - /// - /// 碰撞器节点 - /// - protected CollisionShape2D CollisionShape; - /// - /// 碰撞器形状 - /// - protected RectangleShape2D RectangleShape; - - public ThrowComponent() - { - KinematicBody = new KinematicBody2D(); - KinematicBody.Name = nameof(ThrowComponent); - //只与墙壁碰撞 - KinematicBody.CollisionMask = 1; - KinematicBody.CollisionLayer = 0; - //创建碰撞器 - CollisionShape = new CollisionShape2D(); - CollisionShape.Name = "Collision"; - RectangleShape = new RectangleShape2D(); - CollisionShape.Shape = RectangleShape; - KinematicBody.AddChild(CollisionShape); - } - - public override void Update(float delta) - { - if (!IsOver) - { - KinematicBody.MoveAndSlide(new Vector2(XSpeed, 0).Rotated(Direction * Mathf.Pi / 180)); - Position = new Vector2(0, Position.y - YSpeed * delta); - var rotate = ActivityObject.GlobalRotationDegrees + RotateSpeed * delta; - ActivityObject.GlobalRotationDegrees = rotate; - - //计算阴影位置 - ShadowSprite.GlobalRotationDegrees = rotate; - // ShadowSprite.GlobalRotationDegrees = rotate + (inversionX ? 180 : 0); - ShadowSprite.GlobalPosition = AnimatedSprite.GlobalPosition + new Vector2(0, 2 - Position.y); - var ysp = YSpeed; - YSpeed -= GameConfig.G * delta; - //达到最高点 - if (ysp * YSpeed < 0) - { - OnMaxHeight(-Position.y); - } - - //落地判断 - if (Position.y >= 0) - { - Position = new Vector2(0, 0); - IsOver = true; - OnOver(); - } - } - } - - public virtual void StartThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, - float ySpeed, float rotate) - { - CollisionShape.Disabled = false; - - IsOver = false; - Size = size; - KinematicBody.GlobalPosition = StartPosition = start; - Direction = direction; - XSpeed = xSpeed; - YSpeed = ySpeed; - StartXSpeed = xSpeed; - StartYSpeed = ySpeed; - RotateSpeed = rotate; - - RectangleShape.Extents = Size * 0.5f; - - var mountParent = ActivityObject.GetParent(); - if (mountParent == null) - { - KinematicBody.AddChild(ActivityObject); - } - else if (mountParent != ActivityObject) - { - mountParent.RemoveChild(ActivityObject); - KinematicBody.AddChild(ActivityObject); - } - - Position = new Vector2(0, -startHeight); - - var parent = KinematicBody.GetParent(); - if (parent == null) - { - RoomManager.Current.SortRoot.AddChild(KinematicBody); - } - else if (parent == RoomManager.Current.ObjectRoot) - { - GD.Print("1111"); - parent.RemoveChild(KinematicBody); - RoomManager.Current.SortRoot.AddChild(KinematicBody); - } - - //显示阴影 - ActivityObject.ShowShadowSprite(); - ShadowSprite.Scale = AnimatedSprite.Scale; - } - - /// - /// 停止投抛运动 - /// - public void StopThrow() - { - if (!IsOver) - { - var gp = ActivityObject.GlobalPosition; - var gr = ActivityObject.GlobalRotation; - IsOver = true; - KinematicBody.RemoveChild(ActivityObject); - var parent = KinematicBody.GetParent(); - parent.AddChild(ActivityObject); - ActivityObject.GlobalPosition = gp; - ActivityObject.GlobalRotation = gr; - } - - CollisionShape.Disabled = true; - } - - /// - /// 达到最高点时调用 - /// - protected virtual void OnMaxHeight(float height) - { - - } - - /// - /// 结束的调用 - /// - protected virtual void OnOver() - { - KinematicBody.GetParent().RemoveChild(KinematicBody); - RoomManager.Current.ObjectRoot.AddChild(KinematicBody); - CollisionShape.Disabled = true; - } - -} diff --git a/DungeonShooting_Godot/src/game/item/weapon/ThrowWeapon.cs b/DungeonShooting_Godot/src/game/item/weapon/ThrowWeapon.cs deleted file mode 100644 index 49ae0f7..0000000 --- a/DungeonShooting_Godot/src/game/item/weapon/ThrowWeapon.cs +++ /dev/null @@ -1,41 +0,0 @@ - -using Godot; - -public class ThrowWeapon : ThrowComponent -{ - //是否第一次结束 - private bool fristOver = true; - - public override void StartThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, - float rotate) - { - KinematicBody.ZIndex = 2; - base.StartThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate); - } - - protected override void OnOver() - { - if (fristOver) - { - fristOver = false; - if (ActivityObject is Weapon gun) - { - - } - } - //如果落地高度不够低, 再抛一次 - if (StartYSpeed > 1) - { - base.StartThrow(Size, GlobalPosition, 0, Direction, XSpeed * 0.8f, StartYSpeed * 0.5f, RotateSpeed * 0.5f); - fristOver = true; - } - else //结束 - { - base.OnOver(); - } - } - protected override void OnMaxHeight(float height) - { - KinematicBody.ZIndex = 0; - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs index 5d011d4..6d94c5c 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs @@ -653,9 +653,9 @@ //播放互动效果 if (flag) { - // this.StartThrow(new Vector2(20, 20), GlobalPosition, 0, 0, - // MathUtils.RandRangeInt(-20, 20), MathUtils.RandRangeInt(20, 50), - // MathUtils.RandRangeInt(-180, 180), WeaponSprite); + Throw(new Vector2(30, 15), GlobalPosition, 0, 0, + MathUtils.RandRangeInt(-20, 20), MathUtils.RandRangeInt(20, 50), + MathUtils.RandRangeInt(-180, 180)); } } else //没有武器 diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs index e2d9f6f..59d7abb 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs @@ -86,28 +86,23 @@ /// 子弹预制体 /// public PackedScene BulletPack; - /// - /// 弹壳预制体 - /// - public PackedScene ShellPack; 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 OnFire() { //创建一个弹壳 - // var startPos = GlobalPosition + new Vector2(0, 5); - // var startHeight = 6; - // var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180; - // var xf = MathUtils.RandRangeInt(20, 60); - // var yf = MathUtils.RandRangeInt(60, 120); - // var rotate = MathUtils.RandRangeInt(-720, 720); - // var sprite = ShellPack.Instance(); - // sprite.StartThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite); + var startPos = GlobalPosition + new Vector2(0, 5); + var startHeight = 6; + var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180; + var xf = MathUtils.RandRangeInt(20, 60); + var yf = MathUtils.RandRangeInt(60, 120); + var rotate = MathUtils.RandRangeInt(-720, 720); + var shell = new ShellCase(); + shell.Throw(new Vector2(10, 5), startPos, startHeight, direction, xf, yf, rotate, true); //创建抖动 MainCamera.Main.ProssesDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs index 042b0e2..42e9e93 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs @@ -62,14 +62,14 @@ protected override void OnFire() { //创建一个弹壳 - // var startPos = GlobalPosition + new Vector2(0, 5); - // var startHeight = 6; - // var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180; - // var xf = MathUtils.RandRangeInt(20, 60); - // var yf = MathUtils.RandRangeInt(60, 120); - // var rotate = MathUtils.RandRangeInt(-720, 720); - // var sprite = ShellPack.Instance(); - // sprite.StartThrow(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite); + var startPos = GlobalPosition + new Vector2(0, 5); + var startHeight = 6; + var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180; + var xf = MathUtils.RandRangeInt(20, 60); + var yf = MathUtils.RandRangeInt(60, 120); + var rotate = MathUtils.RandRangeInt(-720, 720); + var shell = new ShellCase(); + shell.Throw(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, true); //创建抖动 MainCamera.Main.ProssesDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } diff --git a/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs b/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs new file mode 100644 index 0000000..22f22e4 --- /dev/null +++ b/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs @@ -0,0 +1,20 @@ + +public class ShellCase : ActivityObject +{ + public ShellCase() : base("res://prefab/weapon/shell/ShellCase.tscn") + { + Thickness = 1; + } + + public override void OnThrowOver() + { + AwaitDestroy(); + } + + private async void AwaitDestroy() + { + //30秒后销毁 + await ToSignal(GetTree().CreateTimer(30), "timeout"); + QueueFree(); + } +} \ No newline at end of file