diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index bf3afb0..25ff66f 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -16,4 +16,4 @@ material = SubResource( 1 ) [node name="AnimatedSprite" parent="." index="2"] -frame = 1 +frame = 3 diff --git a/DungeonShooting_Godot/src/framework/ActivityObject.cs b/DungeonShooting_Godot/src/framework/ActivityObject.cs index df9ff53..53b8e53 100644 --- a/DungeonShooting_Godot/src/framework/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/ActivityObject.cs @@ -32,7 +32,7 @@ private List> _components = new List>(); private bool initShadow; private string _prevAnimation; - + //存储投抛该物体时所产生的数据 private ObjectThrowData _throwData; @@ -124,14 +124,14 @@ public void GetCurrentTexture() { - + } public Texture GetDefaultTexture() { return AnimatedSprite.Frames.GetFrame("default", 0); } - + /// /// 返回是否能与其他ActivityObject互动 /// @@ -151,27 +151,66 @@ { } - + public virtual void PickUp() { - + } public virtual void PickDown() { - + } - public virtual void Throw(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate) + public virtual void Throw(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, + float ySpeed, float rotate) { + + if (_throwData == null) + { + _throwData = new ObjectThrowData(); + } + SetThrowCollision(); + + _throwData.IsOver = false; + _throwData.Size = size; + _throwData.StartPosition = _throwData.CurrPosition = start; + _throwData.Direction = direction; + _throwData.XSpeed = xSpeed; + _throwData.YSpeed = ySpeed; + _throwData.StartXSpeed = xSpeed; + _throwData.StartYSpeed = ySpeed; + _throwData.RotateSpeed = rotate; + _throwData.LinearVelocity = new Vector2(_throwData.XSpeed, 0).Rotated(_throwData.Direction * Mathf.Pi / 180); + + _throwData.RectangleShape.Extents = _throwData.Size * 0.5f; + var parent = GetParent(); + if (parent == null) + { + RoomManager.Current.SortRoot.AddChild(this); + } + else if (parent == RoomManager.Current.ObjectRoot) + { + parent.RemoveChild(this); + RoomManager.Current.SortRoot.AddChild(this); + } + GlobalPosition = start + new Vector2(0, -startHeight); + + //显示阴影 + ShowShadowSprite(); + ShadowSprite.Scale = AnimatedSprite.Scale; } + /// + /// 强制停止投抛运动 + /// public virtual void StopThrow() { - + _throwData.IsOver = true; + RestoreCollision(); } - + /// /// 结束的调用 /// @@ -179,7 +218,7 @@ { GetParent().RemoveChild(this); RoomManager.Current.ObjectRoot.AddChild(this); - Collision.Disabled = true; + RestoreCollision(); } public void AddComponent(Component component) @@ -243,36 +282,38 @@ } } } - + //投抛计算 if (_throwData != null && !_throwData.IsOver) { - MoveAndSlide(new Vector2(_throwData.XSpeed, 0).Rotated(_throwData.Direction * Mathf.Pi / 180)); - Position = new Vector2(0, Position.y - _throwData.YSpeed * delta); + MoveAndSlide(_throwData.LinearVelocity); + Position = new Vector2(Position.x, Position.y - _throwData.YSpeed * delta); var rotate = GlobalRotationDegrees + _throwData.RotateSpeed * delta; GlobalRotationDegrees = rotate; - //计算阴影位置 - ShadowSprite.GlobalRotationDegrees = rotate; - // ShadowSprite.GlobalRotationDegrees = rotate + (inversionX ? 180 : 0); - ShadowSprite.GlobalPosition = AnimatedSprite.GlobalPosition + new Vector2(0, 2 - Position.y); - var ysp = _throwData.YSpeed; _throwData.YSpeed -= GameConfig.G * delta; - //达到最高点 - if (ysp * _throwData.YSpeed < 0) - { - OnMaxHeight(-Position.y); - } - - //落地判断 - if (Position.y >= 0) - { - Position = new Vector2(0, 0); - _throwData.IsOver = true; - OnOver(); - } + // + // //计算阴影位置 + // ShadowSprite.GlobalRotationDegrees = rotate; + // // ShadowSprite.GlobalRotationDegrees = rotate + (inversionX ? 180 : 0); + // ShadowSprite.GlobalPosition = AnimatedSprite.GlobalPosition + new Vector2(0, 2 + _throwData.Y); + // var ysp = _throwData.YSpeed; + // _throwData.YSpeed -= GameConfig.G * delta; + // //达到最高点 + // if (ysp * _throwData.YSpeed < 0) + // { + // OnMaxHeight(_throwData.Y); + // } + // + // //落地判断 + // if (_throwData.Y <= 0) + // { + // //Position = new Vector2(0, 0); + // _throwData.IsOver = true; + // OnOver(); + // } } - + //更新阴影贴图, 使其和动画一致 if (ShadowSprite.Visible) { @@ -289,19 +330,23 @@ public override void _PhysicsProcess(float delta) { - var arr = _components.ToArray(); - for (int i = 0; i < arr.Length; i++) + //更新组件 + if (_components.Count > 0) { - if (IsDestroyed) return; - var temp = arr[i].Value; - if (temp != null && temp.ActivityObject == this && temp.Enable) + var arr = _components.ToArray(); + for (int i = 0; i < arr.Length; i++) { - if (!temp.IsStart) + if (IsDestroyed) return; + var temp = arr[i].Value; + if (temp != null && temp.ActivityObject == this && temp.Enable) { - temp.Start(); - } + if (!temp.IsStart) + { + temp.Start(); + } - temp.PhysicsUpdate(delta); + temp.PhysicsUpdate(delta); + } } } } @@ -334,4 +379,42 @@ return false; } + + private void SetThrowCollision() + { + if (_throwData != null && _throwData.UseOrigin) + { + _throwData.OriginShape = Collision.Shape; + _throwData.OriginPosition = Collision.Position; + _throwData.OriginRotation = Collision.Rotation; + _throwData.OriginScale = Collision.Scale; + _throwData.OriginCollisionEnable = Collision.Disabled; + + if (_throwData.RectangleShape == null) + { + _throwData.RectangleShape = new RectangleShape2D(); + } + + Collision.Shape = _throwData.RectangleShape; + //Collision.Position = Vector2.Zero; + Collision.Rotation = 0; + Collision.Scale = Vector2.One; + Collision.Disabled = false; + _throwData.UseOrigin = false; + } + } + + private void RestoreCollision() + { + if (_throwData != null && !_throwData.UseOrigin) + { + Collision.Shape = _throwData.OriginShape; + Collision.Position = _throwData.OriginPosition; + Collision.Rotation = _throwData.OriginRotation; + Collision.Scale = _throwData.OriginScale; + Collision.Disabled = _throwData.OriginCollisionEnable; + + _throwData.UseOrigin = true; + } + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/ObjectThrowData.cs b/DungeonShooting_Godot/src/framework/ObjectThrowData.cs index 5778f5e..38cc091 100644 --- a/DungeonShooting_Godot/src/framework/ObjectThrowData.cs +++ b/DungeonShooting_Godot/src/framework/ObjectThrowData.cs @@ -51,4 +51,15 @@ /// 碰撞器形状 /// public RectangleShape2D RectangleShape; + + public Vector2 CurrPosition; + public Vector2 LinearVelocity; + + //----------- 用于记录原始信息 -------------- + public bool UseOrigin = true; + public Shape2D OriginShape; + public Vector2 OriginPosition; + public float OriginRotation; + public Vector2 OriginScale; + public bool OriginCollisionEnable; } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/common/NodeExtend.cs b/DungeonShooting_Godot/src/game/common/NodeExtend.cs index 62e5ed5..5823ca2 100644 --- a/DungeonShooting_Godot/src/game/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/game/common/NodeExtend.cs @@ -71,21 +71,20 @@ /// 触发扔掉武器操作 /// /// 触发扔掉该武器的的角色 - public static ThrowComponent StartThrowWeapon(this Weapon weapon, Role master) + public static void StartThrowWeapon(this Weapon weapon, Role master) { if (master.Face == FaceDirection.Left) { weapon.Scale *= new Vector2(1, -1); weapon.RotationDegrees = 180; } - var startPos = master.GlobalPosition;// + new Vector2(0, 0); - var startHeight = 6; + var startHeight = 0; var direction = master.GlobalRotationDegrees + MathUtils.RandRangeInt(-20, 20); var xf = 30; var yf = MathUtils.RandRangeInt(60, 120); var rotate = MathUtils.RandRangeInt(-180, 180); - weapon.Position = Vector2.Zero; - return weapon.StartThrow(new Vector2(20, 20), startPos, startHeight, direction, xf, yf, rotate); + //weapon.Position = Vector2.Zero; + weapon.Throw(new Vector2(20, 20), master.MountPoint.GlobalPosition, startHeight, direction, xf, yf, rotate); } diff --git a/DungeonShooting_Godot/src/game/item/package/Holster.cs b/DungeonShooting_Godot/src/game/item/package/Holster.cs index bbe7c9b..e76045b 100644 --- a/DungeonShooting_Godot/src/game/item/package/Holster.cs +++ b/DungeonShooting_Godot/src/game/item/package/Holster.cs @@ -133,7 +133,7 @@ /// 移除指定位置的武器, 并返回这个武器对象, 如果移除正在使用的这把武器, 则会自动切换到上一把武器 /// /// 所在武器袋的位置索引 - public Weapon RmoveWeapon(int index) + public Weapon RemoveWeapon(int index) { if (index < 0 || index >= SlotList.Length) { diff --git a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs index a9bd5bb..2ded6cf 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs @@ -671,7 +671,7 @@ var slot = roleMaster.Holster.SlotList[roleMaster.Holster.ActiveIndex]; if (slot.Type == Attribute.WeightType) { - var weapon = roleMaster.Holster.RmoveWeapon(roleMaster.Holster.ActiveIndex); + var weapon = roleMaster.Holster.RemoveWeapon(roleMaster.Holster.ActiveIndex); weapon.StartThrowWeapon(roleMaster); roleMaster.PickUpWeapon(this); } diff --git a/DungeonShooting_Godot/src/game/role/Role.cs b/DungeonShooting_Godot/src/game/role/Role.cs index 6075c97..a19b0ec 100644 --- a/DungeonShooting_Godot/src/game/role/Role.cs +++ b/DungeonShooting_Godot/src/game/role/Role.cs @@ -227,7 +227,7 @@ /// public void TriggerThrowWeapon() { - var weapon = Holster.RmoveWeapon(Holster.ActiveIndex); + var weapon = Holster.RemoveWeapon(Holster.ActiveIndex); //播放抛出效果 if (weapon != null) {