use of com.b3dgs.lionengine.game.Feature in project lionengine by b3dgs.
the class FeatureModelTest method testModel.
/**
* Test the feature model.
*/
@Test
void testModel() {
final Featurable featurable = new FeaturableModel(services, setup);
final Transformable transformable = new TransformableModel(services, setup);
featurable.addFeature(transformable);
feature.prepare(new FeatureModel(services, setup) {
@Override
public boolean hasFeature(Class<? extends Feature> feature) {
return false;
}
});
feature.prepare(featurable);
assertEquals(featurable.getFeature(Transformable.class), feature.getFeature(Transformable.class));
assertEquals(transformable, feature.getFeature(Transformable.class));
final Feature feature = featurable.getFeatures().iterator().next();
assertTrue(feature.equals(featurable.getFeature(Recycler.class)) || feature.equals(featurable.getFeature(Identifiable.class)) || feature.equals(transformable), feature.getClass().getName());
for (final Class<? extends Feature> type : feature.getFeaturesType()) {
assertTrue(Recycler.class.isAssignableFrom(type) || Identifiable.class.isAssignableFrom(type) || IdentifiableModel.class.isAssignableFrom(type) || Transformable.class.isAssignableFrom(type) || TransformableModel.class.isAssignableFrom(type), type.toString());
}
assertTrue(feature.hasFeature(Transformable.class));
}
use of com.b3dgs.lionengine.game.Feature in project lionengine by b3dgs.
the class FeaturableConfig method getFeatures.
/**
* Get all available features.
* Default constructor of each feature must be available or with {@link Setup} as single parameter.
*
* @param loader The class loader reference.
* @param services The services reference.
* @param setup The setup reference.
* @param filter The type filter (can be <code>null</code> if none).
* @return The available features.
* @throws LionEngineException If invalid class.
*/
public static List<Feature> getFeatures(ClassLoader loader, Services services, Setup setup, Class<?> filter) {
final Collection<XmlReader> children;
final XmlReader root = setup.getRoot();
if (root.hasNode(NODE_FEATURES)) {
children = setup.getRoot().getChild(FeaturableConfig.NODE_FEATURES).getChildren(FeaturableConfig.NODE_FEATURE);
} else {
children = Collections.emptyList();
}
final List<Feature> features = new ArrayList<>(children.size());
for (final XmlReader featureNode : children) {
final String className = featureNode.getText();
try {
final Class<? extends Feature> clazz = getClass(loader, className);
if (filter == null || filter.isAssignableFrom(clazz)) {
final Feature feature = UtilReflection.createReduce(clazz, services, setup);
features.add(feature);
}
} catch (final NoSuchMethodException | LionEngineException exception) {
throw new LionEngineException(exception, setup.getMedia());
}
}
children.clear();
return features;
}
use of com.b3dgs.lionengine.game.Feature in project lionengine by b3dgs.
the class HandlablesImpl method remove.
/**
* Remove the featurable and all its references.
*
* @param featurable The featurable reference.
* @param id The featurable ID.
*/
public void remove(Featurable featurable, Integer id) {
for (final Class<?> type : featurable.getClass().getInterfaces()) {
remove(type, featurable);
}
for (final Class<? extends Feature> feature : featurable.getFeaturesType()) {
final Feature object = featurable.getFeature(feature);
remove(feature, object);
for (final Class<?> other : UtilReflection.getInterfaces(feature, Feature.class)) {
remove(other, object);
}
}
remove(featurable.getClass(), featurable);
removeSuperClass(featurable, featurable.getClass());
featurables.remove(id);
}
use of com.b3dgs.lionengine.game.Feature in project lionengine by b3dgs.
the class FeaturesTest method testFeatures.
/**
* Test the features.
*/
@Test
void testFeatures() {
assertFalse(features.contains(Feature.class));
final Identifiable identifiable = new IdentifiableModel();
features.add(identifiable);
assertEquals(identifiable, features.get(Identifiable.class));
assertEquals(identifiable, features.get(IdentifiableModel.class));
int i = 0;
for (final Feature current : features.getFeatures()) {
assertEquals(identifiable, current);
i++;
}
assertEquals(1, i);
i = 0;
for (final Class<? extends Feature> type : features.getFeaturesType()) {
assertTrue(Feature.class.isAssignableFrom(type));
i++;
}
assertEquals(2, i);
}
use of com.b3dgs.lionengine.game.Feature in project lionengine by b3dgs.
the class FeaturableConfigTest method testGetFeaturesUnknown.
/**
* Test with unknown feature class.
*/
@Test
void testGetFeaturesUnknown() {
final Xml root = new Xml("test");
root.createChild(FeaturableConfig.NODE_FEATURES).createChild(FeaturableConfig.NODE_FEATURE).setText(Feature.class.getName());
final Media media = Medias.create("Object.xml");
root.save(media);
assertCause(() -> FeaturableConfig.getFeatures(ClassLoader.getSystemClassLoader(), new Services(), new Setup(media)), NoSuchMethodException.class);
}
Aggregations