OSDN Git Service

HiddenBlockの当たり判定システムを刷新。詳細はエディターの文法ヘルプを参照すること。
[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         this.keyState.fire = false;
183         
184         //**イベントリスナー設定**
185         //keyDown
186         window.addEventListener('keydown', this.keyDownEventListener, true);
187         //keyUp
188         window.addEventListener('keyup', this.keyUpEventListener, true);
189 }
190 UIManager.prototype = {
191         keyID: {
192                 //キーコードの設定
193                 cursorLeft: 37,         //左カーソル
194                 cursorRight: 39,        //右カーソル
195                 cursorUp: 38,           //上カーソル
196                 cursorDown: 40,         //下カーソル
197                 goLeft: 37,
198                 goRight: 39,
199                 jump: 38,
200                 fire: 32,
201         },
202         setKeyFromEvent: function(event, state){
203                 var keyCode = event.keyCode;
204                 if(keyCode == mainManager.UIManager.keyID.cursorLeft){
205                         mainManager.UIManager.keyState.cursorLeft = state
206                 }
207                 if(keyCode == mainManager.UIManager.keyID.cursorRight){
208                         mainManager.UIManager.keyState.cursorRight = state
209                 }
210                 if(keyCode == mainManager.UIManager.keyID.cursorUp){
211                         mainManager.UIManager.keyState.cursorUp = state
212                 }
213                 if(keyCode == mainManager.UIManager.keyID.cursorDown){
214                         mainManager.UIManager.keyState.cursorDown = state
215                 }
216                 if(keyCode == mainManager.UIManager.keyID.goLeft){
217                         mainManager.UIManager.keyState.goLeft = state
218                 }
219                 if(keyCode == mainManager.UIManager.keyID.goRight){
220                         mainManager.UIManager.keyState.goRight = state
221                 }
222                 if(keyCode == mainManager.UIManager.keyID.jump){
223                         mainManager.UIManager.keyState.jump = state
224                 }
225                 if(keyCode == mainManager.UIManager.keyID.fire){
226                         mainManager.UIManager.keyState.fire = state
227                 }
228                 
229                 if(mainManager.runningStage){
230                         switch(keyCode){
231                                 case 38:
232                                 case 40:
233                                 case 37:
234                                 case 39:
235                                         event.preventDefault();
236                                         break;
237                         }
238                 }
239         },
240         keyDownEventListener:function(event){
241                 UIManager.prototype.setKeyFromEvent(event, true);
242                 //実行中のステージに通知
243                 if(mainManager.runningStage){
244                         mainManager.runningStage.keyDown(event);
245                 }
246         },
247         keyUpEventListener:function(event){
248                 UIManager.prototype.setKeyFromEvent(event, false);
249                 //実行中のステージに通知
250                 if(mainManager.runningStage){
251                         mainManager.runningStage.keyUp(event);
252                 }
253         },
254         clearInput: function(){
255                 this.keyState.goLeft = false;
256                 this.keyState.goRight = false;
257         },
258         
259 }
260
261 function UserManager(mManager){
262         this.userList = {};
263         this.max_HP = 100;
264         this.HP = this.max_HP;
265 }
266 UserManager.prototype = {
267         loginAs: function(userNameStr){
268                 request = mainManager.networkManager.CreateRequestObject();
269                 
270                 var i = userNameStr.indexOf("@");
271                 if(i != -1){
272                         //任意ステージの実行
273                         var userName = userNameStr.substring(0, i);
274                         var stageName = userNameStr.substring(i + 1);
275                         request.open('GET', URL_PCD_Auth + "?action=devjoin&name=" + encodeURIComponent(userName) + "&stage=" + encodeURIComponent(stageName), false);
276                 } else{
277                         //通常モード
278                         request.open('GET', URL_PCD_Auth + "?action=join&name=" + encodeURIComponent(mainManager.runningStage.authform.userName.value), false);
279                 }
280                 mainManager.networkManager.RequestObjectDisableCache(request);
281                         request.send(null);
282                 
283                 if(request.status == 0){
284                         alert("ネットワークへのアクセスに失敗しました。");
285                 }else if((200 <= request.status && request.status < 300) || (request.status == 304)){
286                         if(isValidResponseText(request.responseText)){
287                                 result = eval(request.responseText);
288                                 if(result[3] == 0){
289                                         alert("ログインできません。データベース通信エラーです。");
290                                         return;
291                                 } else if(result[3] == 11){
292                                         alert("すでにその名前は使われています。他の名前を試してください。");
293                                         return;
294                                 } else if(result[3] == 10){
295                                         alert("ID:" + result[0] + " でログインしました。");
296                                         mainManager.userID = result[0];
297                                         mainManager.timeStamp = result[1];
298                                         mainManager.loadStageFromNetwork(result[2]);
299                                         return;
300                                 }
301                         }
302                 } else{
303                         alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
304                 }
305         },
306         getUserNameByUID: function(uid){
307                 if(uid in this.userList){
308                         return this.userList[uid];
309                 }
310                 return "Anonymous";
311         },
312         damage: function(p){
313                 if(this.HP <= p){
314                         
315                         this.kill();
316                 } else{
317                         this.HP -= p;
318                 }
319         },
320         heal: function(p){
321                 if(this.HP + p > this.max_HP){
322                         this.HP = this.max_HP;
323                 } else{
324                         this.HP += p;
325                 }
326         },
327         kill: function(){
328                 this.HP = 0;
329                 //終了処理および再読み込みはmainManager側で行われる。
330         }, 
331 }
332
333 //
334 //その他のクラス
335 //
336
337 function Point2D(x, y){
338         this.x = x;
339         this.y = y;
340 }
341 Point2D.prototype = {
342         
343 }
344
345 function Rectangle(x, y, width, height){
346         this.origin = new Point2D(x,y);
347         this.size = new Point2D(width,height);
348 }
349 Rectangle.prototype = {
350         
351 }
352
353 function OffsetBox(top, bottom, left, right){
354         this.top = top;
355         this.bottom = bottom;
356         this.left = left;
357         this.right = right;
358 }
359 OffsetBox.prototype = {
360         
361 }
362
363 function RequestData(){
364         //FormDataがIEで動かないので代替
365         this.data = "";
366 }
367 RequestData.prototype = {
368         append: function(name, data){
369                 this.data += this.getURLEncodedString(name) + "=" + this.getURLEncodedString(data) + "&";
370         },
371         getURLEncodedString: function(str){
372                 return encodeURIComponent(str).replace(/%20/g, '+');
373         }
374 }