Newer
Older
RxSprout / app / src / main / java / com / example / sprout / Sprout.java
package com.example.sprout;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.example.sprout.accounts.AccountsConnection;
import com.example.sprout.battles.Battle;
import com.example.sprout.battles.BattlesConnection;
import com.example.sprout.battles.Team;
import com.example.sprout.battles.UpdateActor;
import com.example.sprout.rooms.RoomsConnection;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import framework.gameMain.OvergroundActor;
import framework.model3D.ModelFactory;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.model3D.Universe;
import java3d.Vector3d;

public class Sprout extends Application {

    private RoomsConnection roomsConnection = null;
    private BattlesConnection battlesConnection = null;
    private AccountsConnection accountsConnection = null;

    /*
     * team1: 味方 team2: 敵
     */
    private int team1Id = -1;
    private int team2Id = -1;
    private HashMap<Integer, OvergroundActor> team1Players = new HashMap<Integer, OvergroundActor>();
    private HashMap<Integer, OvergroundActor> team2Players = new HashMap<Integer, OvergroundActor>();
    private HashMap<Integer, UpdateActor> team1bullets = new HashMap<Integer, UpdateActor>();
    private HashMap<Integer, UpdateActor> team2bullets = new HashMap<Integer, UpdateActor>();
    private HashMap<Integer, UpdateActor> team1magics = new HashMap<Integer, UpdateActor>();
    private HashMap<Integer, UpdateActor> team2magics = new HashMap<Integer, UpdateActor>();
    private HashMap<Integer, Boolean> tamatama = new HashMap<Integer, Boolean>();

