AI захватит мир
This commit is contained in:
@@ -4,7 +4,7 @@ using Sasalka;
|
||||
|
||||
public sealed partial class Dedugan : Component
|
||||
{
|
||||
[Property, InlineEditor] public Inventar Inventory { get; private set; } = new();
|
||||
[Property] public Inventar Inventory { get; private set; }
|
||||
|
||||
private Dictionary<Inventar.InventorySlot, (GameObject obj, IUseable useable)> _useableCache = new();
|
||||
[Sync] private bool InAds { get; set; } = false;
|
||||
@@ -12,65 +12,72 @@ public sealed partial class Dedugan : Component
|
||||
|
||||
void InventoryStart()
|
||||
{
|
||||
if ( !Network.IsOwner ) return;
|
||||
if (!Network.IsOwner) return;
|
||||
|
||||
_resolver = new AttachmentSlotResolver( Renderer.GetAttachmentObject );
|
||||
// Создаем инвентарь как компонент
|
||||
Inventory = GameObject.Components.GetOrCreate<Inventar>();
|
||||
_resolver = new AttachmentSlotResolver(Renderer.GetAttachmentObject);
|
||||
|
||||
// Inventory.AddItem( new InventoryItem
|
||||
// {
|
||||
// Definition = ResourceLibrary.Get<ClothingItemDefinition>( "Items/Cloth/cloth_pijama.clitem" )
|
||||
// } );
|
||||
//
|
||||
// Inventory.AddItem( new InventoryItem
|
||||
// {
|
||||
// Definition = ResourceLibrary.Get<ClothingItemDefinition>( "Items/Cloth/cloth_pijama_bottom.clitem" )
|
||||
// } );
|
||||
//
|
||||
// Inventory.AddItem( new InventoryItem
|
||||
// {
|
||||
// Definition = ResourceLibrary.Get<WeaponItemDefinition>( "Items/pistol_test.weapon" )
|
||||
// } );
|
||||
//
|
||||
// var ammo = new InventoryItem
|
||||
// {
|
||||
// Definition = ResourceLibrary.Get<BaseItemDefinition>( "Items/pistol_ammo.inv" )
|
||||
// };
|
||||
// ammo.Count = 30;
|
||||
// ammo.MaxCount = 130;
|
||||
//
|
||||
// Inventory.AddItem( ammo );
|
||||
// Добавляем тестовые предметы (раскомментируйте для тестирования)
|
||||
var clothingItem = new InventoryItem
|
||||
{
|
||||
Definition = ResourceLibrary.Get<ClothingItemDefinition>("Items/Cloth/cloth_pijama.clitem")
|
||||
};
|
||||
Inventory.AddItem(clothingItem);
|
||||
|
||||
var weaponItem = new InventoryItem
|
||||
{
|
||||
Definition = ResourceLibrary.Get<WeaponItemDefinition>("Items/pistol_test.weapon")
|
||||
};
|
||||
Inventory.AddItem(weaponItem);
|
||||
|
||||
var ammoItem = new InventoryItem
|
||||
{
|
||||
Definition = ResourceLibrary.Get<AmmoItemDefinition>("Items/pistol_ammo.inv")
|
||||
};
|
||||
ammoItem.Count = 30;
|
||||
Inventory.AddItem(ammoItem);
|
||||
|
||||
Inventory.OnEquipped += OnItemEquipped;
|
||||
Inventory.OnUnEquipped += OnItemUnEquipped;
|
||||
Inventory.OnItemAdded += OnItemAdded;
|
||||
Inventory.OnItemRemoved += OnItemRemoved;
|
||||
}
|
||||
|
||||
private void OnItemEquipped( InventoryItem item )
|
||||
private void OnItemEquipped(InventoryItem item)
|
||||
{
|
||||
// Если это оружие
|
||||
if ( item.Definition is WeaponItemDefinition weaponDef && weaponDef.Prefab.IsValid() )
|
||||
// Очищаем кэши при экипировке предмета
|
||||
_useableCache.Clear();
|
||||
_resolver?.ClearCache();
|
||||
|
||||
if (item?.Definition is WeaponItemDefinition weaponDef && weaponDef.Prefab.IsValid())
|
||||
{
|
||||
var go = weaponDef.Prefab.Clone();
|
||||
|
||||
AnimationHelper.HoldType = weaponDef.HoldType;
|
||||
|
||||
switch ( weaponDef.Slot )
|
||||
switch (weaponDef.Slot)
|
||||
{
|
||||
case Inventar.InventorySlot.LeftHand | Inventar.InventorySlot.RightHand:
|
||||
case Inventar.InventorySlot.RightHand:
|
||||
go.Parent = Renderer.GetAttachmentObject( "hold_R" );
|
||||
go.Parent = Renderer.GetAttachmentObject("hold_R");
|
||||
break;
|
||||
case Inventar.InventorySlot.LeftHand:
|
||||
go.Parent = Renderer.GetAttachmentObject( "hold_L" );
|
||||
go.Parent = Renderer.GetAttachmentObject("hold_L");
|
||||
break;
|
||||
default:
|
||||
go.Parent = Renderer.GetAttachmentObject( "forward_reference_modelspace" );
|
||||
go.Parent = Renderer.GetAttachmentObject("forward_reference_modelspace");
|
||||
break;
|
||||
}
|
||||
|
||||
go.LocalPosition = weaponDef.WeaponDefinition.Position;
|
||||
go.LocalRotation = weaponDef.WeaponDefinition.Rotation;
|
||||
go.LocalScale = weaponDef.WeaponDefinition.Scale;
|
||||
|
||||
go.Components.Get<UseableBase>().Equipped = true;
|
||||
if (go.Components.TryGet<UseableBase>(out var useable))
|
||||
{
|
||||
useable.Equipped = true;
|
||||
}
|
||||
|
||||
go.NetworkSpawn();
|
||||
|
||||
@@ -83,94 +90,108 @@ public sealed partial class Dedugan : Component
|
||||
};
|
||||
|
||||
AnimationHelper.Handedness = hand;
|
||||
|
||||
RpcSetHoldAnimation( weaponDef.HoldType, hand );
|
||||
|
||||
RpcSetHoldAnimation(weaponDef.HoldType, hand);
|
||||
InAds = true;
|
||||
}
|
||||
// Если это одежда
|
||||
else if ( item.Definition is ClothingItemDefinition clothingDef )
|
||||
else if (item?.Definition is ClothingItemDefinition clothingDef)
|
||||
{
|
||||
WearWorkshop( new List<string>() { clothingDef.ClothUrl } );
|
||||
WearWorkshop(new List<string>() { clothingDef.ClothUrl });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void OnItemUnEquipped( InventoryItem item )
|
||||
private void OnItemUnEquipped(InventoryItem item)
|
||||
{
|
||||
if ( item.Definition is WeaponItemDefinition weaponDef && weaponDef.Prefab.IsValid() )
|
||||
// Очищаем кэши при снятии предмета
|
||||
_useableCache.Clear();
|
||||
_resolver?.ClearCache();
|
||||
|
||||
if (item?.Definition is WeaponItemDefinition weaponDef && weaponDef.Prefab.IsValid())
|
||||
{
|
||||
switch ( weaponDef.Slot )
|
||||
switch (weaponDef.Slot)
|
||||
{
|
||||
case Inventar.InventorySlot.LeftHand | Inventar.InventorySlot.RightHand:
|
||||
case Inventar.InventorySlot.RightHand:
|
||||
case Inventar.InventorySlot.LeftHand:
|
||||
var attachmentName = !weaponDef.Slot.HasFlag( Inventar.InventorySlot.RightHand )
|
||||
var attachmentName = !weaponDef.Slot.HasFlag(Inventar.InventorySlot.RightHand)
|
||||
? "hold_L"
|
||||
: "hold_R";
|
||||
|
||||
Renderer.GetAttachmentObject( attachmentName ).Children.ForEach( child => child.Destroy() );
|
||||
RpcSetHoldAnimation( CitizenAnimationHelper.HoldTypes.None, CitizenAnimationHelper.Hand.Both );
|
||||
Renderer.GetAttachmentObject(attachmentName).Children.ForEach(child => child.Destroy());
|
||||
RpcSetHoldAnimation(CitizenAnimationHelper.HoldTypes.None, CitizenAnimationHelper.Hand.Both);
|
||||
break;
|
||||
default:
|
||||
Renderer.GetAttachmentObject( "forward_reference_modelspace" ).Children
|
||||
.ForEach( child => child.Destroy() );
|
||||
Renderer.GetAttachmentObject("forward_reference_modelspace").Children
|
||||
.ForEach(child => child.Destroy());
|
||||
break;
|
||||
}
|
||||
|
||||
item.Destroy();
|
||||
InAds = false;
|
||||
}
|
||||
else if ( item.Definition is ClothingItemDefinition clothingDef )
|
||||
else if (item?.Definition is ClothingItemDefinition clothingDef)
|
||||
{
|
||||
StripByName( clothingDef.Description );
|
||||
StripByName(clothingDef.Description);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnItemAdded(InventoryItem item)
|
||||
{
|
||||
// Очищаем кэши при добавлении предмета
|
||||
_useableCache.Clear();
|
||||
_resolver?.ClearCache();
|
||||
}
|
||||
|
||||
private void OnItemRemoved(InventoryItem item)
|
||||
{
|
||||
// Очищаем кэши при удалении предмета
|
||||
_useableCache.Clear();
|
||||
_resolver?.ClearCache();
|
||||
}
|
||||
|
||||
[Rpc.Broadcast]
|
||||
public void RpcSetHoldAnimation( CitizenAnimationHelper.HoldTypes HoldType, CitizenAnimationHelper.Hand hand )
|
||||
public void RpcSetHoldAnimation(CitizenAnimationHelper.HoldTypes HoldType, CitizenAnimationHelper.Hand hand)
|
||||
{
|
||||
AnimationHelper.HoldType = HoldType;
|
||||
AnimationHelper.Handedness = hand;
|
||||
}
|
||||
|
||||
|
||||
// AnimationHelper.HoldType = CitizenAnimationHelper.HoldTypes.None;
|
||||
// AnimationHelper.Handedness = CitizenAnimationHelper.Hand.Both;
|
||||
|
||||
|
||||
void InventoryUpdate()
|
||||
{
|
||||
if ( !Network.IsOwner ) return;
|
||||
if (!Network.IsOwner) return;
|
||||
|
||||
// InAds = Input.Down( "Attack2" );
|
||||
|
||||
if ( Input.Pressed( "Attack1" ) )
|
||||
if (Input.Pressed("Attack1"))
|
||||
{
|
||||
if ( UseSystem.TryUse( this ) )
|
||||
if (UseSystem.TryUse(this))
|
||||
{
|
||||
Attack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<IUseable>
|
||||
GetUsables() //Допустим, у джетпака слот Body. Просто дописываешь в GetUsables() Inventar.InventorySlot.Body:
|
||||
public IEnumerable<IUseable> GetUsables()
|
||||
{
|
||||
foreach ( var slot in new[] { Inventar.InventorySlot.LeftHand, Inventar.InventorySlot.RightHand } )
|
||||
// Кэшируем слоты для избежания повторного создания массива
|
||||
var slots = new[] { Inventar.InventorySlot.LeftHand, Inventar.InventorySlot.RightHand };
|
||||
|
||||
foreach (var slot in slots)
|
||||
{
|
||||
if ( !Inventory.EquippedItems.TryGetValue( slot, out var item ) )
|
||||
continue;
|
||||
|
||||
var holder = _resolver.GetSlotObject( slot );
|
||||
var heldObject = holder?.Children.FirstOrDefault();
|
||||
|
||||
if ( heldObject == null )
|
||||
continue;
|
||||
|
||||
if ( _useableCache.TryGetValue( slot, out var cached ) && cached.obj == heldObject )
|
||||
if (!Inventory.EquippedItems.TryGetValue(slot, out var item))
|
||||
{
|
||||
if ( cached.useable != null )
|
||||
continue;
|
||||
}
|
||||
|
||||
var holder = _resolver.GetSlotObject(slot);
|
||||
if (holder == null) continue;
|
||||
|
||||
var heldObject = holder.Children.FirstOrDefault();
|
||||
if (heldObject == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Проверяем кэш
|
||||
if (_useableCache.TryGetValue(slot, out var cached) && cached.obj == heldObject)
|
||||
{
|
||||
if (cached.useable != null)
|
||||
yield return cached.useable;
|
||||
}
|
||||
else
|
||||
@@ -178,7 +199,7 @@ public sealed partial class Dedugan : Component
|
||||
var useable = heldObject.Components.Get<IUseable>();
|
||||
_useableCache[slot] = (heldObject, useable);
|
||||
|
||||
if ( useable != null )
|
||||
if (useable != null)
|
||||
yield return useable;
|
||||
}
|
||||
}
|
||||
@@ -187,7 +208,7 @@ public sealed partial class Dedugan : Component
|
||||
[Rpc.Broadcast]
|
||||
void Attack()
|
||||
{
|
||||
Renderer.Set( "b_attack", true );
|
||||
Renderer.Set("b_attack", true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user