From 896a1dae28045d5e6004b1aed3c45780093acc00 Mon Sep 17 00:00:00 2001 From: dddushesss <37773701+dddushesss@users.noreply.github.com> Date: Mon, 31 Jan 2022 19:29:45 +0300 Subject: [PATCH] fixed despawn items. added ISetUp.cs for building views --- Assets/{ => Scripts}/CheatMenu.cs | 0 Assets/{ => Scripts}/CheatMenu.cs.meta | 0 Assets/Scripts/HexFiled/HexCell.cs | 21 ++++++++++++-------- Assets/Scripts/HexFiled/HexGrid.cs | 12 +++++------ Assets/Scripts/HexFiled/PaintedController.cs | 13 +----------- Assets/Scripts/Items/Building.cs | 6 +++--- Assets/Scripts/Items/ISetUp.cs | 9 +++++++++ Assets/Scripts/Items/ISetUp.cs.meta | 3 +++ Assets/Scripts/Items/Item.cs | 4 +++- Assets/Scripts/Items/ItemFabric.cs | 2 +- Assets/Scripts/Items/ItemViews/BombView.cs | 2 +- Assets/Scripts/Items/ItemViews/TowerView.cs | 11 ++++++---- Assets/Scripts/Units/Unit.cs | 1 + Assets/Scripts/Units/UnitFactory.cs | 4 ++-- 14 files changed, 50 insertions(+), 38 deletions(-) rename Assets/{ => Scripts}/CheatMenu.cs (100%) rename Assets/{ => Scripts}/CheatMenu.cs.meta (100%) create mode 100644 Assets/Scripts/Items/ISetUp.cs create mode 100644 Assets/Scripts/Items/ISetUp.cs.meta diff --git a/Assets/CheatMenu.cs b/Assets/Scripts/CheatMenu.cs similarity index 100% rename from Assets/CheatMenu.cs rename to Assets/Scripts/CheatMenu.cs diff --git a/Assets/CheatMenu.cs.meta b/Assets/Scripts/CheatMenu.cs.meta similarity index 100% rename from Assets/CheatMenu.cs.meta rename to Assets/Scripts/CheatMenu.cs.meta diff --git a/Assets/Scripts/HexFiled/HexCell.cs b/Assets/Scripts/HexFiled/HexCell.cs index e167fd88..7a741794 100644 --- a/Assets/Scripts/HexFiled/HexCell.cs +++ b/Assets/Scripts/HexFiled/HexCell.cs @@ -15,13 +15,21 @@ namespace HexFiled [SerializeField] private HexCell[] neighbors; - private Item _item; + [SerializeField] private Item _item; private UnitColor _color; private MeshRenderer _renderer; public UnitColor Color => _color; - public Item Item => _item; + public Item Item + { + get => _item; + set + { + if (_item == null) + _item = value; + } + } private GameObject _building; public GameObject Building @@ -50,10 +58,7 @@ namespace HexFiled } } - public void SetItem(Item item) - { - _item = item == _item ? null : item; - } + public List GetListNeighbours() @@ -87,14 +92,14 @@ namespace HexFiled HexManager.CellByColor[previousColor].Remove(this); _color = color; HexManager.CellByColor[_color].Add(this); - + OnHexPainted?.Invoke(this); if(color == UnitColor.Grey) return; var vfx = VFXController.Instance.PlayEffect(HexGrid.Colors[color].VFXCellCapturePrefab, transform.position + new Vector3(0,0.1f,0)); MusicController.Instance.AddAudioSource(vfx); MusicController.Instance.PlayRandomClip(MusicController.Instance.MusicData.SfxMusic.Captures, vfx); - OnHexPainted?.Invoke(this); + } } } \ No newline at end of file diff --git a/Assets/Scripts/HexFiled/HexGrid.cs b/Assets/Scripts/HexFiled/HexGrid.cs index 2a210e25..7b52ae65 100644 --- a/Assets/Scripts/HexFiled/HexGrid.cs +++ b/Assets/Scripts/HexFiled/HexGrid.cs @@ -123,12 +123,12 @@ namespace HexFiled } -// #if UNITY_EDITOR -// TMP_Text label = Object.Instantiate(_fieldData.cellLabelPrefab, _gridCanvas.transform, false); -// label.rectTransform.anchoredPosition = -// new Vector2(position.x, position.z); -// label.text = cell.coordinates.ToStringOnSeparateLines(); -// #endif +#if UNITY_EDITOR + TMP_Text label = Object.Instantiate(_fieldData.cellLabelPrefab, _gridCanvas.transform, false); + label.rectTransform.anchoredPosition = + new Vector2(position.x, position.z); + label.text = cell.coordinates.ToStringOnSeparateLines(); +#endif } diff --git a/Assets/Scripts/HexFiled/PaintedController.cs b/Assets/Scripts/HexFiled/PaintedController.cs index 14d6b9a3..0a1008e9 100644 --- a/Assets/Scripts/HexFiled/PaintedController.cs +++ b/Assets/Scripts/HexFiled/PaintedController.cs @@ -17,18 +17,7 @@ namespace HexFiled HexManager.UnitCurrentCell = new Dictionary(); } - public void PaintOnDeath(Unit unit) - { - HexManager.PaintHexList(HexManager.CellByColor[unit.Color], UnitColor.Grey); -// #if UNITY_EDITOR -// -// -// if (HexManager.UnitCurrentCell.Count == 1) -// { -// SceneManager.LoadScene(1); -// } -// #endif - } + public void CheckDeathOrDestroy(HexCell cell) { HexManager.UnitCurrentCell diff --git a/Assets/Scripts/Items/Building.cs b/Assets/Scripts/Items/Building.cs index 1733c57a..f05ab74a 100644 --- a/Assets/Scripts/Items/Building.cs +++ b/Assets/Scripts/Items/Building.cs @@ -20,10 +20,10 @@ namespace Items public void PlaceItem(HexCell cell) { Unit.UseItem(this); - var obj = GameObject.Instantiate(buildingPrefab, + var obj = Instantiate(buildingPrefab, cell.transform.position + buildingPrefab.transform.position, Quaternion.identity); - obj.GetComponent()?.SetUp(Unit.Color); - obj.GetComponent()?.SetUp(Unit); + obj.GetComponent().SetUp(Unit); + cell.Building = obj; OnItemUsed.Invoke(); OnItemUsed = null; diff --git a/Assets/Scripts/Items/ISetUp.cs b/Assets/Scripts/Items/ISetUp.cs new file mode 100644 index 00000000..e969d0af --- /dev/null +++ b/Assets/Scripts/Items/ISetUp.cs @@ -0,0 +1,9 @@ +using Units; + +namespace Items +{ + public interface ISetUp + { + public void SetUp(Unit unit); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Items/ISetUp.cs.meta b/Assets/Scripts/Items/ISetUp.cs.meta new file mode 100644 index 00000000..c902c1e5 --- /dev/null +++ b/Assets/Scripts/Items/ISetUp.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 70c0a37f5f0f4fd3be4c467b404cfa82 +timeCreated: 1643646005 \ No newline at end of file diff --git a/Assets/Scripts/Items/Item.cs b/Assets/Scripts/Items/Item.cs index 800bde5c..eb51c92c 100644 --- a/Assets/Scripts/Items/Item.cs +++ b/Assets/Scripts/Items/Item.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using DefaultNamespace; using HexFiled; using Units; @@ -30,7 +31,7 @@ namespace Items [SerializeField] private Sprite icon; [SerializeField] private bool isInvokeOnPickUp = false; [SerializeField] private ItemType type; - + public ItemType Type => type; public bool IsInvokeOnPickUp => isInvokeOnPickUp; @@ -46,6 +47,7 @@ namespace Items { _instance = GameObject.Instantiate(iconPrefab, cell.transform.position + new Vector3(0, 1, 0),Quaternion.identity, parent.transform); _instance.AddComponent().SetUp(this); + cell.Item = this; _instance.AddComponent().isTrigger = true; return _instance; } diff --git a/Assets/Scripts/Items/ItemFabric.cs b/Assets/Scripts/Items/ItemFabric.cs index dfbb23aa..5c51f3f3 100644 --- a/Assets/Scripts/Items/ItemFabric.cs +++ b/Assets/Scripts/Items/ItemFabric.cs @@ -85,7 +85,7 @@ namespace Items var item = _data.ItemInfos[i].Item; Items.Add(item.Spawn(cell, _itemParrant, itemIcon[item.Type]), cell); - cell.SetItem(_data.ItemInfos[i].Item); + _spawnTime = Random.Range(_data.SpawnTime.from, _data.SpawnTime.to); } } diff --git a/Assets/Scripts/Items/ItemViews/BombView.cs b/Assets/Scripts/Items/ItemViews/BombView.cs index e8d290a4..5566763c 100644 --- a/Assets/Scripts/Items/ItemViews/BombView.cs +++ b/Assets/Scripts/Items/ItemViews/BombView.cs @@ -6,7 +6,7 @@ using UnityEngine; namespace Items { - public class BombView : MonoBehaviour + public class BombView : MonoBehaviour, ISetUp { [SerializeField] private int damage; [SerializeField] private GameObject hit; diff --git a/Assets/Scripts/Items/ItemViews/TowerView.cs b/Assets/Scripts/Items/ItemViews/TowerView.cs index 1eacf339..718fdaf3 100644 --- a/Assets/Scripts/Items/ItemViews/TowerView.cs +++ b/Assets/Scripts/Items/ItemViews/TowerView.cs @@ -5,11 +5,12 @@ using System.Linq; using DefaultNamespace; using DG.Tweening; using HexFiled; +using Items; using Units; using UnityEngine; using Weapons; -public class TowerView : MonoBehaviour +public class TowerView : MonoBehaviour, ISetUp { [Serializable] internal struct Crystal @@ -28,11 +29,11 @@ public class TowerView : MonoBehaviour public UnitColor Color => _color; - public void SetUp(UnitColor unitColor) + public void SetUp(Unit unit) { - _color = unitColor; + _color = unit.Color; - crystals.First(x => x.UnitColor == unitColor).GameObject.SetActive(true); + crystals.First(x => x.UnitColor == (unit.Color)).GameObject.SetActive(true); var capsule = gameObject.AddComponent(); capsule.radius = weapon.disnatce * HexGrid.HexDistance; } @@ -71,4 +72,6 @@ public class TowerView : MonoBehaviour weapon.Fire(transform, new Vector2(direction.x, direction.z)); } } + + } \ No newline at end of file diff --git a/Assets/Scripts/Units/Unit.cs b/Assets/Scripts/Units/Unit.cs index 340aa648..060ffd5c 100644 --- a/Assets/Scripts/Units/Unit.cs +++ b/Assets/Scripts/Units/Unit.cs @@ -329,6 +329,7 @@ namespace Units _unitView.OnHit -= Damage; _isAlive = false; _isBusy = true; + HexManager.PaintHexList(HexManager.CellByColor[Color], UnitColor.Grey); HexManager.UnitCurrentCell.Remove(Color); _animator.SetTrigger("Death"); var vfx = VFXController.Instance.PlayEffect(HexGrid.Colors[Color].VFXDeathPrefab, diff --git a/Assets/Scripts/Units/UnitFactory.cs b/Assets/Scripts/Units/UnitFactory.cs index 98f01505..4f1111be 100644 --- a/Assets/Scripts/Units/UnitFactory.cs +++ b/Assets/Scripts/Units/UnitFactory.cs @@ -63,7 +63,7 @@ namespace Chars player.OnDeath += unit => _uiController.CheatMenu.OnPlayerDeath(); player.OnDeath += p => _uiController.AdsMob.ShowCanvas(unitInfo, this); - player.OnDeath += _paintedController.PaintOnDeath; + player.Spawn(unitInfo.spawnPos); } else @@ -75,7 +75,7 @@ namespace Chars AIAgent agent = new AIAgent(unitInfo, enemy); //enemy.OnPlayerSpawned += x => _controllers.Add(agent); enemy.OnDeath += x => { _controllers.Remove(agent); }; - enemy.OnDeath += _paintedController.PaintOnDeath; + enemy.Spawn(unitInfo.spawnPos); } }