2024-04-06 16:33:57 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-04-10 18:12:40 +08:00
|
|
|
using System.Linq;
|
2024-04-06 16:33:57 +08:00
|
|
|
using BITFALL.Industry;
|
|
|
|
using BITFALL.Items;
|
|
|
|
using BITKit;
|
2024-04-26 03:55:43 +08:00
|
|
|
using BITKit.MarkSystem;
|
2024-04-06 16:33:57 +08:00
|
|
|
using NodeCanvas.Framework;
|
|
|
|
using UnityEngine;
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
|
|
|
namespace BITFALL.Quest
|
|
|
|
{
|
|
|
|
public class CreateItemToContainer : ActionTask<Transform>
|
|
|
|
{
|
2024-04-10 18:12:40 +08:00
|
|
|
[DictionaryReferenceConfig(nameof(allow_quest_item))]
|
|
|
|
public const string allow_quest_item = nameof(allow_quest_item);
|
|
|
|
|
2024-04-06 16:33:57 +08:00
|
|
|
public BBParameter<Vector3> size;
|
|
|
|
public BBParameter<ScriptableItem> itemTemplate;
|
2024-04-10 18:12:40 +08:00
|
|
|
public BBParameter<IBasicItem> item;
|
2024-04-26 03:55:43 +08:00
|
|
|
|
|
|
|
[Inject] private IMarkSystem _markSystem;
|
|
|
|
|
|
|
|
private WorldItemContainer _worldContainer;
|
|
|
|
private IMarkObject _markObject;
|
2024-04-06 16:33:57 +08:00
|
|
|
protected override void OnExecute()
|
|
|
|
{
|
|
|
|
base.OnExecute();
|
|
|
|
|
2024-04-26 03:55:43 +08:00
|
|
|
DI.Inject(this);
|
2024-04-10 18:12:40 +08:00
|
|
|
|
2024-04-06 16:33:57 +08:00
|
|
|
var containers = WorldItemContainer.Query(agent.position, size.value);
|
2024-04-10 18:12:40 +08:00
|
|
|
|
|
|
|
containers = containers.Where(x => x.TryGetComponent<ITag>(out var tag) && tag.GetTags().Contains(allow_quest_item)).ToArray();
|
2024-04-06 16:33:57 +08:00
|
|
|
|
|
|
|
var newItem = itemTemplate.value.Clone().As<IBasicItem>();
|
|
|
|
|
2024-04-26 03:55:43 +08:00
|
|
|
var position = new Vector3();
|
|
|
|
|
2024-04-06 16:33:57 +08:00
|
|
|
if (containers.Length is 0)
|
|
|
|
{
|
|
|
|
newItem = Object.Instantiate(itemTemplate.value.GetPrefab(), agent.position,Quaternion.identity);
|
|
|
|
|
2024-04-26 03:55:43 +08:00
|
|
|
position = newItem.As<MonoBehaviour>().transform.position;
|
|
|
|
|
2024-04-06 16:33:57 +08:00
|
|
|
BIT4Log.Log<CreateItemToContainer>($"未找到容器,已创建物品于场景,物品名称:{newItem.Name}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newItem.GetOrCreateProperty<IsQuestItem>();
|
|
|
|
|
2024-04-10 18:12:40 +08:00
|
|
|
var container =containers.Random();
|
2024-04-26 03:55:43 +08:00
|
|
|
|
|
|
|
position = container.transform.position;
|
2024-04-06 16:33:57 +08:00
|
|
|
|
|
|
|
container.AddInternal(newItem);
|
|
|
|
BIT4Log.Log<CreateItemToContainer>($"已创建:{newItem.Name}于容器:{container.Name}");
|
2024-04-26 03:55:43 +08:00
|
|
|
|
|
|
|
container.OnRelease += OnRelease;
|
|
|
|
|
|
|
|
_worldContainer = container;
|
2024-04-06 16:33:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
newItem.GetOrCreateProperty<IsQuestItem>();
|
|
|
|
|
2024-04-26 03:55:43 +08:00
|
|
|
_markSystem.Register(_markObject = new MarkObject()
|
|
|
|
{
|
|
|
|
Position = position,
|
|
|
|
Object = newItem
|
|
|
|
});
|
2024-04-06 16:33:57 +08:00
|
|
|
|
2024-04-10 18:12:40 +08:00
|
|
|
item.SetValue(newItem);
|
2024-04-06 16:33:57 +08:00
|
|
|
|
|
|
|
EndAction();
|
|
|
|
}
|
2024-04-26 03:55:43 +08:00
|
|
|
private void OnRelease(bool obj)
|
|
|
|
{
|
|
|
|
if (!obj || _worldContainer is null) return;
|
|
|
|
if (_worldContainer.GetItems().Any(x => x.Id == item.value.Id))
|
|
|
|
return;
|
|
|
|
_markSystem.UnRegister(_markObject);
|
|
|
|
_worldContainer.OnRelease -= OnRelease;
|
|
|
|
}
|
2024-04-06 16:33:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|