Exemple #1
0
    void Start()
    {
        // Get current level
        int level = UserData.Instance.Level;

        int   mapCount = maps.Length;
        float mapWidth = maps[0].GetWidth();

        int   buttonCount = -1;
        float cloudY      = -1;

        for (int zone = 0; zone < 2; zone++)
        {
            ZoneType zoneType = zone.ToZoneType();

            if (level <= zoneType.MapCount())
            {
                buttonCount = zoneType.MapCount() + 1;
                cloudY      = clouds.GetChild(zone).position.y;
                break;
            }
        }

        Vector3 position = Vector3.zero;

        for (int i = 0; i < mapCount; i++)
        {
            GameObject bg = new GameObject(string.Format("Background {0}", i + 1));
            bg.transform.SetParent(background);
            bg.transform.localPosition = position;

            SpriteRenderer renderer = bg.AddComponent <SpriteRenderer>();
            renderer.sprite       = maps[i];
            renderer.sortingOrder = -3;

            float height = maps[i].GetHeight();

            position.y += height;

            if (cloudY > 0 && position.y >= cloudY)
            {
                break;
            }
        }

        if (cloudY < 0)
        {
            cloudY = position.y;
        }

        // Set cloud position
        cloud.transform.SetPositionY(cloudY);

        // Set number of maps
        _mapCount = points.childCount;

        if (buttonCount < 0)
        {
            buttonCount = _mapCount;
        }

        // Create buttons
        _buttons = new MapButton[buttonCount];

        bool isNewLevel = UserData.Instance.NewLevel;

        for (int i = 0; i < buttonCount; i++)
        {
            position = points.GetChild(i).position;

            int mapID = i + 1;

            GameObject map = buttonPrefab.Create(background, position);
            map.name = string.Format("Map {0}", mapID);

            bool unlocked = mapID < level || (mapID == level && !isNewLevel);

            MapButton mapButton = map.GetComponent <MapButton>();
            mapButton.Construct(spriteLock, zones[ZoneTypeHelper.GetZoneType(mapID).ToInt()].mapIcon, mapID, unlocked);

            _buttons[i] = mapButton;
        }

        // Destroy points
        Destroy(points.gameObject);

        // Destroy clouds
        Destroy(clouds.gameObject);

        // Fixed width
        float scale = Camera.main.GetWidth() / mapWidth;

        background.SetScale(scale);

        float mapHeight = cloudY * scale;

        //
        _squareRadius = ButtonRadius * ButtonRadius * scale * scale;

        // Create vertical scroll
        _verticalScroll = new VerticalScroll();
        _verticalScroll.Construct(-Camera.main.orthographicSize, mapHeight, Camera.main.GetHeight());

        // Set map position
//		transform.SetPositionY(_verticalScroll.Position);

        // Get current map
        int mapIndex = Mathf.Clamp(UserData.Instance.Map - 1, 0, _buttons.Length - 1);

        if (animal != null)
        {
            // Set animal position
            animal.transform.position = _buttons[mapIndex].transform.position;

            // Set skin
            animal.idleDuration = 0;

            ZoneType zoneType = ZoneTypeHelper.GetZoneType(mapIndex + 1);

            if (!zoneType.IsDefault())
            {
                ZoneSetting zone = zones[zoneType.ToInt()];
                animal.SetClothes(zone.clothesLeft, zone.clothesUp, zone.clothesDown);
                animal.SetCap(zone.capLeft, zone.capUp, zone.capDown);
            }
        }

        MoveObjectToCenter(_buttons[mapIndex].gameObject);
    }