Newer
Older
CactusServer / src / main / java / cactusServer / models / CollisionManager.java
y-ota on 18 Oct 2018 2 KB メソッド分割
package cactusServer.models;

import java.util.ArrayList;
import java.util.HashMap;
import org.ntlab.radishforandroidstudio.framework.model3D.CollisionResult;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.physics.PhysicsUtility;
import org.ntlab.radishforandroidstudio.java3d.Vector3d;

import cactusServer.entities.Instance;
import cactusServer.entities.Object;
import cactusServer.entities.Player;
import net.arnx.jsonic.JSONHint;

public class CollisionManager {
	private static CollisionManager theInstance;
	private HashMap<String, Instance> instances;
	private ArrayList<CollisionResult> resultList;

	private CollisionManager() {
		instances = Instances.getInstance().getInstances();
		resultList = new ArrayList<CollisionResult>();
	}

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

	@JSONHint(ignore = true)
	public ArrayList<CollisionResult> getResultList() {
		return resultList;
	}

	public void collisionRun(long interval) {
		resultList.clear();
		for (String instanceId : instances.keySet()) {
			Instance instance = Instances.getInstance().getInstance(instanceId);
			instance.getUniverse().update(interval);
			collisionPlayersObjects(instanceId, instance);
		}
	}

	public void collisionPlayersObjects(String instanceId, Instance instance) {
		for (Object object : instance.getObjects().values()) {
			for (Player player : Instances.getInstance().getPlayers(instanceId).values()) {
				collisionMove(object, player);
			}
			for (Object object2 : instance.getObjects().values()) {
				if (!object.equals(object2)) {
					collisionMove(object, object2);
				}
			}
		}
	}

	public void collisionMove(Object object, Player player) {
		CollisionResult result;
		result = PhysicsUtility.checkCollision(player.getObject(), null, object.getObject(), null);
		if (result != null) {
			objMove(result, object, false);
		}
	}

	public void collisionMove(Object object1, Object object2) {
		CollisionResult result;
		result = PhysicsUtility.checkCollision(object1.getObject(), null, object2.getObject(), null);
		if (result != null) {
			Vector3d vec1 = object1.getVelocity().getVector3d();
			Vector3d vec2 = object2.getVelocity().getVector3d();
			if (vec1.length() > vec2.length()) {
				objMove(result, object1, true);
			} else {
				objMove(result, object2, true);
			}
		}
	}

	private void objMove(CollisionResult result, Object object, boolean negate) {
		result.normal.scale(-1);
		Vector3d vec = object.getPosition().getVector3d();
		if (negate)
			result.normal.negate();
		vec.add(result.normal);
		object.setPosition(new Position3D(vec));
	}
}