using System; using ColdMint.scripts.camp; using ColdMint.scripts.character; using ColdMint.scripts.damage; using ColdMint.scripts.debug; using ColdMint.scripts.inventory; using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts.pickable; /// /// Templates for all fallen objects /// 所有掉落物的模板 /// public partial class PickAbleTemplate : RigidBody2D, IItem { public int Index { get; set; } //Do not export this field because the ID is specified within yaml. //不要导出此字段,因为ID是在yaml内指定的。 public virtual string Id { get; set; } = "ID"; [Export] protected Texture2D? UniqueIcon { get; set; } public void ShowSelf() { Show(); } public void QueueFreeSelf() { QueueFree(); } public void HideSelf() { Hide(); } public Texture2D Icon => UniqueIcon ?? ItemTypeManager.DefaultIconOf(Id); public new string Name { get { var key = $"item_{Id}"; return TranslationServerUtils.Translate(key) ?? key; } } /// /// Owner /// 主人 /// public new Node2D? Owner { get; set; } /// /// Enabled contact injury /// 启用接触伤害 /// public bool EnableContactInjury; [Export] private int _minContactInjury = 1; [Export] private int _maxContactInjury = 2; public string Description { get { var key = $"item_{Id}_desc"; return TranslationServerUtils.Translate(key) ?? key; } } public int Quantity { get; set; } = 1; /// /// The number of tile maps that come into contact with this item /// 与此物品接触的瓦片地图数量 /// private int _tileMapNumber; /// /// This area represents the collision range of the weapon, and when other nodes enter this area, they will deal damage. /// 这个区域表示武器的碰撞范围,当其他节点进入此区域时,会造成伤害。 /// private Area2D? _damageArea2D; /// /// Whether the item is currently picked up /// 当前物品是否被捡起了 /// public bool Picked { get; set; } public int MaxQuantity { get; set; } = 1; public virtual int ItemType { get => Config.ItemType.Unknown; } private bool _isSelected; public bool IsSelect { get => _isSelected; set { if (_isSelected == value) { return; } _isSelected = value; OnSelectChange(value); } } public IItemContainer? ItemContainer { get; set; } public IItemContainer? SelfItemContainer { get; set; } private Label? _tipLabel; /// /// /// 当选中状态发生改变时 /// /// protected virtual void OnSelectChange(bool isSelected) { } public IItem? CreateItem(int number) { if (number == 0) { return null; } var duplicate = Duplicate(); if (duplicate is PickAbleTemplate pickAbleTemplate) { pickAbleTemplate.CopyAttributes(this); } if (duplicate is not Node2D newNode2D) { return null; } newNode2D.GlobalPosition = GlobalPosition; if (duplicate is not IItem newItem) { duplicate.QueueFree(); return null; } if (number < 0) { newItem.Quantity = Quantity; } else { newItem.Quantity = Math.Min(Quantity, number); } return newItem; } public int MergeableItemCount(IItem other, int unallocatedQuantity) { var freeQuantity = MaxQuantity - Quantity; if (freeQuantity == 0) { return 0; } if (other.Id != Id) { return 0; } return Math.Min(freeQuantity, unallocatedQuantity); } public virtual bool Use(Node2D? owner, Vector2 targetGlobalPosition) { return false; } public virtual void OnThrow(Vector2 velocity) { } public override void _Ready() { _damageArea2D = GetNode("DamageArea2D"); _damageArea2D.BodyEntered += OnBodyEnter; _damageArea2D.BodyExited += OnBodyExited; _tipLabel = GetNodeOrNull