Newer
Older
CactusServer / src / main / java / cactusServer / models / CollisionManager.java
y-ota on 15 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 = new ArrayList<CollisionResult>();

	private CollisionManager() {
		instances = Instances.getInstance().getInstances();
	}

	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);
			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) {
			result.normal.scale(-1);
			Vector3d vec = object.getPosition().getVector3d();
			vec.add(result.normal);
			object.setPosition(new Position3D(vec));
		}
	}

	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()) {
				result.normal.scale(-1);
				Vector3d vec = object1.getPosition().getVector3d();
				vec.add(result.normal);
				object1.setPosition(new Position3D(vec));
			}else {
				result.normal.scale(-1);
				Vector3d vec = object2.getPosition().getVector3d();
				vec.add(result.normal);
				object2.setPosition(new Position3D(vec));
			}
		}
	}
}