OSDN Git Service

add unun event and ununsmell event.
[yukkurioverwint/YukkuriOverwinter.git] / ctrl.js
1 var Ctrl = enchant.Class.create({
2         initialize: function (){
3                 this.game = null;
4                 this.menuBg = null;
5                 this.currentCommand = ECommand.WORLD_CURSOR;
6                 this.backgroundMap = null;
7                 this.player = null;
8                 this.objArray = {};
9                 this.def = null;
10         },
11         init : function (game){
12                 this.game = game;
13         },
14         setDefined:function(def){
15                 this.def = def;
16         },
17         addObj: function(obj){
18                 this.objArray[obj.id] = obj;
19         },
20         getObjs: function(){
21                 return this.objArray;
22         },
23         removeObj:function(obj){
24                 delete this.objArray[obj.id];
25         },
26         setMenuBg: function (menuBg){
27                 console.log("setMenuBg");
28                 this.menuBg = menuBg;
29         },
30         setBackgroundMap: function(backgroundMap){
31                 this.backgroundMap = backgroundMap;
32         },
33         setPlayer: function(player){
34                 this.player = player;
35         },
36         /**
37          * [setCurrentCommand description]
38          * @param {ECommand} cmd [description]
39          */
40         setCurrentCommand: function(cmd){
41                 this.currentCommand = cmd;
42         },
43         getCurrentCommand: function(){
44                 return this.currentCommand;
45         }
46 });
47 var CommandIcon = enchant.Class.create(enchant.Sprite,{
48         initialize: function (x, y, ecmd, index){
49                 enchant.Sprite.call(this, x, y);
50                 this.ecmd = ecmd;
51                 this.index = index;
52         },
53         isCurrent: function(){
54                 return (this.index * 2)%2 !== 0;
55         },
56         setCurrent: function(){
57                 this.frame = (this.index * 2) + 1;
58         },
59         removeCurrent: function(){
60                 this.frame = (this.index * 2);
61         }
62 });
63 var MangIcon = enchant.Class.create({
64         initialize: function (ctl, menuBg){
65                 var self = this;
66                 this.ctl = ctl;
67                 this.cmdIcons = [];
68                 var index = 0;
69                 for (var key in ECommand) {
70                         var value = ECommand[key];
71                         this.cmdIcons[value] = new CommandIcon(40, 40, value, index);
72                         this.cmdIcons[value].image = this.ctl.game.assets[EResPath.COMMAND];
73                         this.cmdIcons[value].x = 40 * index;
74                         this.cmdIcons[value].y = 4;
75
76                         // odd equals current. even equals not current.
77                         this.cmdIcons[value].frame = (this.ctl.getCurrentCommand() == value)? index * 2 + 1: index * 2;
78                         menuBg.addChild(this.cmdIcons[value]);
79                         this.cmdIcons[value].ontouchstart = function(obj){
80                                 // change command icons
81                                 var nowECmd = self.ctl.getCurrentCommand();
82                                 self.cmdIcons[nowECmd].removeCurrent();
83                                 if(!this.isCurrent()){
84                                         this.setCurrent();
85                                 }
86                                 self.ctl.setCurrentCommand(this.index);
87                         };
88                         index++;
89                 }
90         },
91         get: function(commandIndex){
92                 return this.cmdIcons[commandIndex];
93         }
94 });
95 var ATouchEvent = enchant.Class.create({
96         // initialize: function (ctl){
97         //      this.ctl = ctl;
98         // },
99         touchstart: function(e){
100         },
101         touchmove: function(e){
102         },
103         touchend: function(e){
104         }
105 });
106
107 var WorldCursorTouchEvent = enchant.Class.create(ATouchEvent,{
108         initialize: function (ctl){
109                 this.ctl = ctl;
110                 this.touchX = 0;
111                 this.touchY = 0;
112         },
113         touchstart : function(e){
114                 this.touchX = e.x - this.ctl.backgroundMap.x;
115                 this.touchY = e.y - this.ctl.backgroundMap.y;
116         },
117         touchmove : function(e){
118                 var backgroundMap = this.ctl.backgroundMap;
119                 var game = this.ctl.game;
120
121                 if(game.touched){
122                         backgroundMap.x = e.x - this.touchX;
123                         if(backgroundMap.x < -EFieldPos.WIDTH)backgroundMap.x = -EFieldPos.WIDTH;
124                         if(backgroundMap.x > 0)backgroundMap.x = 0;
125
126                         backgroundMap.y = e.y - this.touchY;
127                         if(backgroundMap.y < -EFieldPos.HEIGHT)backgroundMap.y = -EFieldPos.HEIGHT;
128                         if(backgroundMap.y > 0)backgroundMap.y = 0;
129                 }
130                 if(e.x > EFieldPos.WIDTH)game.touched = false;
131                 else if(e.x < 30)game.touched = false;
132         },
133         touchend : function(e){
134         }
135 });
136 var WorldTargetTouchEvent = enchant.Class.create(ATouchEvent,{
137         initialize: function (ctl){
138                 this.ctl = ctl;
139                 this.touchX = 0;
140                 this.touchY = 0;
141         },
142         touchstart :function(e){
143                 if(e.x >= 0 && e.x <= EFieldPos.WIDTH){
144                         var backgroundMap = this.ctl.backgroundMap;
145                         addEffect(this.ctl.backgroundMap, -backgroundMap.x + e.x, -backgroundMap.y + e.y);
146                         this.ctl.player.tweet("ゆっくりりかいしたよ!");
147                         var vx = (-backgroundMap.x + e.x) - this.ctl.player.getX() ;
148                         var vy = (-backgroundMap.y + e.y) - this.ctl.player.getY() ;
149
150                         // this.ctl.player.action.setStatus(EAction.MOVE_TO_COMMAND);
151                         // new MoveCommandEvent({
152                         //      "vx": vx,
153                         //      "vy": vy,
154                         //      "action": this.ctl.player.action,
155                         //      "myYukkuri": this.ctl.player
156                         // });
157
158                         // this.ctl.player.tweet("ゆー...いきたくないのぜ");
159                 }
160         }
161 });
162 var WorldAppleTouchEvent = enchant.Class.create(ATouchEvent,{
163         initialize: function (ctl){
164                 this.ctl = ctl;
165                 this.touchX = 0;
166                 this.touchY = 0;
167         },
168         touchstart :function(e){
169                 if(e.x >= 0 && e.x <= EFieldPos.WIDTH){
170                         var backgroundMap = this.ctl.backgroundMap;
171                         var tx = -backgroundMap.x + e.x;
172                         var ty = -backgroundMap.y + e.y;
173                         new ctl.def.Food("apple", tx, ty);
174                 }
175         }
176 });
177
178
179 var MangTouch = enchant.Class.create({
180         initialize: function (ctl){
181                 this.ctl = ctl;
182                 var cursor = ECommand.WORLD_CURSOR;
183                 this.mang = {};
184                 this.mang[ECommand.WORLD_CURSOR] = new WorldCursorTouchEvent(ctl);
185                 this.mang[ECommand.WORLD_TARGET] = new WorldTargetTouchEvent(ctl);
186                 this.mang[ECommand.WORLD_APPLE] = new WorldAppleTouchEvent(ctl);
187         },
188         get: function(ecmd){
189                 return this.mang[ecmd];
190         }
191 });
192 var MangLabel = enchant.Class.create({
193         initialize: function (ctl, menuBg){
194                 this.ctl = ctl;
195                 var labelGroup = new LabelGroup();
196                 this.START_XPOS = 4;
197                 this.START_YPOS = GAME_HEIGHT / 2;
198                 this.GAUGE_XPOS = 80;
199                 this.menuBg = menuBg;
200                 this.widthArr = [];
201                 this.WIDTH_MARGIN = 4;
202                 this.GAUGE_HEIGHT = 16;
203                 var FONT = "18px 'Times New Roman'";
204                 var COLOR = "#ffffff";
205                 menuBg.image.context.font = FONT;
206                 var self = this;
207                 // this.labels = {};
208                 this.labelParams = {
209                         "yukkuri" : {
210                                 label: null,
211                                 init: function(){
212                                         var TEXT = M.LABEL_YUKKURI;
213                                         var label = new Label(TEXT);
214                                         label.color = COLOR;
215                                         label.font = FONT;
216                                         label.x = self.START_XPOS;
217                                         label.y = self.START_YPOS;
218                                         label.textWidth = menuBg.image.context.measureText(TEXT).width;
219                                         this.label = label;
220                                 },
221                                 draw: function(yukkuriObj){
222                                         //100/maxhp*hp
223                                         var gauge = 100 / yukkuriObj.param.maxYukkuri * yukkuriObj.param.yukkuri;
224                                         self.menuBg.image.context.fillStyle = "#ffffff";
225                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, 100, self.GAUGE_HEIGHT);
226                                         self.menuBg.image.context.fillStyle = "#ff0000";
227                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, gauge, self.GAUGE_HEIGHT);
228                                 }
229                         },
230                         "hungry" : {
231                                 label: null,
232                                 init: function(){
233                                         var TEXT = M.LABEL_HUNGRY;
234                                         var label = new Label(TEXT);
235                                         label.color = COLOR;
236                                         label.font = FONT;
237                                         label.x = self.START_XPOS;
238                                         label.y = self.START_YPOS + 20;
239                                         label.textWidth = menuBg.image.context.measureText(TEXT).width;
240                                         this.label = label;
241                                 },
242                                 draw: function(yukkuriObj){
243                                         var gauge =yukkuriObj.param.hungry;
244                                         self.menuBg.image.context.fillStyle = "#ffffff";
245                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, 100, self.GAUGE_HEIGHT);
246                                         self.menuBg.image.context.fillStyle = "#bb7777";
247                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, gauge, self.GAUGE_HEIGHT);
248                                 }
249                         },
250                         "unun" : {
251                                 label: null,
252                                 init: function(){
253                                         var TEXT = M.LABEL_UNUN;
254                                         var label = new Label(TEXT);
255                                         label.color = COLOR;
256                                         label.font = FONT;
257                                         label.x = self.START_XPOS;
258                                         label.y = self.START_YPOS + 40;
259                                         label.textWidth = menuBg.image.context.measureText(TEXT).width;
260                                         this.label = label;
261                                 },
262                                 draw: function(yukkuriObj){
263                                         var gauge =yukkuriObj.param.unun;
264                                         self.menuBg.image.context.fillStyle = "#ffffff";
265                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, 100, self.GAUGE_HEIGHT);
266                                         self.menuBg.image.context.fillStyle = "#bb7777";
267                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, gauge, self.GAUGE_HEIGHT);
268                                 }
269                         },
270                         "sleep" : {
271                                 label: null,
272                                 init: function(){
273                                         var TEXT = M.LABEL_SLEEP;
274                                         var label = new Label(TEXT);
275                                         label.color = COLOR;
276                                         label.font = FONT;
277                                         label.x = self.START_XPOS;
278                                         label.y = self.START_YPOS + 60;
279                                         label.textWidth = menuBg.image.context.measureText(TEXT).width;
280                                         this.label = label;
281                                 },
282                                 draw: function(yukkuriObj){
283                                         var gauge =yukkuriObj.param.sleep;
284                                         self.menuBg.image.context.fillStyle = "#ffffff";
285                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, 100, self.GAUGE_HEIGHT);
286                                         self.menuBg.image.context.fillStyle = "#bb7777";
287                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, gauge, self.GAUGE_HEIGHT);
288                                 }
289                         },
290                         "stress" : {
291                                 label: null,
292                                 init: function(){
293                                         var TEXT = M.LABEL_STRESS;
294                                         var label = new Label(TEXT);
295                                         label.color = COLOR;
296                                         label.font = FONT;
297                                         label.x = self.START_XPOS;
298                                         label.y = self.START_YPOS + 80;
299                                         label.textWidth = menuBg.image.context.measureText(TEXT).width;
300                                         this.label = label;
301                                 },
302                                 draw: function(yukkuriObj){
303                                         var gauge =yukkuriObj.param.stress;
304                                         self.menuBg.image.context.fillStyle = "#ffffff";
305                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, 100, self.GAUGE_HEIGHT);
306                                         self.menuBg.image.context.fillStyle = "#bb7777";
307                                         self.menuBg.image.context.fillRect(self.GAUGE_XPOS, this.label.y, gauge, self.GAUGE_HEIGHT);
308                                 }
309                         },
310                 };
311
312                 this.menuBg.image.context.fillStyle = '#ff0000';
313                 for(var key in this.labelParams){
314                         var labelObj = this.labelParams[key];
315                         labelObj.init();
316                         labelGroup.addChild(labelObj.label);
317                 }
318                 menuBg.addChild(labelGroup);
319         },
320         draw: function(yukkuriObj){
321                 for(var key in this.labelParams){
322                         var labelObj = this.labelParams[key];
323                         labelObj.draw(yukkuriObj);
324                 }
325
326         }
327 });
328 var MangMsg = enchant.Class.create({
329         initialize: function (ctl){
330                 this.ctl = ctl;
331         },
332         output:function(yukkuri, emsg){
333                 //var M = yukkuri.getMsg();
334                 if(emsg == EMsg.WALK){
335                         if(rand(4) === 0){
336                                 yukkuri.tweet(M.WALK1);
337                         }
338                         else if(rand(4) === 1){
339                                 yukkuri.tweet(M.WALK2);
340                         }
341                         else if(rand(4) === 2){
342                                 yukkuri.tweet(M.WALK3);
343                         }
344                         else if(rand(4) === 3){
345                                 yukkuri.tweet(M.WALK4);
346                         }
347                 }
348                 else if(emsg == EMsg.EAT){
349                         if(rand(3) === 0){
350                                 yukkuri.tweet(M.EAT1);
351                         }
352                         else if(rand(3) === 1){
353                                 yukkuri.tweet(M.EAT2);
354                         }
355                         else if(rand(3) === 2){
356                                 yukkuri.tweet(M.EAT3);
357                         }
358                 }
359                 else if(emsg == EMsg.GET_UP){
360                         yukkuri.tweet(M.GET_UP1);
361                 }
362                 else if(emsg == EMsg.SLEEP){
363                         yukkuri.tweet(M.SLEEP1);
364                 }
365                 else if(emsg == EMsg.DEAD){
366                         yukkuri.tweet(M.DEAD1);
367                 }
368                 else if(emsg == EMsg.UNUN){
369                         if(rand(2) === 0){
370                                 yukkuri.tweet(M.UNUN1);
371                         }else{
372                                 yukkuri.tweet(M.UNUN2);
373                         }
374                 }
375                 else if(emsg == EMsg.UNUN_END){
376                         yukkuri.tweet(M.UNUN_END1);
377                 }
378                 else if(emsg == EMsg.UNUN_SMELL){
379                         yukkuri.tweet(M.UNUN_SPELL1);
380                 }
381
382
383         }
384 });
385
386 // var MangLabel = enchant.Class.create({
387 //      initialize: function (ctl, menuBg){
388 //              this.ctl = ctl;
389 //              var labelGroup = new LabelGroup();
390 //              this.START_XPOS = 4;
391 //              this.START_YPOS = GAME_HEIGHT / 2;
392 //              this.menuBg = menuBg;
393 //              this.widthArr = [];
394 //              this.WIDTH_MARGIN = 4;
395 //              var FONT = "14px 'Times New Roman'";
396 //              var COLOR = "#ffffff";
397 //              menuBg.image.context.font = FONT;
398 //              var self = this;
399 //              this.labels = {
400 //                      "yukkuri" : function(){
401 //                              var TEXT = "ゆっくり";
402 //                              var label = new Label(TEXT);
403 //                              label.color = COLOR;
404 //                              label.font = FONT;
405 //                              label.x = self.START_XPOS;
406 //                              label.y = self.START_YPOS;
407 //                              label.textWidth = menuBg.image.context.measureText(TEXT).width;
408 //                      },
409 //                      "hungry" : function(){
410 //                              var TEXT = "空腹";
411 //                              var label = new Label(TEXT);
412 //                              label.color = COLOR;
413 //                              label.font = FONT;
414 //                              label.x = self.START_XPOS;
415 //                              label.y = self.START_YPOS + 20;
416 //                              label.textWidth = menuBg.image.context.measureText(TEXT).width;
417 //                              return label;
418 //                      },
419 //              };
420
421 //              this.labels["yukkuri"]();
422 //              console.log(this.labels["yukkuri"]().textWidth);
423 //              // for(var key in this.labels){
424 //              //      console.log(key);
425 //              //      var label = this.labels[key]();
426 //              //      // labelGroup.addChild(label);
427 //              // }
428 //              // var width = this.widthArr['yukkuri'];
429 //              // this.menuBg.image.context.fillStyle = '#ff0000';
430 //              // this.menuBg.image.context.fillRect(this.START_XPOS + width, this.START_YPOS, 100, 30);
431 //              // menuBg.addChild(labelGroup);
432 //      },
433 //      draw: function(){
434 //              label = this.labels[key]();
435 //              this.menuBg.image.context.fillStyle = '#ff0000';
436 //              this.menuBg.image.context.fillRect(label.x, label.y, 100, 14);
437 //      }
438 // });
439