Newer
Older
CactusServer / src / main / java / cactusServer / models / CharacterModelManager.java
package cactusServer.models;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.ntlab.radishforandroidstudio.framework.model3D.Model3D;
import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory;
import org.ntlab.radishforandroidstudio.framework.model3D.ModelFileFormatException;

public class CharacterModelManager {
	private static CharacterModelManager theInstance = null;
	private HashMap<Integer, Model3D> characterModels = new HashMap<>();
	private int nextKey = 0;
	private static final String MODEL_PATH = "assets\\";

	private CharacterModelManager() {
		initCharacterModels();
	}

	private void initCharacterModels() {
		String[] initCharacterModelFileNames = { "pocha.stl", "Head4.obj" };
		for (String fileName : initCharacterModelFileNames) {
			String path = createModelFilePath(MODEL_PATH + fileName);
			System.out.println((path == null) ? "nullだよ!" : path); // 確認用
			characterModels.put(nextKey++, loadModel(path));
		}
		// 確認用
		System.out.println(getCharacterModelCount() + "個のマッピングが存在するよ!");
		for (Map.Entry<Integer, Model3D> entry : characterModels.entrySet()) {
			System.out.println(entry.getKey() + ": " + entry.getValue());
		}
	}

	private String createModelFilePath(String fileName) {
		String path = getClass().getResource(fileName).getPath();
		try {
			path = URLDecoder.decode(path, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return path;
	}

	private Model3D loadModel(String fileName) {
		try {
			return ModelFactory.loadModel(fileName, null, false, true);
		} catch (IOException | ModelFileFormatException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static CharacterModelManager getInstance() {
		if (theInstance == null) {
			theInstance = new CharacterModelManager();
		}
		return theInstance;
	}

	public Model3D getCharacterModel(int id) {
		return characterModels.get(id);
	}

	public int getCharacterModelCount() {
		return characterModels.size();
	}
}