fixed item despawn

This commit is contained in:
dddushesss 2022-02-14 04:52:54 +03:00
parent 9ca0f33ceb
commit 76e280dfad
11 changed files with 51 additions and 40 deletions

View File

@ -15,3 +15,4 @@ MonoBehaviour:
icon: {fileID: 21300000, guid: c455216bc89f6c54d90211b86a5cf0e6, type: 3}
type: 0
buildingPrefab: {fileID: 9125080148727291347, guid: 5663dbcf605325241b40bc2afd5b6647, type: 3}
isVisiting: 1

View File

@ -124,7 +124,7 @@ namespace AI
var item = GetNearestItem(agent);
if (((item.dist > 0 && item.dist <= _data.DistaceToCollectBonus) ||
agent.Unit.Mana <= agent.Unit.Data.maxMana * _data.ManaPercentToCollectBonus) &&
(item.hex.Item.Type == ItemType.DEFENCE
(item.hex.Item.Item.Type == ItemType.DEFENCE
? agent.Unit.InventoryDefence.Count
: agent.Unit.Inventory.Count) < agent.Unit.InventoryCapacity / 2)
{

View File

@ -5,6 +5,7 @@ using System.Linq;
using Data;
using DefaultNamespace;
using Items;
using Items.ItemViews;
using Sirenix.OdinInspector;
using UnityEngine;
@ -19,7 +20,7 @@ namespace HexFiled
[HideInInspector] public GameObject BuildingInstance;
[SerializeField] private HexCell[] neighbors;
[SerializeField] private Item _item;
[SerializeField] private ItemView _item;
[SerializeField, AssetsOnly] public GameObject Building;
@ -37,7 +38,7 @@ namespace HexFiled
}
}
public Item Item
public ItemView Item
{
get => _item;
set => _item = value;

View File

@ -23,18 +23,12 @@ namespace Items
[SerializeField] private GameObject usisngVFX;
public BonusType BonusType => bonusType;
public int Value => value;
public GameObject UsisngVFX => usisngVFX;
public override void PickUp(Unit unit)
{
if(bonusType != BonusType.Heal)
base.PickUp(unit);
else
{
VFXController.Instance.PlayEffect(usisngVFX, Unit.Instance.transform);
Unit.UnitView.OnHit.Invoke(-value);
Despawn();
}
}
public void Invoke(Unit unit)
{

View File

@ -39,35 +39,22 @@ namespace Items
public Sprite Icon => icon;
protected Unit Unit;
protected Action<Unit> OnItemUsed;
public UnitColor Color => Unit.Color;
public GameObject Spawn(HexCell cell, GameObject parent, GameObject iconPrefab)
{
_instance = GameObject.Instantiate(iconPrefab, cell.transform.position + new Vector3(0, 1, 0),
Quaternion.identity, parent.transform);
_instance.AddComponent<ItemView>().SetUp(this);
cell.Item = this;
var view = _instance.AddComponent<ItemView>();
view.SetUp(this);
cell.Item = view;
_instance.AddComponent<CapsuleCollider>().isTrigger = true;
return _instance;
}
public virtual void PickUp(Unit unit)
{
_instance.transform.DOMove(unit.UnitView.transform.position, 0.1f).OnComplete(() =>
{
unit.PickUpItem(this);
Despawn();
});
}
public void Despawn()
{
Destroy(_instance.gameObject);
}
public void Dispose()
{

View File

@ -49,7 +49,7 @@ namespace Items
{
cellByColor.Value.ForEach(x =>
{
if (x.Building != null)
if (x.Building != null && x.Item != null)
{
closedList.Add(x);
}

View File

@ -1,4 +1,6 @@
using DG.Tweening;
using DefaultNamespace;
using DG.Tweening;
using Units;
using UnityEngine;
namespace Items.ItemViews
@ -19,10 +21,28 @@ namespace Items.ItemViews
Rotate();
}
private void OnDestroy()
public void PickUp(Unit unit)
{
if (_item is Bonus { BonusType: BonusType.Heal } bonus)
{
VFXController.Instance.PlayEffect(bonus.UsisngVFX, unit.Instance.transform);
unit.UnitView.OnHit.Invoke(-bonus.Value);
Despawn();
return;
}
transform.DOMove(unit.UnitView.transform.position, 0.1f).OnComplete(() =>
{
unit.PickUpItem(_item);
Despawn();
});
}
public void Despawn()
{
transform.DOKill();
Destroy(gameObject);
}
private void Rotate()
{

View File

@ -64,6 +64,11 @@ namespace Items
public void UseAbility(Unit unit, Unit chosenUnit)
{
if (unit == null)
{
DeAim();
return;
}
unit.UseItem(this);
DeAim();
OnItemUsed?.Invoke(unit);

View File

@ -135,7 +135,8 @@ namespace Units
&& value.cell.Equals(_cell.GetNeighbor(direction)))) return;
if (_cell.GetNeighbor(direction).Color == _data.color || _cell.GetNeighbor(direction).Color == _easyCaptureColor)
if (_cell.GetNeighbor(direction).Color == _data.color ||
(_cell.GetNeighbor(direction).Color == _easyCaptureColor && _easyCaptureColor != UnitColor.Grey))
{
DoTransit(direction);
}
@ -211,10 +212,11 @@ namespace Units
{
_mana -= _hexGrid.HexCaptureCost;
}
UnitView.RegenMana();
}
UnitView.RegenMana();
UpdateBarCanvas();
IsBusy = false;
IsHardToCapture = false;

View File

@ -177,7 +177,7 @@ public class UnitView : MonoBehaviour
if (itemView == null || itemView.pickedUp || !_unit.CanPickUpItem(itemView.Item)) return;
itemView.pickedUp = true;
itemView.Item.PickUp(_unit);
itemView.PickUp(Unit);
ItemFabric.Items.Remove(itemView.gameObject);
}

File diff suppressed because one or more lines are too long