BITFALL/Assets/Artists/Scripts/Item/WorldItem.cs

198 lines
5.9 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using System.IO;
2024-04-10 18:12:40 +08:00
using System.Linq;
2023-12-15 00:08:02 +08:00
using BITFALL.Items;
using UnityEditor;
2023-06-08 14:09:50 +08:00
using UnityEngine.UIElements;
2023-12-15 00:08:02 +08:00
namespace BITFALL.Items
2023-06-08 14:09:50 +08:00
{
2023-12-15 00:08:02 +08:00
public class WorldItem : MonoBehaviour,IBasicItem,IDescription,IDisposable
2023-06-08 14:09:50 +08:00
{
#region
2023-12-15 00:08:02 +08:00
[Header(nameof(IAddressable))]
[SerializeField] private ulong addressableId = (ulong)Guid.NewGuid().GetHashCode();
[Header(Constant.Header.Components)]
[SerializeField] private new Rigidbody rigidbody;
2023-06-08 14:09:50 +08:00
[Header(Constant.Header.Settings)]
2024-04-06 16:33:57 +08:00
[SerializeField]private ScriptableItem asset;
2023-12-03 17:35:43 +08:00
[Header(Constant.Header.Optional)]
[SerializeReference, SubclassSelector] private IProperty[] initialProperties;
2023-06-08 14:09:50 +08:00
#endregion
#region
2024-04-10 18:12:40 +08:00
public int Value => asset.Value;
2023-12-15 00:08:02 +08:00
public ulong AddressableId
{
get=>addressableId;
set
{
addressableId = value;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
}
public int Id
{
get
{
if (_id is 0)_id = GetInstanceID();
return _id;
}
set => _id = value;
}
private int _id;
2023-11-15 23:54:54 +08:00
public string Name => asset.Name;
2023-06-08 14:09:50 +08:00
public string Description => asset.Description;
public ItemQuality Quality => asset.Quality;
2023-09-01 14:33:54 +08:00
public string AddressablePath => asset.AddressablePath;
2023-12-03 17:35:43 +08:00
internal Property property
{
get
{
2023-12-15 00:08:02 +08:00
if (BITAppForUnity.IsPlaying is false |!asset || asset.RuntimeProperties is null)
2023-12-03 17:35:43 +08:00
{
return (initialProperties is null)?
new Property() : new Property(initialProperties);
}
if(_property is not null) return _property;
var list = new List<IProperty>();
2024-04-10 18:12:40 +08:00
if (asset && asset.RuntimeProperties is not null)
2023-12-03 17:35:43 +08:00
{
2024-04-10 18:12:40 +08:00
list.AddRange(asset.RuntimeProperties.Select(x=>x is ICloneable cloneable ? cloneable.Clone().As<IProperty>() : x));
2023-12-03 17:35:43 +08:00
}
if(initialProperties is not null)
{
list.AddRange(initialProperties);
}
return _property= new Property(list.ToArray());
}
set=>_property=value;
}
private Property _property;
2023-12-15 00:08:02 +08:00
private object _rigidbody;
2023-06-08 14:09:50 +08:00
public bool Contains<T>() => property.Contains<T>();
public T GetOrAddProperty<T>(Func<T> addFactory) => property.GetOrAddProperty<T>(addFactory);
public T GetOrCreateProperty<T>() => property.GetOrCreateProperty<T>();
public object[] GetProperties() => property.GetProperties();
public bool TryGetProperty<T>(out T value) => property.TryGetProperty<T>(out value);
public bool TryRemoveProperty<T>() => property.TryRemoveProperty<T>();
public bool TrySetProperty<T>(T value) => property.TrySetProperty(value);
public void Read(BinaryReader r)
{
throw new NotImplementedException();
}
public void Write(BinaryWriter w)
{
throw new NotImplementedException();
}
public bool CopyItemsFrom(IBasicItem item)
{
2023-12-03 17:35:43 +08:00
Id=item.Id;
CopyPropertiesFrom(item);
2023-06-08 14:09:50 +08:00
return true;
}
public bool ClearProperties()=>property.ClearProperties();
public bool CopyPropertiesFrom(IPropertable propertable)=>property.CopyPropertiesFrom(propertable);
#endregion
#region
2024-04-06 16:33:57 +08:00
public ScriptableItem Scriptable => asset;
2023-12-15 00:08:02 +08:00
public Rigidbody Rigidbody => rigidbody;
2023-10-20 19:31:12 +08:00
public ManagedItem Pick()
{
2023-10-29 15:27:13 +08:00
var managedItem = new ManagedItem();
managedItem.CopyItemsFrom(this);
return managedItem;
2023-06-08 14:09:50 +08:00
}
public void Picked()
{
Destroy(gameObject);
}
#endregion
2023-12-03 17:35:43 +08:00
public object Clone()
{
return Pick();
}
2023-12-15 00:08:02 +08:00
private void Start()
{
if (isInitialized is false)
{
WorldItemService.Register(this);
isInitialized = true;
}
destroyCancellationToken.Register(() =>
{
if (isDisposed is false)
WorldItemService.Unregister(this);
});
}
private bool isInitialized;
private bool isDisposed;
public void WaitForInitializationComplete()
{
if (isInitialized) return;
Start();
isInitialized = true;
}
public void Dispose()
{
isDisposed = true;
if (this)
{
2024-04-10 18:12:40 +08:00
property = null;
initialProperties = null;
2023-12-15 00:08:02 +08:00
Destroy(gameObject);
}
}
2023-06-08 14:09:50 +08:00
}
#if UNITY_EDITOR
2023-10-24 23:37:59 +08:00
[UnityEditor.CustomEditor(typeof(WorldItem))]
public class WorldItemInspector:BITInspector<WorldItem>
2023-06-08 14:09:50 +08:00
{
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
CreateSubTitle(Constant.Header.Debug);
2023-12-15 00:08:02 +08:00
var label = root.Create<Label>();
label.text = agent.Id.ToString();
2023-12-03 17:35:43 +08:00
var property = root.Create<Label>();
var stringbuilder = new System.Text.StringBuilder();
foreach (var x in agent.property.GetProperties())
{
stringbuilder.AppendLine(x.ToString());
stringbuilder.AppendLine("--------------");
}
property.text = stringbuilder.ToString();
2023-06-08 14:09:50 +08:00
return root;
}
}
#endif
}