/*
* See COPYING in top-level directory.
*/
package com.monkygames.wo.utils;
// === java imports === //
import com.jme3.asset.AssetManager;
import java.util.Vector;
// === jme imports === //
import com.jme3.scene.*;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
// === wo imports === //
import com.jme3.math.Vector3f;
import com.jme3.system.JmeSystem;
import com.monkygames.wo.avatar.Base;
import com.monkygames.wo.io.Avatar3DLoader;
/**
* A table that matches entities from the Avatar file format to a 3D model.
* Note, with jme3 its not necessary to clone manually, just load the models
* from file, the AssetManager handles the cloning and caching.
* @version 1.0
*/
public class BaseTable{
/**
* Body parts.
**/
private String body;
/**
* Holds the paths to the heads.
**/
private Vector <String> headV;
/**
* Contains the path to the Left Eyes.
**/
private Vector<String> eyesLV;
/**
* Contains the path to the right eyes.
**/
private Vector<String> eyesRV;
/**
* Holds the colors of the skin.
**/
private Vector <ColorRGBA> skinColorV;
/**
* Holds the colors of the shirt.
**/
private Vector <ColorRGBA> favoriteColorV;
/**
* The pants color.
**/
private ColorRGBA pantsColor;
/**
* Used for loading models.
**/
private Avatar3DLoader loader;
// ============= Constructors ============== //
public BaseTable(Avatar3DLoader loader){
this.loader = loader;
setupColors();
setupBody();
setupHeads();
setupEyes();
}
// ============= Public Methods ============== //
/**
* Load the head.
**/
public Node loadHead(int type, int color){
Node clone = null;
if(type < headV.size()){
clone = loader.loadNode(headV.elementAt(type));
if(color < skinColorV.size()){
setColor(clone,skinColorV.elementAt(color));
}
}
return clone;
}
/**
* Creates a body node with the specified skin and shirt colors.
* @param skinColor the color of the skin.
* @param shirtColor the color of the shirt.
* @return the node that contains the geometries with the applied colors.
**/
public Node loadBody(int skinColor, int shirtColor){
Node newBody = loader.loadNode(body);
if(skinColor < skinColorV.size()){
Geometry submesh = (Geometry) newBody.getChild("Body-geom-0");
if(submesh != null){
setColor(submesh,skinColorV.elementAt(skinColor));
}
}
if(shirtColor < favoriteColorV.size()){
// set shirt color
Geometry submesh = (Geometry) newBody.getChild("Body-geom-1");
if(submesh != null){
setColor(submesh,favoriteColorV.elementAt(shirtColor));
}
}
// set paints color
Geometry submesh = (Geometry) newBody.getChild("Body-geom-2");
if(submesh != null){
System.out.println("painting the paints");
setColor(submesh,pantsColor);
}
return newBody;
}
/**
* Load the eyes of the avatar.
* @param base contains the information for the eyes to load.
* @param head the node that contains the head.
* @return attaches the eyes to the head node once loaded.
*/
public boolean loadEyes(Base base, Node head){
/*
Node leftEye = loader.loadNode(eyesLV.elementAt(base.type));
Node rightEye = loader.loadNode(eyesRV.elementAt(base.type));
*/
Node leftEye = loader.loadNode(eyesLV.elementAt(0));
Node rightEye = loader.loadNode(eyesRV.elementAt(0));
if(leftEye == null || rightEye == null){
return false;
}
//TODO color eyes
setColor(leftEye,pantsColor);
setColor(rightEye,pantsColor);
//TODO scale eyes
//TODO rotate eyes
//position eyes
System.out.println("Eyes.x,Eyes.y "+base.x+","+base.y);
// calc the x-axis
float xR = -0.216f - 0.08467f*base.x;
float xL = 0.216f + 0.08467f*base.x;
//-.229 = 0
// .8 = 12
// calc the y-axis
float y = 1.5f-base.y*0.072f;
//1.412 = 0
//0.207 = 18
rightEye.move(new Vector3f(xR,y,0.932f));
leftEye.move(new Vector3f( xL, y, 0.932f ));
head.attachChild(leftEye);
head.attachChild(rightEye);
return true;
}
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
/**
* Loads the body parts.
**/
private void setupBody(){
body = "Models/Body.j3o";
}
/**
* Loads all of the heads.
**/
private void setupHeads(){
headV = new Vector<String>();
headV.add("Models/HeadShape1.j3o");
}
private void setupEyes(){
eyesLV = new Vector<String>();
eyesRV = new Vector<String>();
eyesLV.add("Models/Eyes/Eye1L.j3o");
eyesRV.add("Models/Eyes/Eye1R.j3o");
}
private void setupColors(){
// setup skin colors
skinColorV = new Vector<ColorRGBA>();
skinColorV.add(new ColorRGBA(0.941f,0.859f,0.808f,1.0f));
skinColorV.add(new ColorRGBA(0.965f,0.788f,0.580f,1.0f));
skinColorV.add(new ColorRGBA(0.871f,0.627f,0.384f,1.0f));
skinColorV.add(new ColorRGBA(0.957f,0.741f,0.616f,1.0f));
skinColorV.add(new ColorRGBA(0.647f,0.380f,0.220f,1.0f));
skinColorV.add(new ColorRGBA(0.376f,0.243f,0.145f,1.0f));
// pants
pantsColor = new ColorRGBA(0.05f,0.05f,0.05f,1f);
// favorite colors
favoriteColorV = new Vector<ColorRGBA>();
favoriteColorV.add(new ColorRGBA(0.820f,0.282f,0.153f,1.0f));
favoriteColorV.add(new ColorRGBA(0.953f,0.600f,0.282f,1.0f));
favoriteColorV.add(new ColorRGBA(0.941f,0.812f,0.251f,1.0f));
favoriteColorV.add(new ColorRGBA(0.643f,0.745f,0.216f,1.0f));
favoriteColorV.add(new ColorRGBA(0.157f,0.447f,0.235f,1.0f));
favoriteColorV.add(new ColorRGBA(0.133f,0.275f,0.651f,1.0f));
favoriteColorV.add(new ColorRGBA(0.424f,0.651f,0.831f,1.0f));
favoriteColorV.add(new ColorRGBA(0.961f,0.537f,0.537f,1.0f));
favoriteColorV.add(new ColorRGBA(0.451f,0.224f,0.671f,1.0f));
favoriteColorV.add(new ColorRGBA(0.463f,0.353f,0.231f,1.0f));
favoriteColorV.add(new ColorRGBA(0.824f,0.824f,0.824f,1.0f));
favoriteColorV.add(new ColorRGBA(0.263f,0.263f,0.263f,1.0f));
//ColorV.add(new ColorRGBA(0.f,0.f,0.f,1.0f));
//You can also use new ColorRGBA(0.0,1.0,0.0,1.0).
}
/**
* Sets the color of the geometries of the children and home.
* Note, this is a recursive method.
* @param node the node that contains children
**/
private void setColor(Node node, ColorRGBA color){
for(int i = 0; i < node.getQuantity(); i++){
Spatial spatial = node.getChild(i);
if(spatial instanceof Geometry){
//Material material = new Material();
AssetManager assetManager = JmeSystem.newAssetManager(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Desktop.cfg"));
Material material = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
material.setColor("m_Color",color);
//Material geomMaterial = ((Geometry)spatial).getMaterial();
spatial.setMaterial(material);
System.out.println("Spatial: "+spatial.getName());
}else if(spatial instanceof Node){
setColor((Node)spatial,color);
System.out.println("Node: "+node.getName());
}
}
}
/**
* Sets the color of the geometries of the children and home.
* Note, this is a recursive method.
* @param node the node that contains children
**/
private void setColor(Geometry geometry, ColorRGBA color){
AssetManager assetManager = JmeSystem.newAssetManager(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Desktop.cfg"));
Material material = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
material.setColor("m_Color",color);
geometry.setMaterial(material);
System.out.println("Spatial: "+geometry.getName());
}
// ============= Implemented Methods ============== //
// ============= Extended Methods ============== //
// ============= Internal Classes ============== //
// ============= Static Methods ============== //
}
/*
* Local variables:
* c-indent-level: 4
* c-basic-offset: 4
* End:
*
* vim: ts=8 sts=4 sw=4 noexpandtab
*/