getLevel method

Future<Level> getLevel (int id)

Returns a Level with the specific id which is loaded from the web server

Implementation

Future<Level> getLevel(int id) async {
  Future<Map> jsonMap = LevelLoader._makeRequest(id);

  // initialise maps
  Map EntitiesMap = await jsonMap;
  Map paramsMap = EntitiesMap.putIfAbsent("params", () => null);

  this._createLevelStump(paramsMap);

  List<dynamic> entities = EntitiesMap.putIfAbsent("entities", () => List());
  List<Entity> queuedEntities = List<Entity>();

  // Counter to give each element an unique id
  int elementId = 1;

  entities.forEach((e) {
    String type = e.putIfAbsent("type", () => "");
    Entity model;

    if (type == "dot") {
      model = this._getDot(elementId, e);
    } else if (type == "brick") {
      model = this._getBrick(elementId, e);
    } else if (type == "barrier") {
      model = this._getBarrier(elementId, e);
    } else if (type == "doubleup") {
      model = this._getDoubleUp(elementId, e);
    } else if (type == "drill") {
      model = this._getDrill(elementId, e);
    } else if (type == "slowdown") {
      model = this._getSlowDown(elementId, e);
    }

    if (model != null) {
      model.dy = this._level.getVerticalMovementPerUpdate();
      queuedEntities.add(model);
    }
    elementId++;
  });
  this._level.remainingEntities = queuedEntities;
  return this._level;
}