예제 #1
0
        private static void AddPointCollider(GameObject go, CollisionObject collision, SuperImportContext importContext)
        {
            var xpos = importContext.MakeScalar(collision.m_Position.x);
            var ypos = importContext.MakeScalar(collision.m_Position.y);

            go.transform.localPosition    = new Vector3(xpos, ypos);
            go.transform.localEulerAngles = new Vector3(0, 0, importContext.MakeRotation(collision.m_Rotation));

            go.AddComponent <SuperColliderComponent>();
        }
        public static void UpdateProperties(this SuperMap superMap, SuperImportContext importContext)
        {
            var cellSize = superMap.CalculateCellSize();

            // The cell size has to take pixels per unit into account for x and y
            // z is always 1 so we can use Transparency Sort Axis
            float x = importContext.MakeScalar(cellSize.x);
            float y = importContext.MakeScalar(cellSize.y);
            float z = 1.0f;

            superMap.CellSize = new Vector3(x, y, z);
        }
        private static void AddBoxCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            var box = go.AddComponent <BoxCollider2D>();

            box.offset = importContext.MakePointPPU(collision.m_Size.x, -collision.m_Size.y) * 0.5f;
            box.size   = importContext.MakeSize(collision.m_Size);

            var xpos = importContext.MakeScalar(collision.m_Position.x);
            var ypos = importContext.MakeScalar(collision.m_Position.y);

            go.transform.localPosition    = new Vector3(xpos, ypos);
            go.transform.localEulerAngles = new Vector3(0, 0, importContext.MakeRotation(collision.m_Rotation));

            go.AddComponent <SuperColliderComponent>();
        }
