void Start()
    {
        CameraManager.GetInstance().FadeTo("cameraBoard", 1);
        

        _playBoard = JSONObject.JSONToBoard(ref _board, _boardName);
        if (_prefabEnvironmentName != "")
        {
            GameObject prefab = Resources.Load<GameObject>("prefabs/maps/" + _prefabEnvironmentName);
            Environment = Instantiate(prefab);
            _board.transform.GetChild(0).gameObject.SetActive(false);
        }

        _board.AddComponent<PlayBoardBehaviour>();
        _player1 = new Character(2500, _player1GameObject);
		_player2 = new Character(2500, _player2GameObject);
        _player1.Name = ServerManager.GetInstance()._server.Client1._name;
        _player2.Name = ServerManager.GetInstance()._server.Client2._name;
        _uiPlayer1.Character = _player1;
        _uiPlayer2.Character = _player2;

        _gameStarted = false;

        SpellManager.GetInstance();
        SpellManager.GetInstance().Init();
        RunicBoardManager.GetInstance().RegisterBoard(new RunicBoard());
        PlayBoardManager.GetInstance().Init(_playBoard, _player1, _player2);

        Invoke("EndLoading", 1);
    }
    void Awake()
    {
        playBoard = JSONObject.JSONToBoard(ref board, _boardName);
        hexaStart1 = playBoard.GetHexagone(8, 8);
        hexaStart2 = playBoard.GetHexagone(12, 12);

        player1 = new Character(4000, hexaStart1, _player1GameObject);
        player2 = new Character(4000, hexaStart2, _player2GameObject);
        PlayBoardManager.GetInstance().Init(playBoard, player1, player2);

        player1.Name = "Player 1";
        player2.Name = "Player 2";

        rangeTest = new List<Hexagon>();
        rangeTest.Add(hexaStart1);

        //Logger.logLvl = Logger.Type.TRACE;

        SpellManager.GetInstance();
        Logger.Trace("spellManager initialized");

        /*Queue<Element> elements = new Queue<Element>();
        elements.Enqueue(Element.GetElement(3));
        elements.Enqueue(Element.GetElement(3));

        SelfSpell testSp = SpellManager.GetInstance().ElementNode.GetSelfSpell(elements);
        Logger.Trace(testSp._effects.GetIds().Count);
        List<int> effectIds = testSp._effects.GetIds();

        
        /*for(int  i = 0; i < PlayBoardManager.GetInstance().Board._width; i++)
        {
            for (int j = 0; j < PlayBoardManager.GetInstance().Board._height; j++)
            {
                rangeTest.Add(PlayBoardManager.GetInstance().Board.GetHexagone(i, j));
            }
        }*/


        /*for (int i = 0; i < effectIds.Count; i++)
        {
            EffectDirect effectTest = SpellManager.GetInstance().getDirectEffectById((int)effectIds[i]);
            effectTest.ApplyEffect(rangeTest, hexaStart1, player1);
        }*/
    }
 // Use this for initialization
 void Awake()
 {
     _playBoard = JSONObject.JSONToBoard(ref _board, _boardName);
 }
    // Use this for initialization
    void Start()
    {
        _areas = new Dictionary<int, Area>();
        LoadArea();

        Area area = GetAreaById(6);
        PlayBoard playBoard = new PlayBoard(5, 5);

        List<Hexagon> hexas = new List<Hexagon>();
        GameObject glyph = Resources.Load<GameObject>("prefabs/SM_Glyphe_1");
        GameObject board = new GameObject("Board");
        

        hexas.Add(new Hexagon(0, 2, playBoard));
        hexas.Add(new Hexagon(1, 2, playBoard));
        hexas.Add(new Hexagon(2, 2, playBoard)); //centre
        hexas.Add(new Hexagon(3, 2, playBoard));
        hexas.Add(new Hexagon(4, 2, playBoard));

        hexas.Add(new Hexagon(1, 3, playBoard));
        hexas.Add(new Hexagon(2, 3, playBoard));
        hexas.Add(new Hexagon(3, 3, playBoard));
        hexas.Add(new Hexagon(4, 3, playBoard));

        hexas.Add(new Hexagon(2, 4, playBoard));
        hexas.Add(new Hexagon(3, 4, playBoard));
        hexas.Add(new Hexagon(4, 4, playBoard));

        hexas.Add(new Hexagon(0, 1, playBoard));
        hexas.Add(new Hexagon(1, 1, playBoard));
        hexas.Add(new Hexagon(2, 1, playBoard));
        hexas.Add(new Hexagon(3, 1, playBoard));

        hexas.Add(new Hexagon(0, 0, playBoard));
        hexas.Add(new Hexagon(1, 0, playBoard));
        hexas.Add(new Hexagon(2, 0, playBoard));

        for (int i = 0; i < hexas.Count; i++)
        {
            Hexagon hexagon = playBoard.CreateHexagone(hexas[i]._posX, hexas[i]._posY);
            hexagon.GameObject = new GameObject();
            hexagon.GameObject.transform.parent = board.transform;
             

            hexagon.Glyph = GameObject.Instantiate(glyph);
            hexagon.Glyph.transform.parent = hexagon.GameObject.transform;
            hexagon.Glyph.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, 0.2f, 0.75f * hexagon._posY);

            hexagon.CurrentState = Hexagon.State.Targetable;
            hexagon.Orientation = hexas[i].Orientation;

            hexas[i] = hexagon;
            
        }

        if (area.Orientation == Orientation.EnumOrientation.Line || area.Orientation == Orientation.EnumOrientation.Any)
            _finalArea = area.AreaToHexa(Direction.EnumDirection.East, hexas[1]);


        if (area.Orientation == Orientation.EnumOrientation.Diagonal)
            _finalArea = area.AreaToHexa(Direction.EnumDirection.DiagonalNorthEast, hexas[1]);


        for (int i = 0; i < _finalArea.Count; i++)
        {
            _finalArea[i].CurrentState = Hexagon.State.OverEnnemiTargetable;
            Logger.Debug(_finalArea[i]._posX + " " + _finalArea[i]._posY + " : " + _finalArea[i].CurrentState + ", " + _finalArea[i].StateChanged);
        }

        for (int i = 0; i < hexas.Count; i++)
        {
            //Logger.Debug(hexas[i].Orientation);
            if (!_finalArea.Contains(hexas[i]))
            {
                hexas[i].GameObject.SetActive(false);
            }
        }
        hexas[1].GameObject.transform.parent.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

        SetLayerRecursively(board, LayerMask.NameToLayer("Area"));


        /*RenderTexture currentRT = RenderTexture.active;
        RenderTexture.active = cameraArea.targetTexture;
        cameraArea.Render();
        RenderTexture.active = currentRT;*/
        rawImage.material.mainTexture = Resources.Load<RenderTexture>("images/TextureArea");        
    }
    public void UpdateArea()
    {
        SendBoardResponse sbr = RunicBoardManager.GetInstance()._runicBoardBehaviour.LatestSendBoardResponse;
        if (sbr != null && sbr._area != null && sbr._updateArea)
        {            
            sbr._updateArea = false;
            List<Hexagon> _finalArea = new List<Hexagon>();
            Area area = sbr._area;
            PlayBoard playBoard = new PlayBoard(5, 5);

            GameObject glyph = Resources.Load<GameObject>("prefabs/SM_Glyphe_1");

            if(!_alreadyCreated)
            {               
                hexas.Add(new Hexagon(0, 2, playBoard));
                hexas.Add(new Hexagon(1, 2, playBoard));
                hexas.Add(new Hexagon(2, 2, playBoard)); //centre
                hexas.Add(new Hexagon(3, 2, playBoard));
                hexas.Add(new Hexagon(4, 2, playBoard));

                hexas.Add(new Hexagon(1, 3, playBoard));
                hexas.Add(new Hexagon(2, 3, playBoard));
                hexas.Add(new Hexagon(3, 3, playBoard));
                hexas.Add(new Hexagon(4, 3, playBoard));

                hexas.Add(new Hexagon(2, 4, playBoard));
                hexas.Add(new Hexagon(3, 4, playBoard));
                hexas.Add(new Hexagon(4, 4, playBoard));

                hexas.Add(new Hexagon(0, 1, playBoard));
                hexas.Add(new Hexagon(1, 1, playBoard));
                hexas.Add(new Hexagon(2, 1, playBoard));
                hexas.Add(new Hexagon(3, 1, playBoard));

                hexas.Add(new Hexagon(0, 0, playBoard));
                hexas.Add(new Hexagon(1, 0, playBoard));
                hexas.Add(new Hexagon(2, 0, playBoard));

                for (int i = 0; i < hexas.Count; i++)
                {
                    Hexagon hexagon = playBoard.CreateHexagone(hexas[i]._posX, hexas[i]._posY);
                    hexagon.GameObject = new GameObject("Hexagon");
                    hexagon.GameObject.transform.parent = board.transform;
                    hexagon.GameObject.transform.localPosition = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, 0.2f, 0.75f * hexagon._posY);
                    hexagon.GameObject.transform.localScale = Vector3.one;

                    hexagon.Glyph = GameObject.Instantiate(glyph);
                    hexagon.Glyph.transform.parent = hexagon.GameObject.transform;
                    hexagon.Glyph.transform.localPosition = new Vector3(0,0,0);
                    hexagon.Glyph.transform.localScale = Vector3.one;

                    hexagon.CurrentState = Hexagon.State.Targetable;
                    hexagon.Orientation = hexas[i].Orientation;

                    hexas[i] = hexagon;
                }
                _alreadyCreated = true;
            }
            
            foreach(Hexagon hexa in hexas)
            {
                hexa.CurrentState = Hexagon.State.Targetable;
                hexa.GameObject.SetActive(true);
            }

            if (area.Orientation == Orientation.EnumOrientation.Line || area.Orientation == Orientation.EnumOrientation.Any)
                _finalArea = area.AreaToHexa(Direction.EnumDirection.East, hexas[1]);
            
            if (area.Orientation == Orientation.EnumOrientation.Diagonal)
                _finalArea = area.AreaToHexa(Direction.EnumDirection.DiagonalNorthEast, hexas[1]);

            for (int i = 0; i < _finalArea.Count; i++)
            {
                _finalArea[i].CurrentState = Hexagon.State.OverEnnemiTargetable;
                Logger.Debug(_finalArea[i]._posX + " " + _finalArea[i]._posY + " : " + _finalArea[i].CurrentState + ", " + _finalArea[i].StateChanged);
            }

            for (int i = 0; i < hexas.Count; i++)
            {
                //Logger.Debug(hexas[i].Orientation);
                if (!_finalArea.Contains(hexas[i]))
                {
                    hexas[i].GameObject.SetActive(false);
                }
            }
        }
    }
    public static PlayBoard JSONToBoard(ref GameObject board, String fileName)
    {
        board = new GameObject("Board");
        GameObject assets = new GameObject("Assets");
        assets.transform.parent = board.transform;
        JSONObject js = JSONObject.GetJsonObjectFromFile("JsonFiles/Maps/" + fileName);

        int width = (int)js.GetField("Width").n;
        int height = (int)js.GetField("Height").n;

        PlayBoard playBoard = new PlayBoard(width, height);

        JSONObject array = js.GetField("Hexagons");

        GameObject groundEffect = Resources.Load<GameObject>("VFX/Ground/Ground_Effect");
        GameObject shardEffect = Resources.Load<GameObject>("VFX/Shard_Zone/Shard_Zone_Effect"); 
        GameObject glyph = Resources.Load<GameObject>("prefabs/SM_Glyphe_1");

        JSONObject camera = js.GetField("Camera");
        if (camera != null)
        {
            float xCam = camera.GetField("x").f;
            float yCam = camera.GetField("y").f;
            float zCam = camera.GetField("z").f;

            float rotXCam = camera.GetField("rotX").f;
            float rotYCam = camera.GetField("rotY").f;
            float rotZCam = camera.GetField("rotZ").f;
            float rotWCam = camera.GetField("rotW").f;

            Camera.main.transform.position = new Vector3(xCam, yCam, zCam);
            Camera.main.transform.rotation = new Quaternion(rotXCam, rotYCam, rotZCam, rotWCam);
        }

        foreach (JSONObject hexa in array.list)
        {
            Hexagon hexagon = playBoard.CreateHexagone((int)hexa.GetField("posX").n, (int)hexa.GetField("posY").n);
            GameObject prefab = (GameObject)Resources.Load("Prefabs/" + hexa.GetField("gameObject").str, typeof(GameObject));
            hexagon.DefaultColor = prefab.GetComponent<Renderer>().sharedMaterial.color;
            hexagon.GameObject = GameObject.Instantiate(prefab);
            hexagon.GameObject.name = hexa.GetField("gameObject").str;
            hexagon.GameObject.transform.parent = board.transform;
            hexagon.GameObject.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n, 0.75f * hexagon._posY);

            JSONObject quaternion = hexa.GetField("rotation");
            if (quaternion == null)
            {
                hexagon.GameObject.transform.Rotate(EruleRandom.RangeValue(0, 7) * 180f, EruleRandom.RangeValue(0, 11) * 60f, 0f);
            }
            else
            {
                hexagon.GameObject.transform.rotation = new Quaternion(quaternion.GetField("x").f, quaternion.GetField("y").f, quaternion.GetField("z").f, quaternion.GetField("w").f);
            }
            


            hexagon.Glyph = GameObject.Instantiate(glyph);
            hexagon.Glyph.transform.parent = hexagon.GameObject.transform;
            hexagon.Glyph.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n+0.2f, 0.75f * hexagon._posY);
            hexagon.Glyph.SetActive(false);

            hexagon.GroundEffect = GameObject.Instantiate(groundEffect);
            hexagon.GroundEffect.transform.parent = hexagon.GameObject.transform;
            hexagon.GroundEffect.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n + 0.23f, 0.75f * hexagon._posY);
            hexagon.GroundEffect.SetActive(false);
            hexagon.GroundEffect.transform.rotation = Quaternion.Euler(0, EruleRandom.RangeValue(0.0f,360.0f), 0);

            hexagon.ShardEffect = GameObject.Instantiate(shardEffect);
            hexagon.ShardEffect.transform.parent = hexagon.GameObject.transform;
            hexagon.ShardEffect.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n + 0.23f, 0.75f * hexagon._posY);
            hexagon.ShardEffect.SetActive(false);
            hexagon.ShardEffect.transform.rotation = Quaternion.Euler(0, EruleRandom.RangeValue(0.0f, 360.0f), 0);



            hexagon.CurrentState = Hexagon.State.Default;

            if (hexa.GetField("isSpawn") != null)
            {
                hexagon.CurrentState = Hexagon.State.Spawnable;
                hexagon.IsSpawn = true;
            }
            else
            {
                hexagon.IsSpawn = false;
            }

            /*if (hexa.GetField("underground") != null) {
                string undergroundName = hexa.GetField("underground").str;
                if (undergroundName != null)
                {
                    GameObject prefabObstacle = (GameObject)Resources.Load("Prefabs/" + undergroundName, typeof(GameObject));
                    GameObject underground = GameObject.Instantiate(prefabObstacle);

                    underground.transform.parent = hexagon.GameObject.transform;

                    underground.name = undergroundName;
                    underground.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n - 0.2f, 0.75f * hexagon._posY);

                    underground.transform.Rotate(0f, EruleRandom.RangeValue(0, 5) * 60f, 0f);

                    hexagon.Underground = underground;

                }
            }*/


            if (hexa.GetField("obstacle") != null) {
                string obstacleName = hexa.GetField("obstacle").str;
                if (obstacleName != null)
                {
                    GameObject prefabObstacle = (GameObject)Resources.Load("Prefabs/" + obstacleName, typeof(GameObject));
                    GameObject asset = GameObject.Instantiate(prefabObstacle);
                    asset.transform.parent = assets.transform;
                    asset.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n, 0.75f * hexagon._posY);


                    Obstacle obs = new Obstacle(hexagon);
                    obs.GameObject = new GameObject(prefabObstacle.name);
                    obs.GameObject.transform.parent = hexagon.GameObject.transform;
                    obs.GameObject.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n, 0.75f * hexagon._posY);
                    CapsuleCollider collider = obs.GameObject.AddComponent<CapsuleCollider>();
                    collider.height = 5;
                    collider.radius = 0.5f;
                    obs.GameObject.layer = LayerMask.NameToLayer("Obstacle");
                    obs.GameObject.transform.Rotate(0f, EruleRandom.RangeValue(0, 5) * 60f, 0f);
                }
            }

            if (hexa.GetField("powerShard") != null)
            {
                PowerShard ps = PowerShard.JSONToPowerShard(hexa.GetField("powerShard"), hexagon);

                GameObject prefabShard = (GameObject)Resources.Load("Prefabs/" + hexa.GetField("powerShard").GetField("gameObject").str, typeof(GameObject));
                GameObject asset = GameObject.Instantiate(prefabShard);
                asset.transform.parent = assets.transform;
                asset.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n, 0.75f * hexagon._posY);

                ps.GameObject.transform.parent = hexagon.GameObject.transform;
                ps.GameObject.transform.position = new Vector3(0.866f * hexagon._posX - 0.433f * hexagon._posY, hexa.GetField("posZ").n, 0.75f * hexagon._posY);
                ps.GameObject.layer = LayerMask.NameToLayer("Obstacle");

                playBoard.AddPowerShard(ps);
            }
        }

        JSONObject centerJs = js.GetField("Center");
        if (centerJs != null) { 
            Vector3 center = new Vector3(centerJs.GetField("x").n, centerJs.GetField("y").n, centerJs.GetField("z").n);
            playBoard._center = center;
            Logger.Debug(center);
        }
        
        return playBoard;
    }