diff --git a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/MovableObjectModel.java b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/MovableObjectModel.java index 14b34b4..87b5e63 100644 --- a/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/MovableObjectModel.java +++ b/app/src/main/java/org/ntlab/radishforandroidstudio/cactusClient/models/MovableObjectModel.java @@ -1,10 +1,24 @@ package org.ntlab.radishforandroidstudio.cactusClient.models; +import net.arnx.jsonic.JSON; +import net.arnx.jsonic.TypeReference; + import org.ntlab.radishforandroidstudio.cactusClient.connections.ObjectsConnection; import org.ntlab.radishforandroidstudio.framework.gameMain.GameBaseModel; import org.ntlab.radishforandroidstudio.framework.gameMain.RealTime3DFragment; +import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory; +import org.ntlab.radishforandroidstudio.framework.model3D.Object3D; +import org.ntlab.radishforandroidstudio.framework.model3D.Property3D; import org.ntlab.radishforandroidstudio.framework.model3D.Universe; import org.ntlab.radishforandroidstudio.framework.network.CallBack; +import org.ntlab.radishforandroidstudio.java3d.Appearance; +import org.ntlab.radishforandroidstudio.java3d.Material; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; /** * オブジェクトを管理するモデル @@ -14,23 +28,23 @@ private Universe universe; private RealTime3DFragment fragment; private ObjectsConnection con; + private int nextConnectRenaimdTime = 0; + private int connectInterval = 300; private String instanceId; - private String objectId; + private Map visibleobject = new HashMap<>(); - public MovableObjectModel(RealTime3DFragment fragment, Universe universe) { + public MovableObjectModel(RealTime3DFragment fragment, Universe universe,String instanceId) { this.fragment = fragment; this.universe = universe; + this.setInstanceId(instanceId); } @Override - public void setInstanceId(String id) { - this.instanceId = id; + public void setInstanceId(String instanceId) { + this.instanceId = instanceId; } - public void setObjectId(String objectId) { - this.objectId = objectId; - } /** * オブジェクトの状態更新 @@ -39,15 +53,64 @@ */ @Override public void update(double interval) { - con = new ObjectsConnection(instanceId); - con.addQueryParam("instanceId", instanceId); - con.setCallBack(this); - con.doGet(); + nextConnectRenaimdTime -= interval; + if (nextConnectRenaimdTime <= 0) { + nextConnectRenaimdTime = connectInterval; + con = new ObjectsConnection(instanceId); + con.setCallBack(this); + con.doGet(); + } } @Override public void onResponse(String response) { + JSON json = new JSON(); + + + Map o = json.decode(response, new TypeReference>() { + }); + for (Map.Entry entry : o.entrySet()) { + ArrayList properties = new ArrayList<>(); + Object object = entry.getValue(); + + + properties.add(object.getPosition()); + properties.add(object.getAngle()); + + if (!visibleobject.containsKey(entry.getKey())) { + 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 objBody = null; + try { + objBody = ModelFactory.loadModel(fragment.getResources(), "cube-binary.stl", ap1).createObject(); + } catch (Exception e) { + e.printStackTrace(); + } + + MovableObject object1 = new MovableObject(objBody); + visibleobject.put(entry.getKey(), object1); + universe.place(entry.getKey(), object1); + universe.place(object1); + + } + universe.apply(entry.getKey(), properties); + } + + for (String key : visibleobject.keySet()) { + if (!o.containsKey(key)) { + universe.displace(visibleobject.get(key)); + } + } + + } }