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 BulletModelManager {
private static BulletModelManager theInstance = null;
private HashMap<Integer, Model3D> bulletModels = new HashMap<>();
private int nextKey = 0;
private static final String MODEL_PATH = "../../";
private BulletModelManager() {
initBulletModels();
confirmModelMap();
}
private void initBulletModels() {
String[] initCharacterModelFileNames = { "bullet.obj" };
for (String fileName : initCharacterModelFileNames) {
addBulletModel(fileName);
}
}
public static BulletModelManager getInstance() {
if (theInstance == null) {
theInstance = new BulletModelManager();
}
return theInstance;
}
public Model3D getBulletModel(int id) {
return bulletModels.get(id);
}
public int getBulletModelCount() {
return bulletModels.size();
}
public int addBulletModel(String fileName) {
String path = createModelFilePath(MODEL_PATH + fileName);
bulletModels.put(nextKey, loadModel(path));
return nextKey++;
}
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;
}
/**
* 現在のCharacterModelMapの中身の確認用
*/
private void confirmModelMap() {
// 確認用
System.out.println("BulletModelManager確認用");
System.out.println(getBulletModelCount() + "個のマッピングが存在するよ!");
for (Map.Entry<Integer, Model3D> entry : bulletModels.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}