item fixes

This commit is contained in:
dddushesss 2022-01-11 07:02:07 +03:00
parent d7424ed972
commit cac61cfdd7
8 changed files with 31 additions and 33 deletions

View File

@ -14,19 +14,10 @@ MonoBehaviour:
m_EditorClassIdentifier:
items:
- item: {fileID: 11400000, guid: e7adbedb55c5db341a823370b696f709, type: 2}
iconPrefab: {fileID: 3197816592181874056, guid: 2704c4f795b0d7748a3e3fa53be4d893,
type: 3}
icon: {fileID: 21300000, guid: caf8bc0311dd2fc4ca1528a82a063754, type: 3}
_spawnChance: 0.581
_spawnChance: 0
- item: {fileID: 11400000, guid: 62849ddbcd32e834887aac5eb3d98db0, type: 2}
iconPrefab: {fileID: 8639522512577941448, guid: 7b6a7f64e52da514d88aa97ad8f863df,
type: 3}
icon: {fileID: 21300000, guid: 35be128594dcdce48b5d8e5317b38ed9, type: 3}
_spawnChance: 0.67
_spawnChance: 0
- item: {fileID: 11400000, guid: ef628c3158b0ea34bb919ca105507009, type: 2}
iconPrefab: {fileID: 3197816592181874056, guid: f0b68f1afd77d7741a0fe8ff5ba25a77,
type: 3}
icon: {fileID: 21300000, guid: b7771b47a72ca7947bf18f664e53a983, type: 3}
_spawnChance: 0.921
_spawnChance: 1
fromTimeSpawn: 2.93
toTimeSpawn: 10

View File

@ -15,19 +15,20 @@ namespace GameUI
private List<Button> _buttons;
private Button[] _freeButtons;
private Dictionary<Item, Button> _dictionary;
private Dictionary<Button, Item> _dictionary;
public void SetUpUI(int inventoryCapacity)
{
_buttons = new List<Button>();
_dictionary = new Dictionary<Item, Button>();
_dictionary = new Dictionary<Button, Item>();
_freeButtons = new Button[inventoryCapacity];
for (int i = 0; i < inventoryCapacity; i++)
{
var itemGo = Instantiate(item, grid.transform);
var button = itemGo.GetComponentInChildren<Button>();
_buttons.Add(button);
_dictionary.Add(button, null);
button.gameObject.SetActive(false);
}
@ -35,10 +36,10 @@ namespace GameUI
_buttons.ForEach(button => _freeButtons[j++] = button);
}
private void SwitchButton(Item item)
private void SwitchButton(Button button)
{
var button = _dictionary[item];
_dictionary.Remove(item);
var item = _dictionary[button];
button.gameObject.SetActive(false);
for (int i = 0; i < _freeButtons.Length; i++)
{
@ -61,7 +62,7 @@ namespace GameUI
if (button == null)
return;
_dictionary.Add(item, button);
_dictionary[button] = item;
button.gameObject.SetActive(true);
button.image.sprite = item.Icon;
button.onClick.AddListener(() =>
@ -83,7 +84,8 @@ namespace GameUI
break;
}
case Building _building:
_building.Invoke(SwitchButton);
_building.Invoke(() => SwitchButton(button));
OnBuildingInvoked?.Invoke(_building);
break;
}

View File

@ -14,7 +14,7 @@ namespace Items
[SerializeField] private GameObject buildingPrefab;
public void Invoke(Action<Item> action)
public void Invoke(Action action)
{
Unit.UseItem(this);
OnItemUsed += action;
@ -27,7 +27,7 @@ namespace Items
var obj = SpawnHelper.Spawn(buildingPrefab, cell.transform.position + buildingPrefab.transform.position);
obj.GetComponent<TowerView>().SetUp(Unit.Color);
cell.Building = obj.GetComponent<TowerView>();
OnItemUsed?.Invoke(this);
OnItemUsed?.Invoke();
}
}
}

View File

@ -18,7 +18,7 @@ namespace Items
public GameObject IconPrefab => iconPrefab;
protected Unit Unit;
protected Action<Item> OnItemUsed;
protected Action OnItemUsed;
public UnitColor Color => Unit.Color;

View File

@ -69,18 +69,22 @@ namespace Items
private int GetWeightedItemIndex()
{
float randomNum = Random.Range(1, 101)/100f;
List<ItemInfos> possibleTypes = new List<ItemInfos>();
int[] possibleTypes = new int[_data.ItemInfos.Count];
var i = 0;
var j = 0;
_data.ItemInfos.ForEach(item =>
{
if (item.SpawnChance >= randomNum)
{
possibleTypes.Add(item);
possibleTypes[j++] = i;
}
++i;
});
if (possibleTypes.Count > 0)
if (j > 0)
{
return Random.Range(0, possibleTypes.Count - 1);
return possibleTypes[Random.Range(0, j - 1)];
}
return -1;

View File

@ -8,7 +8,8 @@ namespace Items
public class ItemView : MonoBehaviour
{
private Item _item;
public Item Item => _item;
public void SetUp(Item item)
{
@ -16,11 +17,11 @@ namespace Items
Rotate();
}
public Item PickUp(Unit unit)
public ItemView PickUp(Unit unit)
{
transform.DOKill();
_item.PickUp(unit);
return _item;
return this;
}
private void Rotate()

View File

@ -222,11 +222,11 @@ namespace Units
UpdateBarCanvas();
}
public bool PickUpItem(ItemView itemView)
public bool PickUpItem(Item item)
{
if (_inventory.Count < _data.inventoryCapacity)
{
var item = itemView.PickUp(this);
item.PickUp(this);
_inventory.Add(item);
OnItemPickUp?.Invoke(item);
return true;

View File

@ -144,7 +144,7 @@ public class UnitView : MonoBehaviour
ItemView itemView = other.GetComponent<ItemView>();
if (itemView != null && _unit.PickUpItem(itemView))
if (itemView != null && _unit.PickUpItem(itemView.Item))
{
Destroy(itemView.gameObject);
}