Separation NodeUtils InstantiatePackedScene method.
分离NodeUtils.InstantiatePackedScene方法。
This commit is contained in:
parent
b7f72c0456
commit
d587c4fc01
|
@ -6,10 +6,10 @@ uid="uid://dl2r1rydlm7pa"
|
|||
|
||||
[deps]
|
||||
|
||||
files=["res://locals/Slogan.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation"]
|
||||
files=["res://locals/slogan.zh.translation", "res://locals/slogan.en.translation", "res://locals/slogan.ja.translation"]
|
||||
|
||||
source_file="res://locals/Slogan.csv"
|
||||
dest_files=["res://locals/Slogan.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation"]
|
||||
source_file="res://locals/slogan.csv"
|
||||
dest_files=["res://locals/slogan.zh.translation", "res://locals/slogan.en.translation", "res://locals/slogan.ja.translation"]
|
||||
|
||||
[params]
|
||||
|
||||
|
|
|
@ -147,7 +147,7 @@ hotbar_previous={
|
|||
[internationalization]
|
||||
|
||||
locale/translations=PackedStringArray("res://locals/DeathInfo.en.translation", "res://locals/DeathInfo.ja.translation", "res://locals/DeathInfo.zh.translation", "res://locals/InputMapping.en.translation", "res://locals/InputMapping.ja.translation", "res://locals/InputMapping.zh.translation", "res://locals/Log.en.translation", "res://locals/Log.ja.translation", "res://locals/Log.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation", "res://locals/Slogan.zh.translation", "res://locals/UI.en.translation", "res://locals/UI.ja.translation", "res://locals/UI.zh.translation", "res://locals/Item.en.translation", "res://locals/Item.ja.translation", "res://locals/Item.zh.translation", "res://locals/Action.en.translation", "res://locals/Action.ja.translation", "res://locals/Action.zh.translation", "res://locals/Misc.en.translation", "res://locals/Misc.ja.translation", "res://locals/Misc.zh.translation")
|
||||
locale/test="ja"
|
||||
locale/test="en"
|
||||
|
||||
[layer_names]
|
||||
|
||||
|
|
|
@ -671,7 +671,7 @@ public partial class CharacterTemplate : CharacterBody2D
|
|||
return;
|
||||
}
|
||||
|
||||
CallDeferred(nameof(NodeReparent), node2D);
|
||||
NodeUtils.CallDeferredReparent(NodeUtils.FindContainerNode(this, GetNode("/root")), this);
|
||||
switch (item)
|
||||
{
|
||||
case PickAbleTemplate pickAbleTemplate:
|
||||
|
@ -719,15 +719,6 @@ public partial class CharacterTemplate : CharacterBody2D
|
|||
itemSlotNode.RemoveItem(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Replace the parent node of a node</para>
|
||||
/// <para>替换节点的父节点</para>
|
||||
/// </summary>
|
||||
/// <param name="node"></param>
|
||||
private void NodeReparent(Node node)
|
||||
{
|
||||
node.Reparent(NodeUtils.FindContainerNode(node, GetNode("/root")));
|
||||
}
|
||||
|
||||
public sealed override void _PhysicsProcess(double delta)
|
||||
{
|
||||
|
|
|
@ -434,9 +434,10 @@ public partial class Player : CharacterTemplate
|
|||
//If there is a scene of floating text, then we generate floating text.
|
||||
//如果有悬浮文本的场景,那么我们生成悬浮文本。
|
||||
_floatLabel?.QueueFree();
|
||||
_floatLabel = NodeUtils.InstantiatePackedScene<Control>(_floatLabelPackedScene, node);
|
||||
_floatLabel = NodeUtils.InstantiatePackedScene<Control>(_floatLabelPackedScene);
|
||||
if (_floatLabel != null)
|
||||
{
|
||||
NodeUtils.CallDeferredAddChild(node, _floatLabel);
|
||||
var rotationDegreesNode2D = node2D.RotationDegrees;
|
||||
var rotationDegreesNode2DAbs = Math.Abs(rotationDegreesNode2D);
|
||||
_floatLabel.Position = rotationDegreesNode2DAbs > 90
|
||||
|
|
|
@ -78,15 +78,6 @@ public partial class DamageNumberNodeSpawn : Marker2D
|
|||
_defaultGradient.SetColor(1, new Color("#fa5252"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Added a damage digital node</para>
|
||||
/// <para>添加伤害数字节点</para>
|
||||
/// </summary>
|
||||
/// <param name="damageNumber"></param>
|
||||
private void AddDamageNumberNode(Node2D damageNumber)
|
||||
{
|
||||
_rootNode?.AddChild(damageNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Show damage</para>
|
||||
|
@ -106,7 +97,12 @@ public partial class DamageNumberNodeSpawn : Marker2D
|
|||
return;
|
||||
}
|
||||
|
||||
CallDeferred(nameof(AddDamageNumberNode), damageNumber);
|
||||
if (_rootNode == null)
|
||||
{
|
||||
damageNumber.QueueFree();
|
||||
return;
|
||||
}
|
||||
NodeUtils.CallDeferredAddChild(_rootNode, damageNumber);
|
||||
damageNumber.Position = GlobalPosition;
|
||||
if (damageTemplate.MoveLeft)
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace ColdMint.scripts.inventory;
|
|||
|
||||
public readonly struct ItemType(
|
||||
string id,
|
||||
Func<Node?, bool, IItem?> createItemFunc,
|
||||
Func<Node?,IItem?> createItemFunc,
|
||||
Texture2D? icon,
|
||||
int maxStackQuantity)
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ public readonly struct ItemType(
|
|||
/// <para>A function returns a new item instance of this type</para>
|
||||
/// <para>用于创建该类型的物品实例的函数</para>
|
||||
/// </summary>
|
||||
public Func<Node?, bool, IItem?> CreateItemFunc { get; init; } = createItemFunc;
|
||||
public Func<Node?,IItem?> CreateItemFunc { get; init; } = createItemFunc;
|
||||
|
||||
/// <summary>
|
||||
/// <para>Default icon of items of this type</para>
|
||||
|
|
|
@ -41,14 +41,10 @@ public static class ItemTypeManager
|
|||
///<para>Default parent</para>
|
||||
///<para>父节点</para>
|
||||
/// </param>
|
||||
/// <param name="assignedByRootNodeType">
|
||||
///<para>Enabled by default, whether to place a node into a container node that matches the type of the root node after it is instantiated. If the assignment fails by type, it is placed under the default parent node.</para>
|
||||
///<para>默认启用,实例化节点后,是否将其放置到与根节点类型相匹配的容器节点内。如果按照类型分配失败,则放置在默认父节点下。</para>
|
||||
/// </param>
|
||||
/// <seealso cref="CreateItems"/>
|
||||
public static IItem? CreateItem(string id, Node? defaultParentNode = null, bool assignedByRootNodeType = true) =>
|
||||
public static IItem? CreateItem(string id, Node? defaultParentNode = null) =>
|
||||
Registry.TryGetValue(id, out var itemType)
|
||||
? itemType.CreateItemFunc(defaultParentNode, assignedByRootNodeType)
|
||||
? itemType.CreateItemFunc(defaultParentNode)
|
||||
: null;
|
||||
|
||||
|
||||
|
@ -59,12 +55,10 @@ public static class ItemTypeManager
|
|||
/// <param name="id"></param>
|
||||
/// <param name="number"></param>
|
||||
/// <param name="defaultParentNode"></param>
|
||||
/// <param name="assignedByRootNodeType"></param>
|
||||
/// <param name="globalPosition"></param>
|
||||
/// <returns></returns>
|
||||
/// <seealso cref="CreateItem"/>
|
||||
public static IItem?[]? CreateItems(string id, int number, Vector2 globalPosition, Node? defaultParentNode = null,
|
||||
bool assignedByRootNodeType = true)
|
||||
public static IItem?[]? CreateItems(string id, int number, Vector2 globalPosition, Node? defaultParentNode = null)
|
||||
{
|
||||
if (number <= 0)
|
||||
{
|
||||
|
@ -74,7 +68,7 @@ public static class ItemTypeManager
|
|||
var items = new List<IItem?>();
|
||||
for (var i = 0; i < number; i++)
|
||||
{
|
||||
var singleItem = CreateItem(id, defaultParentNode, assignedByRootNodeType);
|
||||
var singleItem = CreateItem(id, defaultParentNode);
|
||||
if (singleItem == null)
|
||||
{
|
||||
continue;
|
||||
|
|
|
@ -46,6 +46,7 @@ public static class ItemTypeRegister
|
|||
LogCat.LogWithFormat("found_files", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
LogCat.LogWithFormat("found_files", files.Length);
|
||||
//将文件解析为项目类型信息
|
||||
//parse files to item type infos
|
||||
|
@ -102,10 +103,18 @@ public static class ItemTypeRegister
|
|||
//构造项目类型,寄存器
|
||||
//construct item type, register
|
||||
var itemType = new ItemType(typeInfo.Id,
|
||||
(defaultParentNode, assignedByRootNodeType) =>
|
||||
defaultParentNode =>
|
||||
{
|
||||
var newItem = NodeUtils.InstantiatePackedScene<IItem>(scene, defaultParentNode, assignedByRootNodeType);
|
||||
setArgs?.Invoke(newItem as Node);
|
||||
var newItem = NodeUtils.InstantiatePackedScene<IItem>(scene);
|
||||
if (newItem is not Node node) return newItem;
|
||||
if (defaultParentNode == null)
|
||||
{
|
||||
node.QueueFree();
|
||||
return null;
|
||||
}
|
||||
setArgs?.Invoke(node);
|
||||
NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(node, defaultParentNode), node);
|
||||
|
||||
return newItem;
|
||||
},
|
||||
icon, typeInfo.MaxStackValue);
|
||||
|
|
|
@ -13,8 +13,7 @@ public partial class Packsack : PickAbleTemplate
|
|||
{
|
||||
private PackedScene? _packedScene;
|
||||
private PacksackUi? _packsackUi;
|
||||
[Export]
|
||||
public int NumberSlots { get; set; }
|
||||
[Export] public int NumberSlots { get; set; }
|
||||
|
||||
public override bool CanPutInPack => false;
|
||||
|
||||
|
@ -28,13 +27,15 @@ public partial class Packsack : PickAbleTemplate
|
|||
|
||||
if (_packsackUi == null)
|
||||
{
|
||||
_packsackUi = NodeUtils.InstantiatePackedScene<PacksackUi>(_packedScene,this);
|
||||
_packsackUi = NodeUtils.InstantiatePackedScene<PacksackUi>(_packedScene);
|
||||
if (_packsackUi != null)
|
||||
{
|
||||
NodeUtils.CallDeferredReparent(this, _packsackUi);
|
||||
_packsackUi.Title = Name;
|
||||
_packsackUi.ItemContainer = ItemContainer;
|
||||
}
|
||||
}
|
||||
|
||||
GameSceneNodeHolder.BackpackUiContainer?.Show();
|
||||
_packsackUi?.Show();
|
||||
}
|
||||
|
@ -52,6 +53,7 @@ public partial class Packsack : PickAbleTemplate
|
|||
{
|
||||
ItemContainer.AddItemSlot(this)?.Hide();
|
||||
}
|
||||
|
||||
_packedScene = GD.Load<PackedScene>("res://prefab/ui/packsackUI.tscn");
|
||||
}
|
||||
}
|
|
@ -159,12 +159,12 @@ public class UniversalItemContainer : IItemContainer
|
|||
return null;
|
||||
}
|
||||
|
||||
var itemSlotNode = NodeUtils.InstantiatePackedScene<ItemSlotNode>(_itemSlotPackedScene, rootNode);
|
||||
var itemSlotNode = NodeUtils.InstantiatePackedScene<ItemSlotNode>(_itemSlotPackedScene);
|
||||
if (itemSlotNode == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NodeUtils.CallDeferredAddChild(rootNode, itemSlotNode);
|
||||
if (SupportSelect)
|
||||
{
|
||||
itemSlotNode.IsSelect = _itemSlotNodes.Count == _selectIndex;
|
||||
|
|
|
@ -32,16 +32,23 @@ public partial class AiCharacterSpawn : Marker2D
|
|||
/// <param name="aiCharacterGenerateEvent"></param>
|
||||
public void OnAiCharacterGenerateEvent(AiCharacterGenerateEvent aiCharacterGenerateEvent)
|
||||
{
|
||||
if (GameSceneNodeHolder.AiCharacterContainer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_packedScene == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var aiCharacter = NodeUtils.InstantiatePackedScene<AiCharacter>(_packedScene,GameSceneNodeHolder.AiCharacterContainer);
|
||||
var aiCharacter = NodeUtils.InstantiatePackedScene<AiCharacter>(_packedScene);
|
||||
if (aiCharacter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(aiCharacter, this), aiCharacter);
|
||||
aiCharacter.Position = GlobalPosition;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using ColdMint.scripts.debug;
|
||||
using ColdMint.scripts.inventory;
|
||||
using ColdMint.scripts.inventory;
|
||||
using ColdMint.scripts.map.events;
|
||||
using Godot;
|
||||
|
||||
|
|
|
@ -54,12 +54,13 @@ public partial class PlayerSpawn : Marker2D
|
|||
}
|
||||
|
||||
var playerNode =
|
||||
NodeUtils.InstantiatePackedScene<Player>(_playerPackedScene, GameSceneNodeHolder.PlayerContainer);
|
||||
NodeUtils.InstantiatePackedScene<Player>(_playerPackedScene);
|
||||
if (playerNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(playerNode, this), playerNode);
|
||||
var itemContainer = GameSceneNodeHolder.HotBar?.GetItemContainer();
|
||||
if (itemContainer == null)
|
||||
{
|
||||
|
|
|
@ -38,6 +38,29 @@ public static class NodeUtils
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <para>Call the method to set the parent node at leisure</para>
|
||||
/// <para>在空闲时刻调用设置父节点的方法</para>
|
||||
/// </summary>
|
||||
/// <param name="parentNode"></param>
|
||||
/// <param name="childNode"></param>
|
||||
public static void CallDeferredReparent(Node parentNode, Node childNode)
|
||||
{
|
||||
parentNode.CallDeferred("reparent", childNode);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <para>Sets child nodes for a node</para>
|
||||
/// <para>为某个节点设置子节点</para>
|
||||
/// </summary>
|
||||
/// <param name="parentNode"></param>
|
||||
/// <param name="childNode"></param>
|
||||
public static void CallDeferredAddChild(Node parentNode, Node childNode)
|
||||
{
|
||||
parentNode.CallDeferred("add_child", childNode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Traverse the child nodes of type T under the parent node</para>
|
||||
/// <para>遍历父节点下T类型的子节点</para>
|
||||
|
@ -173,83 +196,25 @@ public static class NodeUtils
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Instantiate Packed Scene</para>
|
||||
/// <para>实例化场景</para>
|
||||
/// <para>Instantiate the scene and transform it into a node of the target type</para>
|
||||
/// <para>实例化场景并将其转换为目标类型的节点</para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///<para>This method is recommended in place of all packedScene.Instantiate() calls within a project, using it to instantiate a scene, optionally assigned to a container that matches the type of the root node.</para>
|
||||
///<para>推荐使用此方法代替项目内所有的packedScene.Instantiate()调用,使用此方法实例化场景,可选择将其分配到与根节点类型相匹配的容器内。</para>
|
||||
/// </remarks>
|
||||
/// <param name="packedScene">
|
||||
///<para>packedScene</para>
|
||||
///<para>打包的场景</para>
|
||||
/// </param>
|
||||
/// <param name="assignedByRootNodeType">
|
||||
///<para>Enabled by default, whether to place a node into a container node that matches the type of the root node after it is instantiated. If the assignment fails by type, it is placed under the default parent node.</para>
|
||||
///<para>默认启用,实例化节点后,是否将其放置到与根节点类型相匹配的容器节点内。如果按照类型分配失败,则放置在默认父节点下。</para>
|
||||
/// </param>
|
||||
/// <param name="defaultParentNode">
|
||||
/// <para>Default parent. If null is passed, the instantiated node is not put into the parent. You need to place it manually, which is quite useful for delaying setting the parent node.</para>
|
||||
/// <para>默认父节点,如果传入null,则不会将实例化后的节点放入父节点内。您需要手动放置,这对于延迟设置父节点相当有用。</para>
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
/// <seealso cref="InstantiatePackedScene{T}"/>
|
||||
public static Node InstantiatePackedScene(PackedScene packedScene, Node? defaultParentNode = null,
|
||||
bool assignedByRootNodeType = true)
|
||||
{
|
||||
var instantiateNode = packedScene.Instantiate();
|
||||
//An attempt is made to place an instantiated node under a parent node only after the default parent node is set.
|
||||
//只有设定了默认父节点后才会尝试将实例化的节点放置到父节点下。
|
||||
if (defaultParentNode != null)
|
||||
{
|
||||
if (assignedByRootNodeType)
|
||||
{
|
||||
var containerNode = FindContainerNode(instantiateNode, defaultParentNode);
|
||||
containerNode.AddChild(instantiateNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
//If you do not need to assign by type, place it under the default parent node.
|
||||
//如果不需要按照类型分配,那么将其放到默认父节点下。
|
||||
defaultParentNode.AddChild(instantiateNode);
|
||||
}
|
||||
}
|
||||
|
||||
return instantiateNode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Generic version of <see cref="InstantiatePackedScene"/> that transforms the created node to <see cref="T"/> type,
|
||||
/// if the created node can't be transformed to the specified type, return null and release it.
|
||||
/// </para>
|
||||
/// <para><see cref="InstantiatePackedScene"/>的泛型版本,转换创建的节点至<see cref="T"/>类型,如果创建的节点无法转型至指定的类型,返回null并释放它</para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///<para>This method is recommended in place of all packedScene.Instantiate<T>() calls within a project, using it to instantiate a scene, optionally assigned to a container that matches the type of the root node.</para>
|
||||
///<para>推荐使用此方法代替项目内所有的packedScene.Instantiate<T>()调用,使用此方法实例化场景,可选择将其分配到与根节点类型相匹配的容器内。</para>
|
||||
/// </remarks>
|
||||
/// <param name="packedScene">
|
||||
///<para>packedScene</para>
|
||||
///<para>打包的场景</para>
|
||||
/// </param>
|
||||
/// <param name="assignedByRootNodeType">
|
||||
///<para>Enabled by default, whether to place a node into a container node that matches the type of the root node after it is instantiated. If the assignment fails by type, it is placed under the default parent node.</para>
|
||||
///<para>默认启用,实例化节点后,是否将其放置到与根节点类型相匹配的容器节点内。如果按照类型分配失败,则放置在默认父节点下。</para>
|
||||
/// </param>
|
||||
/// <param name="defaultParentNode">
|
||||
/// <para>Default parent. If null is passed, the instantiated node is not put into the parent. You need to place it manually, which is quite useful for delaying setting the parent node.</para>
|
||||
/// <para>默认父节点,如果传入null,则不会将实例化后的节点放入父节点内。您需要手动放置,这对于延迟设置父节点相当有用。</para>
|
||||
/// </param>
|
||||
/// <typeparam name="T">
|
||||
///<para>genericity</para>
|
||||
///<para>泛型</para>
|
||||
/// </typeparam>
|
||||
/// <returns>
|
||||
/// <para>If new node cannot cast to given type, return null</para>
|
||||
/// <para>如果创建的节点无法转型至指定的类型,返回null</para>
|
||||
///<para>If the returned type is the target type, the node converted to the target type is returned, otherwise null is returned</para>
|
||||
///<para>如果返回的类型是目标类型,那么返回转换到目标类型的节点,否则返回null</para>
|
||||
/// </returns>
|
||||
public static T? InstantiatePackedScene<T>(PackedScene packedScene, Node? defaultParentNode = null,
|
||||
bool assignedByRootNodeType = true)
|
||||
public static T? InstantiatePackedScene<T>(PackedScene packedScene)
|
||||
where T : class
|
||||
{
|
||||
var node = InstantiatePackedScene(packedScene, defaultParentNode, assignedByRootNodeType);
|
||||
var node = packedScene.Instantiate();
|
||||
// Check the type conversion and return the result successfully
|
||||
// 检查类型转化,成功返回结果
|
||||
if (node is T result) return result;
|
||||
|
|
|
@ -49,8 +49,9 @@ public partial class ProjectileWeapon : WeaponTemplate
|
|||
//获取第一个抛射体
|
||||
var projectileScene = ProjectileScenes[0];
|
||||
// var projectileScene = _projectileCache[_projectiles[0]];
|
||||
var projectile = NodeUtils.InstantiatePackedScene<ProjectileTemplate>(projectileScene, _projectileContainer);
|
||||
var projectile = NodeUtils.InstantiatePackedScene<ProjectileTemplate>(projectileScene);
|
||||
if (projectile == null) return;
|
||||
NodeUtils.CallDeferredAddChild(_projectileContainer, projectile);
|
||||
projectile.Owner = owner;
|
||||
projectile.Velocity = (enemyGlobalPosition - _marker2D.GlobalPosition).Normalized() * projectile.Speed;
|
||||
projectile.Position = _marker2D.GlobalPosition;
|
||||
|
|
Loading…
Reference in New Issue
Block a user