fromJSON method

EntityManager fromJSON(
  1. Map<String, dynamic> json
)

Restores this instance from the given JSON object.

Implementation

EntityManager fromJSON(Map<String,dynamic> json ) {
	clear();

	final entitiesJSON = json['entities'];
	final messageDispatcherJSON = json['_messageDispatcher'];

	// entities

	final Map<String,dynamic> entitiesMap = {};

	for ( int i = 0, l = entitiesJSON.length; i < l; i ++ ) {
		final entityJSON = entitiesJSON[ i ];
		final type = entityJSON.type;

		dynamic entity;

		switch ( type ) {
			case 'GameEntity':
				entity = GameEntity().fromJSON( entityJSON );
				break;
			case 'MovingEntity':
				entity = MovingEntity().fromJSON( entityJSON );
				break;
			case 'Vehicle':
				entity = Vehicle().fromJSON( entityJSON );
				break;
			case 'Trigger':
				entity = Trigger().fromJSON( entityJSON );
				break;
			default:

				// handle custom type

				final ctor = _typesMap[type];
				if ( ctor != null ) {
					entity = ctor().fromJSON( entityJSON );
				}
          else {
					yukaConsole.warning( 'YUKA.EntityManager: Unsupported entity type: $type');
					continue;
				}
		}

		entitiesMap[entity.uuid] = entity;

		if ( entity.parent == null ) add( entity );
	}

	// resolve UUIDs to game entity objects

	for ( final entity in entitiesMap.values ) {
		entity.resolveReferences( entitiesMap );
	}

	// restore delayed messages
	_messageDispatcher.fromJSON( messageDispatcherJSON );

	return this;
}