?? snakecube3d.java
字號:
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Fog;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Material;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
/*
* Snake 3D by Krisna
*/
public class Snakecube3D {
short[] vert={
0,0,0, 0,5,0, -5,0,0, -5,5,0,
0,0,-5, 0,5,-5, 0,0,0, 0,5,0 ,
0,5,0, 0,5,-5, -5,5,0, -5,5,-5 ,
-5,0,0, -5,5,0, -5,0,-5, -5,5,-5 ,
-5,0,-5, -5,5,-5, 0,0,-5, 0,5,-5 ,
};
byte[] norm = {
0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
-127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0,
0, 0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127,
};
private int [] stripLen={4,4,4,4,4};
private Appearance iAppearancecube;
private VertexBuffer iVbcube;
private VertexArray verticeArray;
private IndexBuffer iIbcube;
private VertexArray normArray;
private Material iMaterial;
private Transform transform;
private Fog fm;
private int color=0xFF0000;
public Snakecube3D() {
init();
}
public Snakecube3D(short index,int color) {
for(short i=0;i<vert.length;i+=3) {
vert[i]=(short)(vert[i]+ 5*index);
// norm[i]=(byte)(norm[i]+ 5*index);
}
this.color=color;
init();
}
public void init() {
transform = new Transform();
transform.postTranslate(0.0f,0.0f,0.0f);
verticeArray=new VertexArray(vert.length/3,3,2);
verticeArray.set(0,vert.length/3,vert);
normArray = new VertexArray(norm.length / 3, 3, 1);
normArray.set(0, norm.length/3, norm);
iVbcube=new VertexBuffer();
iVbcube.setPositions(verticeArray,1.0f,null);
iVbcube.setNormals(normArray);
iIbcube=new TriangleStripArray(0,stripLen);
iAppearancecube= new Appearance();
iMaterial=new Material();
iMaterial.setColor(Material.DIFFUSE,color);
// iMaterial.setColor(Material.SPECULAR,color);
iMaterial.setShininess(10.0f);
/* fm=new Fog();
fm.setColor(0xffffff);//white
fm.setMode(Fog.LINEAR);// type of equation
fm.setLinear(1,100);// nearest and farest objects to be seen
fm.setDensity(0.5f);// how much fog*/
iAppearancecube.setMaterial(iMaterial);
//iAppearancecube.setFog(fm);
}
public void increaseX() {
for(short i=0;i<vert.length;i+=3) {
vert[i]=(short)(vert[i]+5);
}
newVertexarr();
}
public void decreaseX() {
for(short i=0;i<vert.length;i+=3) {
vert[i]=(short)(vert[i]-5);
}
newVertexarr();
}
public void increaseZ() {
for(short i=2;i<vert.length;i+=3) {
vert[i]=(short)(vert[i]+5);
}
newVertexarr();
}
public void decreaseZ() {
for(short i=2;i<vert.length;i+=3) {
vert[i]=(short)(vert[i]-5);
}
newVertexarr();
}
public void newVertexarr() {
verticeArray=new VertexArray(vert.length/3,3,2);
verticeArray.set(0,vert.length/3,vert);
iVbcube=new VertexBuffer();
iVbcube.setPositions(verticeArray,1.0f,null);
iVbcube.setNormals(normArray);
}
public boolean getCheckBound(short dir) {
switch(dir) {
case 1: return vert[35]<=-50;
case 2: return vert[26]>=50;
case 3:return vert[33]>=50;
case 4:return vert[24]<=-50;
}
return false;
}
public short getVertat(short index) {
return vert[index];
}
public boolean getCheckEat(short dir,short x,short z) {
switch(dir) {
case 1:
case 3: return vert[33]==x&&vert[35]==z;
case 2: return vert[27]==x&&vert[29]==z;
case 4:return vert[24]==x&&vert[26]==z;
}
return false;
}
public void setVertix(short[] newver) {
for(int i=0;i<vert.length;i++) {
vert[i]=newver[i];
}
newVertexarr();
}
public boolean getCheckBodyCollision(short dir,Snakecube3D bodypart,int i) {
switch(dir) {
case 1:
case 4: return (vert[33]==bodypart.getVertat((short)33)&&vert[35]==bodypart.getVertat((short)35));
case 2:
case 3:return (vert[24]==bodypart.getVertat((short)24)&&vert[26]==bodypart.getVertat((short)26));
}
return false;
}
public void setIVbcube(VertexBuffer iVbcube) {
this.iVbcube = iVbcube;
}
public void setIIbcube(IndexBuffer iIbcube) {
this.iIbcube = iIbcube;
}
public short[] getVert() {
return vert;
}
public VertexBuffer getIVbcube() {
return iVbcube;
}
public Appearance getIAppearancecube() {
return iAppearancecube;
}
public Transform getTransform() {
return transform;
}
public IndexBuffer getIIbcube() {
return iIbcube;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -