Remade spawn coordinates system

This commit is contained in:
Marco 2025-01-27 17:13:26 +01:00
commit d633618633
5 changed files with 85 additions and 12 deletions

View file

@ -40,7 +40,7 @@ public partial class Barrel : Area2D, IDestructible
private void CreateDebris()
{
this.CreateChild<Barrel>(DebrisScene);
this.CreateSibling<Barrel>(DebrisScene);
// if (DebrisScene == null) return;
// var debris = DebrisScene.Instantiate<Barrel>();
@ -55,7 +55,7 @@ public partial class Barrel : Area2D, IDestructible
GD.PushWarning("Object has no particles associated");
return;
}
var particle = this.CreateChild<GpuParticles2D>(ExplosionParticles);
var particle = this.CreateSibling<GpuParticles2D>(ExplosionParticles);
if (particle == null) return;
particle.Emitting = true;

View file

@ -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

View file

@ -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>("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);
}
}

View file

@ -6,18 +6,58 @@ public static class Tools
{
public static T CreateChild<T>(this Node2D node, PackedScene prefab) where T : Node2D
{
return CreateChild<T>(node, prefab, node.Position);
return CreateChild<T>(node, prefab, node.GlobalPosition);
}
public static T CreateChild<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
{
if (prefab == null) return null;
return CreateChildOf<T>(node, node, prefab, position);
// if (prefab == null) return null;
// var newInstance = prefab.Instantiate<T>();
// 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<T>(this Node2D node, PackedScene prefab) where T : Node2D
{
return CreateSibling<T>(node, prefab, node.GlobalPosition);
}
public static T CreateSibling<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
{
return CreateChildOf<T>(node, node.GetParent<Node2D>(), prefab, position);
}
public static T CreateChildOf<T>(this Node2D node, Node2D parentNode, PackedScene prefab) where T : Node2D
{
return CreateChildOf<T>(node, parentNode, prefab, node.GlobalPosition);
}
public static T CreateChildOf<T>(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<T>();
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;
}

View file

@ -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<Marker2D>("./Muzzle");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
_gameManager = GetNode<GameManager>("/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<Bullet>(BulletScene);
// if (BulletScene == null) return;
//
// var newInstance = BulletScene.Instantiate<Bullet>();
// _gameManager.BulletsContainer.CallDeferred("add_child", newInstance);
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition);
if (bullet == null)
{
GD.PrintErr("Bullet is null, not shooting");
return;
};
bullet.SetDirection(ShootDirection);
bullet.Speed = BulletSpeed;