예제 #4
0
        public void PlaceTileColliders(SuperMap map, SuperTile tile, TileIdMath tileId, Vector3Int pos)
        {
            Assert.IsNotNull(m_Tilemap, "Need a Tilemap component if we are going to gather tile colliders");

            // Tile y position is always off by one
            pos.y++;

            // Do we have any collider objects defined for this tile?
            if (!tile.m_CollisionObjects.IsEmpty())
            {
                var polygons = AcquireTilePolygonCollection(tile, tileId);

                float cell_w     = m_Tilemap.cellSize.x;
                float cell_h     = m_Tilemap.cellSize.y;
                float halfCell_w = m_Tilemap.cellSize.x * 0.5f;
                float halfCell_h = m_Tilemap.cellSize.y * 0.5f;

                foreach (var poly in polygons.Polygons)
                {
                    // Offset the polygon so that it is in the location of the tile
                    var tileHeight = m_ImportContext.MakeScalar(tile.m_Height);
                    var tileDiff   = m_Tilemap.cellSize.y - tileHeight;

                    var offset = Vector2.zero;

                    // Our offset depends on map orientation. Isometric is such a pain in the ass.
                    if (map.m_Orientation == MapOrientation.Isometric)
                    {
                        var x = (pos.x - pos.y) * halfCell_w;
                        var y = (pos.x + pos.y) * halfCell_h;
                        offset = new Vector2(x + halfCell_w, y - tileDiff);
                    }
                    else
                    {
                        offset = new Vector2(pos.x * cell_w, pos.y * cell_h - tileDiff);
                    }

                    var points = poly.Points.Select(pt => pt + offset).ToArray();

                    CollisionClipperKey key = poly.MakeKey();
                    CollisionClipper    clipper;
                    if (!m_CollisionClippers.TryGetValue(key, out clipper))
                    {
                        // Add a new clipper for the layer
                        clipper = new CollisionClipper();
                        m_CollisionClippers.Add(key, clipper);
                    }

                    // Add the path to our clipper
                    if (poly.IsClosed)
                    {
                        clipper.AddClosedPath(points);
                    }
                    else
                    {
                        clipper.AddOpenPath(points);
                    }
                }
            }
        }
        public static void SetWorldPosition(this SuperLayer layer, SuperMap map, SuperImportContext context)
        {
            // Accumlate positions up the tree
            Vector3 position_w = new Vector3();

            foreach (var parent in layer.gameObject.GetComponentsInParent <SuperLayer>())
            {
                position_w   += (Vector3)context.MakePoint(parent.m_OffsetX, parent.m_OffsetY);
                position_w.z += parent.gameObject.GetSuperPropertyValueFloat(StringConstants.Unity_ZPosition, 0);
            }

            // Add an additional offset if our tileset is present. These coordinates have already been transformed.
            if (layer is SuperTileLayer || layer.GetComponent <Tilemap>() != null || layer.GetComponent <SuperTilesAsObjectsTilemap>() != null)
            {
                position_w += context.TilemapOffset;
            }

            layer.transform.position = position_w;

            // This sucks but we have to correct for isometric orientation for image layers
            if (layer is SuperImageLayer && map.m_Orientation == MapOrientation.Isometric)
            {
                float dx = context.MakeScalar(map.m_Height * map.m_TileHeight);
                layer.transform.Translate(-dx, 0, 0);
            }
        }
        private GameObject ProcessImageLayer(GameObject goParent, XElement xLayer)
        {
            Assert.IsNotNull(xLayer);
            Assert.IsNotNull(goParent);

            // Create the game object that contains the layer and add it to the grid parent
            var layerComponent = goParent.AddSuperLayerGameObject <SuperImageLayer>(new SuperImageLayerLoader(xLayer), SuperImportContext);
            var goLayer        = layerComponent.gameObject;

            // This sucks but we have to correct for isometric orientation for image layers
            if (m_MapComponent.m_Orientation == MapOrientation.Isometric)
            {
                float dx = SuperImportContext.MakeScalar(m_MapComponent.m_Height * m_MapComponent.m_TileHeight);
                goLayer.transform.Translate(-dx, 0, 0);
            }

            AddSuperCustomProperties(goLayer, xLayer.Element("properties"));

            m_LayerSorterHelper.SortNewLayer(layerComponent);

            var xImage = xLayer.Element("image");

            if (xImage != null)
            {
                var source = xImage.GetAttributeAs <string>("source");
                layerComponent.m_ImageFilename = source;

                var tex2d = RequestAssetAtPath <Texture2D>(source);
                if (tex2d == null)
                {
                    // Texture was not found yet so report the error to the importer UI and bail
                    ReportError("Missing texture asset for image layer: {0}", source);
                }
                else
                {
                    // Create a sprite for the image
                    try
                    {
                        var sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 1.0f), SuperImportContext.Settings.PixelsPerUnit);
                        SuperImportContext.AddObjectToAsset("_sprite", sprite);

                        var renderer = goLayer.AddComponent <SpriteRenderer>();
                        renderer.sprite = sprite;
                        renderer.color  = new Color(1, 1, 1, layerComponent.CalculateOpacity());
                        AssignMaterial(renderer);
                        AssignSortingLayer(renderer, layerComponent.m_SortingLayerName, layerComponent.m_SortingOrder);
                    }
                    catch (Exception e)
                    {
                        ReportError("Error creating sprite '{0}' for image layer '{1}'\n{2}", source, layerComponent.m_TiledName, e.Message);
                    }
                }
            }

            return(goLayer);
        }
        private void CalculateTransform()
        {
            Matrix4x4 matTileOffset = Matrix4x4.Translate(m_ImportContext.MakePoint(m_Tile.m_TileOffsetX, m_Tile.m_TileOffsetY));

            if (!m_TileId.HasFlip)
            {
                m_Transform = matTileOffset;
                return;
            }

            var tileCenter = m_ImportContext.MakeSize(m_Tile.m_Width, -m_Tile.m_Height) * 0.5f;

            Matrix4x4 matTransIn  = Matrix4x4.identity;
            Matrix4x4 matFlip     = Matrix4x4.identity;
            Matrix4x4 matTransOut = Matrix4x4.identity;

            // Go to the tile center
            matTransIn = Matrix4x4.Translate(-tileCenter);

            // Do the flips
            if (m_TileId.HasHorizontalFlip)
            {
                matFlip *= HorizontalFlipMatrix;
            }

            if (m_TileId.HasVerticalFlip)
            {
                matFlip *= VerticalFlipMatrix;
            }

            if (m_TileId.HasDiagonalFlip)
            {
                matFlip *= DiagonalFlipMatrix;
            }

            // Go out of the tile center
            if (!m_TileId.HasDiagonalFlip)
            {
                matTransOut = Matrix4x4.Translate(tileCenter);
            }
            else
            {
                float diff = m_ImportContext.MakeScalar(m_Tile.m_Height - m_Tile.m_Width) * 0.5f;
                tileCenter.x += diff;
                tileCenter.y -= diff;
                matTransOut   = Matrix4x4.Translate(tileCenter);
            }

            // Put it all together
            Matrix4x4 mat = matTileOffset * matTransOut * matFlip * matTransIn;

            // Remember our transformation matrix
            m_Transform = mat;
        }
        private static void AddEllipseCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            // Add a circle collider if width == height. Otherwise, we have to use am approximate polygon representation.
            if (collision.m_Size.x == collision.m_Size.y)
            {
                var cirlce = go.AddComponent <CircleCollider2D>();
                cirlce.offset = importContext.MakePointPPU(collision.m_Size.x, -collision.m_Size.y) * 0.5f;
                cirlce.radius = importContext.MakeScalar(collision.m_Size.x) * 0.5f;

                var xpos = importContext.MakeScalar(collision.m_Position.x);
                var ypos = importContext.MakeScalar(collision.m_Position.y);
                go.transform.localPosition    = new Vector3(xpos, ypos);
                go.transform.localEulerAngles = new Vector3(0, 0, importContext.MakeRotation(collision.m_Rotation));

                go.AddComponent <SuperColliderComponent>();
            }
            else
            {
                AddPolygonCollider(go, collision, tile, importContext);
            }
        }
        private static void AddEdgeCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            var edge = go.AddComponent <EdgeCollider2D>();

            edge.points = importContext.MakePoints(collision.Points);

            float height = importContext.MakeScalar(tile.m_Height);

            go.transform.localPosition = new Vector3(0, height, 0);

            go.AddComponent <SuperColliderComponent>();
        }
        private Tilemap GetOrAddTilemapComponent(GameObject go)
        {
            if (RendererSorter.IsUsingGroups())
            {
                // If we have a group layer parent then use it instead as we are grouping tiles on the same tilemap (using the z-component of the tile location)
                var grouping = go.GetComponentInParent <SuperGroupLayer>();
                if (grouping != null)
                {
                    // The Tilemap will go onto the group layer
                    go = grouping.gameObject;
                }
            }

            // If we already have a Tilemap component then use it
            var tilemap = go.GetComponent <Tilemap>();

            if (tilemap != null)
            {
                return(tilemap);
            }

            tilemap                    = go.AddComponent <Tilemap>();
            tilemap.tileAnchor         = Vector2.zero;
            tilemap.animationFrameRate = SuperImportContext.Settings.AnimationFramerate;

            if (m_MapComponent.m_Orientation == MapOrientation.Hexagonal)
            {
                tilemap.orientation = Tilemap.Orientation.Custom;

                float ox = SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth) * 0.5f;
                float oy = SuperImportContext.MakeScalar(m_MapComponent.m_TileHeight) * 0.5f;
                tilemap.orientationMatrix = Matrix4x4.Translate(new Vector3(-ox, -oy));
            }
            else if (m_MapComponent.m_Orientation == MapOrientation.Isometric || m_MapComponent.m_Orientation == MapOrientation.Staggered)
            {
                tilemap.orientation = Tilemap.Orientation.Custom;

                float ox = SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth) * 0.5f;
                tilemap.orientationMatrix = Matrix4x4.Translate(new Vector3(-ox, 0));
            }


            AddTilemapRendererComponent(go);

            // Figure out our opacity
            var layer = go.GetComponent <SuperLayer>();

            tilemap.color = new Color(1, 1, 1, layer.CalculateOpacity());

            return(tilemap);
        }
        public void PlaceTileColliders(SuperTile tile, TileIdMath tileId, Vector3Int pos)
        {
            Assert.IsNotNull(m_Tilemap, "Need a Tilemap component if we are going to gather tile colliders");

            // Tile y position is always off by one
            pos.y++;

            // Do we have any collider objects defined for this tile?
            if (!tile.m_CollisionObjects.IsEmpty())
            {
                var polygons = AcquireTilePolygonCollection(tile, tileId);

                foreach (var poly in polygons.Polygons)
                {
                    // Offset the polygon so that it is in the location of the tile
                    var tileHeight = m_ImportContext.MakeScalar(tile.m_Height);
                    var tileDiff   = m_Tilemap.cellSize.y - tileHeight;
                    var offset     = new Vector2(pos.x * m_Tilemap.cellSize.x, pos.y * m_Tilemap.cellSize.y - tileDiff);
                    var points     = poly.Points.Select(pt => pt + offset).ToArray();


                    CollisionClipperKey key = poly.MakeKey();
                    CollisionClipper    clipper;
                    if (!m_CollisionClippers.TryGetValue(key, out clipper))
                    {
                        // Add a new clipper for the layer
                        clipper = new CollisionClipper();
                        m_CollisionClippers.Add(key, clipper);
                    }

                    // Add the path to our clipper
                    if (poly.IsClosed)
                    {
                        clipper.AddClosedPath(points);
                    }
                    else
                    {
                        clipper.AddOpenPath(points);
                    }
                }
            }
        }
        private static void AddPolygonCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            // Note that polygons may need to be decomposed into convex parts
            var points = importContext.MakePoints(collision.Points);

            // Triangulate the polygon points
            var triangulator = new Triangulator();
            var triangles    = triangulator.TriangulatePolygon(points);

            // Gather triangles into a collection of convex polygons
            var composition    = new ComposeConvexPolygons();
            var convexPolygons = composition.Compose(triangles);

            PolygonUtils.AddCompositePolygonCollider(go, convexPolygons);

            // Position is from top-left corner
            float height = importContext.MakeScalar(tile.m_Height);

            go.transform.localPosition = new Vector3(0, height, 0);
        }
