using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlockController : MonoBehaviour { //block for instantiate [SerializeField] private GameObject _characterBlock; [SerializeField] private GameObject _bgBlock; [SerializeField] private RectTransform _bgRoot; [SerializeField] private RectTransform _characterRoot; public E_TEAM CurrentTurn { get; private set; } = E_TEAM.BLUE; private BackgroundBlock[,] _mapPositions; private CharacterBlock _selectedCharacter; private Vector2 _blockSize; public Vector2 BlockSize { get { return _blockSize; } } public CharacterBlock GenerateCharacter(CharacterInfo info,int x, int y) { info.UpdatePosition(x, y); var obj = Instantiate(_characterBlock, transform); var block = obj.GetComponent(); block.SetData(info, _blockSize); block.UpdatePosition(_mapPositions[x, y].transform.position, x, y); return block; } public void Selected(CharacterBlock block) { if(block.Team != CurrentTurn) { return; } //ÀÌ¹Ì Ä¥ÇØÁø ¹üÀ§ Ä÷¯¸¦ ÃʱâÈ­ ResetBackground(); //ÇöÀç ¼±ÅÃµÈ Ä³¸¯ÅÍ¿Í °°À¸¸é ÇöÀç ij¸¯ÅÍ ¾ø¾ÖÁÜ if(_selectedCharacter == block) { _selectedCharacter = null; return; } else { _selectedCharacter = block; } SetBackground(block.CurrentX, block.CurrentY, block.Range); } public void Move(int x, int y) { CurrentTurn = CurrentTurn == E_TEAM.BLUE ? E_TEAM.RED : E_TEAM.BLUE; //¹è°æ ºí·° ¹Þ¾Æ¿È //ÇöÀç ¼±ÅÃµÈ Ä³¸¯Å͸¦ ÇØ´ç ÁÂÇ¥·Î À̵¿½ÃÅ°°í ResetBackground(); //¸ðµç Ä¥ÇØÁø ¹è°æ Ä÷¯ ÃʱâÈ­ _selectedCharacter.UpdatePosition(_mapPositions[x, y].transform.position, x, y); } public void GenerateMap(int x, int y, float padding) { _mapPositions = new BackgroundBlock[x, y]; float width = _bgRoot.rect.width - (padding * (x - 1)); float height = _bgRoot.rect.height - (padding * (y - 1)); _blockSize = new Vector2(width / x, height / y); float xPos = 0; float yPos = 0; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { GameObject obj = Instantiate(_bgBlock, _bgRoot.transform); var block = obj.GetComponent(); block.SetData(j, i); block.SetSize(_blockSize); block.UpdatePosition(new Vector2(xPos, yPos)); _mapPositions[j, i] = block; xPos += _blockSize.x + padding; } yPos += _blockSize.y + padding; xPos = 0; } } private void ResetBackground() { for (int i = 0; i < _mapPositions.GetLength(0); i++) { for (int j = 0; j < _mapPositions.GetLength(1); j++) { if(_mapPositions[i, j].InRage) { _mapPositions[i, j].ResetColor(); } } } } private void SetBackground(int x, int y, int range) { //¹üÀ§ ¾È¿¡ ÀÖÀ¸¸é Ä÷¯ º¯°æ for(int i = -range; i <= range; i++) { for (int j = -range; j <= range; j++) { //Àý´ë°ªÀÇ ÇÕÀÌ rangeÀÎ °æ¿ì? int dx = i < 0 ? i * -1 : i; int dy = j < 0 ? j * -1 : j; if((dx + dy) <= range) { if(x + i >= 0 && x + i < _mapPositions.GetLength(0) && y + j >= 0 && y + j < _mapPositions.GetLength(1)) { _mapPositions[x + i, y + j].SetColor(CurrentTurn); } } } } } }