    public Sprout() {

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {
                // TODO Auto-generated method stub
                ActivityManager activityManager = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
                List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(5);
                int activity_num = taskInfo.get(0).numRunning;
                if (activity_num <= 1) {
                    // saveの処理

                }
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                ActivityManager activityManager = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
                List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(5);
                int activity_num = taskInfo.get(0).numRunning;
                if (activity_num <= 1) {
                    // loadの処理

                }

            }

            @Override
            public void onActivityStarted(Activity activity) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onActivityResumed(Activity activity) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onActivityPaused(Activity activity) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                // TODO Auto-generated method stub

            }

        });

    }

    /**
     * サーバーのBattlesに接続
     */
    public BattlesConnection getBattlesConnection() {
        battlesConnection = new BattlesConnection();
        return battlesConnection;
    }

    public RoomsConnection getRoomsConnection() {
        roomsConnection = new RoomsConnection();
        return roomsConnection;
    }

    public AccountsConnection getAccountsConnection() {
        accountsConnection = new AccountsConnection();
        return accountsConnection;
    }

    public int getUserId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int userId = data.getInt("userId", -1);
        return userId;
    }

    public void setUserId(int userId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("userId", userId);
        editor.commit();
    }

    public int getMyTeamId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int myteamId = data.getInt("myteamId", -1);
        return myteamId;
    }

    public void setMyTeamId(int teamId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("myteamId", teamId);
        editor.commit();
    }

    public int getEnemyTeamId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int enemyteamId = data.getInt("myteamId", -1);
        return enemyteamId;
    }

    public void setEnemyTeamId(int teamId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("enemyteamId", teamId);
        editor.commit();
    }

    public int getRoomId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int roomId = data.getInt("roomId", -1);
        return roomId;
    }

    public void setRoomId(int roomId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("roomId", roomId);
        editor.commit();
    }

    public String getRoomName() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        String roomName = data.getString("roomName", "roomName");
        return roomName;
    }

    public void setRoomName(String roomName) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putString("roomName", roomName);
        editor.commit();
    }

    public int getMemberId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int memberId = data.getInt("memberId", -1);
        return memberId;
    }

    public void setMemberId(int memberId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("memberId", memberId);
        editor.commit();
    }

    public int getMyRole() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int role = data.getInt("role", -1);
        return role;
    }

    public void setMyRole(int role) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("role", role);
        editor.commit();
    }

    public int getBattleId() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        int battleId = data.getInt("battleId", -1);
        return battleId;
    }

    public void setBattleId(int battleId) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putInt("battleId", battleId);
        editor.commit();
    }

    public Boolean getResult() {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        Boolean result = data.getBoolean("result", false);
        return result;
    }

    public void setResult(Boolean result) {
        SharedPreferences data = getSharedPreferences("DataSave", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = data.edit();
        editor.putBoolean("result", result);
        editor.commit();
    }


    public void battleUpdate(Battle battle, Universe universe) {

        if (team1Id == -1 || team2Id == -1) {
            for (Integer teamId : battle.getTeamMap().keySet()) {
                if (teamId == getRoomId()) {
                    team1Id = teamId;
                    setMyTeamId(teamId);
                } else {
                    team2Id = teamId;
                    setEnemyTeamId(teamId);
                }
            }
        }

        playerUpdate(battle, universe);
        bulletUpdate(battle, universe);
        magicUpdate(battle, universe);

    }

    private void playerUpdate(Battle battle, Universe universe) {

        for (Entry<Integer, Team> entry : battle.teamMap.entrySet()) {
            Team team = entry.getValue();
            int teamId = entry.getKey();

			/* キャラクターの作成 */
            try {
                for (int userId : team.getPlayerMap().keySet()) {
                    if (team1Players.get(userId) == null && userId != getUserId() && teamId == getRoomId()) {
                        Object3D pochaBody = ModelFactory.loadModel(getResources(), "pocha.stl").createObject();
                        OvergroundActor oga = new OvergroundActor(pochaBody, null);
                        oga.setPosition(team.findPlayerById(userId).getPlayerPosition3d());
                        team1Players.put(userId, oga);
                        universe.place(team1Players.get(userId));
                    }
                    if (team2Players.get(userId) == null && teamId != getRoomId()) {
                        Object3D pochaBody = ModelFactory.loadModel(getResources(), "pocha.stl").createObject();
                        OvergroundActor oga = new OvergroundActor(pochaBody, null);
                        oga.setPosition(team.findPlayerById(userId).getPlayerPosition3d());
                        team2Players.put(userId, new OvergroundActor(pochaBody, null));
                        universe.place(team2Players.get(userId));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

			/* 位置・向き更新 */
            for (int userId : team.getPlayerMap().keySet()) {
                if (userId != getUserId() && teamId == getRoomId()) {
                    team1Players.get(userId).setPosition(team.findPlayerById(userId).getPlayerPosition3d());
                    team1Players.get(userId).setDirection(team.findPlayerById(userId).getPlayerVector3d());
                }
                if (teamId != getRoomId()) {
                    team2Players.get(userId).setPosition(team.findPlayerById(userId).getPlayerPosition3d());
                    team2Players.get(userId).setDirection(team.findPlayerById(userId).getPlayerVector3d());
                }
            }
        }
    }

    private void bulletUpdate(Battle battle, Universe universe) {

        try {

            for (int id : battle.getTeamMap().get(team1Id).bullets.keySet()) {
                Position3D p = battle.getTeamMap().get(team1Id).bullets.get(id).getBulletPosition3d();
                Vector3d v = battle.getTeamMap().get(team1Id).bullets.get(id).getBulletVector3d();

                if (team1bullets.get(id) == null) {
                    UpdateActor bullet = null;

					/* 弾の発射 */
                    Object3D bulletBody = null;

                    bulletBody = ModelFactory.loadModel(getResources(), "gun.obj").createObject();

                    bullet = new UpdateActor(bulletBody, null);

                    bullet.setPosition(p);
                    bullet.setDirection(v);

                    universe.place(bullet);

					/* 弾を消す */
                    // universe.displace(bullet);

                    team1bullets.put(id, bullet);
                    team1bullets.get(id).setPosition(p);
                } else {
                    /*for (UpdateActor bullet : team1bullets.values()) {
                        bullet.setPosition(p);
						bullet.setDirection(v);
					}*/
                    team1bullets.get(id).setPosition(p);
                    team1bullets.get(id).setDirection(v);
                }
            }
            for (int bulletId : team1bullets.keySet()) {

                if (battle.getTeamMap().get(team1Id).bullets.containsKey(bulletId) == false) {
                    universe.displace(team1bullets.get(bulletId));
                }
                // universe.place(bullet);
            }

            for (int id : battle.getTeamMap().get(team2Id).bullets.keySet()) {
                Position3D p = battle.getTeamMap().get(team2Id).bullets.get(id).getBulletPosition3d();
                Vector3d v = battle.getTeamMap().get(team2Id).bullets.get(id).getBulletVector3d();

                if (team2bullets.get(id) == null) {
                    UpdateActor bullet = null;

					/* 弾の発射 */
                    Object3D bulletBody = null;

                    bulletBody = ModelFactory.loadModel(getResources(), "gun.obj").createObject();

                    bullet = new UpdateActor(bulletBody, null);

                    bullet.setPosition(p);
                    bullet.setDirection(v);

                    universe.place(bullet);

					/* 弾を消す */
                    // universe.displace(bullet);

                    team2bullets.put(id, bullet);
                    team2bullets.get(id).setPosition(p);
                } else {

					/*for (UpdateActor bullet : team2bullets.values()) {
                        bullet.setPosition(p);
						bullet.setDirection(v);

					}*/
                    team2bullets.get(id).setPosition(p);
                    team2bullets.get(id).setDirection(v);
                }
                for (int bulletId : team2bullets.keySet()) {

                    if (battle.getTeamMap().get(team2Id).bullets.containsKey(bulletId) == false) {
                        universe.displace(team2bullets.get(bulletId));
                    }
                    // universe.place(bullet);
                }

                if (battle.getTeamMap().get(team2Id).bullets.containsKey(id) == false) {
                    universe.displace(team2bullets.get(id));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void magicUpdate(Battle battle, Universe universe) {
        try {

            for (int id : battle.getTeamMap().get(team1Id).magics.keySet()) {
                Position3D p = battle.getTeamMap().get(team1Id).magics.get(id).getMagicPosition3d();
                Vector3d v = battle.getTeamMap().get(team1Id).magics.get(id).getMagicVector3d();

                if (team1magics.get(id) == null) {
                    UpdateActor magic = null;

					/* 魔法の発射 */
                    Object3D magicBody = null;

                    magicBody = ModelFactory.loadModel(getResources(), "gun.obj").createObject();

                    magic = new UpdateActor(magicBody, null);

                    magic.setPosition(p);
                    magic.setDirection(v);

                    universe.place(magic);

					/* 魔法を消す */
                    // universe.displace(bullet);

                    team1magics.put(id, magic);
                    team1magics.get(id).setPosition(p);
                } else {

                    for (UpdateActor magic : team1magics.values()) {
                        magic.setPosition(p);
                        magic.setDirection(v);

                        // universe.place(bullet);

                    }
                }

                if (battle.getTeamMap().get(team1Id).bullets.containsKey(id) == false) {
                    universe.displace(team1magics.get(id));
                }

            }

            for (int id : battle.getTeamMap().get(team2Id).magics.keySet()) {
                Position3D a = battle.getTeamMap().get(team2Id).magics.get(id).getMagicPosition3d();
                Vector3d b = battle.getTeamMap().get(team2Id).magics.get(id).getMagicVector3d();

                if (team2magics.get(id) == null) {
                    UpdateActor magic = null;

					/* 弾の発射 */
                    Object3D magicBody = null;

                    magicBody = ModelFactory.loadModel(getResources(), "gun.obj").createObject();

                    magic = new UpdateActor(magicBody, null);

                    magic.setPosition(a);
                    magic.setDirection(b);

                    universe.place(magic);

					/* 魔法を消す */
                    // universe.displace(bullet);

                    team2magics.put(id, magic);
                    team2magics.get(id).setPosition(a);
                } else {

                    for (UpdateActor magic : team2magics.values()) {
                        magic.setPosition(a);
                        magic.setDirection(b);

                    }
                }

                if (battle.getTeamMap().get(team2Id).bullets.containsKey(id) == false) {
                    universe.displace(team2magics.get(id));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* getTeamMap簡略 */
    public Team findTeamById(int id, Battle battle) {
        return battle.getTeamMap().get(id);
    }
}