예제 #1
0
 void LoadMap()
 {
     UnloadCurrentMap();
     TiledMap = new Map(Maps[CurrentMap], true, MapsPath, this.gameObject, defaultMaterial, sortingOrder);
     TiledMap.GenerateCollidersFromLayer("Collider_0");
     TiledMap.GenerateCollidersFromLayer("Colliders");
     Debug.Log(TiledMap.ToString());
     MapObjectLayer mol = TiledMap.GetLayer("PropertyTest") as MapObjectLayer;
     if (mol != null)
     {
         Debug.Log(mol.GetPropertyAsBoolean("test"));
     }
     // Center camera
     _camera.CamPos = new Vector3(TiledMap.Width / 2.0f, -TiledMap.Height / 2.0f, _camera.CamPos.z);
     _camera.PixelsPerUnit = TiledMap.TileHeight;
 }
        public void LoadMap(string map, Vector2 _doorFromPosition)
        {
            if (map.Equals(_currentMap.MapObject.name))
                return;

            Map lastMap = _currentMap;
            _currentMap.MapObject.SetActive(false);

            Map nextMap = null;
            // If map was already loaded, just enable its GameObject
            if (_loadedMaps.TryGetValue(map, out nextMap))
            {
                nextMap.MapObject.SetActive(true);
                _currentMap = nextMap;
            }
            else
            {
                // Else, load the map and generate its collisions
                for (int i = 0; i < _maps.Length; i++)
                {
                    if (_maps[i].name.Equals(map))
                    {
                        nextMap = new Map(_maps[i].text, map, _tiledMapComponent.MapTMXPath, _tiledMapComponent.gameObject, _tiledMapComponent.materialDefaultFile, _tiledMapComponent.DefaultSortingOrder, true);
                        nextMap.GenerateTileCollisions();
                        nextMap.GenerateCollidersFromLayer("WallColliders");
                        nextMap.GenerateCollidersFromLayer("Doors", true, true);
                        _loadedMaps.Add(map, nextMap);
                        _currentMap = nextMap;
                        break;
                    }
                }
            }
            if (nextMap == null)
                return;

            // Set the player's position on the new map
            SetPlayerNewPosition(lastMap, _doorFromPosition);
        }
        // Callback for when the map has been loaded.
        void OnMapLoaded(Map map)
        {
            // Set the loaded map
            tiledMap = map;

            /*
             * Generate the map with the following options:
             *
             * TileManager's GameObject is the parent in the hierarchy
             * The default material is the default sprite's material
             */
            tiledMap.Generate(this.gameObject, defaultMaterial);

            // Generate any tile collisions
            tiledMap.GenerateTileCollisions();

            MapObjectLayer resourcesObjectLayer;    // The object layer for resources

            // Check if the map has a resources object layer
            if ((resourcesObjectLayer = tiledMap.GetObjectLayer("Resources")) != null)
            {
                // Get the list of MapObjects on the resources layer
                List<MapObject> resourceObjects = resourcesObjectLayer.Objects;

                // Check if the game is new
                if (GameMaster.Instance.IsNew)
                {
                    // The game is new so add all the resources to the list
                    foreach (var resource in resourceObjects)
                    {
                        resourcePositions.Add(ToMap(resource.Bounds));
                    } // end foreach

                    // Finally, save the resource positions list
                    GameMaster.Instance.SaveResources();
                } // end if
                else
                {
                    // The game isn't new so load all the saved resources
                    resourcePositions = GameMaster.Instance.LoadResources();
                } // end else

                // Loop through the objects to check if they're on the resources list
                foreach (var resource in resourceObjects)
                {
                    // Make sure the resource is in the list
                    if (resourcePositions.Contains(ToMap(resource.Bounds)))
                    {
                        // Generate the prefab for the resource
                        tiledMap.GeneratePrefab(resource, Vector2.zero, null, false, true);
                    } // end if
                } // end foreach
            } // end if

            // Check if the map has a markets object layer
            if (tiledMap.GetObjectLayer("Markets") != null)
            {
                // Get the list of MapObjects on the markets layer
                List<MapObject> marketObjects = tiledMap.GetObjectLayer("Markets").Objects;

                // Add all the markets to the list
                foreach (var market in marketObjects)
                {
                    marketPositions.Add(ToMap(market.Bounds));
                } // end foreach

                // Generate the colliders from this layer
                tiledMap.GenerateCollidersFromLayer("Markets");
                // Generate the prefabs from this layer
                tiledMap.GeneratePrefabsFromLayer("Markets", Vector2.zero, false, true);
            } // end if

            // Finally, Update the TileUtils static class
            SetDimensions(tiledMap.MapRenderParameter.TileHeight, tiledMap.MapRenderParameter.Width,
                tiledMap.MapRenderParameter.Height);
        }