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

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