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_Auth = URL_PCD_Root + "auth.php";
34 var URL_PCD_Audio = URL_PCD_Root + "audio/";
35 var URL_PCD_Stage = URL_PCD_Root + "stage/";
37 // ゲームを呼び出す関数 適切なdiv要素で呼び出すとゲームを初期化できる。
39 // audio, corelib, images, stageの各フォルダをコピーしたdirにhtmlを置き、<div>.InitGameManager() と実行するスクリプトを書く
40 // stageName == nullだとステージを開始しない。指定する時は.jsおよびパスを省く
45 var CollideBottom = 4;
49 HTMLDivElement.prototype.InitGameManager = function(stageName)
51 if(this instanceof HTMLDivElement)
53 var man = new GameManager(this, null);
54 if(stageName) man.loadStageFromNetwork.apply(man, [stageName]);
58 throw new TypeError("InitGameManager はdiv要素にしか実行できません");
66 function GameManager(parent, debugTextName){
69 if(debugTextName == undefined) debugTextName = "DebugText";
70 if(parent == undefined) parent = document.getElementById("MainArea");
73 if(parent.style.position != 'absolute') parent.style.position = 'relative';
77 this.networkManager = new NetworkManager(this);
78 this.UIManager = new UIManager(this);
79 this.userManager = new UserManager(this);
80 this.mainArea = parent;
81 //必要最低限のCanvasとコンテキストの設定
82 this.mainCanvas = createCanvas("MainCanvas", 640, 480, 0, 0, 1, parent);
83 this.mainCanvas.style.border = "solid 1px";
84 this.mainContext = this.mainCanvas.getContext('2d');
85 this.debugText = document.getElementById(debugTextName); //要素が存在しないとnullになり、デバッグが無効になる
86 if(!this.debugText) this.debugText = null;
90 if(!this.isAvailableBrowser()){
94 this.mainContext.fillStyle = "rgba(200,255,200,0.5)";
95 this.mainContext.strokeStyle = "rgba(0, 0, 0, 0.5)";
96 this.mainContext.font = "normal 20px sans-serif";
97 //実行中のGameStageオブジェクトを格納
98 this.runningStage = null;
99 this.runningStageName = null;
100 //現在存在しているWidghetのリストを格納
101 this.runningWidgets = [];
105 //タイマーカウントの秒あたりの回数を設定
106 this.tickPerSecond = 60;
107 // pauseStage()が呼ばれたときにnullじゃなくなる
108 this.stagePausedFunction = null;
110 this.backgroundMusic = null;
112 //コールバックを行うために、イベントリスナーのmanagerプロパティにGameManagerのインスタンスを代入する。
114 timerTickEventListener.manager = this;
115 window.setInterval(timerTickEventListener, 1000/this.tickPerSecond);
117 //各種コールバック(使用元のスクリプトで使う用)
118 this.stageStartedEvent = null; //ステージが開始されたときに呼ばれる。引数: stage
119 this.stageStoppedEvent = null; //ステージが終了されたときに呼ばれる。引数: stage
121 GameManager.prototype = {
122 timerTick: function(){
123 //各オブジェクトの単位時間ごとの動作と再描画を行う
126 if(this.stagePausedFunction == null){
128 if(this.runningStage){
130 this.runningStage.timerTick();
133 // runningStage.timerTick() 内でpauseStage()された時、ここで再度判定しないとWidghetのtickが実行されてしまう
134 if(this.stagePausedFunction == null){
136 for(var i = 0; i < this.runningWidgets.length; i++){
137 var w = this.runningWidgets[i];
139 // Widghetのtick()からfalseで帰ってきたらWidghetを開放
140 this.removeWidget(w);
146 this.stagePausedFunction();
151 if(this.runningStage){
153 this.runningStage.draw();
156 for(var i = 0; i < this.runningWidgets.length; i++){
157 var w = this.runningWidgets[i];
161 //各オブジェクトの走査が終わってから、死亡判定およびステージ再読み込みを行う。
162 if(this.runningStage && this.runningStage.userControlledCharacter){
163 if(this.runningStage.userControlledCharacter.HP == 0){
164 this.runningStage.userControlledCharacter.HP = this.runningStage.userControlledCharacter.max_HP;
165 this.addWidget(new MessageWidgetClass(this, ["死んでしまった……\n", null, function(w){
166 w.manager.UIManager.clearInput();
167 w.manager.runningStage.userControlledCharacter.HP = w.manager.runningStage.userControlledCharacter.max_HP;
168 if(w.manager.runningStageName){
169 w.manager.loadStageFromNetwork(w.manager.runningStageName);
171 //ローカルモード時は動作を停止させるだけ
172 w.manager.stopStage();
179 addWidget: function(w){
181 this.runningWidgets.push(w);
183 removeWidget: function(w){
184 if(removeObjectFromArray(this.runningWidgets, w))
189 runStage: function(stage){
191 //実行中のステージがあれば終了処理を行わせる。
192 if(this.runningStage){
196 //GameManager側の情報をGameStageに渡す。
197 stage.manager = this;
198 stage.mainCanvas = this.mainCanvas
199 stage.debugCanvas = this.debugCanvas
200 stage.mainContext = this.mainContext
201 stage.debugContext = this.debugContext
202 //GameStage側の初期化処理を行わせる。
205 this.networkManager.joinStage(stage);
206 //runningStageに登録することで、イベントの通知が開始され、GameStageは実行状態に入る。
207 this.runningStage = stage;
209 if(this.stageStartedEvent)
211 this.stageStartedEvent.apply(window, [stage]);
215 pauseStage: function(func){
216 //ステージの実行を一時停止する。一時停止中、funcに指定された関数が毎tick毎に呼ばれる
217 if(this.stagePausedFunction == null){
218 this.stagePausedFunction = func;
221 //ステージが一時停止中のfunc()の中から二重にpauseStage()を呼んではいけない
225 resumeStage: function(){
227 if(this.stagePausedFunction != null) {
228 //必ずpauseStage()の引数に指定したfunc()の中から呼ばれる・・・はず。
229 this.stagePausedFunction = null;
235 stopStage: function(){
237 if(this.runningStage){
238 //runningStageから設定解除することで、イベントの通知は行われなくなる。
239 var aGameStage = this.runningStage;
240 this.runningStage = null;
241 //GameStage側の終了処理を行わせる。
242 aGameStage.stopStage();
243 //GameStageインスタンスからGameManagerの情報を削除する。
244 aGameStage.manager = null;
245 aGameStage.mainCanvas = null;
246 aGameStage.debugCanvas = null;
247 aGameStage.mainContext = null;
248 aGameStage.debugContext = null;
250 //画面上に表示されたすべてのWidgetを解放する
251 for(;this.runningWidgets.length>0;)
253 this.removeWidget(this.runningWidgets[0]);
256 if(this.stageStoppedEvent)
258 this.stageStoppedEvent.apply(window, [aGameStage]);
262 loadStageFromLocal: function(code){
265 URL_PCD_Auth = URL_PCD_Root + "auth.php";
266 URL_PCD_Audio = URL_PCD_Root + "audio/";
267 URL_PCD_Stage = URL_PCD_Root + "stage/";
269 var stage = eval(code);
270 mainManager.runStage(stage);
272 loadStageFromNetwork: function(name){
273 //URL_PCD_Stage/name.jsを利用してステージを作成する。
274 request = this.networkManager.CreateRequestObject();
276 request.open('GET', URL_PCD_Stage + name + ".js", false);
277 this.networkManager.RequestObjectDisableCache(request);
280 if(request.status == 0){
281 alert("ネットワークにアクセスできません。" + request.status + ":" + request.statusText);
282 }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
283 var stage = eval(request.responseText);
284 this.runStage(stage);
285 this.runningStageName = name;
287 alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
290 debugOut: function(str){
291 if(this.debugText != null)
296 this.debugText.value = str.replace(/\n/g,"\r\n") + this.debugText.value;
299 this.debugText.innerHTML = str + this.debugText.value;
303 isAvailableBrowser: function(){
304 //ブラウザの判別を行う。実行不可能な場合はfalseを返す。
305 //http://d.hatena.ne.jp/Naotsugu/20110927/1317140891
306 var userAgent = window.navigator.userAgent.toLowerCase();
307 var appVersion = window.navigator.appVersion.toLowerCase();
309 if (userAgent.indexOf('opera') != -1) {
311 this.debugOut("Browser:Opera\n");
312 } else if (userAgent.indexOf('msie') != -1) {
313 if (appVersion.indexOf("msie 9.") != -1) {
315 this.debugOut("Browser:IE9\n");
316 } else if (appVersion.indexOf("msie 8.") != -1) {
318 this.debugOut("Browser:IE8\n");
319 } else if (appVersion.indexOf("msie 7.") != -1) {
321 this.debugOut("Browser:IE7\n");
322 } else if (appVersion.indexOf("msie 6.") != -1) {
324 this.debugOut("Browser:IE6\n");
326 this.debugOut("Browser:IE?\n");
329 } else if (userAgent.indexOf('chrome') != -1) {
331 this.debugOut("Browser:Chrome\n");
332 } else if (userAgent.indexOf('safari') != -1) {
334 this.debugOut("Browser:Safari\n");
335 } else if (userAgent.indexOf('gecko') != -1) {
337 this.debugOut("Browser:Gecko\n");
340 this.debugOut("Browser:Unknown\n");
342 //描画コンテキストからHTML5対応チェック
343 if(!this.mainCanvas || !this.mainCanvas.getContext){
345 alert("このゲームを遊ぶためには、HTML5に対応しているブラウザ(Google Chrome等)でアクセスしてください...。");
350 setBackgroundMusic: function(name){
351 if(this.backgroundMusic){
353 this.backgroundMusic.pause();
354 this.backgroundMusic = null;
357 //引数がnullでなければaudioオブジェクトを取得
358 this.backgroundMusic = createAudio(name);
359 if(this.backgroundMusic){
361 this.backgroundMusic.loop = true;
362 this.backgroundMusic.play();
371 //イベントリスナーにおけるthisは、イベントリスナーを登録したオブジェクトまたはwindowになり、通常とは異なるので注意。
373 function timerTickEventListener(event)
375 timerTickEventListener.manager.timerTick(event);