diff --git a/DungeonShooting_Godot/prefab/weapon/bullet/Bullet.tscn b/DungeonShooting_Godot/prefab/weapon/bullet/Bullet.tscn index cc8610a..7b14bc0 100644 --- a/DungeonShooting_Godot/prefab/weapon/bullet/Bullet.tscn +++ b/DungeonShooting_Godot/prefab/weapon/bullet/Bullet.tscn @@ -13,19 +13,25 @@ } ] [sub_resource type="RectangleShape2D" id=2] -extents = Vector2( 5.5, 2 ) +extents = Vector2( 6, 2 ) [node name="Bullet" type="Node"] script = ExtResource( 1 ) -CollisionLayer = 2 [node name="ShadowSprite" type="Sprite" parent="."] material = ExtResource( 2 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] -position = Vector2( -1, 0 ) +position = Vector2( 2.38419e-07, 0 ) frames = SubResource( 1 ) [node name="Collision" type="CollisionShape2D" parent="."] position = Vector2( 1.5, 0 ) + +[node name="CollisionArea" type="Area2D" parent="."] +collision_layer = 0 +collision_mask = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="CollisionArea"] +position = Vector2( 2, 0 ) shape = SubResource( 2 ) diff --git a/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png b/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png new file mode 100644 index 0000000..3cea799 --- /dev/null +++ b/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png Binary files differ diff --git a/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png.import b/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png.import new file mode 100644 index 0000000..b2003cd --- /dev/null +++ b/DungeonShooting_Godot/resource/sprite/bullet/bullet2.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/bullet2.png-6ca9527061677971732c8192cb1aa209.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://resource/sprite/bullet/bullet2.png" +dest_files=[ "res://.import/bullet2.png-6ca9527061677971732c8192cb1aa209.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=false +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/DungeonShooting_Godot/scene/Room.tscn b/DungeonShooting_Godot/scene/Room.tscn index 9f664bf..43c2477 100644 --- a/DungeonShooting_Godot/scene/Room.tscn +++ b/DungeonShooting_Godot/scene/Room.tscn @@ -15,7 +15,6 @@ [node name="Camera2D" type="Camera2D" parent="."] position = Vector2( 253, 219 ) current = true -process_mode = 0 limit_smoothed = true editor_draw_drag_margin = true script = ExtResource( 5 ) diff --git a/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs b/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs index 4c43084..5dd8c0a 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs @@ -8,13 +8,20 @@ public Bullet(string scenePath, float maxDistance, Vector2 position, float rotation, uint targetLayer) : base(scenePath) { + CollisionArea = GetNode("CollisionArea"); + CollisionArea.CollisionMask = targetLayer; + CollisionArea.Connect("body_entered", this, nameof(_BodyEntered)); + + Collision.Disabled = true; + MaxDistance = maxDistance; - CollisionMask = targetLayer; Position = position; Rotation = rotation; ShadowOffset = new Vector2(0, 5); } + public Area2D CollisionArea { get; } + // 最大飞行距离 private float MaxDistance; @@ -34,35 +41,29 @@ public override void _PhysicsProcess(float delta) { base._PhysicsProcess(delta); - //移动 - var kinematicCollision = MoveAndCollide(new Vector2(FlySpeed * delta, 0).Rotated(Rotation)); - //有碰撞 - if (kinematicCollision == null) + Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation); + //距离太大, 自动销毁 + CurrFlyDistance += FlySpeed * delta; + if (CurrFlyDistance >= MaxDistance) { - //距离太大, 自动销毁 - CurrFlyDistance += FlySpeed * delta; - if (CurrFlyDistance >= MaxDistance) - { - Destroy(); - } - } - else - { - var collider = kinematicCollision.Collider; - if (collider is Role role) - { - role.Hit(1); - } - - //播放受击动画 - Node2D hit = ResourceManager.Load(ResourcePath.prefab_effect_Hit_tscn).Instance(); - hit.RotationDegrees = MathUtils.RandRangeInt(0, 360); - hit.GlobalPosition = kinematicCollision.Position; - GameApplication.Instance.Room.GetRoot(true).AddChild(hit); - Destroy(); } + } + private void _BodyEntered(Node2D other) + { + if (other is Role role) + { + role.Hit(1); + } + + //播放受击动画 + Node2D hit = ResourceManager.Load(ResourcePath.prefab_effect_Hit_tscn).Instance(); + hit.RotationDegrees = MathUtils.RandRangeInt(0, 360); + hit.GlobalPosition = GlobalPosition; + GameApplication.Instance.Room.GetRoot(true).AddChild(hit); + + Destroy(); } } \ No newline at end of file