Hi everybody,
i’ve got a problem. I am going to make a “simple” 3D-Space-Shooter. The player is in a spaceship and around him are some enemys. For every entity in the 3D Space there shall be a marker. This marker is shown, but if the playership is looking the exact opposite direction, the marker is still visible, although the entity is not on the screen. Maybe some of you know the solution for this. The HashSet contains the entity for the position as the key and the picture as the value. The main part is in the update-method. Here is the code:
[java]
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.jme3.asset.AssetManager;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.ui.Picture;
import de.cet.pa.controller.Start;
import de.cet.pa.controller.eventsystem.EntityEvent;
import de.cet.pa.controller.eventsystem.EnvironmentListener;
import de.cet.pa.controller.eventsystem.ProjectileListener;
import de.cet.pa.controller.eventsystem.ShipListener;
import de.cet.pa.model.ControllableShip;
import de.cet.pa.model.Entity;
import de.cet.pa.model.Environment;
import de.cet.pa.model.Projectile;
import de.cet.pa.model.Shield;
import de.cet.pa.model.Ship;
public class HUDManager implements ShipListener, EnvironmentListener,
ProjectileListener {
private int width;
private int height;
private Node guiNode;
private AssetManager assetManager;
Camera cam;
HashMap<Entity, Picture> entitys = new HashMap<Entity, Picture>();
ControllableShip ship;
public HUDManager(Node guiNode, int width, int height, Camera cam) {
this.guiNode = guiNode;
this.width = width;
this.height = height;
this.cam = cam;
this.assetManager = Start.MAIN.getAssetManager();
this.assetManager.registerLocator("assets/", FileLocator.class);
initCrossHair();
initBackground();
initInterfaceBackgroundRight();
initInterfaceBackgroundLeft();
}
private void initBackground() {
Picture backgroundRight = new Picture("BackgroundRight");
backgroundRight.setImage(this.assetManager,
"Textures/Hud/BackgroundSideRight.png", true);
float picwidth = 500;
float picheight = 300;
backgroundRight.setWidth(picwidth);
backgroundRight.setHeight(picheight);
float x = this.width - picwidth;
float y = 0;
backgroundRight.setPosition(x, y);
this.guiNode.attachChild(backgroundRight);
Picture backgroundLeft = new Picture("BackgroundLeft");
backgroundLeft.setImage(this.assetManager,
"Textures/Hud/BackgroundSideLeft.png", true);
backgroundLeft.setWidth(picwidth);
backgroundLeft.setHeight(picheight);
x = 0;
y = 0;
backgroundLeft.setPosition(x, y);
this.guiNode.attachChild(backgroundLeft);
}
private void initInterfaceBackgroundLeft() {
Picture interfaceBackgroundRight = new Picture(
"interfaceBackgroundLeft");
interfaceBackgroundRight.setImage(this.assetManager,
"Textures/Hud/Interface2.png", true);
float picwidth = 290;
float picheight = 45;
interfaceBackgroundRight.setWidth(picwidth);
interfaceBackgroundRight.setHeight(picheight);
float x = 0;
float y = 0;
interfaceBackgroundRight.setPosition(x, y);
this.guiNode.attachChild(interfaceBackgroundRight);
}
private void initInterfaceBackgroundRight() {
Picture interfaceBackgroundRight = new Picture(
"interfaceBackgroundRight");
interfaceBackgroundRight.setImage(this.assetManager,
"Textures/Hud/Interface.png", true);
float picwidth = 200;
float picheight = 50;
interfaceBackgroundRight.setWidth(picwidth);
interfaceBackgroundRight.setHeight(picheight);
float x = this.width - picwidth;
float y = 0;
interfaceBackgroundRight.setPosition(x, y);
this.guiNode.attachChild(interfaceBackgroundRight);
}
public void initCrossHair() {
Picture crossHair = new Picture("CrossHair");
crossHair.setImage(this.assetManager, "Textures/Hud/CrossHair.png",
true);
float picwidth = 30;
float picheight = 30;
crossHair.setWidth(picwidth);
crossHair.setHeight(picheight);
float x = (this.width / 2) - (picwidth / 2);
float y = (this.height / 2) - (picheight / 2);
crossHair.setPosition(x, y);
this.guiNode.attachChild(crossHair);
}
public Picture initEnemyMarker(Entity e) {
Picture pic = new Picture("test");
pic = new Picture("Marker");
pic.setImage(this.assetManager, "Textures/Hud/Marker.png", true);
float picwidth = 100;
float picheight = 100;
pic.setWidth(picwidth);
pic.setHeight(picheight);
pic.setPosition(
this.cam.getScreenCoordinates(e.getWorldTranslation()).x
- (picwidth / 2),
this.cam.getScreenCoordinates(e.getWorldTranslation()).y
- (picheight / 2));
this.guiNode.attachChild(pic);
return pic;
}
public void update(float tpf) {
Iterator<Entry<Entity, Picture>> it = this.entitys.entrySet()
.iterator();
while (it.hasNext()) {
Map.Entry<Entity, Picture> pairs = it.next();
pairs.getValue().setPosition(
this.cam.getScreenCoordinates(pairs.getKey()
.getWorldTranslation()).x - 50,
this.cam.getScreenCoordinates(pairs.getKey()
.getWorldTranslation()).y - 50);
}
}
private float calculateHealthBar(Entity e) {
float result = 100;
result = (e.getMaxHull() / 100) * e.getHull();
return result;
}
private float calculateShieldBar(Shield s) {
float result = 100;
result = (s.getMaxShield() / 100) * s.getShield();
return result;
}
public void showPic(String path, float width, float height, float xpos,
float ypos) {
}
@Override
public void projectileCreated(EntityEvent<Projectile> e) {
}
@Override
public void projectileRemoved(EntityEvent<Projectile> e) {
}
@Override
public void environmentCreated(EntityEvent<? extends Environment> e) {
this.entitys.put(e.entity, initEnemyMarker(e.entity));
}
@Override
public void environmentRemoved(EntityEvent<? extends Environment> e) {
this.guiNode.detachChild(this.entitys.get(e.entity));
this.entitys.remove(e.entity);
}
@Override
public void shipCreated(EntityEvent<? extends Ship> e) {
this.entitys.put(e.entity, initEnemyMarker(e.entity));
}
@Override
public void playerShipCreated(EntityEvent<ControllableShip> e) {
this.ship = e.entity;
}
@Override
public void shipRemoved(EntityEvent<? extends Ship> e) {
this.guiNode.detachChild(this.entitys.get(e.entity));
this.entitys.remove(e.entity);
}
}
[/java]