예제 #13
0
        private bool ProcessGridObject(XElement xMap)
        {
            // Add the grid to the map
            var goGrid = new GameObject("Grid");

            goGrid.transform.SetParent(m_MapComponent.gameObject.transform);

            m_GridComponent = goGrid.AddComponent <Grid>();

            // Grid cell size always has a z-value of 1 so that we can use custom axis sorting
            float sx = SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth);
            float sy = SuperImportContext.MakeScalar(m_MapComponent.m_TileHeight);

            m_GridComponent.cellSize = new Vector3(sx, sy, 1);
            Vector3 tilemapOffset = new Vector3(0, 0, 0);

            switch (m_MapComponent.m_Orientation)
            {
#if UNITY_2018_3_OR_NEWER
            case MapOrientation.Isometric:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;
                tilemapOffset = new Vector3(-sx * 0.5f, -sy, 0);
                break;

            case MapOrientation.Staggered:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;

                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        // Y - Odd
                        tilemapOffset = new Vector3(0, -sy, 0);
                    }
                    else
                    {
                        // Y-Even
                        tilemapOffset = new Vector3(sx * 0.5f, -sy, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    // X-Ood
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        tilemapOffset = new Vector3(0, -sy, 0);
                    }
                    else
                    {
                        // X-Even
                        tilemapOffset = new Vector3(0, -sy * 1.5f, 0);
                    }
                }
                break;

            case MapOrientation.Hexagonal:
                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    // Pointy-top hex maps
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.XYZ;

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        // Y-Odd
                        tilemapOffset = new Vector3(0, -sy, 0);
                    }
                    else
                    {
                        // Y-Even
                        tilemapOffset = new Vector3(0, -sy * 0.25f, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    // Flat-top hex maps. Reverse x and y on size.
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.YXZ;
                    m_GridComponent.cellSize    = new Vector3(sy, sx, 1);

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        // X-Odd
                        tilemapOffset = new Vector3(-sx * 0.75f, -sy * 1.5f, 0);
                    }
                    else
                    {
                        // X-Even
                        tilemapOffset = new Vector3(0, -sy * 1.5f, 0);
                    }
                }
                break;
#endif
            default:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Rectangle;
                tilemapOffset = new Vector3(0, -sy, 0);
                break;
            }

            SuperImportContext.TilemapOffset = tilemapOffset;

            return(true);
        }
        private void PlaceTileAsObject(GameObject goTilemap, SuperTile tile, int cx, int cy, TileIdMath tileId, Vector3Int pos3)
        {
            Assert.IsNotNull(goTilemap.GetComponentInParent <SuperMap>());
            Assert.IsNotNull(goTilemap.GetComponentInParent <SuperLayer>());

            var superMap   = goTilemap.GetComponentInParent <SuperMap>();
            var superLayer = goTilemap.GetComponentInParent <SuperLayer>();
            var color      = new Color(1, 1, 1, superLayer.CalculateOpacity());

            string tileName = string.Format("tile ({0}, {1})", cx, cy);
            var    goTRS    = new GameObject(string.Format("{0} (TRS)", tileName));

            goTilemap.AddChildWithUniqueName(goTRS);

            // Create a faux SuperObject component to add to our placed tile
            // We need this in case we have custom scripts that are looking for tile object via component
            {
                var tileObject = goTRS.AddComponent <SuperObject>();
                tileObject.m_Id        = m_ObjectIdCounter++;
                tileObject.m_TiledName = string.Format("AsObject_{0}", tileObject.m_Id);
                tileObject.m_X         = pos3.x * superMap.m_TileWidth;
                tileObject.m_Y         = -pos3.y * superMap.m_TileHeight;
                tileObject.m_Width     = tile.m_Width;
                tileObject.m_Height    = tile.m_Height;
                tileObject.m_TileId    = (uint)tile.m_TileId;
                tileObject.m_Visible   = true;

                // Does the tile have any properties?
                if (!tile.m_CustomProperties.IsEmpty())
                {
                    var component = tileObject.gameObject.AddComponent <SuperCustomProperties>();
                    component.m_Properties = new List <CustomProperty>();
                    component.m_Properties.CombineFromSource(tile.m_CustomProperties);
                }
            }

            Vector3 translate, rotate, scale;

            tile.GetTRS(tileId.FlipFlags, out translate, out rotate, out scale);

            var cellPos = superMap.CellPositionToLocalPosition(pos3.x, pos3.y);

            translate.x += cellPos.x;
            translate.y += cellPos.y;

            if (m_MapComponent.m_Orientation == MapOrientation.Isometric || m_MapComponent.m_Orientation == MapOrientation.Staggered)
            {
                translate.x -= SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth) * 0.5f;
            }
            else if (m_MapComponent.m_Orientation == MapOrientation.Hexagonal)
            {
                translate.x -= SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth) * 0.5f;
                translate.y -= SuperImportContext.MakeScalar(m_MapComponent.m_TileHeight) * 0.5f;
            }

            // Add the game object for the tile
            goTRS.transform.localPosition = translate;
            goTRS.transform.localRotation = Quaternion.Euler(rotate);
            goTRS.transform.localScale    = scale;

            // Add the sprite renderer component
            var renderer = goTRS.AddComponent <SpriteRenderer>();

            renderer.sprite = tile.m_Sprite;
            renderer.color  = color;
            AssignMaterial(renderer);
            AssignSpriteSorting(renderer);

            if (!tile.m_AnimationSprites.IsEmpty())
            {
                var tileAnimator = goTRS.AddComponent <TileObjectAnimator>();
                tileAnimator.m_AnimationFramerate = SuperImportContext.Settings.AnimationFramerate;
                tileAnimator.m_AnimationSprites   = tile.m_AnimationSprites;
            }

            // Add any colliders that may be on the tile object
            tile.AddCollidersForTileObject(goTRS, SuperImportContext);
        }
예제 #15
0
        private bool ProcessGridObject(XElement xMap)
        {
            // Add the grid to the map
            var goGrid = new GameObject("Grid");

            goGrid.transform.SetParent(m_MapComponent.gameObject.transform);

            // The grid is added to the asset because without it we get prefab modifications for all collision geometry and tile matrices
            // Note: this is crashing Unity 2018.3. Unfortunately prefab instances will be fatter in those versions of Unity. :(
#if UNITY_2019_1_OR_NEWER
            SuperImportContext.AddObjectToAsset("_grid", goGrid);
#endif

            m_GridComponent = goGrid.AddComponent <Grid>();


            // Grid cell size always has a z-value of 1 so that we can use custom axis sorting
            float sx = SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth);
            float sy = SuperImportContext.MakeScalar(m_MapComponent.m_TileHeight);
            m_GridComponent.cellSize = new Vector3(sx, sy, 1);
            var tileAnchor = new Vector3(0, 0, 0);

            switch (m_MapComponent.m_Orientation)
            {
#if UNITY_2018_3_OR_NEWER
            case MapOrientation.Isometric:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;
                tileAnchor = new Vector3(-1.5f, -0.5f, 0);
                break;

            case MapOrientation.Staggered:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;

                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        tileAnchor = new Vector3(-1, -1, 0);
                    }
                    else
                    {
                        tileAnchor = new Vector3(-0.5f, -1.5f, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        tileAnchor = new Vector3(-1, -1, 0);
                    }
                    else
                    {
                        // X-Even
                        tileAnchor = new Vector3(-1.5f, -1.5f, 0);
                    }
                }
                break;

            case MapOrientation.Hexagonal:
                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    // Pointy-top hex maps
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.XYZ;

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        // Y-Odd
                        tileAnchor = new Vector3(-0.5f, -4 / 3.0f, 0);
                    }
                    else
                    {
                        // Y-Even
                        tileAnchor = new Vector3(0, -1 / 3.0f, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    // Flat-top hex maps. Reverse x and y on size.
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.YXZ;
                    m_GridComponent.cellSize    = new Vector3(sy, sx, 1);

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        tileAnchor = new Vector3(-2, -1, 0);
                    }
                    else
                    {
                        tileAnchor = new Vector3(-1.5f, 0, 0);
                    }
                }
                break;
