load static method

Future<void> load(
  1. String file,
  2. LevelScene scene
)

Implementation

static Future<void> load(String file, LevelScene scene) async{
  String data;
  if(kIsWeb){
    data = file;//utf8.decode(file.bytes);
  }
  else{
    data = await File(file).readAsString();
  }

  dynamic convert = json.decode(data);

  for(String keys in convert.keys){
    switch (keys) {
      case 'tileSets':
      if(convert[keys] == null) break;
        for(String set in convert[keys].keys){
          final Map<String,dynamic> grid = convert[keys][set]['grid'];
          int width = grid['width'];
          int height = grid['height'];
          await scene.updateTileset(
            path:convert[keys][set]['image'],
            name:convert[keys][set]['name'],
            gridUpdate:true,
            type: GridType.values[grid['type']],
            gridWidth: width,
            gridHeight: height,
            collisions: _getTileCollicions(grid['collisions'],grid['length'])
          );
        }
        break;
      case 'loadedObjects':
        if(convert[keys] == null) break;
        for(String set in convert[keys].keys){
          List<Rect> locations = [];
          List<String>? names = [];
          List<double>? scales = [];

          if(convert[keys][set]['objectScale'] != null){
            List<dynamic> temp = convert[keys][set]['objectScale'];
            for(int i = 0; i < temp.length; i++){
              scales.add(double.parse(temp[i].toString()));
            }
          }
          else{
            scales = null;
          }

          if(convert[keys][set]['spriteNames'] != null){
            List<dynamic> temp = convert[keys][set]['spriteNames'];
            for(int i = 0; i < temp.length; i++){
              names.add(temp[i].toString());
            }
          }
          else{
            names = null;
          }

          if(convert[keys][set]['spriteLocations'] != null){
            for(String location in convert[keys][set]['spriteLocations'].keys){
              locations.add(
                Rect.fromLTWH(
                  convert[keys][set]['spriteLocations'][location]['x'],
                  convert[keys][set]['spriteLocations'][location]['y'],
                  convert[keys][set]['spriteLocations'][location]['w'],
                  convert[keys][set]['spriteLocations'][location]['h']
                )
              );
            }
          }
          await scene.loadObject(
            convert[keys][set]['image'],
            LoadedType.values[convert[keys][set]['loadedType']],
            show: convert[keys][set]['visible'],
            locations: locations,
            scales: scales,
            objType: ObjectType.values[convert[keys][set]['objectType']],
            names: names
          );
        }
        break;
      case 'levels':
        List<Levels> levels = [];
        for(String level in convert[keys].keys){
          Grid grid = _getGrid(convert[keys][level]['grid']);
          levels.add(Levels(
            name: convert[keys][level]['name'],
            tileLayer: _getTileLayers(convert[keys][level]['tileLayer']),
            animations: _getAnimations(convert[keys][level]['animations']),
            objects: _getObjects(convert[keys][level]['objects'],scene),
            maxGridSize: Size(convert[keys][level]['maxGridWidth'].toDouble(),convert[keys][level]['maxGridHeight'].toDouble()),
            grid: grid
          ));
        }
        if(levels.isNotEmpty){
          scene.importLevels(levels);
        }
        break;
      default:
    }
  }
}