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.reflect.ClassReflection;

import java.util.BitSet;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Set;

/**
 * During saving, this class is responsible for collecting all used
 * component types which aren't annotated with {@link Transient}.
 */
public class ComponentCollector {
	private BitSet componentIds = new BitSet();
	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();
		IdentityHashMap, String> lookup = save.componentIdentifiers;

		Set names = new HashSet();
		BitSet bs = componentIds;
		for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
			Class type = cm.typeFactory.getTypeFor(i).getType();
			if (ClassReflection.getDeclaredAnnotation(type, Transient.class) != null)
				continue;

			lookup.put(type, resolveNameId(names, 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) {
		EntityManager em = world.getEntityManager();

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy