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;
86 this.enableStageDebug = false;
91 if(!this.isAvailableBrowser()){
95 this.mainContext.fillStyle = "rgba(200,255,200,0.5)";
96 this.mainContext.strokeStyle = "rgba(0, 0, 0, 0.5)";
97 this.mainContext.font = "normal 20px sans-serif";
98 //実行中のGameStageオブジェクトを格納
99 this.runningStage = null;
100 //現在存在しているWidghetのリストを格納
101 this.runningWidghets = [];
105 //タイマーカウントの秒あたりの回数を設定
106 this.tickPerSecond = 60;
107 // pauseStage()が呼ばれたときにnullじゃなくなる
108 this.stagePausedFunction = null;
110 this.backgroundMusic = null;
111 this.addWidget(new UserStateWidgetClass(this));
113 //コールバックを行うために、イベントリスナーのmanagerプロパティにGameManagerのインスタンスを代入する。
115 timerTickEventListener.manager = this;
116 window.setInterval(timerTickEventListener, 1000/this.tickPerSecond);
118 GameManager.prototype = {
119 timerTick: function(){
120 //各オブジェクトの単位時間ごとの動作と再描画を行う
123 if(this.stagePausedFunction == null){
125 if(this.runningStage){
127 this.runningStage.timerTick();
130 // runningStage.timerTick() 内でpauseStage()された時、ここで再度判定しないとWidghetのtickが実行されてしまう
131 if(this.stagePausedFunction == null){
133 for(var i = 0; i < this.runningWidghets.length; i++){
134 var w = this.runningWidghets[i];
136 // Widghetのtick()からfalseで帰ってきたらWidghetを開放
137 this.removeWidget(w);
143 this.stagePausedFunction();
147 if(this.runningStage){
149 this.runningStage.draw();
152 for(var i = 0; i < this.runningWidghets.length; i++){
153 var w = this.runningWidghets[i];
157 addWidget: function(w){
159 this.runningWidghets.push(w);
161 removeWidget: function(w){
162 removeObjectFromArray(this.runningWidghets, w);
165 runStage: function(stage){
167 //実行中のステージがあれば終了処理を行わせる。
168 if(this.runningStage){
172 //GameManager側の情報をGameStageに渡す。
173 stage.manager = this;
174 stage.mainCanvas = this.mainCanvas
175 stage.debugCanvas = this.debugCanvas
176 stage.mainContext = this.mainContext
177 stage.debugContext = this.debugContext
178 //GameStage側の初期化処理を行わせる。
181 this.networkManager.joinStage(stage);
182 //runningStageに登録することで、イベントの通知が開始され、GameStageは実行状態に入る。
183 this.runningStage = stage;
185 pauseStage: function(func){
186 //ステージの実行を一時停止する。一時停止中、funcに指定された関数が毎tick毎に呼ばれる
187 if(this.stagePausedFunction == null){
188 this.stagePausedFunction = func;
191 //ステージが一時停止中のfunc()の中から二重にpauseStage()を呼んではいけない
195 resumeStage: function(){
197 if(this.stagePausedFunction != null) {
198 //必ずpauseStage()の引数に指定したfunc()の中から呼ばれる・・・はず。
199 this.stagePausedFunction = null;
205 stopStage: function(){
207 if(this.runningStage){
208 //runningStageから設定解除することで、イベントの通知は行われなくなる。
209 var aGameStage = this.runningStage;
210 this.runningStage = null;
211 //GameStage側の終了処理を行わせる。
212 aGameStage.stopStage();
213 //GameStageインスタンスからGameManagerの情報を削除する。
214 aGameStage.manager = null;
215 aGameStage.mainCanvas = null;
216 aGameStage.debugCanvas = null;
217 aGameStage.mainContext = null;
218 aGameStage.debugContext = null;
221 loadStageFromLocal: function(code){
224 URL_PCD_Auth = URL_PCD_Root + "auth.php";
225 URL_PCD_Audio = URL_PCD_Root + "audio/";
226 URL_PCD_Stage = URL_PCD_Root + "stage/";
228 var stage = eval(code);
229 mainManager.runStage(stage);
231 loadStageFromNetwork: function(name){
232 //URL_PCD_Stage/name.jsを利用してステージを作成する。
233 request = this.networkManager.CreateRequestObject();
235 request.open('GET', URL_PCD_Stage + name + ".js", false);
236 this.networkManager.RequestObjectDisableCache(request);
239 if(request.status == 0){
240 alert("ネットワークにアクセスできません。" + request.status + ":" + request.statusText);
241 }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
242 var stage = eval(request.responseText);
243 mainManager.runStage(stage);
245 alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
248 debugOut: function(str){
249 if(this.debugText != null)
254 this.debugText.value = str.replace(/\n/g,"\r\n") + this.debugText.value;
257 this.debugText.innerHTML = str + this.debugText.value;
261 isAvailableBrowser: function(){
262 //ブラウザの判別を行う。実行不可能な場合はfalseを返す。
263 //http://d.hatena.ne.jp/Naotsugu/20110927/1317140891
264 var userAgent = window.navigator.userAgent.toLowerCase();
265 var appVersion = window.navigator.appVersion.toLowerCase();
267 if (userAgent.indexOf('opera') != -1) {
269 this.debugOut("Browser:Opera\n");
270 } else if (userAgent.indexOf('msie') != -1) {
271 if (appVersion.indexOf("msie 9.") != -1) {
273 this.debugOut("Browser:IE9\n");
274 } else if (appVersion.indexOf("msie 8.") != -1) {
276 this.debugOut("Browser:IE8\n");
277 } else if (appVersion.indexOf("msie 7.") != -1) {
279 this.debugOut("Browser:IE7\n");
280 } else if (appVersion.indexOf("msie 6.") != -1) {
282 this.debugOut("Browser:IE6\n");
284 this.debugOut("Browser:IE?\n");
287 } else if (userAgent.indexOf('chrome') != -1) {
289 this.debugOut("Browser:Chrome\n");
290 } else if (userAgent.indexOf('safari') != -1) {
292 this.debugOut("Browser:Safari\n");
293 } else if (userAgent.indexOf('gecko') != -1) {
295 this.debugOut("Browser:Gecko\n");
298 this.debugOut("Browser:Unknown\n");
300 //描画コンテキストからHTML5対応チェック
301 if(!this.mainCanvas || !this.mainCanvas.getContext){
303 alert("このゲームを遊ぶためには、HTML5に対応しているブラウザ(Google Chrome等)でアクセスしてください...。");
308 setBackgroundMusic: function(name){
309 if(this.backgroundMusic){
311 this.backgroundMusic.pause();
312 this.backgroundMusic = null;
315 //引数がnullでなければaudioオブジェクトを取得
316 this.backgroundMusic = createAudio(name);
317 if(this.backgroundMusic){
319 this.backgroundMusic.loop = true;
320 this.backgroundMusic.play();
329 //イベントリスナーにおけるthisは、イベントリスナーを登録したオブジェクトまたはwindowになり、通常とは異なるので注意。
331 function timerTickEventListener(event)
333 timerTickEventListener.manager.timerTick(event);