diff --git a/Scenes/Barrel.cs b/Scenes/Barrel.cs index 6a5d267c..2f3ff425 100644 --- a/Scenes/Barrel.cs +++ b/Scenes/Barrel.cs @@ -40,7 +40,7 @@ public partial class Barrel : Area2D, IDestructible private void CreateDebris() { - this.CreateChild(DebrisScene); + this.CreateSibling(DebrisScene); // if (DebrisScene == null) return; // var debris = DebrisScene.Instantiate(); @@ -55,7 +55,7 @@ public partial class Barrel : Area2D, IDestructible GD.PushWarning("Object has no particles associated"); return; } - var particle = this.CreateChild(ExplosionParticles); + var particle = this.CreateSibling(ExplosionParticles); if (particle == null) return; particle.Emitting = true; diff --git a/Scenes/player.tscn b/Scenes/player.tscn index 9412b2a0..12523313 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -159,7 +159,7 @@ position = Vector2(0, 1) shape = SubResource("RectangleShape2D_ai4rh") [node name="Muzzle" type="Marker2D" parent="."] -position = Vector2(5, 0) +position = Vector2(-1, 0) [node name="Node2D" type="Node2D" parent="."] @@ -201,8 +201,9 @@ collision_mask = 128 shape = SubResource("CircleShape2D_e6woi") debug_color = Color(1, 0.00817797, 0.0443347, 0.42) -[node name="Weapon" parent="." instance=ExtResource("9_wblq0")] +[node name="Weapon" parent="." node_paths=PackedStringArray("Muzzle") instance=ExtResource("9_wblq0")] BulletScene = ExtResource("2_ov36d") +Muzzle = NodePath("../Muzzle") RateOfFire = 0.1 BulletCapacity = 100 ReloadTime = 0.4 diff --git a/Scripts/GameManager.cs b/Scripts/GameManager.cs index 77be729e..5630474c 100644 --- a/Scripts/GameManager.cs +++ b/Scripts/GameManager.cs @@ -20,6 +20,8 @@ public partial class GameManager : Node2D public InventoryManager Inventory => _inventoryManager; + private Node2D _bulletsContainer; + public Node2D BulletsContainer => _bulletsContainer; // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -29,6 +31,8 @@ public partial class GameManager : Node2D _inventoryManager = GetNode("InventoryManager"); + SpawnBulletsContainer(); + if (PlayerSpawnMarker != null) { SpawnPlayer(); @@ -57,4 +61,12 @@ public partial class GameManager : Node2D _cameraTarget.GlobalPosition = _player.Position; } } + + private void SpawnBulletsContainer() + { + _bulletsContainer = new Node2D(); + _bulletsContainer.Name = "BulletsContainer"; + + AddChild(_bulletsContainer); + } } diff --git a/Scripts/Tools.cs b/Scripts/Tools.cs index 459a5d16..1fb617b6 100644 --- a/Scripts/Tools.cs +++ b/Scripts/Tools.cs @@ -6,18 +6,58 @@ public static class Tools { public static T CreateChild(this Node2D node, PackedScene prefab) where T : Node2D { - return CreateChild(node, prefab, node.Position); + return CreateChild(node, prefab, node.GlobalPosition); } public static T CreateChild(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D { - if (prefab == null) return null; + return CreateChildOf(node, node, prefab, position); + + // if (prefab == null) return null; + // var newInstance = prefab.Instantiate(); + // node.GetParent().CallDeferred("add_child", newInstance); + // // Need to use parent instead of owner because tilemap scenes have no owner + // //node.Owner.CallDeferred("add_child", newInstance); + // newInstance.Transform = node.GlobalTransform; + // newInstance.Position = position; + // + // return newInstance; + } + + public static T CreateSibling(this Node2D node, PackedScene prefab) where T : Node2D + { + return CreateSibling(node, prefab, node.GlobalPosition); + } + + public static T CreateSibling(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D + { + return CreateChildOf(node, node.GetParent(), prefab, position); + } + + public static T CreateChildOf(this Node2D node, Node2D parentNode, PackedScene prefab) where T : Node2D + { + return CreateChildOf(node, parentNode, prefab, node.GlobalPosition); + } + + public static T CreateChildOf(this Node2D node, Node2D parentNode, PackedScene prefab, Vector2 position) where T : Node2D + { + if (prefab == null) + { + GD.PrintErr("Tried to instantiate a null prefab"); + return null; + } + + if (parentNode == null) + { + GD.PrintErr("Tried to instantiate child of a null parent"); + return null; + } + var newInstance = prefab.Instantiate(); - node.GetParent().CallDeferred("add_child", newInstance); - // Need to use parent instead of owner because tilemap scenes have no owner - //node.Owner.CallDeferred("add_child", newInstance); - newInstance.Transform = node.GlobalTransform; - newInstance.Position = position; + //node.GetParent().CallDeferred("add_child", newInstance); + parentNode.CallDeferred("add_child", newInstance); + //newInstance.Transform = node.GlobalTransform; + newInstance.Position = parentNode.ToLocal(position); return newInstance; } diff --git a/Scripts/Weapon.cs b/Scripts/Weapon.cs index b8577741..b3c9b77e 100644 --- a/Scripts/Weapon.cs +++ b/Scripts/Weapon.cs @@ -1,5 +1,6 @@ using Godot; using System; +using System.Diagnostics; using Cirno.Scripts; public partial class Weapon : Node2D @@ -31,12 +32,18 @@ public partial class Weapon : Node2D private Timer _cooldownTimer; private Marker2D _muzzle; + + private Node2D _bulletsContainer; + + private GameManager _gameManager; // Called when the node enters the scene tree for the first time. public override void _Ready() { _muzzle = GetNode("./Muzzle"); _cooldownTimer = GetNode("./ShootTimer"); + + _gameManager = GetNode("/root/GameScene"); } public void Reload() @@ -74,7 +81,20 @@ public partial class Weapon : Node2D // TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius? // TODO: Create not as child but as standalone - var bullet = this.CreateChild(BulletScene); + + // if (BulletScene == null) return; + // + // var newInstance = BulletScene.Instantiate(); + // _gameManager.BulletsContainer.CallDeferred("add_child", newInstance); + + var bullet = this.CreateChildOf(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition); + + if (bullet == null) + { + GD.PrintErr("Bullet is null, not shooting"); + return; + }; + bullet.SetDirection(ShootDirection); bullet.Speed = BulletSpeed;