package com.example.sprout.battles; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView; import com.example.sprout.R; import com.example.sprout.R.id; import com.example.sprout.R.layout; import com.example.sprout.Sprout; import net.arnx.jsonic.JSON; import java.util.ArrayList; import java.util.HashMap; import framework.gameMain.OvergroundActor; import framework.gameMain.RealTime3DActivity; import framework.model3D.ModelFactory; import framework.model3D.Object3D; import framework.model3D.Position3D; import framework.network.CallBack; import framework.physics.Ground; import framework.physics.Velocity3D; import java3d.AmbientLight; import java3d.Color3f; import java3d.DirectionalLight; import java3d.Vector3d; import java3d.Vector3f; public class BattleActivity extends RealTime3DActivity { private Sprout sprout; private BattlesConnection battlesConnection; public static boolean BATTLE_TEST_MODE = true; private int roomId = -1; private int userId = -1; private int battleId = -1; private Battle battle; private OvergroundActor player; private UpdateActor bullet; private PlayerData playerData = new PlayerData(); private ArrayList<TextView> memberNameView = new ArrayList<TextView>(); private int memberCount = 0; private TextView remainTime; private TextView remainMemberA, remainMemberB; private TextView roundA, roundB, roundNow; private Button endButton, backButton; private ImageButton menuButton; private ProgressBar myHpBar; private HashMap<Integer, ProgressBar> memberHpBar = new HashMap<Integer, ProgressBar>(); private long recharge = 0; private boolean battleEnd = false; private Ground stage; private boolean isTouched = false; private boolean touchRight = false; private boolean touchLeft = false; private float touchX = 0.0f; private float touchY = 0.0f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*activity_battleの表示*/ LayoutInflater inflater = LayoutInflater.from(this); View battleView = inflater.inflate(layout.activity_battle, null, true); addContentView(battleView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); sprout = (Sprout) getApplication(); if (BATTLE_TEST_MODE) { userId = 1000; roomId = 1000; battleId = 1000; sprout.setUserId(userId); sprout.setRoomId(roomId); sprout.setBattleId(battleId); } else { userId = sprout.getUserId(); roomId = sprout.getRoomId(); battleId = sprout.getBattleId(); } /*環境光*/ AmbientLight amblight = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f)); universe.placeLight(amblight); /*平行光源*/ DirectionalLight dirlight = new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f), //光の色 new Vector3f(0.0f, -1.0f, -0.5f) //光の方向ベクトル ); universe.placeLight(dirlight); /*キャラクターの作成*/ Object3D pochaBody = null; Object3D bulletBody = null; try { pochaBody = ModelFactory.loadModel(getResources(), "pocha.stl").createObject(); player = new OvergroundActor(pochaBody, null); player.setPosition(new Position3D(-30.0, 0.0, 7.6)); universe.place(player); bulletBody = ModelFactory.loadModel(getResources(), "gun.obj").createObject(); bullet = new UpdateActor(bulletBody, null); } catch (Exception e) { e.printStackTrace(); } battlesConnection = sprout.getBattlesConnection(); battlesConnection.setCallBack(new CallBack() { @Override public void onResponse(String response) {//返事が来たとき battle = JSON.decode(response, Battle.class); /*名前の代入*/ for (int memberId : battle.getTeamMap().get(roomId).getPlayerMap().keySet()) { if (memberId != userId) { memberNameView.get(memberCount).setText(findByPlayerId(roomId, memberId).getUserName()); memberCount++; } } /*HPの初期設定*/ memberCount = 0; for (int memberId : battle.getTeamMap().get(roomId).getPlayerMap().keySet()) { if (memberId != userId) { memberHpBar.get(memberCount).setMax(findByPlayerId(roomId, memberId).getHp()); memberCount++; } else { myHpBar.setMax(findByPlayerId(roomId, userId).getHp()); } } /*初期位置の設定*/ player.setPosition(findByPlayerId(roomId, userId).getPlayerPosition3d()); } }); battlesConnection.addPathParam(String.valueOf(battleId)); battlesConnection.doGet(); /*ステージの作成*/ Stage stg = new Stage(); Object3D stageObj = stg.stageCreate(); stage = new Ground(stageObj); universe.place(stage); /*Object3D stageObj = null; try { stageObj = ModelFactory.loadModel(getResources(), "teststage.obj").createObject(); stage = new Ground(stageObj); universe.place(stage); } catch (Exception e) { e.printStackTrace(); }*/ camera.setFieldOfView(2);//視野 /*レイアウトまとめ*/ layoutSettings(); } @Override public void onResume() { super.onResume(); this.start(100L, 30L, true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.battle, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void progress(long interval) { /*体力増減*/ //if(myHpBar.getProgress() >= 0) myHpBar.setProgress(count--); /*スレッドサンプル new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); */ /*キャラクターの移動*/ Velocity3D curV = player.getVelocity(); if (touchLeft) { player.rotY(0.1 * (0.5f - touchX) * (double) (interval / 15.0)); curV.setX(player.getDirection().getX() * 40.0 * (0.5f - touchY)); curV.setZ(player.getDirection().getZ() * 40.0 * (0.5f - touchY)); player.setVelocity(curV); } else { curV.setX(0.0); curV.setZ(0.0); player.setVelocity(curV); } /*Weaponの通信*/ Velocity3D d = bullet.getVelocity(); if (touchRight) { if (recharge >= 500 && sprout.getMyRole() == 0) { recharge = 0; bullet.setPosition(player.getPosition()); bullet.setDirection(player.getDirection()); d.setX(bullet.getDirection().getX() * 40.0); d.setZ(bullet.getDirection().getZ() * 40.0); bullet.setVelocity(d); /* ウェポンの情報のセット */ playerData.setWeaponPosition(bullet.getPosition()); playerData.setWeaponVector(bullet.getDirection()); playerData.setWeaponVelocity(bullet.getVelocity()); } else if (recharge >= 1000 && sprout.getMyRole() == 1) { recharge = 0; bullet.setPosition(player.getPosition()); bullet.setDirection(player.getDirection()); d.setX(bullet.getDirection().getX() * 10.0); d.setZ(bullet.getDirection().getZ() * 10.0); bullet.setVelocity(d); /* ウェポンの情報のセット */ playerData.setWeaponPosition(bullet.getPosition()); playerData.setWeaponVector(bullet.getDirection()); playerData.setWeaponVelocity(bullet.getVelocity()); } } recharge += interval; /*通信関係*/ battlesConnection = sprout.getBattlesConnection(); battlesConnection.setCallBack(new CallBack() { @Override public void onResponse(String response) {//返事が来たとき battle = JSON.decode(response, Battle.class); sprout.battleUpdate(battle, universe); /*決着で結果画面へ移動*/ //代入 remainTime.setText(String.valueOf(battle.getTime() / 1000)); /*体力増減*/ memberCount = 0; for (int memberId : battle.getTeamMap().get(roomId).getPlayerMap().keySet()) { if (memberId != userId) { memberHpBar.get(memberCount).setProgress(findByPlayerId(roomId, memberId).getHp()); memberCount++; } else { myHpBar.setProgress(findByPlayerId(roomId, userId).getHp()); } } //バトル終了 if (battle.getState().equals("end")) battleEnd = true; if (playerData.getResultReceive() == true) { sprout.setResult(battle.getTeamMap().get(sprout.getMyTeamId()).result); Intent intent = new Intent(BattleActivity.this, ResultActivity.class); startActivity(intent); finish(); } } }); /*プレイヤーの情報のセット*/ playerData.setUserPosition(player.getPosition()); playerData.setUserVector(player.getDirection()); /*バトル終了状態のセット*/ if (battleEnd) playerData.setResultReceive(battleEnd); /*playerDataの送信*/ battlesConnection.addPathParam(String.valueOf(battleId)); battlesConnection.addFormParam("roomId", String.valueOf(roomId)); battlesConnection.addFormParam("userId", String.valueOf(userId)); String putData = JSON.encode(playerData); battlesConnection.doPut(); /*カメラの位置設定*/ Vector3d charaVector3d = player.getDirection(); charaVector3d.normalize();//キャラの向きを単位ベクトルに camera.setViewPoint(player.getPosition() .add(-5.0 * charaVector3d.getX(), charaVector3d.getY() + 5.0, -5.0 * charaVector3d.getZ()));//視点 camera.setViewLine(new Vector3d(5.0 * charaVector3d.getX(), charaVector3d.getY() - 2.5, 5.0 * charaVector3d.getZ()));//視線 initialize(); } /*画面操作*/ @SuppressLint("NewApi") @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int count = event.getPointerCount(); if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { isTouched = true; touchRight = false; touchLeft = true; //左側がタッチされたら if (event.getX() <= event.getDevice().getMotionRange(MotionEvent.AXIS_X).getMax() / 2.0f) { float maxX = event.getDevice().getMotionRange(MotionEvent.AXIS_X).getMax() / 2.0f; float minX = event.getDevice().getMotionRange(MotionEvent.AXIS_X).getMin(); float maxY = event.getDevice().getMotionRange(MotionEvent.AXIS_Y).getMax(); float minY = event.getDevice().getMotionRange(MotionEvent.AXIS_Y).getMin(); touchX = (event.getX() - minX) / (maxX - minX); touchY = (event.getY() - minY) / (maxY - minY); } else { //右側がタッチされたら touchRight = true; touchLeft = false; touchX = 0.0f; touchY = 0.0f; } } else if (event.getAction() == MotionEvent.ACTION_UP) { isTouched = false; touchRight = false; touchLeft = false; } return true; } private void layoutSettings() { menuButton = (ImageButton) findViewById(id.battleMenuButton); backButton = (Button) findViewById(id.backBattleButton); endButton = (Button) findViewById(id.endBattleButton); /*メニューを開く*/ menuButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (endButton.getVisibility() != View.VISIBLE) { endButton.setVisibility(View.VISIBLE); backButton.setVisibility(View.VISIBLE); } else { endButton.setVisibility(View.INVISIBLE); backButton.setVisibility(View.INVISIBLE); } } }); /*バトルに戻る*/ backButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (backButton.getVisibility() != View.INVISIBLE) { endButton.setVisibility(View.INVISIBLE); backButton.setVisibility(View.INVISIBLE); } } }); /*ルーム一覧画面に戻る*/ endButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { } }); /*ラウンド関係*/ roundA = (TextView) findViewById(id.roundAteam); roundB = (TextView) findViewById(id.roundBteam); roundNow = (TextView) findViewById(id.roundNow); /*メンバーの名前*/ memberNameView.add((TextView) findViewById(id.member1)); memberNameView.add((TextView) findViewById(id.member2)); memberNameView.add((TextView) findViewById(id.member3)); /*メンバー数*/ remainMemberA = (TextView) findViewById(id.remainMemberA); remainMemberB = (TextView) findViewById(id.remainMemberB); /*体力*/ myHpBar = (ProgressBar) findViewById(id.myHpBar1); memberHpBar.put(0, ((ProgressBar) findViewById(id.memberHpBar1))); memberHpBar.put(1, ((ProgressBar) findViewById(id.memberHpBar2))); memberHpBar.put(2, ((ProgressBar) findViewById(id.memberHpBar3))); /*残り時間*/ remainTime = (TextView) findViewById(id.remainTime); } private void initialize() { playerData.setUserPosition(null); playerData.setUserVector(null); playerData.setWeaponPosition(null); playerData.setWeaponVector(null); playerData.setWeaponVelocity(null); touchRight = false; } private Player findByPlayerId(int teamId, int userId) { return battle.getTeamMap().get(teamId).getPlayerMap().get(userId); } @Override public void onPause() { super.onPause(); stop(); } }