package org.ntlab.radishforandroidstudio.cactusClient.controller; import android.content.res.Resources; import net.arnx.jsonic.JSON; import net.arnx.jsonic.TypeReference; import org.ntlab.radishforandroidstudio.cactusClient.connections.BulletGetConnection; import org.ntlab.radishforandroidstudio.cactusClient.models.bullet.Bullet; import org.ntlab.radishforandroidstudio.cactusClient.models.bullet.OtherPlayerBullet; import org.ntlab.radishforandroidstudio.framework.gameMain.OnlineModel; 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.java3d.Appearance; import org.ntlab.radishforandroidstudio.java3d.Material; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; public class BulletsModel implements OnlineModel { private Universe universe; private Resources resources; private BulletGetConnection con; private int nextConnectRenaimdTime = 0; static final private int CONNECT_INTERVAL = 100; private Map<String, Integer> visibleBullets = new HashMap<>(); private Map<String, Integer> playerVisibleBullets = new HashMap<>(); private String instanceId; private String playerId; private Set<String> bulletsSet; public enum BulletType { Normal } public BulletsModel(Resources fragment, Universe universe) { this.universe = universe; this.resources = fragment; } @Override public void setInstanceId(String id) { this.instanceId = id; } public void setPlayerId(String id) { playerId = id; } /** * 玉の状態を更新する * * @param interval 前回の更新からの時間差 * @author s.iwatani */ @Override public void update(double interval) { nextConnectRenaimdTime -= interval; if (nextConnectRenaimdTime <= 0 && instanceId != null) { nextConnectRenaimdTime = CONNECT_INTERVAL; con = new BulletGetConnection(instanceId); con.setCallBack(this); con.doGet(); } } @Override public void onResponse(String response) { Map<String, Integer> lastVisibleBullets = new HashMap<>(visibleBullets); Map<String, Integer> lastPlayerVisibleBullets = new HashMap<>(playerVisibleBullets); visibleBullets.clear(); JSON json = new JSON(); if (!response.equals("{}")) { ArrayList<Map<String, Bullet>> m = json.decode(response, new TypeReference<ArrayList<Map<String, Bullet>>>() {}); for (Map<String, Bullet> eachPlayerBullets : m) { for (Map.Entry<String, Bullet> entry : eachPlayerBullets.entrySet()) { visibleBullets.put(entry.getKey(), 1); ArrayList<Property3D> properties = new ArrayList<>(); Bullet b = entry.getValue(); // 自分自身は無視する if (b.getPlayerID().equals(playerId)) { playerVisibleBullets.put(entry.getKey(), 1); continue; } // 情報の取得 properties.add(b.getPosition()); properties.add(b.getAngle()); if (!universe.doHaveObj(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 pochaBody = null; try { pochaBody = ModelFactory.loadModel(resources, "pocha.stl", ap1).createObject(); pochaBody.scale(0.5); } catch (Exception e) { e.printStackTrace(); } OtherPlayerBullet bullet = new OtherPlayerBullet(pochaBody); universe.place(entry.getKey(), bullet); } universe.apply(entry.getKey(), properties); } } } // 見えなくなったキャラクターの削除 for (String key : lastVisibleBullets.keySet()) { if (visibleBullets.get(key) == null) { universe.displace(key); } } // 消えた弾の削除(プレイヤーのみ) for (String key : lastPlayerVisibleBullets.keySet()) { bulletsSet.add(key); } } /** * 消えた弾のうち,セットしたプレイヤーに該当する弾を取得する * * @return ArrayList<String> */ public ArrayList<String> getDeletedPlayerBullets() { ArrayList<String> bullets = new ArrayList<>(); for (String key : bulletsSet) { bullets.add(key); } bulletsSet.clear(); return bullets; } }