diff --git a/prefab/Gun.tscn b/prefab/Gun.tscn index 14823b5..4c34c81 100644 --- a/prefab/Gun.tscn +++ b/prefab/Gun.tscn @@ -2,14 +2,15 @@ [ext_resource path="res://resource/sprite/gun/gun1.png" type="Texture" id=1] [ext_resource path="res://src/gun/OrdinaryGun.cs" type="Script" id=2] -[ext_resource path="res://prefab/effect/Fire.tscn" type="PackedScene" id=3] +[ext_resource path="res://prefab/bullet/OrdinaryBullets.tscn" type="PackedScene" id=3] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 9.5, 3.5 ) [node name="Gun" type="Node2D"] +z_index = 5 script = ExtResource( 2 ) -FirePrefab = ExtResource( 3 ) +bulletPacked = ExtResource( 3 ) [node name="GunSprite" type="Sprite" parent="."] position = Vector2( 4, -3 ) diff --git a/prefab/Role.tscn b/prefab/Role.tscn index e0d66fb..ddc7240 100644 --- a/prefab/Role.tscn +++ b/prefab/Role.tscn @@ -78,7 +78,6 @@ [node name="Role" type="KinematicBody2D"] collision_layer = 0 -collision_mask = 3 script = ExtResource( 2 ) Texture = ExtResource( 3 ) @@ -86,7 +85,6 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" -frame = 2 playing = true [node name="HitArea" type="Area2D" parent="."] diff --git a/prefab/bullet/Bullet.tscn b/prefab/bullet/Bullet.tscn deleted file mode 100644 index 21eba41..0000000 --- a/prefab/bullet/Bullet.tscn +++ /dev/null @@ -1,27 +0,0 @@ -[gd_scene load_steps=5 format=2] - -[ext_resource path="res://src/bullet/HighSpeedBullet.cs" type="Script" id=1] -[ext_resource path="res://prefab/effect/Hit.tscn" type="PackedScene" id=2] - -[sub_resource type="Curve" id=1] -_data = [ Vector2( 0, 0.781588 ), 0.0, 0.0, 0, 0, Vector2( 1, 1 ), 0.0, 0.0, 0, 0 ] - -[sub_resource type="Gradient" id=2] -colors = PoolColorArray( 1, 1, 1, 0.313726, 1, 1, 1, 0.784314 ) - -[node name="Bullet" type="Node2D"] -script = ExtResource( 1 ) -Hit = ExtResource( 2 ) - -[node name="Line" type="Line2D" parent="."] -modulate = Color( 1, 0.937255, 0.843137, 1 ) -points = PoolVector2Array( 0, 0, 0, 10 ) -width = 1.0 -width_curve = SubResource( 1 ) -default_color = Color( 1, 1, 1, 1 ) -gradient = SubResource( 2 ) -begin_cap_mode = 2 -end_cap_mode = 2 - -[node name="RayCast2D" type="RayCast2D" parent="."] -visible = false diff --git a/prefab/bullet/HighSpeedBullet.tscn b/prefab/bullet/HighSpeedBullet.tscn new file mode 100644 index 0000000..e8ce680 --- /dev/null +++ b/prefab/bullet/HighSpeedBullet.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://src/bullet/HighSpeedBullet.cs" type="Script" id=1] +[ext_resource path="res://prefab/effect/Hit.tscn" type="PackedScene" id=2] + +[sub_resource type="Curve" id=1] +_data = [ Vector2( 0, 0.781588 ), 0.0, 0.0, 0, 0, Vector2( 1, 1 ), 0.0, 0.0, 0, 0 ] + +[sub_resource type="Gradient" id=2] +colors = PoolColorArray( 1, 1, 1, 0, 1, 1, 1, 0.705882 ) + +[node name="HighSpeedBullet" type="Node2D"] +script = ExtResource( 1 ) +Hit = ExtResource( 2 ) + +[node name="Line" type="Line2D" parent="."] +modulate = Color( 1, 0.937255, 0.843137, 1 ) +points = PoolVector2Array( 0, 0, 0, 10 ) +width = 1.0 +width_curve = SubResource( 1 ) +default_color = Color( 1, 1, 1, 1 ) +gradient = SubResource( 2 ) +begin_cap_mode = 2 +end_cap_mode = 2 + +[node name="RayCast2D" type="RayCast2D" parent="."] +visible = false diff --git a/prefab/bullet/OrdinaryBullets.tscn b/prefab/bullet/OrdinaryBullets.tscn new file mode 100644 index 0000000..e62a12b --- /dev/null +++ b/prefab/bullet/OrdinaryBullets.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://src/bullet/OrdinaryBullets.cs" type="Script" id=1] +[ext_resource path="res://resource/sprite/bullet/bullet.png" type="Texture" id=2] +[ext_resource path="res://prefab/effect/Hit.tscn" type="PackedScene" id=3] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 0.5, 0.5 ) + +[node name="OrdinaryBullets" type="KinematicBody2D"] +collision_layer = 2 +script = ExtResource( 1 ) +__meta__ = { +"_edit_horizontal_guides_": [ ], +"_edit_vertical_guides_": [ ] +} +Hit = ExtResource( 3 ) + +[node name="Bullet" type="Sprite" parent="."] +position = Vector2( 1, 0 ) +scale = Vector2( 0.063, 1 ) +texture = ExtResource( 2 ) +offset = Vector2( -7.5, 0 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0.5, 0 ) +shape = SubResource( 1 ) diff --git a/prefab/effect/Fire.tscn b/prefab/effect/Fire.tscn deleted file mode 100644 index e7a0704..0000000 --- a/prefab/effect/Fire.tscn +++ /dev/null @@ -1,24 +0,0 @@ -[gd_scene load_steps=6 format=2] - -[ext_resource path="res://resource/sprite/effect/fire/fire1.png" type="Texture" id=1] -[ext_resource path="res://resource/sprite/effect/fire/fire2.png" type="Texture" id=3] -[ext_resource path="res://resource/sprite/effect/fire/fire3.png" type="Texture" id=4] -[ext_resource path="res://src/effect/Hit.cs" type="Script" id=5] - -[sub_resource type="SpriteFrames" id=1] -animations = [ { -"frames": [ ExtResource( 1 ), ExtResource( 3 ), ExtResource( 4 ) ], -"loop": true, -"name": "Fire", -"speed": 20.0 -} ] - -[node name="Fire" type="AnimatedSprite"] -modulate = Color( 1, 0.72549, 0.25098, 1 ) -frames = SubResource( 1 ) -animation = "Fire" -frame = 2 -offset = Vector2( 3.5, 0 ) -script = ExtResource( 5 ) - -[connection signal="animation_finished" from="." to="." method="_on_Hit_animation_finished"] diff --git a/prefab/effect/Hit.tscn b/prefab/effect/Hit.tscn index 9c4ee57..685cc64 100644 --- a/prefab/effect/Hit.tscn +++ b/prefab/effect/Hit.tscn @@ -18,8 +18,6 @@ [node name="Hit" type="AnimatedSprite"] frames = SubResource( 1 ) animation = "Hit" -frame = 1 -playing = true offset = Vector2( 1, 11 ) script = ExtResource( 6 ) diff --git a/resource/sprite/bullet/bullet.aseprite b/resource/sprite/bullet/bullet.aseprite new file mode 100644 index 0000000..19447ed --- /dev/null +++ b/resource/sprite/bullet/bullet.aseprite Binary files differ diff --git a/resource/sprite/bullet/bullet.png b/resource/sprite/bullet/bullet.png new file mode 100644 index 0000000..fbf24ef --- /dev/null +++ b/resource/sprite/bullet/bullet.png Binary files differ diff --git a/resource/sprite/bullet/bullet.png.import b/resource/sprite/bullet/bullet.png.import new file mode 100644 index 0000000..f4fef3f --- /dev/null +++ b/resource/sprite/bullet/bullet.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/bullet.png-d058b3cd1f93a450750c6a914d76f944.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://resource/sprite/bullet/bullet.png" +dest_files=[ "res://.import/bullet.png-d058b3cd1f93a450750c6a914d76f944.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/resource/sprite/effect/fire/fire1.png b/resource/sprite/effect/fire/fire1.png deleted file mode 100644 index 0182820..0000000 --- a/resource/sprite/effect/fire/fire1.png +++ /dev/null Binary files differ diff --git a/resource/sprite/effect/fire/fire1.png.import b/resource/sprite/effect/fire/fire1.png.import deleted file mode 100644 index 706efdd..0000000 --- a/resource/sprite/effect/fire/fire1.png.import +++ /dev/null @@ -1,35 +0,0 @@ -[remap] - -importer="texture" -type="StreamTexture" -path="res://.import/fire1.png-9157e69715657d6c4e7a7928690aea64.stex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://resource/sprite/effect/fire/fire1.png" -dest_files=[ "res://.import/fire1.png-9157e69715657d6c4e7a7928690aea64.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/resource/sprite/effect/fire/fire2.png b/resource/sprite/effect/fire/fire2.png deleted file mode 100644 index b496b6b..0000000 --- a/resource/sprite/effect/fire/fire2.png +++ /dev/null Binary files differ diff --git a/resource/sprite/effect/fire/fire2.png.import b/resource/sprite/effect/fire/fire2.png.import deleted file mode 100644 index f2645fd..0000000 --- a/resource/sprite/effect/fire/fire2.png.import +++ /dev/null @@ -1,35 +0,0 @@ -[remap] - -importer="texture" -type="StreamTexture" -path="res://.import/fire2.png-cfe4f0158a6bbc63fff15fa1cf1ac348.stex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://resource/sprite/effect/fire/fire2.png" -dest_files=[ "res://.import/fire2.png-cfe4f0158a6bbc63fff15fa1cf1ac348.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/resource/sprite/effect/fire/fire3.png b/resource/sprite/effect/fire/fire3.png deleted file mode 100644 index d51825b..0000000 --- a/resource/sprite/effect/fire/fire3.png +++ /dev/null Binary files differ diff --git a/resource/sprite/effect/fire/fire3.png.import b/resource/sprite/effect/fire/fire3.png.import deleted file mode 100644 index 4d2b4fc..0000000 --- a/resource/sprite/effect/fire/fire3.png.import +++ /dev/null @@ -1,35 +0,0 @@ -[remap] - -importer="texture" -type="StreamTexture" -path="res://.import/fire3.png-c4546af8de7681c2b4ff9b8c644c077b.stex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://resource/sprite/effect/fire/fire3.png" -dest_files=[ "res://.import/fire3.png-c4546af8de7681c2b4ff9b8c644c077b.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/scene/Room.tscn b/scene/Room.tscn index c310d11..db8a22c 100644 --- a/scene/Room.tscn +++ b/scene/Room.tscn @@ -23,8 +23,6 @@ [node name="Player" parent="." instance=ExtResource( 1 )] position = Vector2( 196, 128 ) -collision_layer = 1 -collision_mask = 1 [node name="RemoteTransform2D" type="RemoteTransform2D" parent="Player"] remote_path = NodePath("../../Camera2D") diff --git a/src/bullet/Bullet.cs b/src/bullet/Bullet.cs index a56e98d..ec5ac06 100644 --- a/src/bullet/Bullet.cs +++ b/src/bullet/Bullet.cs @@ -1,27 +1,27 @@ using Godot; /// -/// 子弹 +/// 子弹接口 /// -public abstract class Bullet : Node2D +public interface IBullet { /// /// 攻击目标阵营 /// - public CampEnum TargetCamp { get; private set; } + CampEnum TargetCamp { get; } /// /// 发射该子弹的武器 /// - public Gun Gun { get; private set; } + Gun Gun { get; } /// /// 发射该子弹的物体对象 /// - public Node2D Master { get; private set; } - - public void Init(CampEnum target, Gun gun, Node2D master) - { - TargetCamp = target; - Gun = gun; - Master = master; - } + Node2D Master { get; } + /// + /// 初始化基础数据 + /// + /// 攻击的目标阵营 + /// 发射该子弹的枪对象 + /// 发射该子弹的角色 + void Init(CampEnum target, Gun gun, Node2D master); } \ No newline at end of file diff --git a/src/bullet/HighSpeedBullet.cs b/src/bullet/HighSpeedBullet.cs index e71eeba..039c416 100644 --- a/src/bullet/HighSpeedBullet.cs +++ b/src/bullet/HighSpeedBullet.cs @@ -3,29 +3,45 @@ /// /// 高速子弹 /// -public class HighSpeedBullet : Bullet +public class HighSpeedBullet : Node2D, IBullet { + public CampEnum TargetCamp { get; private set; } + + public Gun Gun { get; private set; } + + public Node2D Master { get; private set; } + + /// + /// 碰撞物体后产生的火花 + /// [Export] public PackedScene Hit; + //射线检测节点 private RayCast2D RayCast2D; - //最大飞行距离 - private float Distance; private Line2D Line; private float ca = 1; - public void InitData(float distance, Color color) + public void Init(CampEnum target, Gun gun, Node2D master) { + TargetCamp = target; + Gun = gun; + Master = master; + + //飞行距离 + var distance = MathUtils.RandRange(gun.Attribute.MinDistance, gun.Attribute.MaxDistance); + + //初始化子弹数据 RayCast2D = GetNode("RayCast2D"); Line = GetNode("Line"); - Distance = distance; - Modulate = color; + Modulate = Colors.White; + // 目标点 Vector2 targetPos = new Vector2(distance, 0); - - // RayCast2D.CastTo = targetPos; RayCast2D.ForceRaycastUpdate(); - if (RayCast2D.IsColliding()) { //碰到物体 + if (RayCast2D.IsColliding()) + { + //碰到物体 Vector2 collPosition = RayCast2D.GetCollisionPoint(); Node2D hit = Hit.Instance(); hit.RotationDegrees = MathUtils.RandRangeInt(0, 360); diff --git a/src/bullet/OrdinaryBullets.cs b/src/bullet/OrdinaryBullets.cs new file mode 100644 index 0000000..7da7f58 --- /dev/null +++ b/src/bullet/OrdinaryBullets.cs @@ -0,0 +1,80 @@ +using Godot; + +/// +/// 普通的子弹 +/// +public class OrdinaryBullets : KinematicBody2D, IBullet +{ + public CampEnum TargetCamp { get; private set; } + + public Gun Gun { get; private set; } + + public Node2D Master { get; private set; } + + /// + /// 碰撞物体后产生的火花 + /// + [Export] public PackedScene Hit; + + // 起始点坐标 + private Vector2 StartPosition; + // 最大飞行距离 + private float MaxDistance; + // 子弹飞行速度 + private float FlySpeed = 1500; + //当前子弹已经飞行的距离 + private float CurrFlyDistance = 0; + //碰撞数据 + private KinematicCollision2D CollisionData = null; + + //子弹的精灵 + private Sprite BulletSprite; + + public void Init(CampEnum target, Gun gun, Node2D master) + { + TargetCamp = target; + Gun = gun; + Master = master; + + MaxDistance = MathUtils.RandRange(gun.Attribute.MinDistance, gun.Attribute.MaxDistance); + StartPosition = GlobalPosition; + //Scale = new Vector2(0, 1); + BulletSprite = GetNode("Bullet"); + // BulletSprite.Visible = false; + + Scale = new Vector2(FlySpeed / 60 * 1.5f, 1); + + GD.Print("---------------1" + GlobalPosition); + } + + public override void _PhysicsProcess(float delta) + { + GD.Print("---------------2" + GlobalPosition); + + if (CollisionData != null) //碰到物体 + { + // 销毁 + QueueFree(); + Node2D hit = Hit.Instance(); + hit.RotationDegrees = MathUtils.RandRangeInt(0, 360); + hit.GlobalPosition = CollisionData.Position; + GetTree().CurrentScene.AddChild(hit); + return; + } + + // 移动 + CollisionData = MoveAndCollide(Vector2.Right.Rotated(GlobalRotation) * FlySpeed * delta); + + if (CurrFlyDistance == 0) + { + BulletSprite.Visible = true; + } + + CurrFlyDistance += FlySpeed * delta; + if (CurrFlyDistance >= MaxDistance) + { + QueueFree(); + } + } + +} \ No newline at end of file diff --git a/src/effect/Hit.cs b/src/effect/Hit.cs index 3bb55f5..99b4b4d 100644 --- a/src/effect/Hit.cs +++ b/src/effect/Hit.cs @@ -2,6 +2,13 @@ public class Hit : AnimatedSprite { + + public override void _Ready() + { + Frame = 0; + Playing = true; + } + /// /// 动画结束, 销毁 /// diff --git a/src/gun/Gun.cs b/src/gun/Gun.cs index f32823c..a511109 100644 --- a/src/gun/Gun.cs +++ b/src/gun/Gun.cs @@ -254,10 +254,10 @@ continuousCount = continuousCount > 0 ? continuousCount - 1 : 0; fireInterval = 60 / Attribute.StartFiringSpeed; attackTimer += fireInterval; - + //触发开火函数 Fire(); - + //开火发射的子弹数量 var bulletCount = MathUtils.RandRangeInt(Attribute.MaxFireBulletCount, Attribute.MinFireBulletCount); //枪口角度 @@ -301,4 +301,36 @@ /// 如果做霰弹枪效果, 一次开火发射5枚子弹, 则该函数调用5次 /// protected abstract void ShootBullet(); + + /// + /// 实例化并返回子弹对象 + /// + /// 子弹的预制体 + protected T CreateBullet(PackedScene bulletPack, Vector2 globalPostion, float globalRotation, Node parent = null) where T : Node2D, IBullet + { + return (T) CreateBullet(bulletPack, globalPostion, globalRotation, parent); + } + + + protected IBullet CreateBullet(PackedScene bulletPack, Vector2 globalPostion, float globalRotation, Node parent = null) + { + // 实例化子弹 + Node2D bullet = bulletPack.Instance(); + // 设置坐标 + bullet.GlobalPosition = globalPostion; + // 旋转角度 + bullet.GlobalRotation = globalRotation; + if (parent == null) + { + GetTree().CurrentScene.AddChild(bullet); + } + else + { + parent.AddChild(bullet); + } + // 调用初始化 + IBullet result = (IBullet)bullet; + result.Init(TargetCamp, this, null); + return result; + } } \ No newline at end of file diff --git a/src/gun/OrdinaryGun.cs b/src/gun/OrdinaryGun.cs index b99c584..1bb47c5 100644 --- a/src/gun/OrdinaryGun.cs +++ b/src/gun/OrdinaryGun.cs @@ -1,20 +1,17 @@ using Godot; + /// /// 普通的枪 /// public class OrdinaryGun : Gun { - [Export] public PackedScene FirePrefab; - //子弹 - private PackedScene bulletPacked; - + [Export] public PackedScene bulletPacked; protected override void Init() { - //子弹 - bulletPacked = ResourceLoader.Load("res://prefab/bullet/Bullet.tscn"); + } protected override void Fire() @@ -25,28 +22,6 @@ protected override void ShootBullet() { //创建子弹 - var bullet = CreateBullet(bulletPacked); - //位置 - bullet.GlobalPosition = FirePoint.GlobalPosition; - //角度 - bullet.Rotation = (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle(); - GetTree().CurrentScene.AddChild(bullet); - //飞行距离 - var distance = MathUtils.RandRange(Attribute.MinDistance, Attribute.MaxDistance); - //初始化子弹数据 - bullet.InitData(distance, Colors.White); - - //枪口火焰 - Node2D hit = FirePrefab.Instance(); - hit.GlobalRotationDegrees = GlobalRotationDegrees; - hit.GlobalPosition = FirePoint.GlobalPosition; - GetTree().CurrentScene.AddChild(hit); - } - - protected T CreateBullet(PackedScene bulletPack) where T : Bullet - { - T bullet = bulletPack.Instance(); - bullet.Init(TargetCamp, this, null); - return bullet; + CreateBullet(bulletPacked, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle()); } } \ No newline at end of file diff --git a/src/role/Player.cs b/src/role/Player.cs index 9cc0538..9c97124 100644 --- a/src/role/Player.cs +++ b/src/role/Player.cs @@ -29,7 +29,7 @@ CurrGun = gun; var attr = new GunAttribute(); - attr.StartFiringSpeed = 600; + attr.StartFiringSpeed = 360; attr.StartScatteringRange = 5; attr.FinalScatteringRange = 60; attr.ScatteringRangeAddValue = 2f; diff --git a/src/weapon/Weapon.cs b/src/weapon/Weapon.cs deleted file mode 100644 index e7816d4..0000000 --- a/src/weapon/Weapon.cs +++ /dev/null @@ -1,8 +0,0 @@ - -/// -/// 武器基类 -/// -public class Weapon -{ - -} \ No newline at end of file