All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.artemis.ComponentCollector Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.artemis;

import com.artemis.annotations.Transient;
import com.artemis.io.SaveFileFormat;
import com.artemis.utils.BitVector;

import java.util.*;

/**
 * During saving, this class is responsible for collecting all used
 * component types which aren't annotated with {@link Transient}.
 */
public class ComponentCollector {
	private BitVector componentIds = new BitVector();
	private Set> referencedComponents = new HashSet>();

	private World world;

	public ComponentCollector(World world) {
		this.world = world;
	}

	public void preWrite(SaveFileFormat save) {
		componentIds.clear();
		referencedComponents.clear();

		inspectComponentTypes(save);
		extractComponents(save);
	}

	protected void extractComponents(SaveFileFormat save) {
		ComponentManager cm = world.getComponentManager();
		Map, String> lookup = save.componentIdentifiers.typeToName;

		Set names = new HashSet();
		BitVector bs = componentIds;
		SaveFileFormat.ComponentIdentifiers identifiers = save.componentIdentifiers;
		for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
			Class type = cm.typeFactory.getTypeFor(i).getType();
			lookup.put(type, resolveNameId(names, type));
			if (identifiers.typeToId.get(type) == null) {
				identifiers.typeToId.put(type, lookup.size());
				identifiers.idToType.put(lookup.size(), type);
			}
		}
	}

	private String resolveNameId(Set existing, Class type) {
		String name = type.getSimpleName();
		if (existing.contains(name)) {
			int index = 2;
			while(existing.contains(name + "_" + index++));
			name += "_" + (index - 1);
		}

		existing.add(name);
		return name;
	}

	protected void inspectComponentTypes(SaveFileFormat save) {
		ComponentManager cm = world.getComponentManager();

		int[] ids = save.entities.getData();
		for (int i = 0, s = save.entities.size(); s > i; i++)
			componentIds.or(cm.componentBits(ids[i]));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy