diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/connections/CharacterConnection.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/connections/CharacterConnection.java new file mode 100644 index 0000000..ade9809 --- /dev/null +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/connections/CharacterConnection.java @@ -0,0 +1,9 @@ +package org.ntlab.radishforandroidstudio.cactusClient.connections; + +import org.ntlab.radishforandroidstudio.framework.network.HttpAsyncConnection; + +public class CharacterConnection extends HttpAsyncConnection { + public CharacterConnection(String playerId) { + super("http://nitta-lab-www.is.konan-u.ac.jp:8080/CactusServer/rest/instances/players/" + playerId); + } +} diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModel.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModel.java index ac6e6eb..c6efb0b 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModel.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModel.java @@ -6,6 +6,8 @@ * @author s.iwatani */ public interface CactusModel { + int MAX_CONNECTION_CONTINUITY_TIME = 5000; // ミリ秒 + /** * モデルの状態を更新する * @@ -13,4 +15,12 @@ * @param interval 前回の更新からの時間差 */ void update(double interval); + + /** + * instanceのIDをセットする + * + * @author s.iwatani + * @param id InstanceのID + */ + void setInstanceId(String id); } diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModels.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModels.java index 0b27dfa..5d0696d 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModels.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/CactusModels.java @@ -14,6 +14,7 @@ private ArrayList models = new ArrayList<>(); private RealTime3DFragment fragment = null; private Universe universe = null; + private String instanceId = ""; public CactusModels() { } @@ -24,6 +25,23 @@ } /** + * CactusModelを追加 + * instanceIDのセットなどは実行されないことを注意 + * + * @param model 追加したいCactusModel + */ + public void addModel(CactusModel model) { + models.add(model); + } + + public void setInstanceId(String id) { + for(CactusModel model : models) { + model.setInstanceId(id); + } + instanceId = id; + } + + /** * モデルの状態を更新する * * @author s.iwatani @@ -31,6 +49,7 @@ */ @Override public void update(double interval) { + System.out.println("Update"); for (CactusModel model: models) { model.update(interval); } diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OtherPlayerCharactersModel.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OtherPlayerCharactersModel.java index c486b4c..68a5844 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OtherPlayerCharactersModel.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OtherPlayerCharactersModel.java @@ -40,7 +40,10 @@ public OtherPlayerCharactersModel(RealTime3DFragment fragment, Universe universe) { this.fragment = fragment; this.universe = universe; - con = new CharactersConnection("a"); + } + + public void setInstanceId(String id) { + con = new CharactersConnection(id); con.setCallBack(this); } @@ -53,65 +56,65 @@ @Override public void update(double interval) { nextConnectRenaimdTime -= interval; - if (nextConnectRenaimdTime <= 0) { + if (nextConnectRenaimdTime <= 0 && con != null) { nextConnectRenaimdTime = connectInterval; - con.doAnything(); con.doGet(); +// con.doAnything(); } } @Override public void onResponse(String response) { - Map lastVisibleCharacters = new HashMap<>(visibleCharacters); - visibleCharacters.clear(); - - JSON json = new JSON(); - Map m = json.decode(response); - for (Map.Entry entry: m.entrySet()) { - visibleCharacters.put(entry.getKey(), 1); - - ArrayList properties = new ArrayList<>(); - Map player = entry.getValue(); - - // 情報の取得 - Map position = (Map)player.get("position"); - Map angle = (Map)player.get("angle"); - Position3D positionProp = new Position3D(((BigDecimal)position.get("x")).doubleValue(), ((BigDecimal)position.get("y")).doubleValue(), ((BigDecimal)position.get("z")).doubleValue()); - Quaternion3D quaProp = new Quaternion3D(((BigDecimal)angle.get("vx")).doubleValue(), ((BigDecimal)angle.get("vy")).doubleValue(), ((BigDecimal)angle.get("vz")).doubleValue(), ((BigDecimal)angle.get("a")).doubleValue()); - properties.add(positionProp); - properties.add(quaProp); - - if (!universe.doHaveObj(entry.getKey())) { - OvergroundActor pocha; - Appearance ap1 = new Appearance(); - Material mat = new Material(); - mat.setDiffuseColor(0.0f, 0.3f, 1.0f); - mat.setAmbientColor(0.0f, 0.0f, 0.0f); - mat.setEmissiveColor(0.0f, 0.0f, 0.0f); - mat.setSpecularColor(0.0f, 0.0f, 0.0f); - mat.setShininess(5.0f); - ap1.setMaterial(mat); - - Object3D pochaBody = null; - Animation3D pochaAnimation = null; - try { - pochaBody = ModelFactory.loadModel(fragment.getResources(), "pocha.stl", ap1).createObject(); - pochaBody.scale(10.0); - } catch (Exception e) { - e.printStackTrace(); - } - OtherPlayerCharacter chara = new OtherPlayerCharacter(pochaBody); - universe.place(entry.getKey(), chara); - universe.place(chara); - } - universe.apply(entry.getKey(), properties); - } - - // 見えなくなったキャラクターの削除 - for (String key : lastVisibleCharacters.keySet()) { - if (visibleCharacters.get(key) == null) { - universe.displace(key); - } - } +// Map lastVisibleCharacters = new HashMap<>(visibleCharacters); +// visibleCharacters.clear(); +// +// JSON json = new JSON(); +// Map m = json.decode(response); +// for (Map.Entry entry: m.entrySet()) { +// visibleCharacters.put(entry.getKey(), 1); +// +// ArrayList properties = new ArrayList<>(); +// Map player = entry.getValue(); +// +// // 情報の取得 +// Map position = (Map)player.get("position"); +// Map angle = (Map)player.get("angle"); +// Position3D positionProp = new Position3D(((BigDecimal)position.get("x")).doubleValue(), ((BigDecimal)position.get("y")).doubleValue(), ((BigDecimal)position.get("z")).doubleValue()); +// Quaternion3D quaProp = new Quaternion3D(((BigDecimal)angle.get("x")).doubleValue(), ((BigDecimal)angle.get("y")).doubleValue(), ((BigDecimal)angle.get("z")).doubleValue(), ((BigDecimal)angle.get("w")).doubleValue()); +// properties.add(positionProp); +// properties.add(quaProp); +// +// if (!universe.doHaveObj(entry.getKey())) { +// OvergroundActor pocha; +// Appearance ap1 = new Appearance(); +// Material mat = new Material(); +// mat.setDiffuseColor(0.0f, 0.3f, 1.0f); +// mat.setAmbientColor(0.0f, 0.0f, 0.0f); +// mat.setEmissiveColor(0.0f, 0.0f, 0.0f); +// mat.setSpecularColor(0.0f, 0.0f, 0.0f); +// mat.setShininess(5.0f); +// ap1.setMaterial(mat); +// +// Object3D pochaBody = null; +// Animation3D pochaAnimation = null; +// try { +// pochaBody = ModelFactory.loadModel(fragment.getResources(), "pocha.stl", ap1).createObject(); +// pochaBody.scale(10.0); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// OtherPlayerCharacter chara = new OtherPlayerCharacter(pochaBody); +// universe.place(entry.getKey(), chara); +// universe.place(chara); +// } +// universe.apply(entry.getKey(), properties); +// } +// +// // 見えなくなったキャラクターの削除 +// for (String key : lastVisibleCharacters.keySet()) { +// if (visibleCharacters.get(key) == null) { +// universe.displace(key); +// } +// } } } diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OwnPlayer.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OwnPlayer.java index 6265720..522fc33 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OwnPlayer.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/OwnPlayer.java @@ -3,6 +3,9 @@ import android.content.res.Resources; import android.view.MotionEvent; +import net.arnx.jsonic.JSON; + +import org.ntlab.radishforandroidstudio.cactusClient.connections.CharacterConnection; import org.ntlab.radishforandroidstudio.framework.animation.Animation3D; import org.ntlab.radishforandroidstudio.framework.event.PadEvent; import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor; @@ -10,24 +13,31 @@ import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory; import org.ntlab.radishforandroidstudio.framework.model3D.Object3D; import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; +import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; import org.ntlab.radishforandroidstudio.framework.model3D.Universe; +import org.ntlab.radishforandroidstudio.framework.network.CallBack; +import org.ntlab.radishforandroidstudio.framework.physics.Solid3D; import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D; import org.ntlab.radishforandroidstudio.framework.view3D.Camera3D; import org.ntlab.radishforandroidstudio.java3d.Appearance; import org.ntlab.radishforandroidstudio.java3d.Material; import org.ntlab.radishforandroidstudio.java3d.Vector3d; -public class OwnPlayer implements PadListener { +public class OwnPlayer implements PadListener, CactusModel, CallBack { private boolean isTouched = false; private float touchX = 0.0f; private float touchY = 0.0f; private OvergroundActor actor; private Camera3D camera; private Player player; + private CharacterConnection con; + private int nextConnectRenaimdTime = 0; + private int connectInterval = 300; + private String playerId; - public OwnPlayer(Player player, Resources resources, Universe universe, Camera3D camera) { + public OwnPlayer(Player player, Resources resources, Universe universe, Camera3D camera, String playerId) { this.player = player; - + setPlayerId(playerId); // キャラクタの作成 Appearance ap1 = new Appearance(); Material m = new Material(); @@ -53,6 +63,11 @@ camera.setViewLine(actor.getDirection()); camera.setFieldOfView(1.5); camera.setBackClipDistance(10000.0); + updateCamera(); + } + + public void setPlayerId(String id) { + playerId = id; } public void updateCamera() { @@ -63,17 +78,6 @@ camera.setViewLine(new Vector3d(5.0 * charaVector3d.getX(), charaVector3d.getY() - 2.5, 5.0 * charaVector3d.getZ() + touchX));//視線 } - public void progress(long interval) { - updateCamera(); - - if (isTouched) { - Velocity3D vel = actor.getVelocity(); - vel.setX(touchX); - vel.setY(touchY); - actor.setVelocity(vel); - } - } - @Override public boolean onEvent(PadEvent event) { Vector3d charaVector3d = actor.getDirection(); @@ -95,4 +99,46 @@ } return false; } + + @Override + public void update(double interval) { +// updateCamera(); +// +// if (isTouched) { +// Velocity3D vel = actor.getVelocity(); +// vel.setX(touchX); +// vel.setY(touchY); +// actor.setVelocity(vel); +// } +// +// nextConnectRenaimdTime -= interval; +// if (nextConnectRenaimdTime <= 0 && con != null) { +// nextConnectRenaimdTime = connectInterval; +// +// Quaternion3D q = ((Solid3D)(actor.getBody())).getQuaternion(); +// player.setPosition(actor.getPosition()); +// player.setQuaternion3D(q); +// +// JSON json = new JSON(); +// con.addFormParam("characterID", json.encode(player.getCharacterID())); +// con.addFormParam("cameraState", json.encode(player.getCameraState())); +// con.addFormParam("position", json.encode(player.getPosition())); +// con.addFormParam("animationClassToStart", json.encode(player.getEmoteState())); +// con.addFormParam("angle", "{ \"x\":" + q.getX() + ", \"y\":" + q.getY() + ", \"z\":" + q.getZ() + ", \"w\":" + q.getW() + "}"); +// con.doPut(); +// } + } + + @Override + public void setInstanceId(String id) { + player.setInstanceID(id); + + con = new CharacterConnection(playerId); + con.setCallBack(this); + } + + @Override + public void onResponse(String response) { + + } } diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/CharactersFragment.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/CharactersFragment.java index 8de0258..a85f43b 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/CharactersFragment.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/CharactersFragment.java @@ -111,6 +111,9 @@ Bundle bundle = new Bundle(); bundle.putSerializable("characterId", characterIds.get(position)); bundle.putSerializable("player", (Player)player.values().toArray()[0]); + String[] splitedUri = ((String)(player.keySet().toArray()[0])).split("/"); + bundle.putString("playerId", splitedUri[splitedUri.length-1]); + bundle.putString("instanceId", instanceId); PlayerFragment fragment = new PlayerFragment(); fragment.setArguments(bundle); diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/PlayerFragment.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/PlayerFragment.java index 67a1ae2..fc0e37d 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/PlayerFragment.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/PlayerFragment.java @@ -6,26 +6,19 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import org.ntlab.radishforandroidstudio.R; +import org.ntlab.radishforandroidstudio.cactusClient.models.CactusModels; import org.ntlab.radishforandroidstudio.cactusClient.models.OwnPlayer; import org.ntlab.radishforandroidstudio.cactusClient.models.Player; import org.ntlab.radishforandroidstudio.framework.RWT.RWTPad; import org.ntlab.radishforandroidstudio.framework.RWT.RWTUIFragment; -import org.ntlab.radishforandroidstudio.framework.animation.Animation3D; -import org.ntlab.radishforandroidstudio.framework.event.PadEvent; import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor; import org.ntlab.radishforandroidstudio.framework.gameMain.RealTime3DFragment; -import org.ntlab.radishforandroidstudio.framework.listener.PadListener; import org.ntlab.radishforandroidstudio.framework.model3D.BaseObject3D; -import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory; -import org.ntlab.radishforandroidstudio.framework.model3D.Object3D; -import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; import org.ntlab.radishforandroidstudio.framework.physics.Ground; -import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D; import org.ntlab.radishforandroidstudio.java3d.AmbientLight; import org.ntlab.radishforandroidstudio.java3d.Appearance; import org.ntlab.radishforandroidstudio.java3d.Color3f; @@ -33,13 +26,13 @@ import org.ntlab.radishforandroidstudio.java3d.IndexedTriangleArray; import org.ntlab.radishforandroidstudio.java3d.Material; import org.ntlab.radishforandroidstudio.java3d.Point3d; -import org.ntlab.radishforandroidstudio.java3d.Vector3d; import org.ntlab.radishforandroidstudio.java3d.Vector3f; public class PlayerFragment extends RealTime3DFragment { private OvergroundActor pocha; RWTPad pad = null; private OwnPlayer player; + protected CactusModels cactudModel = null; public PlayerFragment() { // Required empty public constructor @@ -47,8 +40,9 @@ @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + cactudModel = new CactusModels(this, universe); + //環境光 AmbientLight amblight = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f)); @@ -91,9 +85,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); Bundle bundle = getArguments(); - - // プレイヤーの作成 - player = new OwnPlayer((Player)bundle.getSerializable("player"), getResources(), universe, camera); + // プレイヤーの作成とインスタンスIDのセット + player = new OwnPlayer((Player)bundle.getSerializable("player"), getResources(), universe, camera, bundle.getString("playerId")); + cactudModel.addModel(player); + cactudModel.setInstanceId(bundle.getString("instanceId")); initGameWindowView(); return parentView; @@ -101,7 +96,7 @@ @Override protected void progress(long interval) { - player.progress(interval); + cactudModel.update(interval); } diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/StartFragment.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/StartFragment.java index a665386..5ab28fd 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/StartFragment.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/views/StartFragment.java @@ -10,7 +10,10 @@ import android.view.ViewGroup; import android.widget.Button; +import net.arnx.jsonic.JSON; + import org.ntlab.radishforandroidstudio.R; +import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; public class StartFragment extends Fragment { diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/framework/gameMain/RealTime3DFragment.java b/app/src/main/java/org/ntlab/radishforandroidstudio/framework/gameMain/RealTime3DFragment.java index 77ff09d..f777fad 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/framework/gameMain/RealTime3DFragment.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/framework/gameMain/RealTime3DFragment.java @@ -22,15 +22,12 @@ protected Camera3D camera; protected RWTSurfaceView view; protected View parentView = null; - private CactusModels models = null; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); universe = new Universe(); camera = new Camera3D(universe); - - models = new CactusModels(this, universe); } @Nullable @@ -49,7 +46,6 @@ @Override protected void update(long interval) { progress(interval); - models.update(interval); universe.update(interval); camera.adjust(interval); view.requestRender();