package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
public class TicTacToeMain extends SimpleApplication
{
public static void main(String [] args)
{
TicTacToeMain application = new TicTacToeMain();
application.start();
}
Node selectables;
Geometry selectPos;
@Override
public void simpleInitApp()
{
setupPointer();
setupSelectPos();
setupKeys();
selectables = new Node(“Selectables”);
rootNode.attachChild(selectables);
selectables.attachChild(makeBigSquare(“1 big square”,-4f,3f,0f));
selectables.attachChild(makeSquare(“1 small square”,-4.6f,3.6f,0f));
selectables.attachChild(makeSquare(“2 small square”,-4.0f,3.6f,0f));
selectables.attachChild(makeSquare(“3 small square”,-3.4f,3.6f,0f));
selectables.attachChild(makeSquare(“4 small square”,-4.6f,3f,0f));
selectables.attachChild(makeSquare(“5 small square”,-4.0f,3f,0f));
selectables.attachChild(makeSquare(“6 small square”,-3.4f,3f,0f));
selectables.attachChild(makeSquare(“7 small square”,-4.6f,2.4f,0f));
selectables.attachChild(makeSquare(“8 small square”,-4.0f,2.4f,0f));
selectables.attachChild(makeSquare(“9 small square”,-3.4f,2.4f,0f));
selectables.attachChild(makeBigSquare(“2 big square”,-1f,1f,0f));
selectables.attachChild(makeSquare(“10 small square”,-1.6f,1.6f,0f));
selectables.attachChild(makeSquare(“11 small square”,-1f,1.6f,0f));
selectables.attachChild(makeSquare(“12 small square”,-0.4f,1.6f,0f));
selectables.attachChild(makeSquare(“13 small square”,-1.6f,1f,0f));
selectables.attachChild(makeSquare(“14 small square”,-1f,1f,0f));
selectables.attachChild(makeSquare(“15 small square”,-0.4f,1f,0f));
selectables.attachChild(makeSquare(“16 small square”,-1.6f,0.4f,0f));
selectables.attachChild(makeSquare(“17 small square”,-1f,0.4f,0f));
selectables.attachChild(makeSquare(“18 small square”,-0.4f,0.4f,0f));
selectables.attachChild(makeBigSquare(“3 big square”,2f,-1f,0f));
selectables.attachChild(makeSquare(“19 small square”,1.4f,-0.4f,0f));
selectables.attachChild(makeSquare(“20 small square”,2f,-0.4f,0f));
selectables.attachChild(makeSquare(“21 small square”,2.6f,-0.4f,0f));
selectables.attachChild(makeSquare(“22 small square”,1.4f,-1f,0f));
selectables.attachChild(makeSquare(“23 small square”,2f,-1f,0f));
selectables.attachChild(makeSquare(“24 small square”,2.6f,-1f,0f));
selectables.attachChild(makeSquare(“25 small square”,1.4f,-1.6f,0f));
selectables.attachChild(makeSquare(“26 small square”,2f,-1.6f,0f));
selectables.attachChild(makeSquare(“27 small square”,2.6f,-1.6f,0f));
}
private void setupKeys()
{
inputManager.addMapping(“Select”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, “Select”);
}
private ActionListener actionListener = new ActionListener()
{
public void onAction(String name, boolean keyPressed, float tpf)
{
if(name.equals(“Select”) && !keyPressed)
{
CollisionResults res = new CollisionResults();
Ray r = new Ray(cam.getLocation(), cam.getDirection());
selectables.collideWith(r, res);
if(res.size() > 0)
{
CollisionResult closest = res.getClosestCollision();
selectPos.setLocalTranslation(closest.getContactPoint());
selectables.attachChild(selectPos);
}
}
}
};
@Override
public void simpleUpdate(float tpf)
{
}
protected Geometry makeSquare(String name, float x, float y, float z)
{
Box b = new Box(new Vector3f(x,y,z),0.25f,0.25f,0f);
Geometry g = new Geometry(name, b);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.randomColor());
g.setMaterial(mat1);
return g;
}
protected Geometry makeBigSquare(String name, float x, float y, float z)
{
Box b = new Box(new Vector3f(x,y,z),1,1,0);
Geometry g = new Geometry(name, b);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.randomColor());
g.setMaterial(mat1);
return g;
}
protected void setupSelectPos()
{
Sphere s = new Sphere(30,30,0.2f);
selectPos = new Geometry(“chosen”, s);
Material mat2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat2.setColor(“Color”, ColorRGBA.Red);
selectPos.setMaterial(mat2);
}
protected void setupPointer()
{
guiNode.detachAllChildren();
guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);
BitmapText c = new BitmapText(guiFont, false);
c.setSize(guiFont.getCharSet().getRenderedSize()*2);
c.setText("+");
c.setLocalTranslation(settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, settings .getHeight() / 2 + c.getLineHeight() / 2, 0);
guiNode.attachChild©;
}
}
please can somebody help me, i would like to know how to display an x or o on the screen when a user clicks a square and keep it there
Don’t double post!