2 ****PCD-2013 GameScriptCoreLibrary****
3 Tokyo Gakugei University Senior High School.
6 Mac OSX: Safari, Chrome
7 Windows: Chrome, IE(9 or later)
21 mainManager = new GameManager();
29 var loc = document.location.href;
31 var URL_PCD_Root = loc.slice(0, loc.lastIndexOf("/") + 1);
33 //var URL_PCD_Root = "http://localhost/pcd2013dev/www/";
34 //var URL_PCD_Root = "http://192.168.6.242/pcd2013dev/www/";
35 //var URL_PCD_Root = "http://192.168.0.3/PCD2013GSCL/www/";
36 //var URL_PCD_Root = "http://192.168.6.242/pcd2013hikarupsp/pcd2013dev/www/";
37 var URL_PCD_Auth = URL_PCD_Root + "auth.php";
38 var URL_PCD_Audio = URL_PCD_Root + "audio/";
39 var URL_PCD_Stage = URL_PCD_Root + "stage/";
41 // ゲームを呼び出す関数 適切なdiv要素で呼び出すとゲームを初期化できる。
43 // audio, corelib, images, stageの各フォルダをコピーしたdirにhtmlを置き、<div>.InitGameManager() と実行するスクリプトを書く
44 // stageName == nullだとステージを開始しない。指定する時は.jsおよびパスを省く
46 HTMLDivElement.prototype.InitGameManager = function(stageName)
48 if(this instanceof HTMLDivElement)
50 var man = new GameManager(this, null);
51 if(stageName) man.loadStageFromNetwork.apply(man, [stageName]);
55 throw new TypeError("InitGameManager はdiv要素にしか実行できません");
63 function GameManager(parent, debugTextName){
66 if(debugTextName == undefined) debugTextName = "DebugText";
67 if(parent == undefined) parent = document.getElementById("MainArea");
70 if(parent.style.position != 'absolute') parent.style.position = 'relative';
74 this.networkManager = new NetworkManager(this);
75 this.UIManager = new UIManager(this);
76 this.userManager = new UserManager(this);
77 this.mainArea = parent;
78 //必要最低限のCanvasとコンテキストの設定
79 this.mainCanvas = createCanvas("MainCanvas", 640, 480, 0, 0, 1, parent);
80 this.mainCanvas.style.border = "solid 1px";
81 this.mainContext = this.mainCanvas.getContext('2d');
82 this.debugText = document.getElementById(debugTextName); //要素が存在しないとnullになり、デバッグが無効になる
83 if(!this.debugText) this.debugText = null;
87 if(!this.isAvailableBrowser()){
91 this.mainContext.fillStyle = "rgba(200,255,200,0.5)";
92 this.mainContext.strokeStyle = "rgba(0, 0, 0, 0.5)";
93 this.mainContext.font = "normal 20px sans-serif";
94 //実行中のGameStageオブジェクトを格納
95 this.runningStage = null;
96 this.runningStageName = null;
97 //現在存在しているWidghetのリストを格納
98 this.runningWidghets = [];
102 //タイマーカウントの秒あたりの回数を設定
103 this.tickPerSecond = 60;
104 // pauseStage()が呼ばれたときにnullじゃなくなる
105 this.stagePausedFunction = null;
107 this.backgroundMusic = null;
108 this.addWidget(new UserStateWidgetClass(this));
110 //コールバックを行うために、イベントリスナーのmanagerプロパティにGameManagerのインスタンスを代入する。
112 timerTickEventListener.manager = this;
113 window.setInterval(timerTickEventListener, 1000/this.tickPerSecond);
115 GameManager.prototype = {
116 timerTick: function(){
117 //各オブジェクトの単位時間ごとの動作と再描画を行う
120 if(this.stagePausedFunction == null){
122 if(this.runningStage){
124 this.runningStage.timerTick();
127 // runningStage.timerTick() 内でpauseStage()された時、ここで再度判定しないとWidghetのtickが実行されてしまう
128 if(this.stagePausedFunction == null){
130 for(var i = 0; i < this.runningWidghets.length; i++){
131 var w = this.runningWidghets[i];
133 // Widghetのtick()からfalseで帰ってきたらWidghetを開放
134 this.removeWidget(w);
140 this.stagePausedFunction();
144 if(this.runningStage){
146 this.runningStage.draw();
149 for(var i = 0; i < this.runningWidghets.length; i++){
150 var w = this.runningWidghets[i];
154 //各オブジェクトの走査が終わってから、死亡判定およびステージ再読み込みを行う。
155 if(this.userManager.HP == 0){
157 this.UIManager.clearInput();
158 if(this.runningStageName){
159 this.loadStageFromNetwork(this.runningStageName);
161 //ローカルモード時は動作を停止させるだけ
164 this.userManager.HP = this.userManager.max_HP;
167 addWidget: function(w){
169 this.runningWidghets.push(w);
171 removeWidget: function(w){
172 removeObjectFromArray(this.runningWidghets, w);
175 runStage: function(stage){
177 //実行中のステージがあれば終了処理を行わせる。
178 if(this.runningStage){
182 //GameManager側の情報をGameStageに渡す。
183 stage.manager = this;
184 stage.mainCanvas = this.mainCanvas
185 stage.debugCanvas = this.debugCanvas
186 stage.mainContext = this.mainContext
187 stage.debugContext = this.debugContext
188 //GameStage側の初期化処理を行わせる。
191 this.networkManager.joinStage(stage);
192 //runningStageに登録することで、イベントの通知が開始され、GameStageは実行状態に入る。
193 this.runningStage = stage;
195 pauseStage: function(func){
196 //ステージの実行を一時停止する。一時停止中、funcに指定された関数が毎tick毎に呼ばれる
197 if(this.stagePausedFunction == null){
198 this.stagePausedFunction = func;
201 //ステージが一時停止中のfunc()の中から二重にpauseStage()を呼んではいけない
205 resumeStage: function(){
207 if(this.stagePausedFunction != null) {
208 //必ずpauseStage()の引数に指定したfunc()の中から呼ばれる・・・はず。
209 this.stagePausedFunction = null;
215 stopStage: function(){
217 if(this.runningStage){
218 //runningStageから設定解除することで、イベントの通知は行われなくなる。
219 var aGameStage = this.runningStage;
220 this.runningStage = null;
221 //GameStage側の終了処理を行わせる。
222 aGameStage.stopStage();
223 //GameStageインスタンスからGameManagerの情報を削除する。
224 aGameStage.manager = null;
225 aGameStage.mainCanvas = null;
226 aGameStage.debugCanvas = null;
227 aGameStage.mainContext = null;
228 aGameStage.debugContext = null;
231 loadStageFromLocal: function(code){
234 URL_PCD_Auth = URL_PCD_Root + "auth.php";
235 URL_PCD_Audio = URL_PCD_Root + "audio/";
236 URL_PCD_Stage = URL_PCD_Root + "stage/";
238 var stage = eval(code);
239 mainManager.runStage(stage);
241 loadStageFromNetwork: function(name){
242 //URL_PCD_Stage/name.jsを利用してステージを作成する。
243 request = this.networkManager.CreateRequestObject();
245 request.open('GET', URL_PCD_Stage + name + ".js", false);
246 this.networkManager.RequestObjectDisableCache(request);
249 if(request.status == 0){
250 alert("ネットワークにアクセスできません。" + request.status + ":" + request.statusText);
251 }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
252 var stage = eval(request.responseText);
253 this.runStage(stage);
254 this.runningStageName = name;
256 alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
259 debugOut: function(str){
260 if(this.debugText != null)
265 this.debugText.value = str.replace(/\n/g,"\r\n") + this.debugText.value;
268 this.debugText.innerHTML = str + this.debugText.value;
272 isAvailableBrowser: function(){
273 //ブラウザの判別を行う。実行不可能な場合はfalseを返す。
274 //http://d.hatena.ne.jp/Naotsugu/20110927/1317140891
275 var userAgent = window.navigator.userAgent.toLowerCase();
276 var appVersion = window.navigator.appVersion.toLowerCase();
278 if (userAgent.indexOf('opera') != -1) {
280 this.debugOut("Browser:Opera\n");
281 } else if (userAgent.indexOf('msie') != -1) {
282 if (appVersion.indexOf("msie 9.") != -1) {
284 this.debugOut("Browser:IE9\n");
285 } else if (appVersion.indexOf("msie 8.") != -1) {
287 this.debugOut("Browser:IE8\n");
288 } else if (appVersion.indexOf("msie 7.") != -1) {
290 this.debugOut("Browser:IE7\n");
291 } else if (appVersion.indexOf("msie 6.") != -1) {
293 this.debugOut("Browser:IE6\n");
295 this.debugOut("Browser:IE?\n");
298 } else if (userAgent.indexOf('chrome') != -1) {
300 this.debugOut("Browser:Chrome\n");
301 } else if (userAgent.indexOf('safari') != -1) {
303 this.debugOut("Browser:Safari\n");
304 } else if (userAgent.indexOf('gecko') != -1) {
306 this.debugOut("Browser:Gecko\n");
309 this.debugOut("Browser:Unknown\n");
311 //描画コンテキストからHTML5対応チェック
312 if(!this.mainCanvas || !this.mainCanvas.getContext){
314 alert("このゲームを遊ぶためには、HTML5に対応しているブラウザ(Google Chrome等)でアクセスしてください...。");
319 setBackgroundMusic: function(name){
320 if(this.backgroundMusic){
322 this.backgroundMusic.pause();
323 this.backgroundMusic = null;
326 //引数がnullでなければaudioオブジェクトを取得
327 this.backgroundMusic = createAudio(name);
328 if(this.backgroundMusic){
330 this.backgroundMusic.loop = true;
331 this.backgroundMusic.play();
340 //イベントリスナーにおけるthisは、イベントリスナーを登録したオブジェクトまたはwindowになり、通常とは異なるので注意。
342 function timerTickEventListener(event)
344 timerTickEventListener.manager.timerTick(event);