#endif
            default:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Rectangle;
                tileAnchor = new Vector3(0, -1, 0);
                break;
            }

            SuperImportContext.TileAnchor = tileAnchor;

            return(true);
        }
예제 #16
0
        private bool ProcessGridObject(XElement xMap)
        {
            // Add the grid to the map
            var goGrid = new GameObject("Grid");

            goGrid.transform.SetParent(m_MapComponent.gameObject.transform);

            // The grid is added to the asset because without it we get prefab modifications for all collision geometry and tile matrices
            SuperImportContext.AddObjectToAsset("_grid", goGrid);

            m_GridComponent = goGrid.AddComponent <Grid>();

            // Grid cell size always has a z-value of 1 so that we can use custom axis sorting
            float sx = SuperImportContext.MakeScalar(m_MapComponent.m_TileWidth);
            float sy = SuperImportContext.MakeScalar(m_MapComponent.m_TileHeight);

            m_GridComponent.cellSize = new Vector3(sx, sy, 1);
            var localPosition = new Vector3(0, 0, 0);

            switch (m_MapComponent.m_Orientation)
            {
#if UNITY_2018_3_OR_NEWER
            case MapOrientation.Isometric:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;
                localPosition = new Vector3(0, -sy, 0);
                break;

            case MapOrientation.Staggered:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Isometric;

                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        localPosition = new Vector3(sx * 0.5f, -sy, 0);
                    }
                    else
                    {
                        localPosition = new Vector3(sx, -sy, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        localPosition = new Vector3(sx * 0.5f, -sy, 0);
                    }
                    else
                    {
                        localPosition = new Vector3(sx * 0.5f, -sy * 1.5f, 0);
                    }
                }
                break;

            case MapOrientation.Hexagonal:
                if (m_MapComponent.m_StaggerAxis == StaggerAxis.Y)
                {
                    // Pointy-top hex maps
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.XYZ;

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        localPosition = new Vector3(sx * 0.5f, sy * -0.5f, 0);
                    }
                    else
                    {
                        localPosition = new Vector3(sx * 0.5f, sy * 0.25f, 0);
                    }
                }
                else if (m_MapComponent.m_StaggerAxis == StaggerAxis.X)
                {
                    // Flat-top hex maps. Reverse x and y on size.
                    m_GridComponent.cellLayout  = GridLayout.CellLayout.Hexagon;
                    m_GridComponent.cellSwizzle = GridLayout.CellSwizzle.YXZ;
                    m_GridComponent.cellSize    = new Vector3(sy, sx, 1);

                    if (m_MapComponent.m_StaggerIndex == StaggerIndex.Odd)
                    {
                        localPosition = new Vector3(sx * -0.25f, -sy, 0);
                    }
                    else
                    {
                        localPosition = new Vector3(sx * 0.5f, -sy, 0);
                    }
                }
                break;
#endif
            default:
                m_GridComponent.cellLayout = GridLayout.CellLayout.Rectangle;
                localPosition = new Vector3(0, -sy, 0);
                break;
            }

            m_GridComponent.transform.localPosition = localPosition;

            return(true);
        }