use of io.github.voidzombie.nhglib.runtime.messaging.Message in project nhglib by VoidZombie.
the class SceneManager method loadAssets.
private void loadAssets(Integer entity) {
final ModelComponent modelComponent = modelMapper.get(entity);
// Group all assets
final Array<Asset> allAssets = new Array<>();
final Asset modelAsset = modelComponent.asset;
for (PbrMaterial mat : modelComponent.pbrMaterials) {
if (mat.albedoAsset != null) {
allAssets.add(mat.albedoAsset);
}
if (mat.metalnessAsset != null) {
allAssets.add(mat.metalnessAsset);
}
if (mat.roughnessAsset != null) {
allAssets.add(mat.roughnessAsset);
}
if (mat.normalAsset != null) {
allAssets.add(mat.normalAsset);
}
if (mat.ambientOcclusionAsset != null) {
allAssets.add(mat.ambientOcclusionAsset);
}
}
// Start loading them
assets.queueAsset(modelAsset);
// Wait for them
messaging.get(Strings.Events.assetLoaded).subscribe(new Consumer<Message>() {
@Override
public void accept(Message message) throws Exception {
Asset asset = (Asset) message.data.get(Strings.Defaults.assetKey);
if (asset != null) {
if (asset.is(modelAsset.alias)) {
Model model = assets.get(asset);
modelComponent.model = new ModelInstance(model);
if (modelComponent.model.animations.size > 0) {
modelComponent.animationController = new AnimationController(modelComponent.model);
}
assets.queueAssets(allAssets);
} else {
if (allAssets.contains(asset, false)) {
temporaryLoadedAssets.add(asset);
allAssets.removeValue(asset, false);
Bundle bundle = new Bundle();
bundle.put("size", allAssets.size);
bundle.put("modelComponent", modelComponent);
sizeSubject.onNext(bundle);
}
}
}
}
});
}
use of io.github.voidzombie.nhglib.runtime.messaging.Message in project nhglib by VoidZombie.
the class Main method engineStarted.
@Override
public void engineStarted() {
super.engineStarted();
Nhg.debugLogs = true;
Gdx.input.setCursorCatched(true);
world = new NhgWorld(nhg.messaging, nhg.entities, nhg.assets, new DefaultWorldStrategy(), new Bounds(2f, 2f, 2f));
fpsLogger = new FPSLogger();
renderer20 = new ImmediateModeRenderer20(false, true, 0);
nhg.input.addListener(this);
nhg.assets.queueAsset(new Asset("scene", "myscene.nhs", Scene.class));
nhg.assets.queueAsset(new Asset("inputMap", "input.nhc", JsonValue.class));
GraphicsSystem graphicsSystem = nhg.entities.getEntitySystem(GraphicsSystem.class);
graphicsSystem.setClearColor(Color.GRAY);
Environment environment = graphicsSystem.getEnvironment();
GammaCorrectionAttribute gammaCorrectionAttribute = new GammaCorrectionAttribute();
gammaCorrectionAttribute.gammaCorrection = true;
environment.set(gammaCorrectionAttribute);
// Subscribe to asset events
nhg.messaging.get(Strings.Events.assetLoaded, Strings.Events.assetLoadingFinished).subscribe(new Consumer<Message>() {
@Override
public void accept(Message message) throws Exception {
if (message.is(Strings.Events.assetLoaded)) {
Asset asset = (Asset) message.data.get(Strings.Defaults.assetKey);
if (asset.is("scene")) {
scene = nhg.assets.get(asset);
world.loadScene(scene);
world.setReferenceEntity("camera");
ModelBuilder mb = new ModelBuilder();
Model planeModel = mb.createBox(2f, 0.01f, 20f, new Material(), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates);
int plane = scene.sceneGraph.createSceneEntity("plane");
scene.sceneGraph.addSceneEntity(plane);
ModelComponent modelComponent = nhg.entities.createComponent(plane, ModelComponent.class);
modelComponent.initWithModel(planeModel);
NodeComponent nodeComponent = nhg.entities.getComponent(plane, NodeComponent.class);
nodeComponent.setTranslation(0, 0, 0, true);
Integer cameraEntity = scene.sceneGraph.getSceneEntity("camera");
cameraNode = nhg.entities.getComponent(cameraEntity, NodeComponent.class);
} else if (asset.is("inputMap")) {
nhg.input.fromJson((JsonValue) nhg.assets.get(asset));
nhg.input.setActiveContext("game", true);
nhg.input.setActiveContext("global", true);
}
}
}
});
}
Aggregations