Пример #1
0
        public void PlaceTileColliders(SuperMap map, SuperTile tile, TileIdMath tileId, Vector3Int pos)
        {
            // Do we have any collider objects defined for this tile?
            if (!tile.m_CollisionObjects.IsEmpty())
            {
                var polygons = AcquireTilePolygonCollection(tile, tileId, map.m_Orientation);

                foreach (var poly in polygons.Polygons)
                {
                    // Offset the polygon so that it is in the location of the tile
                    var offset = map.CellPositionToLocalPosition(pos.x, pos.y, m_ImportContext);

                    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 AddCollider(this CollisionObject collision, SuperTile tile, GameObject goParent, SuperImportContext importContext)
        {
            var go = new GameObject(collision.m_ObjectName);

            if (collision.CollisionShapeType == CollisionShapeType.Polygon)
            {
                AddPolygonCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Polyline)
            {
                AddEdgeCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Ellipse)
            {
                AddEllipseCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Rectangle)
            {
                AddBoxCollider(go, collision, tile, importContext);
            }

            // Additional settings on the collider that was just added
            var addedCollider = go.GetComponent <Collider2D>();

            if (addedCollider != null)
            {
                addedCollider.isTrigger = collision.m_IsTrigger;
            }

            goParent.AddChildWithUniqueName(go);
        }
Пример #3
0
        private void AssignCollisionObjectProperties(CollisionObject collision, SuperTile tile)
        {
            // Check properties for layer name
            var layerProperty = GetCollisionOrTileOrTilesetProperty(collision, tile, StringConstants.Unity_Layer);

            if (layerProperty != null)
            {
                // Explicit request to assign a layer to a collision. Report errors if the layer is missing.
                m_Importer.CheckLayerName(layerProperty.m_Value);
                collision.m_PhysicsLayer = layerProperty.m_Value;
            }
            else if (!string.IsNullOrEmpty(collision.m_ObjectName))
            {
                // Try to go off of object name but layer must exist (and we don't check for it)
                if (LayerMask.NameToLayer(collision.m_ObjectName) != -1)
                {
                    collision.m_PhysicsLayer = collision.m_ObjectName;
                }
            }

            if (string.IsNullOrEmpty(collision.m_PhysicsLayer))
            {
                collision.m_PhysicsLayer = "Default";
            }

            // Check properties for trigger setting
            var triggerProperty = GetCollisionOrTileOrTilesetProperty(collision, tile, StringConstants.Unity_IsTrigger);

            if (triggerProperty != null)
            {
                collision.m_IsTrigger = triggerProperty.GetValueAsBool();
            }
        }
Пример #4
0
    public bool ValidateMove(Vector3 targetLocation, bool isPlayer, List <TileType> tileTypes)
    {
        // can be null as it loads
        if (grid == null)
        {
            return(false);
        }

        Vector3Int tilePos = grid.WorldToCell(targetLocation);

        //Debug.Log("***************************************************");
        //Debug.Log("targetLoc: " + targetLocation + " targetCell: " + tilePos + " count: " + grid.transform.childCount);
        for (int i = 0; i < grid.transform.childCount; i++)
        {
            GameObject child   = grid.transform.GetChild(i).gameObject;
            Tilemap    tilemap = child.GetComponent <Tilemap>();
            //Debug.Log("tilemap: " + tilemap.name + ", rule: " + rule);
            SuperTile tile = tilemap.GetTile <SuperTile>(tilePos);
            if (tile != null)
            {
                GridTile gridTile = getTileProperties(tile);
                //Debug.Log("Tile matched");
                if (gridTile.cost >= 0)
                {
                    if (isPlayer)
                    {
                        if (gridTile.tileType == TileType.LOCATION)
                        {
                            foreach (GameObject location in locations)
                            {
                                if (Vector3.Distance(location.transform.position, targetLocation) <= 0.05f)
                                {
                                    //Debug.Log("Enter location: " + location.name);
                                    saveLoadHandler.Save();
                                    StartCoroutine(LoadLocationScene(location.name));
                                    return(true);
                                }
                            }
                            return(false);
                        }
                        else
                        {
                            questHandler.IncrementTime(gridTile.cost);
                            return(true);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    // only allow the first rule to be processed
                    return(false);
                }
            }
        }
        return(false);
    }
Пример #5
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);
                    }
                }
            }
        }
Пример #6
0
        private void PlaceTileAsTile(GameObject goTilemap, SuperTile tile, TileIdMath tileId, Vector3Int pos3)
        {
            var tilemap = goTilemap.GetComponent <Tilemap>();

            tilemap.SetTile(pos3, tile);

            // Do we have any colliders on the tile to be gathered?
            m_CurrentCollisionBuilder.PlaceTileColliders(tile, tileId, pos3);
        }
