⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 competition3d.java

📁 一款模仿劲舞团的手机游戏
💻 JAVA
字号:


import java.io.IOException;
import java.util.Hashtable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Image2D;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.Texture2D;
import javax.microedition.m3g.World;

/**
 * @author trojan
 * 
 * 3D������,������Ⱦ�����еģ�D
 */
public class Competition3D {

	// ------------------------------�����------------------------------

	private MyMIDLET midlet;// �������

	private int width;// ��Ļ�Ŀ�

	private int height;// ��Ļ�ĸ�

	private Hashtable gameInfo;// ��Ϸ������

	private Hashtable firstCharacterInfo;// ����һ������

	private Hashtable secondCharacterInfo;// �����������

	private Graphics3D g3d;// ��D���ƶ���

	private World[] firstWorld;// ����һģ������

	private World[] secondWorld;// ����������

	private int musicSpeed;// �����ٶ�

	private float playRate;// ��������

	private String fristAppPath;// ����һ���·��

	private String secondAppPath;// ��������·��

	private String scenePath;// ����·��

	private Background firstBackground;// ����һ����·��

	private Background secondBackground;// ���������·��

	private int[][] m3gTexIndex = new int[][] { { 332, 368, 330 },
			{ 344, 380, 342 }, { 336, 372, 334 }, { 342, 378, 340 },
			{ 356, 392, 354 }, { 338, 374, 336 }, { 354, 390, 352 },
			{ 348, 384, 346 }, { 350, 386, 348 } };// ������ÿ��ģ����ͼ���ֵ��û�ID

	// --------------------------����ֽ���----------------------------
	//
	//
	// --------------------------��������------------------------------

	// ���췽��
	public Competition3D(MyMIDLET midlet, Hashtable gameInfo,
			Hashtable firstCharacterInfo, Hashtable secondCharacterInfo,
			World[] firstWorld, World[] secondWorld, int width, int height) {
		this.midlet = midlet;
		this.firstWorld = firstWorld;
		this.secondWorld = secondWorld;
		this.gameInfo = gameInfo;
		this.firstCharacterInfo = firstCharacterInfo;
		this.secondCharacterInfo = secondCharacterInfo;
		this.width = width;
		this.height = height;
		initialize();
	}

	// ��ʼ������
	protected void initialize() {

		midlet.setPercent("5%");

		scenePath = gameInfo.get("gameScenePath").toString();

		musicSpeed = Integer.parseInt(gameInfo.get("musicSpeed").toString());

		playRate = (float) 4000 / (float) musicSpeed;

		fristAppPath = firstCharacterInfo.get("cAppPath").toString();

		secondAppPath = secondCharacterInfo.get("cAppPath").toString();

		g3d = Graphics3D.getInstance();

		midlet.setPercent("10%");

		initializeModel();
	}

	// 3D��Ⱦ����
	public void render(Graphics g, int time, int index, int character) {

		g3d.bindTarget(g);

		// ��Ⱦ����һ
		if (character == 1) {
			g3d.setViewport(width / 2 - 88, height / 2 - 30, 88, 120);
			firstWorld[index].animate((int) Math.floor(time * playRate));
			g3d.render(firstWorld[index]);
		}
		// ��Ⱦ�����
		if (character == 2) {
			g3d.setViewport(width / 2, height / 2 - 30, 88, 120);
			secondWorld[index].animate((int) Math.floor(time * playRate));
			g3d.render(secondWorld[index]);
		}

		g3d.releaseTarget();
	}

	// ��ʼ��ģ��
	protected void initializeModel() {
		// ����ÿһ��ģ��
		for (int index = 0; index < firstWorld.length; index++) {

			midlet.setPercent(String.valueOf(index + 1) + "1%");

			// ���������ͼ
			for (int i = 0; i < 3; i++) {
				attachTexture(index, i, 1);
				attachTexture(index, i, 2);
			}

			midlet.setPercent(String.valueOf(index + 1) + "3%");

			// ��������
			firstBackground = firstWorld[index].getBackground();
			secondBackground = secondWorld[index].getBackground();
			midlet.setPercent(String.valueOf(index + 1) + "5%");
			Image2D firstBG = createImage2D("/images/" + scenePath + "_1.gif");
			Image2D secondBG = createImage2D("/images/" + scenePath + "_2.gif");
			midlet.setPercent(String.valueOf(index + 1) + "7%");
			firstBackground.setImage(firstBG);
			secondBackground.setImage(secondBG);
			midlet.setPercent(String.valueOf(index + 1) + "9%");
			firstWorld[index].setBackground(firstBackground);
			secondWorld[index].setBackground(secondBackground);
			midlet.setPercent(String.valueOf(index + 2) + "0%");
		}
	}

	// ����Image2D
	private Image2D createImage2D(String imagePath) {
		Image image = null;
		Image2D image2D = null;
		try {
			image = Image.createImage(imagePath);
		} catch (IOException e) {
			e.printStackTrace();
			midlet.setPercent("���ִ�����Ϸ�˳�");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e1) {
				// TODO �Զ���� catch ��
				e1.printStackTrace();
			}
			midlet.destroyApp(false);
			midlet.destroyed();
		}

		if (image != null) {
			image2D = new Image2D(Image2D.RGB, image);
		}
		return image2D;
	}

	// �����������
	private Texture2D createTexture(String appPath) {
		Image2D image2D = createImage2D(appPath);
		Texture2D texture = new Texture2D(image2D);
		return texture;
	}

	// ������?��
	private void attachTexture(int m3gIndex, int partIndex, int character) {
		if (character == 1) {
			// ��ݱ����ģ���û�ID�ҵ�Ҫ��ͼ��Mesh
			Mesh mesh = (Mesh) firstWorld[m3gIndex]
					.find(m3gTexIndex[m3gIndex][partIndex]);

			// ��ȡ���,��������,�������
			Appearance app = mesh.getAppearance(0);
			Texture2D texture = createTexture("/images/app0" + fristAppPath
					+ "_" + partIndex + ".png");
			app.setTexture(0, texture);
		}
		if (character == 2) {
			Mesh mesh = (Mesh) secondWorld[m3gIndex]
					.find(m3gTexIndex[m3gIndex][partIndex]);
			Appearance app = mesh.getAppearance(0);
			Texture2D texture = createTexture("/images/app0" + secondAppPath
					+ "_" + partIndex + ".png");
			app.setTexture(0, texture);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -