Newer
Older
CactusServer / src / main / java / cactusServer / entities / Instance.java
package cactusServer.entities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory;
import org.ntlab.radishforandroidstudio.framework.model3D.ModelFileFormatException;
import org.ntlab.radishforandroidstudio.framework.model3D.Object3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
//import org.ntlab.radishforandroidstudio.framework.model3D.Angle;
import org.ntlab.radishforandroidstudio.framework.model3D.Universe;
import org.ntlab.radishforandroidstudio.framework.physics.AngularVelocity3D;
import org.ntlab.radishforandroidstudio.framework.physics.Ground;
import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D;

import cactusServer.entities.Entity;
import cactusServer.entities.Object;
import cactusServer.entities.Area.Allowed;
import cactusServer.entities.Object.Attribute;
import cactusServer.utils.RandomStringGenerator;
import cactusServer.entities.Character;
import net.arnx.jsonic.JSONHint;

/**
 * インスタンス
 * 
 * @author r-isitani
 *
 */
public class Instance extends Entity {
	private String name;
	private State state;
	private String stageID;

	private Universe universe;
	private Ground stage;
	private HashMap<String, Area> areaMap = new HashMap<>();
	private HashMap<String, Object> objMap = new HashMap<>();
	private HashMap<String, Character> characterMap = new HashMap<>();
	@JSONHint(ignore = true)
	public static final int UNIQUE_ID_LENGTH = 12;

	private Instance() {
		// JSONDecode時の呼び出し用
	}

	public Instance(String name, State state, String stageID) {
		setName(name);
		setState(state);
		setStageID(stageID);
		//initUniverse();
	}

	private void initUniverse() {
		universe = new Universe();

		// stageURIを元にして読み込むステージのファイル名(相対パス)を作る予定
		String stageFileName = stageID;

		// ステージの3Dデータを読み込み配置する
		Object3D stageObj;
		try {
			stageObj = ModelFactory.loadModel(stageFileName, null, false, true).createObject();
			stage = new Ground(stageObj);
			universe.place(stage);
		} catch (IOException | ModelFileFormatException e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		return name;
	}

	public State getState() {
		return state;
	}

	public String getStageID() {
		return stageID;
	}

	@JSONHint(ignore = true)
	public Universe getUniverse() {
		return universe;
	}

	@JSONHint(ignore = true)
	public Set<String> getAreaIdSet() {
		return areaMap.keySet();
	}

	public Area getArea(String areaId) {
		return areaMap.get(areaId);
	}

	@JSONHint(ignore = true)
	public Set<String> getObjIdSet() {
		return objMap.keySet();
	}

	public Object getObject(String objId) {
		return objMap.get(objId);
	}

	@JSONHint(ignore = true)
	public List<Character> getCharacters() {
		return new ArrayList<Character>(characterMap.values());
	}

	public Character getCharacter(String characterId) {
		return characterMap.get(characterId);
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setState(State state) {
		this.state = state;
	}

	public void setStageID(String stageURI) {
		this.stageID = stageURI;
	}

	public IDAddressedEntity createArea(String name, Plain[] region, HashSet<Allowed> permissions) {
		String id = RandomStringGenerator.generateUniqueString(Area.UNIQUE_ID_LENGTH,
				RandomStringGenerator.ALPHA_NUMERIC, areaMap.keySet());
		Area area = new Area(name, region, permissions);
		areaMap.put(id, area);
		return new IDAddressedEntity(id, area);
	}

	public IDAddressedEntity createObject(Position3D position, Velocity3D velocity, AngularVelocity3D angularVelocity,
			Angle angle, Attribute attribute, String model) {
		String id = RandomStringGenerator.generateUniqueString(Object.UNIQUE_ID_LENGTH,
				RandomStringGenerator.ALPHA_NUMERIC, objMap.keySet());
		Object object = new Object(position, velocity, angularVelocity, angle, attribute, model);
		objMap.put(id, object);
		universe.place(object.getPlaceable());
		return new IDAddressedEntity(id, object);
	}

	public IDAddressedEntity createCharacter(String instanceId, String accountURI, String name, Position3D position,
			Angle angle, String model) {
		String id = RandomStringGenerator.generateUniqueString(Character.UNIQUE_ID_LENGTH,
				RandomStringGenerator.ALPHA_NUMERIC, characterMap.keySet());
		Character character = new Character(instanceId, accountURI, name, position, angle, model);
		characterMap.put(id, character);
		return new IDAddressedEntity(id, character);
	}
	
	public Instance update(Instance.State state) {
		setState(state);
		return this;
	}

	public Character updateCharacter(String characterId, Position3D position, Angle angle, String modelID) {
		Character character = characterMap.get(characterId);
		character.update(position, angle, modelID, null); // areaURI(第4引数)の更新は呼び出し先で自動で行われる
		return character;
	}

	public Character destroyCharacter(String characterId) {
		return characterMap.remove(characterId);
	}

	public Area destroyArea(String areaId) {
		return areaMap.remove(areaId);
	}

	public Object destroyObject(String objId) {
		return objMap.remove(objId);
	}

	public static enum State {
		AVAILABLE, MAINTENANCE;
	}
}