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

com.artemis.link.EntityBagFieldMutator Maven / Gradle / Ivy

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

import com.artemis.Component;
import com.artemis.Entity;
import com.artemis.EntitySubscription;
import com.artemis.World;
import com.artemis.utils.Bag;
import com.artemis.utils.reflect.Field;
import com.artemis.utils.reflect.ReflectionException;

import static com.artemis.Aspect.all;

class EntityBagFieldMutator implements MultiFieldMutator, Component> {
	private final Bag empty = new Bag();
	private EntitySubscription all;

	@Override
	public void validate(int sourceId, Bag entities, LinkListener listener) {
		for (int i = 0; entities.size() > i; i++) {
			Entity e = entities.get(i);
			if (!all.getActiveEntityIds().unsafeGet(e.getId())) {
				entities.remove(i--);
				if (listener != null)
					listener.onTargetDead(sourceId, e.getId());
			}
		}
	}

	@Override
	public Bag read(Component c, Field f) {
		try {
			Bag e = (Bag) f.get(c);
			return (e != null) ? e : empty;
		} catch (ReflectionException exc) {
			throw new RuntimeException(exc);
		}
	}

	@Override
	public void setWorld(World world) {
		all = world.getAspectSubscriptionManager().get(all());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy