Example #1
0
    public bool Load(FullSerializer.fsData jsIsland)
    {
        var jsIslandData = jsIsland.AsDictionary;

        Id = (int)jsIslandData["id"].AsInt64;

        var islandPos = GameLevelJsonLoader.GetPos(jsIslandData["pos"]);
        var size      = GameLevelJsonLoader.GetSize(jsIslandData["size"]);
        var resIndex  = jsIslandData["res"].AsInt64;

        Pos  = islandPos;
        Size = size;

        // back
        var sprite  = _gameLevel.spriteLevelBack[(int)resIndex];
        var backObj = EGHelpers.CreateSprite(Pos, sprite, "back_" + Id, _parentTransform, false);

        DebugLogger.WriteInfo("Island.Load sprite.rect = " + sprite.rect.ToString());
        backObj.transform.localScale = new Vector3(Size.x * 100f / sprite.rect.width,
                                                   Size.y * 100f / sprite.rect.height, 1f);
        var sprRender = backObj.GetComponent <SpriteRenderer>();

        sprRender.sortingOrder = -2;

        if (jsIslandData.ContainsKey("backMesgColor"))
        {
            var     sBackMesgColor = jsIslandData["backMesgColor"].AsString;
            float[] colors         = sBackMesgColor.Split <float>(',');
            sprRender.color = new Color(colors[0], colors[1], colors[2]);
        }
        if (jsIslandData.ContainsKey("trees"))
        {
            var jsTreeList = jsIslandData["trees"].AsList;
            foreach (var jsTree in jsTreeList)
            {
                var jsTreeData = jsTree.AsDictionary;
                var treePos    = GameLevelJsonLoader.GetPos(jsTreeData["pos"]);
                var treeObj    = EGHelpers.CreateSpriteByScript <Tree>(islandPos + treePos, _gameLevel.spriteTree, "tree",
                                                                       _parentTransform, GameLevel.TerrainTagName);
            }
        }

        return(true);
    }
Example #2
0
    public void LoadLevel(string levelParams)
    {
        _player.Reset();

        FullSerializer.fsData   jsData = null;
        FullSerializer.fsResult res    = FullSerializer.fsJsonParser.Parse(levelParams, out jsData);
        DebugLogger.WriteInfo("GameLevel.LoadLevel res.Failed = {0}", res.Failed.ToInt());
        if (res.Failed)
        {
            DebugLogger.WriteError("GameLevel.LoadLevel error = {0}", res.FormattedMessages);
            return;
        }
        DebugLogger.WriteInfo("GameLevel.LoadLevel data.IsDictionary = {0}", jsData.IsDictionary.ToInt());
        if (!jsData.IsDictionary)
        {
            DebugLogger.WriteError("GameLevel.LoadLevel json data is incorrect format");
            return;
        }

        var isDict = jsData.IsDictionary;

        if (!isDict)
        {
            DebugLogger.WriteError("GameLevel.LoadLevel json data must be have node 'level'");
            return;
        }
        jsData = jsData.AsDictionary["level"];
        DebugLogger.WriteInfo("GameLevel.LoadLevel data.AsDictionary = {0}", jsData.IsDictionary.ToInt());
        if (!jsData.IsDictionary)
        {
            DebugLogger.WriteError("GameLevel.LoadLevel level data is not dictionary");
            return;
        }

        var jsLevelParams = jsData.AsDictionary;
        var jsTerrain     = jsLevelParams["terrain"];

        if (!jsTerrain.IsDictionary)
        {
            DebugLogger.WriteError("GameLevel.LoadLevel terrain data is not dictionary");
            return;
        }

        _levelAllSprites = new GameObject("GameLevel");

        var jsIslands    = jsTerrain.AsDictionary["islands"];
        var jsIslandList = jsIslands.AsList;

        var islandList = new List <Island>();

        foreach (var jsIsland in jsIslandList)
        {
            var island = new Island(this, _levelAllSprites.transform);
            island.Load(jsIsland);
            islandList.Add(island);
        }

        if (islandList.Count == 1)
        {
            var island = islandList[0];
            Field = new Rect(new Vector2(island.Pos.x - 0.5f * island.Size.x, island.Pos.y - 0.5f * island.Size.y),
                             new Vector2(island.Size.x, island.Size.y));
        }
        else
        {
            var minPosX = float.MaxValue;
            var maxPosX = float.MinValue;
            var minPosY = float.MaxValue;
            var maxPosY = float.MinValue;
            foreach (var island in islandList)
            {
                if (minPosX > island.Pos.x - 0.5f * island.Size.x)
                {
                    minPosX = island.Pos.x - 0.5f * island.Size.x;
                }
                if (maxPosX < island.Pos.x + 0.5f * island.Size.x)
                {
                    maxPosX = island.Pos.x + 0.5f * island.Size.x;
                }
                if (minPosY > island.Pos.y - 0.5f * island.Size.y)
                {
                    minPosY = island.Pos.y - 0.5f * island.Size.y;
                }
                if (maxPosY < island.Pos.y + 0.5f * island.Size.y)
                {
                    maxPosY = island.Pos.y + 0.5f * island.Size.y;
                }
            }
            Field = new Rect(new Vector2(minPosX, minPosY), new Vector2(maxPosX - minPosX, maxPosY - minPosX));
        }

        if (jsTerrain.AsDictionary.ContainsKey("bridges"))
        {
            var jsBrigdeList = jsTerrain.AsDictionary["bridges"].AsList;
            foreach (var jsBrigde in jsBrigdeList)
            {
                var bridge = new Bridge(this, _levelAllSprites.transform, islandList);
                if (!bridge.Load(jsBrigde))
                {
                    DebugLogger.WriteError("GameLevel.LoadLevel load bridge is failed");
                }
            }
        }

        foreach (var island in islandList)
        {
            island.CreateBorders();
        }

        if (jsLevelParams.ContainsKey("enemy"))
        {
            var jsEnemy = jsLevelParams["enemy"];
            if (jsEnemy.AsDictionary.ContainsKey("portals"))
            {
                var jsPortalList = jsEnemy.AsDictionary["portals"].AsList;
                foreach (var jsPortal in jsPortalList)
                {
                    var jsPortalData = jsPortal.AsDictionary;
                    int portalLevel  = jsPortalData.ContainsKey("level") ? (int)jsPortalData["level"].AsInt64 : 0;

                    var portalPos = GameLevelJsonLoader.GetPos(jsPortalData["pos"]);
                    int islandId  = jsPortalData.ContainsKey("islandId") ? (int)jsPortalData["islandId"].AsInt64 : -1;
                    var portal    = EGHelpers.CreateObjectByPrefab <Portal>((islandId == -1 ? portalPos :
                                                                             (portalPos + islandList.Find(it => it.Id == islandId).Pos)), prefabPortal, _levelAllSprites.transform);
                    portal.Level = portalLevel;
                    portal.AddObserver(_DiedObject);
                    _enemyManager.AddEnemy(portal);
                }
            }
        }

        var jsBonus     = jsLevelParams["bonus"];
        var jsBonusData = jsBonus.AsDictionary;
        var jsGoodBonus = jsBonusData["player"].AsDictionary;

        PlayerUpgrade.SleepTime          = (float)jsGoodBonus["sleepTime"].AsDouble;
        PlayerUpgrade.SpeedTime          = (float)jsGoodBonus["speedTime"].AsDouble;
        PlayerUpgrade.ShieldTime         = (float)jsGoodBonus["shieldTime"].AsDouble;
        PlayerUpgrade.ChildrenShieldTime = (float)jsGoodBonus["childrenShieldTime"].AsDouble;
        PlayerUpgrade.Health             = (float)jsGoodBonus["health"].AsDouble;
        var jsEnemyBonus = jsBonusData["enemy"].AsDictionary;

        EnemyUpgrade.SleepTime  = (float)jsEnemyBonus["sleepTime"].AsDouble;
        EnemyUpgrade.SpeedTime  = (float)jsEnemyBonus["speedTime"].AsDouble;
        EnemyUpgrade.ShieldTime = (float)jsEnemyBonus["shieldTime"].AsDouble;
        EnemyUpgrade.Health     = (float)jsEnemyBonus["health(%)"].AsDouble;
        _enemyCountText.text    = String.Format("E: {0}", _enemyManager.Count.ToString());
    }