OSDN Git Service

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