BBNodeController.java :  » Game » infinitewars » logic » nodes » lod » Java Open Source

Java Open Source » Game » infinitewars 
infinitewars » logic » nodes » lod » BBNodeController.java
package logic.nodes.lod;

import logic.nodes.ModelNode;
import logic.nodes.TeamNode;
import map.asteroids.Asteroid;
import map.spawnStation.SpawnStation;
import fileHandling.TextureLoader;
import gameStates.IngameState;

import com.jme.bounding.BoundingBox;
import com.jme.renderer.Renderer;
import com.jme.scene.BillboardNode;
import com.jme.scene.Controller;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.system.DisplaySystem;

public class BBNodeController extends Controller {
  
  private static final long serialVersionUID = 1L;
  
  public static final String ICON_FILE = "/icon.png";
  public static final String TRANS_ICON_FILE = "/icon_trans.png";
  
  private static BlendState blendState;
  
  private LODNode lodNode;
  private BillboardNode bbNode;
  
  public BBNodeController(LODNode lodNode) {
    this.lodNode = lodNode;

    if(blendState == null) {
      blendState = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
      blendState.setBlendEnabled(true);
      blendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
      blendState.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
      blendState.setTestEnabled(true);
      blendState.setTestFunction(BlendState.TestFunction.GreaterThan);
    }
  }
  
  @Override
  public void update(float time) {
    if(bbNode != null && !IngameState.removeControllers.containsKey(lodNode)) 
      IngameState.removeControllers.put(lodNode, this);
    else if(lodNode.getBound() != null) {
      bbNode = new BillboardNode("BillboardNode for " + lodNode.getName());
      BoundingBox bound = (BoundingBox)lodNode.getBBNodeBound();
      
      float factor = ((lodNode instanceof Asteroid) ? 0.09f : 3f);
      float width = (bound.xExtent < bound.zExtent ? bound.zExtent : bound.xExtent) * factor;
      float height = bound.yExtent * factor;
//      float width = bound.getRadius() * factor;
//      float height = width;
      
      Quad quad = new Quad("Quad for " + lodNode.getName(), width, height);
      bbNode.attachChild(quad);
      quad.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
      quad.setRenderState(TextureLoader.getTextureState(getPickIconPath(lodNode, true)));
      quad.setRenderState(blendState);
      bbNode.setLightCombineMode(LightCombineMode.Off);
      bbNode.updateRenderState();
      
      lodNode.setBillboardNode(bbNode);
      IngameState.removeControllers.put(lodNode, this);
    }
  }
  
  private String getPickIconPath(ModelNode pick, boolean trans) {
    String image = null;
    if(pick instanceof TeamNode) {
      TeamNode pickedNode = (TeamNode)pick;
      if(pickedNode instanceof SpawnStation && pickedNode.getTeam() == null) 
        image = "data/models/spawnStation";
      else image = pickedNode.getFilePath();
    } else if(pick instanceof Asteroid) {
      String name = ((Asteroid)pick).getName().toLowerCase();
      image = "data/models/asteroids/" + name;
    }
    if(image == null) return null;
    image += "/" + (trans ? TRANS_ICON_FILE : ICON_FILE);
    return image;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.