Пример #7
0
 public TsxTile(SuperTile tile)
 {
     this.tile  = tile;
     properties = new Dictionary <string, string>();
     foreach (var property in tile.m_CustomProperties)
     {
         properties.Add(property.m_Name, property.m_Value);
     }
 }
        public TilePolygonCollection(SuperTile tile, TileIdMath tileId, SuperImportContext importContext)
        {
            m_ImportContext = importContext;

            m_Tile   = tile;
            m_TileId = tileId;

            CalculateTransform();
            CollectTilePolygons();
        }
        public TilePolygonCollection(SuperTile tile, TileIdMath tileId, SuperImportContext importContext, MapOrientation orientation)
        {
            m_ImportContext = importContext;

            m_Tile   = tile;
            m_TileId = tileId;

            m_Transform = m_Tile.GetTransformMatrix(m_TileId.FlipFlags, orientation);

            CollectTilePolygons();
        }
 private void PlaceTile(GameObject goTilemap, int cx, int cy, SuperTile tile, Vector3Int pos3, TileIdMath tileId)
 {
     if (m_TilesAsObjects)
     {
         PlaceTileAsObject(goTilemap, tile, cx, cy, tileId, pos3);
     }
     else
     {
         PlaceTileAsTile(goTilemap, tile, tileId, pos3);
     }
 }
Пример #11
0
    /// <summary>
    /// Called by Unity on the frame when a script is enabled just before any of the Update methods are called the first time.
    /// </summary>
    private void Start()
    {
        SuperTileLayer[] superTileLayers = FindObjectsOfType <SuperTileLayer>();
        SuperTileLayer   testTileIdLayer = superTileLayers.FirstOrDefault(stl => stl.gameObject.name == "TestTileId");
        int tileId = testTileIdLayer.GetTileId(0, 0);

        Debug.Log(tileId);
        SuperTile customTile = testTileIdLayer.SuperMap.GetCustomTile(tileId);
        string    propVal    = customTile.GetPropertyValueAsString("testProp");

        Debug.Log(propVal);
    }
Пример #12
0
        private TilePolygonCollection AcquireTilePolygonCollection(SuperTile tile, TileIdMath tileId, MapOrientation orientation)
        {
            TilePolygonCollection polygons;

            if (m_TilePolygonDatabase.TryGetValue(tileId.ImportedlTileId, out polygons))
            {
                return(polygons);
            }

            // If we're here then we don't have a polygon collection for this tile yet
            polygons = new TilePolygonCollection(tile, tileId, m_ImportContext, orientation);
            m_TilePolygonDatabase.Add(tileId.ImportedlTileId, polygons);
            return(polygons);
        }
Пример #13
0
        public static void AddCollidersForTileObject(this SuperTile tile, GameObject goParent, SuperImportContext importContext)
        {
            Assert.IsNotNull(tile);
            Assert.IsNotNull(goParent);
            Assert.IsNotNull(importContext);

            if (!tile.m_CollisionObjects.IsEmpty())
            {
                foreach (var collision in tile.m_CollisionObjects)
                {
                    collision.AddCollider(tile, goParent, importContext);
                }
            }
        }
        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>();
        }
Пример #15
0
    static void CreatePrefab()
    {
        SuperTile superTile = new SuperTile();
        Texture2D texture   = (Texture2D)Selection.activeObject;

        superTile.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
        string path = "Assets/Resources/Sprites/tool/" + Selection.activeObject.name + ".asset";

        path = AssetDatabase.GenerateUniqueAssetPath(path);
        AssetDatabase.CreateAsset(superTile, path);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = superTile;
    }
        private void PlaceTileAsTile(GameObject goTilemap, SuperTile tile, TileIdMath tileId, Vector3Int pos3)
        {
            // Burn our layer index into the z component of the tile position
            // This allows us to support Tilemaps being shared by groups
            pos3.z = RendererSorter.CurrentTileZ;

            // Set the tile (sprite, transform matrix, flags)
            var tilemap = goTilemap.GetComponentInParent <Tilemap>();

            tilemap.SetTile(pos3, tile);
            tilemap.SetTransformMatrix(pos3, tile.GetTransformMatrix(tileId.FlipFlags));
            tilemap.SetTileFlags(pos3, TileFlags.LockAll);

            // Do we have any colliders on the tile to be gathered?
            m_CurrentCollisionBuilder.PlaceTileColliders(m_MapComponent, tile, tileId, pos3);
        }
Пример #17
0
        private void ProcessAnimationElement(SuperTile tile, XElement xAnimation)
        {
            var fps        = m_Importer.SuperImportContext.Settings.AnimationFramerate;
            var animations = new AnimationBuilder(fps);

            var frameSprites   = xAnimation.Elements("frame").Select(f => f.GetAttributeAs <int>("tileid")).Select(index => m_TilesetScript.m_Tiles[index].m_Sprite).ToArray();
            var frameDurations = xAnimation.Elements("frame").Select(f => f.GetAttributeAs <int>("duration")).Select(ms => ms / 1000.0f).ToArray();

            for (int i = 0; i < frameSprites.Length; i++)
            {
                var sprite   = frameSprites[i];
                var duration = frameDurations[i];
                animations.AddFrames(sprite, duration);
            }

            tile.m_AnimationSprites = animations.Sprites.ToArray();
        }
Пример #18
0
    private GridTile getTileProperties(SuperTile tile)
    {
        GridTile gridTile = new GridTile();

        foreach (CustomProperty prop in tile.m_CustomProperties)
        {
            if (prop.m_Name == "cost")
            {
                gridTile.cost = prop.GetValueAsInt();
            }
            else if (prop.m_Name == "tileType")
            {
                gridTile.tileType = prop.GetValueAsEnum <TileType>();
            }
        }
        return(gridTile);
    }
Пример #19
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");

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

                foreach (var poly in polygons.Polygons)
                {
                    // Offset the polygon so that it is in the location of the tile
                    var offset = map.CellPositionToLocalPosition(pos.x, pos.y);

                    if (map.m_Orientation == MapOrientation.Isometric || map.m_Orientation == MapOrientation.Staggered)
                    {
                        offset -= m_ImportContext.MakePointPPU(map.m_TileWidth, 0) * 0.5f;
                    }
                    else if (map.m_Orientation == MapOrientation.Hexagonal)
                    {
                        offset -= m_ImportContext.MakePointPPU(map.m_TileWidth, map.m_TileHeight) * 0.5f;
                    }

                    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 void PlaceTileAsTile(GameObject goTilemap, SuperTile tile, TileIdMath tileId, Vector3Int pos3)
        {
            // Burn our layer index into the z component of the tile position
            // This is needed for when using a custom sort axis
            pos3.z = m_TileLayerCounter;

            // Set the flip data
            var tilemapData = goTilemap.GetComponentInParent <TilemapData>();

            tilemapData.SetFlipFlags(pos3, tileId.FlipFlags);

            // Set the tile
            var tilemap = goTilemap.GetComponentInParent <Tilemap>();

            tilemap.SetTile(pos3, tile);

            // Do we have any colliders on the tile to be gathered?
            m_CurrentCollisionBuilder.PlaceTileColliders(m_MapComponent, tile, tileId, pos3);
        }
Пример #21
0
        private void PlaceTile(GameObject goTilemap, int cx, int cy, SuperTile tile, Vector3Int pos3, TileIdMath tileId)
        {
            // We're either placing tiles or objects as tiles
            Assert.IsTrue(m_TilesAsObjects || m_MapComponent.m_Orientation == MapOrientation.Isometric || goTilemap.GetComponent <Tilemap>() != null);

            // Bake transform flags into the z component (for flipped/rotated tiles)
            pos3.z = tileId.PlacementZ;

            bool tilesAsObjects = m_MapComponent.m_Orientation == MapOrientation.Isometric || m_TilesAsObjects;

            if (tilesAsObjects)
            {
                PlaceTileAsObject(goTilemap, tile, cx, cy, pos3);
            }
            else
            {
                PlaceTileAsTile(goTilemap, tile, tileId, pos3);
            }
        }
Пример #22
0
        private void ProcessAnimationElement(SuperTile tile, XElement xAnimation)
        {
            var fps        = m_Importer.SuperImportContext.Settings.AnimationFramerate;
            var animations = new AnimationBuilder(fps);

            foreach (var xFrame in xAnimation.Elements("frame"))
            {
                var frameId       = xFrame.GetAttributeAs <int>("tileid");
                var frameDuration = xFrame.GetAttributeAs <int>("duration") / 1000.0f;

                SuperTile frame;
                if (m_TilesetScript.TryGetTile(frameId, out frame))
                {
                    animations.AddFrames(frame.m_Sprite, frameDuration);
                }
            }

            tile.m_AnimationSprites = animations.Sprites.ToArray();
        }
        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);
                    }
                }
            }
        }
        public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperTile tile, string typeName)
        {
            // Load our "local" properties first
            var component  = go.AddComponent <SuperCustomProperties>();
            var properties = CustomPropertyLoader.LoadCustomPropertyList(xProperties);

            // Do we have any properties from a tile to add?
            if (tile != null)
            {
                properties.CombineFromSource(tile.m_CustomProperties);
            }

            // Add properties from our object type (this should be last)
            properties.AddPropertiesFromType(typeName, SuperImportContext);

            // Sort the properties alphabetically
            component.m_Properties = properties.OrderBy(p => p.m_Name).ToList();

            AssignUnityTag(component);
            AssignUnityLayer(component);
        }
        public static void AddCollider(this CollisionObject collision, SuperTile tile, GameObject goParent, SuperImportContext importContext)
        {
            var go = new GameObject(collision.m_ObjectName);

            if (collision.CollisionShapeType == CollisionShapeType.Polygon)
            {
                AddPolygonCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Polyline)
            {
                AddEdgeCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Ellipse)
            {
                AddEllipseCollider(go, collision, tile, importContext);
            }
            else if (collision.CollisionShapeType == CollisionShapeType.Rectangle)
            {
                AddBoxCollider(go, collision, tile, importContext);
            }

            goParent.AddChildWithUniqueName(go);
        }
Пример #26
0
        private void PlaceTileAsTile(GameObject goTilemap, Tilemap tilemap, TilemapRenderer tilemapRenderer, SuperTile tile, TileIdMath tileId, Vector3Int pos3)
        {
            // Burn our layer index into the z component of the tile position
            // This allows us to support Tilemaps being shared by groups
            pos3.z = RendererSorter.CurrentTileZ;

            if (SuperImportContext.LayerIgnoreMode != LayerIgnoreMode.Visual)
            {
                // Set the tile (sprite, transform matrix, flags)
                tilemap.SetTile(pos3, tile);
                tilemap.SetTransformMatrix(pos3, tile.GetTransformMatrix(tileId.FlipFlags, m_MapComponent.m_Orientation));

#if UNITY_2018_3_OR_NEWER
                if (tilemapRenderer.mode == TilemapRenderer.Mode.Individual)
                {
                    // Each tile color must be set individually
                    tilemap.SetColor(pos3, tilemap.color);
                }
#endif

                tilemap.SetTileFlags(pos3, TileFlags.LockAll);
            }

            if (SuperImportContext.LayerIgnoreMode != LayerIgnoreMode.Collision)
            {
                // Do we have any colliders on the tile to be gathered?
                m_CurrentCollisionBuilder.PlaceTileColliders(m_MapComponent, tile, tileId, pos3);
            }
        }
Пример #27
0
        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      = superLayer.CalculateColor();

            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, m_MapComponent.m_Orientation, out translate, out rotate, out scale);

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

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

            // 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, m_CurrentTileLayer.m_TiledName);
            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);
        }
Пример #28
0
 public bool TryGetTile(int tileId, out SuperTile tile)
 {
     tile = m_Tiles.FirstOrDefault(t => t.m_TileId == tileId);
     return(tile != null);
 }
Пример #29
0
        private void ProcessTileObject(SuperObject superObject, XElement xObject)
        {
            Assert.IsNull(superObject.m_SuperTile);
            Assert.IsNotNull(GlobalTileDatabase, "Cannot process tile objects without a tileset database");

            SuperTile tile       = null;
            var       tileId     = new TileIdMath(superObject.m_TileId);
            int       justTileId = tileId.JustTileId;

            // Are we getting the tile from a template?
            var template = xObject.GetAttributeAs("template", "");

            if (!string.IsNullOrEmpty(template))
            {
                var asset = Importer.RequestAssetAtPath <ObjectTemplate>(template);
                if (asset == null)
                {
                    Importer.ReportError("Template file '{0}' was not found.", template);
                    return;
                }

                tile = asset.m_Tile;
                if (tile == null)
                {
                    Importer.ReportError("Missing tile '{0}' from template '{1}' on tile object '{2}'", justTileId, template, superObject.name);
                    return;
                }
            }

            // Are we getting the tile from our tile database?
            if (tile == null)
            {
                GlobalTileDatabase.TryGetTile(justTileId, out tile);

                if (tile == null)
                {
                    Importer.ReportError("Missing tile '{0}' on tile object '{1}'", justTileId, template, superObject.name);
                    return;
                }
            }

            // Our type may come from the tile as well (this is 'Typed Tiles' in Tiled)
            if (string.IsNullOrEmpty(superObject.m_Type))
            {
                superObject.m_Type = tile.m_Type;
            }

            // Construct the game objects for displaying a single tile
            var  inversePPU = Importer.SuperImportContext.Settings.InversePPU;
            bool flip_h     = tileId.HasHorizontalFlip;
            bool flip_v     = tileId.HasVerticalFlip;

            var scale = Vector3.one;

            scale.x = xObject.GetAttributeAs("width", 1.0f);
            scale.y = xObject.GetAttributeAs("height", 1.0f);

            scale.x /= tile.m_Width;
            scale.y /= tile.m_Height;

            var tileOffset      = new Vector3(tile.m_TileOffsetX * inversePPU, -tile.m_TileOffsetY * inversePPU);
            var translateCenter = new Vector3(tile.m_Width * 0.5f * inversePPU, tile.m_Height * 0.5f * inversePPU);

            // Our root object will contain the translation, rotation, and scale of the tile object
            var goTRS = superObject.gameObject;

            goTRS.transform.localScale = scale;

            // Add another object to handle tile flipping
            // This object will center us into the tile and perform the flips through scaling
            // This object also contains the tile offset in her transform
            var goCF = new GameObject();

            goCF.name = string.Format("{0} (CF)", superObject.m_TiledName);
            goTRS.AddChildWithUniqueName(goCF);

            goCF.transform.localPosition = translateCenter + tileOffset;
            goCF.transform.localRotation = Quaternion.Euler(0, 0, 0);
            goCF.transform.localScale    = new Vector3(flip_h ? -1 : 1, flip_v ? -1 : 1, 1);

            // Note: We may not want to put the tile "back into place" depending on our coordinate system
            var fromCenter = -translateCenter;

            // Isometric maps referece tile objects by bottom center
            if (SuperMap.m_Orientation == MapOrientation.Isometric)
            {
                fromCenter.x -= Importer.SuperImportContext.MakeScalar(tile.m_Width * 0.5f);
            }

            // Add another child, putting our coordinates back into the proper place
            var goTile = new GameObject(superObject.m_TiledName);

            goCF.AddChildWithUniqueName(goTile);
            goTile.transform.localPosition = fromCenter;
            goTile.transform.localRotation = Quaternion.Euler(0, 0, 0);
            goTile.transform.localScale    = Vector3.one;

            if (Importer.SuperImportContext.LayerIgnoreMode != LayerIgnoreMode.Visual)
            {
                // Add the renderer
                var renderer = goTile.AddComponent <SpriteRenderer>();
                renderer.sprite = tile.m_Sprite;
                renderer.color  = new Color(1, 1, 1, superObject.CalculateOpacity());
                Importer.AssignMaterial(renderer, m_ObjectLayer.m_TiledName);
                Importer.AssignSpriteSorting(renderer);

                // Add the animator if needed
                if (!tile.m_AnimationSprites.IsEmpty())
                {
                    var tileAnimator = goTile.AddComponent <TileObjectAnimator>();
                    tileAnimator.m_AnimationFramerate = AnimationFramerate;
                    tileAnimator.m_AnimationSprites   = tile.m_AnimationSprites;
                }
            }

            if (Importer.SuperImportContext.LayerIgnoreMode != LayerIgnoreMode.Collision)
            {
                // Add any colliders that were set up on the tile in the collision editor
                tile.AddCollidersForTileObject(goTile, Importer.SuperImportContext);
            }

            // Store a reference to our tile object
            superObject.m_SuperTile = tile;
        }
Пример #30
0
        private void PlaceTileAsObject(GameObject goTilemap, SuperTile tile, int cx, int cy, 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.IsNullOrEmpty())
                {
                    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((FlipFlags)pos3.z, out translate, out rotate, out scale);

            translate.x += pos3.x * superMap.CellSize.x;
            translate.y += pos3.y * superMap.CellSize.y;

            // If this is an isometric map than we have an additional translate to consider to place the tile
            if (m_MapComponent.m_Orientation == MapOrientation.Isometric)
            {
                translate.x -= m_MapComponent.m_TileWidth * 0.5f * SuperImportContext.Settings.InversePPU;
                translate.y -= m_MapComponent.m_TileHeight * 0.5f * SuperImportContext.Settings.InversePPU;
            }

            // 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;
            AssignSortingLayer(renderer, superLayer.m_SortingLayerName, superLayer.m_SortingOrder);

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

            // Add any colliders that may be on the tile object
            tile.AddCollidersForTileObject(goTRS, SuperImportContext);
        }