OSDN Git Service

9833722ab1cd8144cd11c7455c431a1308818829
[h58pcdgame/GameScriptCoreLibrary.git] / www / corelib / coresubc.js
1
2 //
3 //サブマネージャークラス
4 //
5 //これらのクラスとメインマネージャーは直接プロパティを参照し合うので注意
6
7 function NetworkManager(mManager){
8         this.timeStamp = 0;
9         this.syncIntervalMs = 128;
10         
11         window.setInterval(this.syncTimerTickEventListener, this.syncIntervalMs);
12 }
13 NetworkManager.prototype = {
14         //from http://hakuhin.jp/js/xmlhttprequest.html
15         CreateRequestObject: function(){
16                 var rq = null;
17                 // XMLHttpRequest
18                 try{
19                         // XMLHttpRequest オブジェクトを作成
20                         rq = new XMLHttpRequest();
21                 }catch(e){}
22                 // Internet Explorer
23                 try{
24                         rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
25                 }catch(e){}
26                 try{
27                         rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
28                 }catch(e){}
29                 try{
30                         rq = new ActiveXObject('MSXML2.XMLHTTP');
31                 }catch(e){}
32                 if(rq == null){
33                         return null;
34                 }
35                 return rq;
36         },
37         RequestObjectDisableCache: function(rq){
38                 //call after open request.
39                 //disable cache
40                 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
41                 rq.setRequestHeader('Pragma', 'no-cache');                              // HTTP/1.0 における汎用のヘッダフィールド
42                 rq.setRequestHeader('Cache-Control', 'no-cache');               // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
43                 rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
44                 
45         },
46         sync: function(){
47                 //ネットワーク同期
48                 //サーバーに更新情報をアップロード
49                 if(mainManager.userID != 0){
50                         //ユーザーIDが設定されている=オンライン状態
51                         request = this.CreateRequestObject();
52                         //同期モード
53                         request.open('POST', URL_PCD_Root + "update.php?uid=" + mainManager.userID + "&action=refresh", false);
54                         request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
55                         //送信データを準備
56                         aData = new RequestData();
57                         if(mainManager.runningStage){
58                                 mainManager.runningStage.appendSyncDataTo(aData);
59                         }
60                         //送信
61                         this.RequestObjectDisableCache(request);
62                         request.send(aData.data);
63                         //レスポンス確認
64                         if(request.status == 0){
65                                 alert("ネットワークにアクセスできません。" + this.status + ":" + this.statusText);
66                         } else if((200 <= request.status && request.status < 300) || (request.status == 304)){
67                                 var res = request.responseText;
68                                 if(isValidResponseText(res)){
69                                         var retArray = eval(res);
70                                         this.timeStamp = retArray[0];
71                                         //削除処理
72                                         for(var i = 0; i < retArray[3].length; i++){
73                                                 mainManager.debugOut("Network:removeObject:ObjectID=" + retArray[3][i] + "\n");
74                                                 var anObject = mainManager.runningStage.getGlobalStageObject(retArray[3][i]);
75                                                 if(anObject){
76                                                         mainManager.runningStage.removeStageObject(anObject);
77                                                 }
78                                         }
79                                         //更新処理
80                                         for(var i = 0; i < retArray[1].length; i++){
81                                                 //mainManager.debugOut("Network:refreshObject:ObjectID=" + retArray[1][i] + "\n");
82                                                 var anArray = retArray[1][i];
83                                                 var anObject = mainManager.runningStage.getGlobalStageObject(anArray[0]);
84                                                 if(anObject){
85                                                         anObject.origin.x = anArray[1];
86                                                         anObject.origin.y = anArray[2];
87                                                         anObject.movingSpeed.x = anArray[3];
88                                                         anObject.movingSpeed.y = anArray[4];
89                                                         anObject.attribute = eval(anArray[6]); 
90                                                         anObject.updateAttribute();
91                                                 }
92                                         }
93                                         //追加処理
94                                         for(var i = 0; i < retArray[2].length; i++){
95                                                 var anArray = retArray[2][i];
96                                                 var args = eval(anArray[7]);
97                                                 var anObject = eval("new " + anArray[5] + "(mainManager.runningStage, args);");
98                                                 if(anObject){
99                                                         mainManager.debugOut("Network:addObject:ObjectID=" + anArray[0] + "\n");
100                                                         anObject.objectID = anArray[0];
101                                                         anObject.ownerUID = anArray[8];
102                                                         anObject.origin.x = anArray[1];
103                                                         anObject.origin.y = anArray[2];
104                                                         anObject.movingSpeed.x = anArray[3];
105                                                         anObject.movingSpeed.y = anArray[4];
106                                                         anObject.attribute = eval(anArray[6]); 
107                                                         anObject.updateAttribute();
108                                                         mainManager.runningStage.addStageObject(anObject, true);
109                                                 }
110                                         }
111                                         //ユーザーリスト
112                                         mainManager.userManager.userList = retArray[4];
113                                         return;
114                                 }
115                         } else{
116                                 alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
117                         }
118                 }
119         },
120         joinStage: function(stage){
121                         //サーバーに追加オブジェクトの情報をアップロード
122                 if(mainManager.userID != 0){
123                         request = this.CreateRequestObject();
124                         //同期モード
125                         request.open('POST', URL_PCD_Root + "update.php?uid=" + mainManager.userID + "&action=add", false);
126                         request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
127                         //送信データを準備
128                         aData = new RequestData();
129                         stage.appendInitialSyncDataTo(aData);
130                         //送信
131                         this.RequestObjectDisableCache(request);
132                         request.send(aData.data);
133                         //レスポンス確認
134                         if(request.status == 0){
135                                 alert("ネットワークにアクセスできません。" + this.status + ":" + this.statusText);
136                         }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
137                                 var res = request.responseText;
138                                 if(isValidResponseText(res)){
139                                         var retArray = eval(res);
140                                         this.timeStamp = retArray[0];
141                                         //割り当てられたobjectIDを設定する
142                                         for(var i = 0; i < stage.globalStageObjectList.length; i++){
143                                                 stage.globalStageObjectList[i].objectID = retArray[1][i];
144                                         }
145                                         //すでにステージ上にある他のユーザーのオブジェクトを追加する
146                                         for(var i = 0; i < retArray[2].length; i++){
147                                                 var anArray = retArray[2][i];
148                                                 var args = eval(anArray[7]);
149                                                 var anObject = eval("new " + anArray[5] + "(stage, args);");
150                                                 if(anObject){
151                                                         mainManager.debugOut("Network:addObject:ObjectID=" + anArray[0] + "\n");
152                                                         anObject.objectID = anArray[0];
153                                                         anObject.ownerUID = anArray[8];
154                                                         anObject.origin.x = anArray[1];
155                                                         anObject.origin.y = anArray[2];
156                                                         anObject.movingSpeed.x = anArray[3];
157                                                         anObject.movingSpeed.y = anArray[4];
158                                                         anObject.attribute = eval(anArray[6]);
159                                                         stage.addStageObject(anObject, true);
160                                                 }
161                                         }
162                                 }
163                         }else{
164                                 alert("サーバーがエラーを返しました。" + this.status + ":" + this.statusText);
165                         }
166                         
167                 }
168         },
169         syncTimerTickEventListener: function(event){
170                 mainManager.networkManager.sync();
171         },
172 }
173
174 function UIManager(mManager){
175         //ユーザー入出力関連
176         //キーボード状態を格納するプロパティの設定
177         this.keyState = new Object();
178         this.keyState.goLeft = false;
179         this.keyState.goRight = false;
180         this.keyState.goUp = false;
181         this.keyState.goDown = false;
182         
183         //**イベントリスナー設定**
184         //keyDown
185         window.addEventListener('keydown', this.keyDownEventListener, true);
186         //keyUp
187         window.addEventListener('keyup', this.keyUpEventListener, true);
188 }
189 UIManager.prototype = {
190         keyID: {
191                 //キーコードの設定
192                 cursorLeft: 37,         //左カーソル
193                 cursorRight: 39,        //右カーソル
194                 cursorUp: 38,           //上カーソル
195                 cursorDown: 40,         //下カーソル
196                 goLeft: 37,
197                 goRight: 39,
198                 jump: 38,
199         },
200         setKeyFromEvent: function(event, state){
201                 var keyCode = event.keyCode;
202                 if(keyCode == mainManager.UIManager.keyID.cursorLeft){
203                         mainManager.UIManager.keyState.cursorLeft = state
204                 }
205                 if(keyCode == mainManager.UIManager.keyID.cursorRight){
206                         mainManager.UIManager.keyState.cursorRight = state
207                 }
208                 if(keyCode == mainManager.UIManager.keyID.cursorUp){
209                         mainManager.UIManager.keyState.cursorUp = state
210                 }
211                 if(keyCode == mainManager.UIManager.keyID.cursorDown){
212                         mainManager.UIManager.keyState.cursorDown = state
213                 }
214                 if(keyCode == mainManager.UIManager.keyID.goLeft){
215                         mainManager.UIManager.keyState.goLeft = state
216                 }
217                 if(keyCode == mainManager.UIManager.keyID.goRight){
218                         mainManager.UIManager.keyState.goRight = state
219                 }
220                 if(keyCode == mainManager.UIManager.keyID.jump){
221                         mainManager.UIManager.keyState.jump = state
222                 }
223                 
224                 if(mainManager.runningStage){
225                         switch(keyCode){
226                                 case 38:
227                                 case 40:
228                                 case 37:
229                                 case 39:
230                                         event.preventDefault();
231                                         break;
232                         }
233                 }
234         },
235         keyDownEventListener:function(event){
236                 UIManager.prototype.setKeyFromEvent(event, true);
237                 //実行中のステージに通知
238                 if(mainManager.runningStage){
239                         mainManager.runningStage.keyDown(event);
240                 }
241         },
242         keyUpEventListener:function(event){
243                 UIManager.prototype.setKeyFromEvent(event, false);
244                 //実行中のステージに通知
245                 if(mainManager.runningStage){
246                         mainManager.runningStage.keyUp(event);
247                 }
248         },
249         clearInput: function(){
250                 this.keyState.goLeft = false;
251                 this.keyState.goRight = false;
252         },
253         
254 }
255
256 function UserManager(mManager){
257         this.userList = {};
258         this.max_HP = 100;
259         this.HP = this.max_HP;
260 }
261 UserManager.prototype = {
262         loginAs: function(userNameStr){
263                 request = mainManager.networkManager.CreateRequestObject();
264                 
265                 var i = userNameStr.indexOf("@");
266                 if(i != -1){
267                         //任意ステージの実行
268                         var userName = userNameStr.substring(0, i);
269                         var stageName = userNameStr.substring(i + 1);
270                         request.open('GET', URL_PCD_Auth + "?action=devjoin&name=" + encodeURIComponent(userName) + "&stage=" + encodeURIComponent(stageName), false);
271                 } else{
272                         //通常モード
273                         request.open('GET', URL_PCD_Auth + "?action=join&name=" + encodeURIComponent(mainManager.runningStage.authform.userName.value), false);
274                 }
275                 mainManager.networkManager.RequestObjectDisableCache(request);
276                         request.send(null);
277                 
278                 if(request.status == 0){
279                         alert("ネットワークへのアクセスに失敗しました。");
280                 }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
281                         if(isValidResponseText(request.responseText)){
282                                 result = eval(request.responseText);
283                                 if(result[3] == 0){
284                                         alert("ログインできません。データベース通信エラーです。");
285                                         return;
286                                 } else if(result[3] == 11){
287                                         alert("すでにその名前は使われています。他の名前を試してください。");
288                                         return;
289                                 } else if(result[3] == 10){
290                                         alert("ID:" + result[0] + " でログインしました。");
291                                         mainManager.userID = result[0];
292                                         mainManager.timeStamp = result[1];
293                                         mainManager.loadStageFromNetwork(result[2]);
294                                         return;
295                                 }
296                         }
297                 } else{
298                         alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
299                 }
300         },
301         getUserNameByUID: function(uid){
302                 if(uid in this.userList){
303                         return this.userList[uid];
304                 }
305                 return "Anonymous";
306         },
307         damage: function(p){
308                 if(this.HP <= p){
309                         
310                         this.kill();
311                 } else{
312                         this.HP -= p;
313                 }
314         },
315         heal: function(p){
316                 if(this.HP + p > this.max_HP){
317                         this.HP = this.max_HP;
318                 } else{
319                         this.HP += p;
320                 }
321         },
322         kill: function(){
323                 this.HP = 0;
324                 //終了処理および再読み込みはmainManager側で行われる。
325         }, 
326 }
327
328 //
329 //その他のクラス
330 //
331
332 function Point2D(x, y){
333         this.x = x;
334         this.y = y;
335 }
336 Point2D.prototype = {
337         
338 }
339
340 function Rectangle(x, y, width, height){
341         this.origin = new Point2D(x,y);
342         this.size = new Point2D(width,height);
343 }
344 Rectangle.prototype = {
345         
346 }
347
348 function OffsetBox(top, down, left, right){
349         this.top = top;
350         this.down = down;
351         this.left = left;
352         this.right = right;
353 }
354 OffsetBox.prototype = {
355         
356 }
357
358 function RequestData(){
359         //FormDataがIEで動かないので代替
360         this.data = "";
361 }
362 RequestData.prototype = {
363         append: function(name, data){
364                 this.data += this.getURLEncodedString(name) + "=" + this.getURLEncodedString(data) + "&";
365         },
366         getURLEncodedString: function(str){
367                 return encodeURIComponent(str).replace(/%20/g, '+');
368         }
369 }