// Previously tried solution with collision detection on all separate clients for all players(instead of events).
    // But the result was that some tile marking got skipped if the clients skipped walking over them because of a bad connection.
    // This way we can be sure all tiles are getting marked.
    public void SetTileMarker(InGameMazeTile tile, PlayerCharacter player)
    {
        if (GameRules.GamePlayerType == GamePlayerType.SinglePlayer ||
            GameRules.GamePlayerType == GamePlayerType.SplitScreenMultiplayer)
        {
            player.LastTile = tile;

            MazeTilePath mazeTilePath = (MazeTilePath)tile.GetBackgrounds().FirstOrDefault(background => background is MazeTilePath);
            if (mazeTilePath == null)
            {
                return;
            }

            PlayerMark playerMark = new PlayerMark(mazeTilePath.ConnectionScore);

            HandlePlayerMarkerSprite(tile, player.PlayerNumber, playerMark);
            HandlePlayerTileMarkerEnds(tile);
            HandleNumberOfUnmarkedTiles();

            tile.ResetPlayerMarkEndsRenderer();

            tile.TriggerTransformations();
        }
        else
        {
            PlayerMarksTileEvent playerMarksTileEvent = new PlayerMarksTileEvent();
            playerMarksTileEvent.SendPlayerMarksTileEvent(tile.GridLocation, player);
        }
    }
    public void OnEvent(EventData photonEvent)
    {
        byte eventCode = photonEvent.Code;

        if (eventCode == PlayerMarksTileEvent.PlayerMarksTileEventCode)
        {
            object[]     data         = (object[])photonEvent.CustomData;
            GridLocation tileLocation = new GridLocation((int)data[0], (int)data[1]);
            PlayerNumber playerNumber = (PlayerNumber)data[2];

            InGameMazeTile tile = Level.TilesByLocation[tileLocation] as InGameMazeTile;

            MazeTilePath mazeTilePath = (MazeTilePath)tile.GetBackgrounds().FirstOrDefault(background => background is MazeTilePath);
            if (mazeTilePath == null)
            {
                return;
            }

            PlayerMark playerMark = new PlayerMark(mazeTilePath.ConnectionScore);

            HandlePlayerMarkerSprite(tile, playerNumber, playerMark);
            HandlePlayerTileMarkerEnds(tile);
            HandleNumberOfUnmarkedTiles();

            tile.ResetPlayerMarkEndsRenderer();

            tile.TriggerTransformations();
        }
        else if (eventCode == LoadNextMazeLevelEvent.LoadNextMazeLevelEventCode)
        {
            object[] data        = (object[])photonEvent.CustomData;
            string   pickedLevel = (string)data[0];

            MazeLevelData mazeLevelData = MazeLevelLoader.LoadMazeLevelData(pickedLevel);

            if (mazeLevelData == null)
            {
                Logger.Error($"Could not load maze level data for the randomly picked maze level {pickedLevel}");
            }

            PersistentGameManager.SetCurrentSceneName(pickedLevel);

            IEnumerator loadLevelCoroutine = LoadOverworldCoroutine("Overworld");
            StartCoroutine(loadLevelCoroutine);
        }
        else if (eventCode == LoadOverworldEvent.LoadOverworldEventCode)
        {
            object[] data          = (object[])photonEvent.CustomData;
            string   overworldName = (string)data[0];

            PersistentGameManager.SetLastMazeLevelName(PersistentGameManager.CurrentSceneName);
            PersistentGameManager.SetCurrentSceneName(PersistentGameManager.OverworldName);

            IEnumerator loadLevelCoroutine = LoadOverworldCoroutine("Overworld");
            StartCoroutine(loadLevelCoroutine);
        }
        else if (eventCode == PlayerCollidesWithMusicInstrumentCaseEvent.PlayerCollidesWithMusicInstrumentCaseEventCode)
        {
            object[]     data         = (object[])photonEvent.CustomData;
            GridLocation tileLocation = new GridLocation((int)data[0], (int)data[1]);
            PlayerNumber playerNumber = (PlayerNumber)data[2];

            InGameMazeTile tile = Level.TilesByLocation[tileLocation] as InGameMazeTile;

            MusicInstrumentCase musicInstrumentCase = (MusicInstrumentCase)tile.GetAttributes().FirstOrDefault(attribute => attribute is MusicInstrumentCase);
            if (musicInstrumentCase == null)
            {
                Logger.Error("Could not find musicInstrumentCase");
            }

            MazePlayerCharacter player = GameManager.Instance.CharacterManager.GetPlayers <MazePlayerCharacter>()[playerNumber];
            musicInstrumentCase.PlayerCollisionOnTile(player);
        }
        else if (eventCode == EnemyCollidesWithMusicInstrumentCaseEvent.EnemyCollidesWithMusicInstrumentCaseEventCode)
        {
            object[]     data         = (object[])photonEvent.CustomData;
            GridLocation tileLocation = new GridLocation((int)data[0], (int)data[1]);
            int          enemyId      = (int)data[2];

            InGameMazeTile tile = Level.TilesByLocation[tileLocation] as InGameMazeTile;

            MusicInstrumentCase musicInstrumentCase = (MusicInstrumentCase)tile.GetAttributes().FirstOrDefault(attribute => attribute is MusicInstrumentCase);
            if (musicInstrumentCase == null)
            {
                Logger.Error("Could not find musicInstrumentCase");
            }

            MazeCharacterManager characterManager = GameManager.Instance.CharacterManager as MazeCharacterManager;

            EnemyCharacter enemyCharacter = characterManager.Enemies.FirstOrDefault(enemy => enemy.PhotonView.ViewID == enemyId);
            if (enemyCharacter == null)
            {
                Logger.Error("Could not find enemy character");
            }
            musicInstrumentCase.EnemyCollisinOnTile(enemyCharacter);
        }
    }