Newer
Older
CarrotServer / src / game / GameManager.java
t-nakanishi on 18 Jul 2017 3 KB [add] project
package game;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import account.Account;
import framework.model3D.CollisionResult;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.physics.PhysicsUtility;
import framework.physics.Velocity3D;
import room.Room;

public class GameManager implements IGameElement,Runnable {
	
	//シングルトンパターン
	private static GameManager theInstance;
	private GameManager() {
	}
	public static GameManager getInstance() {
		if (theInstance == null) {
			theInstance = new GameManager();
			System.out.println("GameManager theInstance.start() 前");
			theInstance.start();	//start()起動
		}
		return theInstance;
	}
	
	public static final int DELAY_TIME = 100;	//1000ms = 1s 
	
	int countTime = 0;
	
	//GameをgameListで持つ
	private ArrayList<Game> gameList = new ArrayList<Game>();
	
	//タイマー処理部分開始
	public void start() {
		ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
		scheduler.scheduleAtFixedRate(this, 0, DELAY_TIME, TimeUnit.MILLISECONDS);
	}
	
	//あたり判定・レーザ光線や爆弾の処理 run()
	@Override
	public void run() {

System.out.println("scheduler : " + countTime);
		countTime++;
		
		//gameがある時に
		if(gameList.size() > 0) {
			Game game;
			for(int j=0; j<gameList.size(); j++) {
				game = getGame(j);
				//removeFlagがtrueの時にgameを消去
				if(game.getRemoveFlag() == true) {
					game.getRoom().removeGame();
					removeGame(game);
				//Flagがfalseならupdate
				} else {
					getGame(j).update(countTime);;
				}
			}
		}

	}
	
	
	//ゲームを増やす
	public void addGame(Game game) {
		game.placeStage();
		gameList.add(game);
	}

	//ゲームを減らす
	public void removeGame(Game game) {
		gameList.remove(game);
	}

	//ゲームの個数をカウント
	public int getGameCount() {
		return gameList.size();
	}
	
	//n番目のゲーム情報を取得
	public Game getGame(int n){
		return gameList.get(n);
	}
	
	//重複して在籍しているgameが無いか探す
	public void searchGameSid(Room room) {

System.out.println("                    searchGameSid:in");

		for(int i=0; i<Room.MAX_MEMBERS; i++) {
			String sId = room.getMember(i).getsId();
		

System.out.println("                    searchGameSid:for文 " + i + " 回目");
			
			//++++++++++++++++++++++++++++++++++visitor++++++++++++++++++++++++++++++++++
	
			PlayerSearchVisitor visitor = new PlayerSearchVisitor(sId);
			accept(visitor);
			
			Team team = visitor.getTeam();					//所属チームの特定

System.out.println("                    searchGameSid:Team ok");

			Account account = visitor.getPlayerAccount();	//個人を特定
			

System.out.println("                    searchGameSid:Account ok");
			
			//++++++++++++++++++++++++++++++++++visitor++++++++++++++++++++++++++++++++++
			
			//該当した場合、そのアカウントをゲームから削除する
			if(account != null) {
				team.removeMember(account);

System.out.println("                    searchGameSid:ifの中のremoveMember");

		}
		}
	}
	
	//visitorによる処理を受け入れる
	public boolean accept(IGameElementVisitor v) {
		if (!v.visit(this)) { return false; }
		for (Game game: gameList) {
			if (!game.accept(v)) return false;
		}
		return true;
	}
	
}