Thanks @normen and @pspeed
Sorry for the hypothetical. I am just trying to understand the concept.
I want a box that spawns smaller boxes.
This is a test. I cant figure out how to get the AssetManager inside the controller.
for cases where I want to get new Materials or other Models from my assets.
Am I not using Controller correctly?
Thanks for the info.
[java]
package com.scriptblocks.flappymonkey.controller;
import com.jme3.asset.AssetManager;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Box;
import java.io.IOException;
public class SpawnerControl extends AbstractControl {
private static final Box box;
public float speed = 3;
private float timeSenceLastCreation = 0;
Material childMat;
static {
box = new Box(.5f, .5f, .5f);
box.scaleTextureCoordinates(new Vector2f(1f, .5f));
}
@Override
protected void controlUpdate(float tpf) {
timeSenceLastCreation = timeSenceLastCreation + tpf;
if (timeSenceLastCreation > speed) {
System.out.println("Create");
makeBlock(new Vector3f(0, 0, 0));
timeSenceLastCreation = 0;
}
}
protected Node getRootNode() {
//assuming parent is roodNode
return spatial.getParent();
}
protected AssetManager getAssetManager() {
return null; //How to get this
}
protected Material getMaterial() {
return ((Geometry)spatial).getMaterial();
}
public Control cloneForSpatial(Spatial spatial) {
SpawnerControl control = new SpawnerControl();
return control;
}
…
public void makeBlock(Vector3f loc) {
Geometry block = new Geometry("brick", box);
block.setMaterial(getMaterial());
block.setLocalTranslation(loc);
block.addControl(new BlockControl());
getRootNode().attachChild(block);
}
}
[/java]
[java]
package com.scriptblocks.flappymonkey;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.scriptblocks.flappymonkey.controller.SpawnerControl;
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
geom.addControl(new SpawnerControl());
rootNode.attachChild(geom);
}
@Override
public void simpleUpdate(float tpf) {
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]