Hi everyone. I have a problem with my game. I shoot a ball into wooden boxes. How to do that for every box mattered point? I sit on this 2 days and I do not have anything concrete. Can anyone help ? I’m a new jMonkey programmer and i don’t know everything. Please, help me.
Main class
public void simpleInitApp() {
initCrossHairs();
initMaterial();
initAudio();
setupLight();
setDisplayFps(false);
setDisplayStatView(false);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
hud = new HUD(assetManager, guiNode, settings.getWidth(), settings.getHeight());
hud.reset();
cam.setLocation(new Vector3f(0.26924422f, 6.646658f, 22.265987f));
cam.setRotation(new Quaternion(-2.302544E-4f, 0.99302495f, -0.117888905f, -0.0019395084f));
bulletAppState = new BulletAppState();
bulletAppState.setEnabled(true);
stateManager.attach(bulletAppState);
bullet = new Sphere(20, 20, 1.0f, true, false);
bullet.setTextureMode(TextureMode.Projected);
bulletCollisionShape = new SphereCollisionShape(1.0f);
Physics.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
speed = 1.3f;
flyCam.setMoveSpeed(50);
///////////Strzelanie i Spawning
Material boxes = new Material(getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
boxes.setTexture("DiffuseMap", getAssetManager().loadTexture("Interface/box_texture.jpg"));
Mesh box = new Box(15, 1, 15);
Geometry floor = new Geometry("floor", box);
floor.setMaterial(boxes);
TangentBinormalGenerator.generate(floor);
floor.setLocalTranslation(0, -2.5f, 0);
getRootNode().attachChild(floor);
RigidBodyControl floor_control = new RigidBodyControl(0);
floor.addControl(floor_control);
bulletAppState.getPhysicsSpace().add(floor_control);
for (int i = 0; i < 10; i++)
{
list.add(new BoxGuy(this, list.size()));
}
inputManager.addListener(new ActionListener() {
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals("Shoot") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(matBullet);
Vector3f camLocation = getCamera().getLocation();
bulletg.setLocalTranslation(getCamera().getDirection().scaleAdd(3, camLocation));
bulletg.setLocalTranslation(getCamera().getDirection().scaleAdd(3, camLocation));
bulletg.setLocalScale(bulletSize);
bulletCollisionShape = new SphereCollisionShape(bulletSize);
RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, bulletSize * 10);
bulletNode.setCcdMotionThreshold(0.001f);
bulletNode.setLinearVelocity(cam.getDirection().mult(40));
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
audio_gun.playInstance();
}
///zerknij na to jeszcze
if (binding.equals("Spawn")) {
if (isPressed) {
spawning = true;
}
} else if (binding.equals("Left")) {
left = isPressed;
} else if (binding.equals("Right")) {
right = isPressed;
} else if (binding.equals("Up")) {
up = isPressed;
} else if (binding.equals("Down")) {
down = isPressed;
} else if (binding.equals("Jump")) {
if (isPressed) {
player.jump();
}
}
}
}, "Jump", "Shoot", "Spawn", "Left", "Right", "Up", "Down");
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("Spawn", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
assetManager.registerLocator("town.zip", ZipLocator.class);
sceneModel = assetManager.loadModel("main.scene");
sceneModel.setLocalScale(2f);
CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 10, 0));
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
private void initAudio() {
/* gun shot sound is to be triggered by a mouse click. */
audio_gun = new AudioNode(assetManager, "Sounds/Effects/Gun.wav", false);
audio_gun.setPositional(false);
audio_gun.setLooping(false);
audio_gun.setVolume(10);
rootNode.attachChild(audio_gun);
}
private void setupLight() {
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.1f));
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}
private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
public void initMaterial() {
matBullet = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
key2.setGenerateMips(true);
Texture tex2 = assetManager.loadTexture(key2);
matBullet.setTexture("ColorMap", tex2);
Texture west = assetManager.loadTexture("Textures/Sky/west.jpg");
Texture east = assetManager.loadTexture("Textures/Sky/east.jpg");
Texture north = assetManager.loadTexture("Textures/Sky/north.jpg");
Texture south = assetManager.loadTexture("Textures/Sky/south.jpg");
Texture up_sky = assetManager.loadTexture("Textures/Sky/up.jpg");
Texture down_sky = assetManager.loadTexture("Textures/Sky/down.jpg");
Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up_sky, down_sky);
rootNode.attachChild(sky);
}
protected void initCrossHairs() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
public void collide(Bone bone, PhysicsCollisionObject object, PhysicsCollisionEvent event) {
if (object.getUserObject() != null && object.getUserObject() instanceof Geometry) {
Geometry geom = (Geometry) object.getUserObject();
if ("Floor".equals(geom.getName())) {
return;
}
hud.addPoints(0);
}
}
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.4f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
listener.setLocation(cam.getLocation());
listener.setRotation(cam.getRotation());
for (BoxGuy b : list) {
b.simpleUpdate(tpf);
}
if (spawning) {
for(int i=0; i<10; i++)
{
list.add(new BoxGuy(this, list.size()));
}
spawning = false;
}
hud.updateHUD();
}
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName)
{
}
public void onAnimChange(AnimControl control, AnimChannel channel, String animName)
{
}
public BulletAppState getBulletAppState() {
return bulletAppState;
}
}
Boxes class
public class BoxGuy {
Geometry guy;
RigidBodyControl guy_control;
ShootingTown app;
int health = 10;
public BoxGuy(ShootingTown s, int i) {
app = s;
Material boxes = new Material(s.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
boxes.setTexture("DiffuseMap", s.getAssetManager().loadTexture("Interface/box_texture.jpg"));
Mesh box = new Box(1.5f, 1.5f, 1.5f);
guy = new Geometry(Integer.toString(i), box);
guy.setMaterial(boxes);
TangentBinormalGenerator.generate(guy);
guy.setLocalTranslation((float) (Math.random() * 400) - 150, 30, (float) (Math.random() * 50) - 25);
app.getRootNode().attachChild(guy);
guy_control = new RigidBodyControl(10);
guy.addControl(guy_control);
app.getBulletAppState().getPhysicsSpace().add(guy_control);
}
public void simpleUpdate(float tpf)
{
}
public void hurt() {
guy.setLocalScale((float) Math.random() * .25f + .75f);
health--;
if (health < 1) {
guy.removeFromParent();
app.getBulletAppState().getPhysicsSpace().remove(guy_control);
}
}
public int getId()
{
return Integer.parseInt(guy.getName());
}
}