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

import org.ntlab.radishforandroidstudio.framework.gameMain.Actor;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
//import org.ntlab.radishforandroidstudio.framework.model3D.Angle;

import cactusServer.models.Instances;
import net.arnx.jsonic.JSONHint;

public class Player extends Entity3D {
	private String instanceURI;
	private String characterURI;
	private String areaURI;
	private Position3D position;
	private Angle angle;
	private CameraState cameraState;
	private EmoteState emoteState;
	@JSONHint(ignore = true)
	public static final int UNIQUE_ID_LENGTH = 12;

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

	public Player(String instanceURI, String characterURI, CameraState cameraState,
			EmoteState.EmoteType animationClassToStart) {
		setInstanceURI(instanceURI);
		setCharacterURI(characterURI);
		setCameraState(cameraState);
		setAnimationClassToStart(animationClassToStart);
		readCharacterData(characterURI);
		// こんな感じで当該コンストラクタの最後でplaceableの注入を行う(具象クラスの実引数をまだ把握していない)
		// setPlaceable(new Actor());
		// 所属するInstanceが持つUniverseにplaceableを配置する
		String[] instanceURISplit = instanceURI.split("/"); // ……/instances/{instanceId}
		String instanceId = instanceURISplit[instanceURISplit.length - 1];
		Instances.getInstance().getInstance(instanceId).getUniverse().place(this.getPlaceable());
	}

	private Character getCharacter(String characterURI) {
		String[] characterURISplit = characterURI.split("/"); // ……/instances/{instanceId}/characters/{characterId}
		int splitSize = characterURISplit.length;
		String characterId = characterURISplit[splitSize - 1];
		String instanceId = characterURISplit[splitSize - 3];
		return Instances.getInstance().getInstance(instanceId).getCharacter(characterId);
	}

	private void readCharacterData(String characterURI) {
		Character character = getCharacter(characterURI);
		setPosition(character.getPosition());
		setAngle(character.getAngle());
		setAreaURI(character.getAreaURI());
		setCharacterURI(characterURI);
	}

	public String getInstanceURI() {
		return instanceURI;
	}

	public String getCharacterURI() {
		return characterURI;
	}

	public String getAreaURI() {
		return areaURI;
	}

	public Position3D getPosition() {
		return position;
	}

	public Angle getAngle() {
		return angle;
	}

	public CameraState getCameraState() {
		return cameraState;
	}

	public EmoteState getEmoteState() {
		return emoteState;
	}

	public void setInstanceURI(String instanceURI) {
		this.instanceURI = instanceURI;
	}

	public void setCharacterURI(String characterURI) {
		this.characterURI = characterURI;
	}

	public void setAreaURI(String areaURI) {
		this.areaURI = areaURI;
	}

	public void setPosition(Position3D position) {
		this.position = position;
	}

	public void setAngle(Angle angle) {
		this.angle = angle;
	}

	public void setCameraState(CameraState cameraState) {
		this.cameraState = cameraState;
	}

	public void setEmoteState(EmoteState emoteState) {
		this.emoteState = emoteState;
	}

	public void setAnimationClassToStart(EmoteState.EmoteType animationClassToStart) {
		setEmoteState(new EmoteState(animationClassToStart));
	}

	public Player update(String characterURI, Position3D position, Angle angle, CameraState cameraState,
			EmoteState.EmoteType animationClassToStart) {
		setCharacterURI(characterURI);
		setPosition(position);
		setAngle(angle);
		setCameraState(cameraState);
		updateEmoteType(animationClassToStart);
		updateAreaURI();
		return this;
	}

	private void updateAreaURI() {
		String[] areaURISplit = areaURI.split("/"); // ……/instances/{instanceId}/areas/{areaId}
		int splitSize = areaURISplit.length;
		String instanceId = areaURISplit[splitSize - 3];
		String previousAreaId = areaURISplit[splitSize - 1];
		Instance instance = Instances.getInstance().getInstance(instanceId);
		Area previousArea = instance.getArea(previousAreaId);
		if (!previousArea.isSurroundingPosition(position)) {
			// キャラクターのポジションを用いてどのエリア内にいるかを判定して当該エリアのURIに更新する
			setAreaURI(findAreaURI(instance));
		}
	}

	private String findAreaURI(Instance instance) {
		// キャラクターのポジションを用いてどのエリア内にいるかを判定して当該エリアのURIに更新する
		for (String areaId : instance.getAreaIdSet()) {
			Area area = instance.getArea(areaId);
			if (area.isSurroundingPosition(position)) {
				int subStringEndIndex = areaURI.length() - Area.UNIQUE_ID_LENGTH; // …/{instanceId}/areas/ まで
				return areaURI.substring(0, subStringEndIndex) + areaId;
			}
		}
		return "";
	}

	private void updateEmoteType(EmoteState.EmoteType animationClassToStart) {
		emoteState.setEmoteType(animationClassToStart);
	}
	
	public void changeCharacter(String characterURI) {
		writeCharacterData(this.characterURI);
		readCharacterData(characterURI);
	}

	public void destroy() {
		writeCharacterData(characterURI);
	}

	private void writeCharacterData(String characterURI) {
		Character character = getCharacter(characterURI);
		character.update(position, angle, null, areaURI); // model(第3引数)の更新は出来ない
	}
}