OSDN Git Service

グローバル・デバッグモードを追加。collisionCanvasの表示を強化。デバッグモードをオンにすると見えるようになる。
[h58pcdgame/GameScriptCoreLibrary.git] / www / corelib / classes / GameStageClass.js
1
2 //
3 //\83Q\81[\83\80\83X\83e\81[\83W
4 //
5
6 function GameStage(){
7         //****\83R\83\93\83X\83g\83\89\83N\83^****
8         //**\83C\83\93\83X\83^\83\93\83X\95Ï\90\94\90é\8c¾\81E\8f\89\8aú\89»**
9         //\83^\83C\83}\81[\83J\83E\83\93\83g\82ð\8f\89\8aú\89»
10         this.tickCount = 0;
11         //GameManager\82©\82ç\93n\82³\82ê\82é\83v\83\8d\83p\83e\83B
12         this.manager = null;
13         this.mainCanvas = null;
14         this.debugCanvas = null;
15         this.mainContext = null;
16         this.debugContext = null;
17         //StageObject\82Ì\83\8a\83X\83g
18         this.stageObjectList = new Array();
19         this.globalStageObjectList = new Array();
20         //\8fÕ\93Ë\94»\92è\97pcanvas
21         this.collisionMapCanvas = null;
22         this.collisionMapContext = null;
23         this.debugMode = false;
24         //\94w\8ci\82Ì\89æ\91\9c\82Ìimg\83G\83\8c\83\81\83\93\83g\83I\83u\83W\83F\83N\83g\82ð\8ew\92è
25         this.background = null;
26         //\83\86\81[\83U\81[\82ª\91\80\8dì\82µ\82Ä\82¢\82é\83L\83\83\83\89\83N\83^\81[\83I\83u\83W\83F\83N\83g
27         this.userControlledCharacter = null;
28 }
29 GameStage.prototype = {
30         //\88È\89º\82Ì\8aÖ\90\94\82ð\83I\81[\83o\81[\83\89\83C\83h\82µ\82Ä\83X\83e\81[\83W\82ð\8dì\90¬\82·\82é\81B
31         keyDown: function(event){
32                 //\83L\81[\93ü\97Í
33         },
34         keyUp: function(event){
35                 //\83L\81[\93ü\97Í
36         },
37         timerTick: function(){
38                 //\83^\83C\83}\81[
39                 this.tickCount++;
40                 //\91S\82Ä\82Ì\83I\83u\83W\83F\83N\83g\82ð\8cv\8eZ
41                 for(var sp in this.stageObjectList)
42                 {
43                         var tickAllObjects = true;
44                         //\82Ü\82¸\81A\83N\83\89\83X\92è\8b`.tick() (\97á\82¦\82ÎBlockStageObjectClass.tick = function(){};) \82ª\92è\8b`\82³\82ê\82Ä\82½\82ç\8eÀ\8ds
45                         if(sp.tick)
46                         {
47                                 tickAllObjects = sp.tick(this.stageObjectList[sp]);
48                         }
49                         //\8e\9f\82É\8ae\8eíStageObject\82Ìtick\82ð\8eÀ\8ds
50                         if(tickAllObjects)
51                         {
52                                 for(var i = 0; i < this.stageObjectList[sp].length; i++){
53                                         
54                                         this.stageObjectList[sp][i].tick();
55                                         
56                                 }
57                         }
58                 }
59                 // \91\80\8dì\92\86\83L\83\83\83\89\82Ì\88Ú\93®\8f\88\97\9d
60                 if(this.userControlledCharacter)
61                 {
62                         if(this.manager.UIManager.keyState.jump)
63                         {
64                                 //\83W\83\83\83\93\83v
65                                 this.userControlledCharacter.jump();
66                         } else{
67                                 this.userControlledCharacter.jumpEnd();
68                         }
69                         if(!(this.manager.UIManager.keyState.goLeft && this.manager.UIManager.keyState.goRight))
70                         {
71                                 //\8d\89E
72                                 if(this.manager.UIManager.keyState.goLeft){
73                                         this.userControlledCharacter.goLeft();
74                                 }
75                                 if(this.manager.UIManager.keyState.goRight){
76                                         this.userControlledCharacter.goRight();
77                                 }
78                         }
79                 }
80         },
81         draw: function(){
82                 this.drawBackground();
83
84                 this.drawAsPoint(0, 0);
85         },
86         drawAsPoint: function(x, y){
87                 //\82·\82×\82Ä\82Ì\83I\83u\83W\83F\83N\83g\82ð\8dÄ\95`\89æ
88                 //x, y\82Í\95`\89æ\88Ê\92u\82Ì\83I\83t\83Z\83b\83g
89                 for(var sp in this.stageObjectList)
90                 {
91                         //\8ae\8eíStageObject\82Ìdraw\82ð\8eÀ\8ds
92                         for(var i = 0, li = this.stageObjectList[sp].length; i < li; i++){
93                                 var stgobj = this.stageObjectList[sp][i];
94                                 var px = stgobj.origin.x - x, py = stgobj.origin.y - y;
95                                 
96                                 if(px > -stgobj.size.x && py > -stgobj.size.y && px < 640 && py < 480)
97                                 {
98                                         stgobj.draw(px, py);
99                                         if(this.debugMode){
100                                                 stgobj.debugDraw(px, py);
101                                         }
102                                 }
103                         }
104                 }
105         },
106         drawBackground: function(){
107                 if(this.background)
108                 {
109                         this.mainContext.drawImage(this.background, 0, 0, 640, 480);
110                         
111                 } else{
112                         //\83L\83\83\83\93\83o\83X\82ð\91S\8fÁ\8b\8e
113                         this.mainContext.clearRect(0, 0, this.mainCanvas.width, this.mainCanvas.height);
114                 }
115                 this.collisionMapContext.clearRect(0, 0, this.collisionMapCanvas.width, this.collisionMapCanvas.height);
116         },
117         runStage: function(){
118                 //\83X\83e\81[\83W\8f\89\8aú\89»\8f\88\97\9d
119                 this.tickCount = 0;
120                 //\8fÕ\93Ë\83}\83b\83v\8f\89\8aú\89»
121                 this.collisionMapCanvas = createCanvas("collisionMapCanvas", this.mainCanvas.width, this.mainCanvas.height, this.mainCanvas.width, 0, 1, this.manager.mainArea);
122                 this.collisionMapContext = this.collisionMapCanvas.getContext('2d');
123                 this.collisionMapContext.fillStyle = "rgba(0,0,0, 0.2)";
124                 this.collisionMapContext.strokeStyle = "rgba(0,0,0, 0.2)";
125                 this.collisionMapContext.scale(1, 1);
126         },
127         stopStage: function(){
128                 //\83X\83e\81[\83W\8fI\97¹\8f\88\97\9d
129                 destroyDOMObjectByID(this.collisionMapCanvas.id);
130                 this.collisionMapCanvas = null;
131                 this.collisionMapContext = null;
132                 this.stageObjectList = null;
133         },
134         addStageObject: function(aStageObject, isGlobalObject){
135                 //stageObject\82ð\83\8a\83X\83g\82É\92Ç\89Á\82·\82é
136                 //isGlobalObject:\83l\83b\83g\83\8f\81[\83N\8dX\90V\82ª\95K\97v\82È\8fê\8d\87true\82ð\93n\82·\81B\95s\97v\82È\8fê\8d\87\82Í\8fÈ\97ª\82Å\82«\82é\81B
137                 //stageObjectList\82Í\83I\83u\83W\83F\83N\83g\82Ì\83N\83\89\83X\82²\82Æ\82Ì\98A\91z\94z\97ñ\82É\82È\82Á\82Ä\82¢\82é\81B
138                 var sp = aStageObject.constructor;
139                 if(this.stageObjectList[sp] == undefined)
140                 {
141                         this.stageObjectList[sp] = [];
142                 }
143                 this.stageObjectList[sp].push(aStageObject);
144                 if(isGlobalObject){
145                         this.globalStageObjectList.push(aStageObject);
146                 }
147         },
148         removeStageObject: function(aStageObject){
149                 //stageObject\82ð\83\8a\83X\83g\82©\82ç\8dí\8f\9c\82·\82é
150                 var sp = aStageObject.constructor;
151                 if(sp in this.stageObjectList)
152                 {
153                         removeObjectFromArray(this.stageObjectList[sp], aStageObject);
154                         if(this.stageObjectList[sp].length == 0)
155                         {
156                                 //\8bó\82É\82È\82Á\82½\83N\83\89\83X\82Ì\94z\97ñ\82Í\8dí\8f\9c\82·\82é
157                                 removeObjectFromArray(this.stageObjectList, sp);
158                         }
159                 }
160                 //globalStageObjectList\82©\82ç\8dí\8f\9c\81B\91\8dÝ\82µ\82È\82¢\8fê\8d\87\82Í\89½\82à\8bN\82«\82È\82¢\82Ì\82Å\82±\82ê\82Å\91å\8fä\95v
161                 removeObjectFromArray(this.globalStageObjectList, aStageObject);
162         },
163         eachStageObject: function(f){
164                 //\83\8a\83X\83g\8fã\82Ì\82·\82×\82Ä\82Ì\83I\83u\83W\83F\83N\83g\88ê\82Â\82¸\82Â\82ð\88ø\90\94\82Æ\82µ\82Ä\8aÖ\90\94f\82ð\82»\82ê\82¼\82ê\8eÀ\8ds\82·\82é\81B
165                 //\8aÖ\90\94f\82ªfalse\82ð\95Ô\82µ\82½\8fê\8d\87\81A\82»\82±\82Å\8eÀ\8ds\82ð\92\86\92f\82µ\82Äfalse\82ð\95Ô\82·\81B
166                 for(var sp in this.stageObjectList)
167                 {
168                         for(var i = 0; i < this.stageObjectList[sp].length; i++){
169                                 
170                                 var stgobj = this.stageObjectList[sp][i];
171                                 if(!f(stgobj)) return false;
172                         }
173                 }
174                 return true;
175         },
176         eachStageObjectById: function(id, f){
177                 for(var sp in this.stageObjectList)
178                 {
179                         for(i = 0; i < this.stageObjectList[sp].length; i++){
180                                 
181                                 var stgobj = this.stageObjectList[sp][i];
182                                 if(stgobj.id == id)
183                                 {
184                                         if(!f(stgobj)) return false;
185                                 }
186                         }
187                 }
188                 return true;
189         },
190         collideJudge: function(obj, x, y, items){
191                 var gainItem = items instanceof Array;
192                 var retv = false;
193                 this.eachStageObject(function(f){
194                         if(f !== obj)
195                         {
196                                 if(obj.isCollided(f, x, y))
197                                 {
198                                         if(f.isPhantom)
199                                         {
200                                                 if(gainItem)
201                                                 {
202                                                         for(var titem in items)
203                                                         {
204                                                                 
205                                                                 if(titem == f) return true;
206                                                         }
207                                                         items.push(f);
208                                                 }
209                                         }else
210                                         {
211                                                 retv = true;
212                                                 return true;
213                                                 //return false;
214                                         }
215                                 }
216                         }
217                         return true;
218                 });
219                 
220                 return retv;
221         },
222         moveTo : function(obj, x, y){
223                 //\93\96\82½\82è\94»\92è\8f\88\97\9d
224                 var sx = obj.origin.x, sy = obj.origin.y;
225                 var lx = x - sx, ly = y - sy;
226                 if(lx == 0 && ly == 0) return;
227                 var dx, dy, ct;
228                 var collideDirection = 0;       //\89º\88Ê4bit : \8fã \89º \8d¶ \89E
229                 ct = Math.max(Math.abs(lx), Math.abs(ly));
230                 dx = lx / ct; dy = ly / ct;
231                 var fx = dx == 0, fy = dy == 0;
232                 var ex = sx, ey = sy;
233                 var gotItems = [];
234                 for(var i = 0; i < ct  && !(fx && fy); i++)
235                 {
236                         //\89¼\82É\82¨\82¢\82Ä\82Ý\82é\8dÀ\95W
237                         //var ex = fx ? rx : sx + dx * i, ey = fy ? ry : sy + dy * i;
238                         
239                         if(!fx && this.collideJudge(obj, ex + dx, ey, gotItems))
240                         {
241                                 if(this.collideJudge(obj, ex + dx, ey - 2, gotItems))
242                                 {
243                                         fx = true;
244                                         collideDirection |= (dx > 0 ? 1 : 2);
245                                 }else
246                                 {
247                                         ey -= 2;
248                                 }
249                         }
250                         
251                         if(!fy && this.collideJudge(obj, ex, ey + dy, gotItems))
252                         {
253                                 fy = true;
254                                 collideDirection |= (dy > 0 ? 4 : 8);
255                         }
256                         
257                         if(!fx) ex += dx;
258                         if(!fy) ey += dy;
259                         if(fx && fy) break;
260                 }
261                 obj.origin.x = ex;
262                 obj.origin.y = ey;
263                 
264                 for(var i in gotItems)
265                 {
266                         var item = gotItems[i];
267                         if(item.itemAttacked)
268                         {
269                                 //\83A\83C\83e\83\80\82ð\8eæ\93¾\82µ\82½\82Æ\82«\82Ì\8f\88\97\9d\82ð\8cÄ\82Ô(\92N\82ª\8eæ\93¾\82µ\82½\82©\81A\8eæ\93¾\82µ\82½\93z\82Ì\8c»\8dÝ\88Ê\92u\82ð\93n\82·\81j
270                                 item.itemAttacked(obj, ex, ey);
271                         }
272                 }
273                 
274                 return collideDirection;
275         },
276         eachOwnStageObject: function(f){
277                 //\8e©\95ª\8e©\90g\82ª\8f\8a\97L\82·\82é\83I\83u\83W\83F\83N\83g\82·\82×\82Ä\82É\91Î\82µ\82Ä\8aÖ\90\94f\82ð\93K\97p\82·\82é
278                 for(var sp in this.stageObjectList)
279                 {
280                         for(var i = 0; i < this.stageObjectList[sp].length; i++){
281                                 var stgobj = this.stageObjectList[sp][i];
282                                 if(stgobj.ownerUID == this.manager.userID){
283                                         f(stgobj);
284                                 }
285                         }
286                 }
287         },
288         getGlobalStageObject: function(objid){
289                 for(var i = 0; i < this.globalStageObjectList.length; i++){
290                         if(objid == this.globalStageObjectList[i].objectID){
291                                 return this.globalStageObjectList[i];
292                         }
293                 }
294                 return null;
295         },
296         appendSyncDataTo: function(data){
297                 for(var i = 0; i < this.globalStageObjectList.length; i++){
298                         var anObj = this.globalStageObjectList[i];
299                         if(anObj.ownerUID == this.manager.userID){
300                                 data.append(i, anObj.objectID + "|" + anObj.origin.x + "|" + anObj.origin.y + "|" + anObj.movingSpeed.x + "|" +  anObj.movingSpeed.y+ "|" + anObj.attribute);
301                         }
302                 }
303         },
304         appendInitialSyncDataTo: function(data){
305                 for(var i = 0; i < this.globalStageObjectList.length; i++){
306                         anObj = this.globalStageObjectList[i];
307                         data.append(i, anObj.origin.x.toString() + "|" + anObj.origin.y.toString() + "|" + anObj.movingSpeed.x.toString() + "|" +  anObj.movingSpeed.y.toString() + "|" + anObj.className + "|" + parseArrayToStringSource(anObj.attribute) + "|" + parseArrayToStringSource(anObj.constructorArgs));
308                 }
309         },
310 };
311