public void AddUnit(UnitView unitView, Vector2Int tileXY, int sortOrder, RectInt localBounds)
        {
            UnitMapState state = new UnitMapState(unitView, tileXY, sortOrder, localBounds);

            RectInt worldBounds = new RectInt();

            worldBounds.min = tileXY + localBounds.min;
            worldBounds.max = tileXY + localBounds.max;

            // Add to list
            m_Units.Add(state);

            // Add to map
            for (int x = worldBounds.min.x; x < worldBounds.max.x; ++x)
            {
                if (x < 0 || x >= m_UnitMap.GetLength(0))
                {
                    continue;
                }

                for (int y = worldBounds.min.y; y < worldBounds.max.y; ++y)
                {
                    if (y < 0 || y >= m_UnitMap.GetLength(1))
                    {
                        continue;
                    }
                    m_UnitMap[x, y].Add(state);
                    m_UnitMap[x, y].Sort((a, b) => a.sortOrder.CompareTo(b.sortOrder));
                }
            }

            // Refresh minimap
            RefreshMinimapTiles(worldBounds.min, worldBounds.max);
        }
 public UnitMapState(UnitView view, Vector2Int currentTileXY, int sortOrder, RectInt localBounds)
 {
     this.view          = view;
     this.currentTileXY = currentTileXY;
     this.sortOrder     = sortOrder;
     this.localBounds   = localBounds;
 }
        public void MoveUnit(UnitView unitView, Vector2Int tileXY)
        {
            UnitMapState state = _RemoveUnit(unitView);

            if (state != null)
            {
                AddUnit(unitView, tileXY, state.sortOrder, state.localBounds);
            }
        }
Beispiel #4
0
        private GameObject CreateUnit(string resourcePath, Transform parent, int id, Vector2Int gridPosition)
        {
            GameObject goUnit = Instantiate(Resources.Load <GameObject>("Game/" + resourcePath));

            goUnit.transform.SetParent(parent);
            goUnit.transform.localScale = Vector3.one;
            UnitView unit = goUnit.GetComponent <UnitView>();

            unit.Initialize(m_Tilemap, this);
            unit.SetPosition(gridPosition);

            return(goUnit);
        }
        private UnitMapState _RemoveUnit(UnitView unitView)
        {
            // Find unit in list
            int index = m_Units.FindIndex((state2) => state2.view == unitView);

            if (index < 0)
            {
                return(null);
            }

            UnitMapState state = m_Units[index];

            // Remove from list
            m_Units.RemoveAt(index);

            RectInt worldBounds = new RectInt();

            worldBounds.min = state.currentTileXY + state.localBounds.min;
            worldBounds.max = state.currentTileXY + state.localBounds.max;

            // Remove from map
            for (int x = worldBounds.min.x; x < worldBounds.max.x; ++x)
            {
                if (x < 0 || x >= m_UnitMap.GetLength(0))
                {
                    continue;
                }

                for (int y = worldBounds.min.y; y < worldBounds.max.y; ++y)
                {
                    if (y < 0 || y >= m_UnitMap.GetLength(1))
                    {
                        continue;
                    }
                    m_UnitMap[x, y].Remove(state);
                }
            }


            // Refresh minimap
            RefreshMinimapTiles(worldBounds.min, worldBounds.max);

            return(state);
        }
        /// <summary>
        /// Sets a unit overlay.
        /// </summary>
        public void SetOverlay(UnitView overlayView, Vector2Int sizeInTiles, Vector2 minTileOffset)
        {
            // Remove previous overlay
            if (overlayView != m_UnitView && m_UnitView != null)
            {
                Destroy(m_UnitView.gameObject);
            }

            ClearStatusOverlay();

            // Disable sprite overlay
            m_SpriteOverlay.enabled = false;

            // Initialize view for overlay
            overlayView.transform.SetParent(transform);
            overlayView.SetCanShowTextOverlay(false);

            m_UnitView = overlayView;

            // Initialize tile status overlay
            m_StatusTiles         = new SpriteRenderer[sizeInTiles.x, sizeInTiles.y];
            m_StatusTileContainer = new GameObject("StatusTileContainer");
            m_StatusTileContainer.transform.SetParent(transform);

            minTileOffset.y *= -1;

            for (int x = 0; x < sizeInTiles.x; ++x)
            {
                for (int y = 0; y < sizeInTiles.y; ++y)
                {
                    m_StatusTiles[x, y] = Instantiate(m_StatusTilePrefab).GetComponent <SpriteRenderer>();
                    m_StatusTiles[x, y].transform.SetParent(m_StatusTileContainer.transform);
                    m_StatusTiles[x, y].transform.localPosition = new Vector3(m_StatusTiles[x, y].sprite.rect.width * x, m_StatusTiles[x, y].sprite.rect.height * -y) + (Vector3)minTileOffset;
                }
            }
        }
 public void AddUnit(UnitView unitView, Vector2Int tileXY, int sortOrder)
 {
     AddUnit(unitView, tileXY, sortOrder, new RectInt(0, 0, 1, 1));
 }
 public void RemoveUnit(UnitView unitView)
 {
     _RemoveUnit(unitView);
 }