diff --git a/DungeonShooting_Godot/prefab/role/Enemy.tscn b/DungeonShooting_Godot/prefab/role/Enemy.tscn index e074e72..3e3b357 100644 --- a/DungeonShooting_Godot/prefab/role/Enemy.tscn +++ b/DungeonShooting_Godot/prefab/role/Enemy.tscn @@ -24,10 +24,6 @@ [node name="AnimatedSprite" parent="." index="2"] material = SubResource( 2 ) -frame = 0 - -[node name="Collision" parent="." index="3"] -position = Vector2( 0, -8 ) [node name="ViewRay" type="RayCast2D" parent="." index="6"] position = Vector2( 0, -8 ) diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index 7154560..fafdc9d 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -24,4 +24,4 @@ [node name="AnimatedSprite" parent="." index="2"] material = SubResource( 2 ) -frame = 1 +frame = 0 diff --git a/DungeonShooting_Godot/prefab/role/Role.tscn b/DungeonShooting_Godot/prefab/role/Role.tscn index 37fe18d..e19e22b 100644 --- a/DungeonShooting_Godot/prefab/role/Role.tscn +++ b/DungeonShooting_Godot/prefab/role/Role.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=24 format=2] +[gd_scene load_steps=25 format=2] [ext_resource path="res://resource/materlal/Blend.tres" type="Material" id=1] [ext_resource path="res://addons/dungeonShooting_plugin/ActivityObjectTemplate.cs" type="Script" id=2] @@ -81,7 +81,10 @@ } ] [sub_resource type="CircleShape2D" id=34] -radius = 7.00446 +radius = 4.0 + +[sub_resource type="RectangleShape2D" id=35] +extents = Vector2( 5, 7.5 ) [sub_resource type="RectangleShape2D" id=16] extents = Vector2( 5, 8.25 ) @@ -106,9 +109,19 @@ playing = true [node name="Collision" type="CollisionShape2D" parent="."] -position = Vector2( 0, -7.75 ) +visible = false +position = Vector2( 0, -4 ) shape = SubResource( 34 ) +[node name="HurtArea" type="Area2D" parent="."] +collision_layer = 0 +collision_mask = 0 +monitoring = false + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtArea"] +position = Vector2( 0, -7.5 ) +shape = SubResource( 35 ) + [node name="InteractiveArea" type="Area2D" parent="."] visible = false collision_layer = 0 diff --git a/DungeonShooting_Godot/src/game/GameConfig.cs b/DungeonShooting_Godot/src/game/GameConfig.cs index 998f5c4..22ce9d0 100644 --- a/DungeonShooting_Godot/src/game/GameConfig.cs +++ b/DungeonShooting_Godot/src/game/GameConfig.cs @@ -19,4 +19,5 @@ /// 游戏视图大小 /// public static readonly Vector2 ViewportSize = new Vector2(480, 270); + //public static Vector2 ViewportSize => OS.WindowSize / WindowScale; } \ 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 f6db78c..1b896bc 100644 --- a/DungeonShooting_Godot/src/game/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/game/common/NodeExtend.cs @@ -21,4 +21,21 @@ } return null; } + + /// + /// 尝试将一个 Node2d 节点转换成一个 ActivityObject 对象, 如果转换失败, 则返回 null + /// + public static T AsActivityObject(this Node2D node2d) where T : ActivityObject + { + if (node2d is T p) + { + return p; + } + var parent = node2d.GetParent(); + if (parent != null && parent is T p2) + { + return p2; + } + return null; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs b/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs index 914722a..956f1c5 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/bullet/Bullet.cs @@ -5,21 +5,9 @@ /// public class Bullet : ActivityObject { - 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; - Position = position; - Rotation = rotation; - ShadowOffset = new Vector2(0, 5); - } - + /// + /// 碰撞区域 + /// public Area2D CollisionArea { get; } // 最大飞行距离 @@ -31,6 +19,22 @@ //当前子弹已经飞行的距离 private float CurrFlyDistance = 0; + public Bullet(string scenePath, float maxDistance, Vector2 position, float rotation, uint targetLayer) : + base(scenePath) + { + CollisionArea = GetNode("CollisionArea"); + CollisionArea.CollisionMask = targetLayer; + CollisionArea.Connect("area_entered", this, nameof(OnArea2dEntered)); + CollisionArea.Connect("body_entered", this, nameof(OnBodyEntered)); + + Collision.Disabled = true; + + MaxDistance = maxDistance; + Position = position; + Rotation = rotation; + ShadowOffset = new Vector2(0, 5); + } + public override void _Ready() { base._Ready(); @@ -51,19 +55,33 @@ } } - private void _BodyEntered(Node2D other) + private void OnArea2dEntered(Area2D other) { - if (other is Role role) + var role = other.AsActivityObject(); + if (role != null) { role.Hurt(1); + + //播放受击动画 + // Node2D hit = ResourceManager.Load(ResourcePath.prefab_effect_Hit_tscn).Instance(); + // hit.RotationDegrees = Utils.RandRangeInt(0, 360); + // hit.GlobalPosition = GlobalPosition; + // GameApplication.Instance.Room.GetRoot(true).AddChild(hit); + + DoDestroy(); } + } - //播放受击动画 - // Node2D hit = ResourceManager.Load(ResourcePath.prefab_effect_Hit_tscn).Instance(); - // hit.RotationDegrees = Utils.RandRangeInt(0, 360); - // hit.GlobalPosition = GlobalPosition; - // GameApplication.Instance.Room.GetRoot(true).AddChild(hit); + private void OnBodyEntered(Node2D other) + { + if (!(other is Role)) + { + DoDestroy(); + } + } + private void DoDestroy() + { SpecialEffectManager.Play(ResourcePath.resource_effects_Hit_tres, "default", GlobalPosition, Mathf.Deg2Rad(Utils.RandRangeInt(0, 360)), Vector2.One, new Vector2(1, 11), 0); diff --git a/DungeonShooting_Godot/src/game/role/Role.cs b/DungeonShooting_Godot/src/game/role/Role.cs index e3a2568..ae5d0f2 100644 --- a/DungeonShooting_Godot/src/game/role/Role.cs +++ b/DungeonShooting_Godot/src/game/role/Role.cs @@ -13,10 +13,10 @@ public const uint DefaultAttackLayer = PhysicsLayer.Player | PhysicsLayer.Enemy | PhysicsLayer.Wall | PhysicsLayer.Props; /// - /// 动画播放器 + /// 伤害区域 /// - public AnimationPlayer AnimationPlayer { get; private set; } - + public Area2D HurtArea { get; private set; } + /// /// 重写的纹理, 即将删除, 请直接更改 AnimatedSprite.Frames /// @@ -218,11 +218,15 @@ public override void _Ready() { base._Ready(); - AnimationPlayer = GetNode("AnimationPlayer"); _startScale = Scale; MountPoint = GetNode("MountPoint"); MountPoint.Master = this; BackMountPoint = GetNode("BackMountPoint"); + + HurtArea = GetNode("HurtArea"); + HurtArea.CollisionLayer = CollisionLayer; + HurtArea.CollisionMask = 0; + //即将删除 if (OverrideTexture != null) { diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index 87299d1..50d18db 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -79,28 +79,29 @@ //播放bgm SoundManager.PlayMusic(ResourcePath.resource_sound_bgm_Intro_ogg, -17f); - var enemy1 = new Enemy(); - enemy1.Name = "Enemy"; - enemy1.PutDown(new Vector2(150, 300)); - enemy1.PickUpWeapon(WeaponManager.GetGun("1003")); - enemy1.PickUpWeapon(WeaponManager.GetGun("1001")); - - for (int i = 0; i < 10; i++) - { - var enemyTemp = new Enemy(); - enemyTemp.Name = "EnemyTemp" + i; - enemyTemp.PutDown(new Vector2(150 + (i + 1) * 20, 300)); - enemyTemp.PickUpWeapon(WeaponManager.GetGun("1003")); - enemyTemp.PickUpWeapon(WeaponManager.GetGun("1001")); - } - var enemy2 = new Enemy(); - enemy2.Name = "Enemy2"; - enemy2.PutDown(new Vector2(540, 100)); - enemy2.PickUpWeapon(WeaponManager.GetGun("1002")); - enemy2.PickUpWeapon(WeaponManager.GetGun("1004")); - enemy2.PickUpWeapon(WeaponManager.GetGun("1003")); - + // var enemy1 = new Enemy(); + // enemy1.Name = "Enemy"; + // enemy1.PutDown(new Vector2(150, 300)); + // enemy1.PickUpWeapon(WeaponManager.GetGun("1003")); + // enemy1.PickUpWeapon(WeaponManager.GetGun("1001")); + // + // for (int i = 0; i < 10; i++) + // { + // var enemyTemp = new Enemy(); + // enemyTemp.Name = "EnemyTemp" + i; + // enemyTemp.PutDown(new Vector2(150 + (i + 1) * 20, 300)); + // enemyTemp.PickUpWeapon(WeaponManager.GetGun("1003")); + // enemyTemp.PickUpWeapon(WeaponManager.GetGun("1001")); + // } + // + // var enemy2 = new Enemy(); + // enemy2.Name = "Enemy2"; + // enemy2.PutDown(new Vector2(540, 100)); + // enemy2.PickUpWeapon(WeaponManager.GetGun("1002")); + // enemy2.PickUpWeapon(WeaponManager.GetGun("1004")); + // enemy2.PickUpWeapon(WeaponManager.GetGun("1003")); + // var enemy3 = new Enemy(); enemy3.Name = "Enemy3"; enemy3.PutDown(new Vector2(540, 300));