garden.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /**
  2. * See LICENSE file.
  3. *
  4. * Menu Scene.
  5. */
  6. (function() {
  7. HN.Garden= function() {
  8. HN.Garden.superclass.constructor.call(this);
  9. return this;
  10. };
  11. HN.Garden.prototype= {
  12. ambient: 1,
  13. initialize : function(ctx,size,maxGrassHeight) {
  14. return this;
  15. },
  16. paint : function(director, time){
  17. },
  18. gradient: null,
  19. lerpTime: 10000, // time taken to fade sky colors
  20. nextLerpTime: 15000, // after fading, how much time to wait to fade colors again.
  21. colors: [
  22. [ 0x00, 0x3f, 0x7f, //0x00, 0x00, 0x3f,
  23. 0x00, 0x3f, 0x7f,
  24. 0x1f, 0x5f, 0xc0,
  25. 0x3f, 0xa0, 0xff ],
  26. [ 0x00, 0x3f, 0x7f,
  27. 0xa0, 0x5f, 0x7f,
  28. 0xff, 0x90, 0xe0,
  29. 0xff, 0x90, 0x00 ],
  30. [ 0x00, 0x3f, 0x7f, //0x00, 0x00, 0x00,
  31. 0x00, 0x2f, 0x7f,
  32. 0x00, 0x28, 0x50,
  33. 0x00, 0x1f, 0x3f ],
  34. [ 0x00, 0x3f, 0x7f, //0x1f, 0x00, 0x5f,
  35. 0x3f, 0x2f, 0xa0,
  36. 0xa0, 0x1f, 0x1f,
  37. 0xff, 0x7f, 0x00 ] ],
  38. ambients: [ 1, .35, .05, .5 ], // ambient intensities for each sky color
  39. lerpindex: 0, // start with this sky index.
  40. /**
  41. * fade sky colors
  42. * @param time current time
  43. * @param last how much time to take fading colors
  44. */
  45. lerp: function( ctx, time, last ) {
  46. this.gradient= ctx.createLinearGradient(0,0,0,this.height);
  47. var i0= this.lerpindex%this.colors.length;
  48. var i1= (this.lerpindex+1)%this.colors.length;
  49. for( var i=0; i<4; i++ ) {
  50. var rgb='rgb(';
  51. for( var j=0; j<3; j++ ) {
  52. rgb+= Math.floor( (this.colors[i1][i*3+j]-this.colors[i0][i*3+j])*time/last + this.colors[i0][i*3+j]);
  53. if ( j<2 ) rgb+=',';
  54. }
  55. rgb+=')';
  56. this.gradient.addColorStop( i/3, rgb );
  57. }
  58. this.ambient= (this.ambients[i1]-this.ambients[i0])*time/last + this.ambients[i0];
  59. }
  60. };
  61. extend( HN.Garden, CAAT.Actor);
  62. })();
  63. (function() {
  64. HN.Cloud= function() {
  65. HN.Cloud.superclass.constructor.call(this);
  66. return this;
  67. };
  68. HN.Cloud.prototype= {
  69. scene: null,
  70. setScene : function(scene) {
  71. this.scene= scene;
  72. return this;
  73. },
  74. setupBehavior : function(director) {
  75. this.setBackgroundImage( director.getImage('cloudb'+ ((4*Math.random())>>0) ) );
  76. var me= this;
  77. var ix0, ix1, iy0, iy1;
  78. var from= Math.random();
  79. var dw= director.width;
  80. var dh= director.height;
  81. var ih= this.backgroundImage.height;
  82. var iw= this.backgroundImage.width;
  83. var t= 40000 + 5000*Math.random()*4;
  84. ix0= -iw + -iw*2*Math.random();
  85. iy0= dh*Math.random()/2;
  86. ix1= dw;
  87. iy1= iy0 + 50*Math.random()/2;
  88. var me= this;
  89. var pb= new CAAT.PathBehavior().
  90. setPath( new CAAT.Path().setLinear(ix0, iy0, ix1, iy1 ) );
  91. this.emptyBehaviorList();
  92. this.addBehavior(
  93. pb.
  94. setFrameTime( this.scene.time, t ).
  95. addListener( {
  96. behaviorExpired : function(behavior, time, actor) {
  97. ix0= -iw + -iw*2*Math.random();
  98. iy0= dh*Math.random()/2;
  99. ix1= dw;
  100. iy1= iy0 + 50*Math.random()/2;
  101. t= 40000 + 5000*Math.random()*4;
  102. behavior.path.setLinear( ix0, iy0, ix1, iy1 );
  103. behavior.setTimeOffset(0).setFrameTime( me.scene.time, t );
  104. }
  105. }).
  106. setTimeOffset( Math.random() ) );
  107. return this;
  108. }
  109. }
  110. extend( HN.Cloud, CAAT.Actor );
  111. })();
  112. (function() {
  113. HN.ScoreItem= function() {
  114. return this;
  115. };
  116. HN.ScoreItem.prototype= {
  117. score: 0,
  118. level: 0,
  119. mode: '',
  120. date: '',
  121. initialize : function(score, level, mode) {
  122. this.score= score;
  123. this.level= level;
  124. this.mode= mode;
  125. window.score=this.score;
  126. window.level=level;
  127. // updateShare(window.level,window.score);
  128. // Play68.setRankingLevelScoreDesc(window.level,window.score);
  129. console.log(window.level+'**'+window.score);
  130. var d= new Date();
  131. this.date= ''+d.getFullYear()+'/'+this.pad(1+d.getMonth())+'/'+this.pad(d.getDate());
  132. return this;
  133. },
  134. pad : function( n ) {
  135. n= ''+n;
  136. if ( n.length==1 ) {
  137. n= '0'+n;
  138. }
  139. return n;
  140. }
  141. };
  142. HN.Scores= function() {
  143. return this;
  144. };
  145. HN.Scores.prototype= {
  146. maxScoreRows: 10,
  147. scores: null,
  148. initialize : function() {
  149. var rows= 0, i;
  150. if ( null!=this.scores ) {
  151. rows= this.scores.length;
  152. for( i=0; i<rows; i++ ) {
  153. this.setDOM( i+'_1', this.scores[i].score);
  154. this.setDOM( i+'_2', this.scores[i].level);
  155. this.setDOM( i+'_3', this.scores[i].mode);
  156. this.setDOM( i+'_4', this.scores[i].date);
  157. }
  158. } else {
  159. this.scores= [];
  160. }
  161. for( i=rows; i<10; i++ ) {
  162. for( var j=1; j<=4; j++ ) {
  163. this.setDOM( i+'_'+j, '');
  164. }
  165. }
  166. return this;
  167. },
  168. setDOM : function( elem, value ) {
  169. var dom= document.getElementById(elem);
  170. if ( null!=dom ) {
  171. dom.innerHTML= value;
  172. }
  173. return this;
  174. },
  175. addScore : function( score, level, mode ) {
  176. // quitar filas hasta que entre una.
  177. while ( this.scores.length>=this.maxScoreRows ) {
  178. this.scores.splice( this.scores.length-1, 1 );
  179. }
  180. // busca donde insertar el elemento.
  181. var i=0;
  182. for( i=0; i<this.scores.length; i++ ) {
  183. if ( score>this.scores[i].score ) {
  184. break;
  185. }
  186. }
  187. this.scores.splice( i, 0, new HN.ScoreItem().initialize(score, level, mode ) );
  188. CAAT.modules.LocalStorage.prototype.save('sumon_scores_1', this.scores);
  189. this.initialize();
  190. return this;
  191. },
  192. setData : function() {
  193. this.scores= CAAT.modules.LocalStorage.prototype.load('sumon_scores_1');
  194. return this;
  195. }
  196. };
  197. })();
  198. (function() {
  199. HN.GardenScene= function() {
  200. if ( CAAT.browser!=='iOS' ) {
  201. this.scores= new HN.Scores().setData().initialize();
  202. }
  203. return this;
  204. };
  205. HN.GardenScene.prototype= {
  206. gameScene: null,
  207. directorScene: null,
  208. director: null,
  209. buttonImage: null,
  210. scores: null,
  211. music: null,
  212. sound: null,
  213. createClouds : function() {
  214. for(var i=0; i<5; i++ ) {
  215. var cl= new HN.Cloud().
  216. setId('cloud'+i).
  217. setScene( this.directorScene ).
  218. setupBehavior(this.director);
  219. this.directorScene.addChild(cl);
  220. }
  221. },
  222. createModeButtons : function() {
  223. var me= this;
  224. var m= [];
  225. m.push(new CAAT.SpriteImage().initialize( me.director.getImage('mode-classic'), 1,3 ));
  226. m.push(new CAAT.SpriteImage().initialize( me.director.getImage('mode-progressive'), 1,3 ));
  227. m.push(new CAAT.SpriteImage().initialize( me.director.getImage('mode-respawn'), 1,3 ));
  228. var modes= [ HN.GameModes.classic, HN.GameModes.progressive, HN.GameModes.respawn ];
  229. var i,w= 0;
  230. for( i=0; i<m.length; i++ ) {
  231. w= Math.max(w,m[i].singleWidth);
  232. }
  233. var margin= 20;
  234. w+=margin;
  235. var dw= (me.director.width-w*m.length)/2 + margin/2;
  236. function createb(index) {
  237. var text= new CAAT.SpriteImage().
  238. initialize( me.director.getImage('mode-text'), 1,3 ).
  239. setAnimationImageIndex([index]);
  240. var c= new CAAT.ActorContainer().create().setBounds(
  241. dw + w*index,
  242. me.director.width>me.director.height ? me.director.height/2- 10 : me.director.height/2-100,
  243. Math.max( m[index].singleWidth, text.singleWidth),
  244. m[index].singleWidth+text.singleHeight );
  245. var b= new CAAT.Actor().
  246. setAsButton(m[index], 0,1,2,0, function() {
  247. me.director.audioPlay('11');
  248. me.startGame(me.director,0,modes[index]);
  249. }).
  250. setBounds(
  251. (c.width-m[index].singleWidth)/2,
  252. 0,
  253. m[index].singleWidth,
  254. m[index].singleHeight );
  255. var t = new CAAT.Actor().
  256. setBackgroundImage(text).
  257. setBounds(
  258. (c.width - text.singleWidth) / 2,
  259. b.height,
  260. text.singleWidth,
  261. text.singleHeight);
  262. c.addChild(b);
  263. c.addChild(t);
  264. return c;
  265. }
  266. this.directorScene.addChild( createb(0) );
  267. this.directorScene.addChild( createb(1) );
  268. this.directorScene.addChild( createb(2) );
  269. },
  270. createHowtoButton : function( info_howto_ci ) {
  271. var director= this.director;
  272. var ihw= info_howto_ci.singleWidth;
  273. var ihh= info_howto_ci.singleHeight;
  274. var me= this;
  275. var sb = new CAAT.ScaleBehavior().setValues( 1,5, 1,5 );
  276. var _howto= new CAAT.Actor().
  277. setBackgroundImage(new CAAT.SpriteImage().initialize( director.getImage('howto'),1,1 ) ).
  278. setOutOfFrameTime().
  279. setAlpha(.9);
  280. var pbOut= new CAAT.PathBehavior().
  281. setValues( new CAAT.Path().setLinear( _howto.x,0,700,0 ) ).
  282. setInterpolator(new CAAT.Interpolator().createBounceOutInterpolator(false) ).
  283. addListener( {
  284. behaviorExpired : function(behavior, time, actor) {
  285. _howto.setOutOfFrameTime();
  286. }
  287. });
  288. var pbIn= new CAAT.PathBehavior().
  289. setValues(new CAAT.Path().setLinear( 700,0,0,0 )).
  290. setInterpolator( new CAAT.Interpolator().createBounceOutInterpolator(false) );
  291. _howto.mouseClick= function( e ) {
  292. _howto.emptyBehaviorList().
  293. setFrameTime( me.directorScene.time, Number.MAX_VALUE ).
  294. addBehavior( pbOut.setFrameTime( me.directorScene.time, 1000 ) );
  295. };
  296. var howto= new CAAT.Actor().
  297. setAsButton(info_howto_ci.getRef(), 3,4,5,3,
  298. function() {
  299. director.audioPlay('11');
  300. _howto.emptyBehaviorList().
  301. setFrameTime( me.directorScene.time, Number.MAX_VALUE ).
  302. addBehavior( pbIn.setFrameTime( me.directorScene.time, 1000 ) );
  303. }).
  304. setBounds( 10, director.height-10-ihh-ihh-5, ihw, ihh );
  305. return {
  306. howto: howto,
  307. howtod:_howto
  308. };
  309. },
  310. /**
  311. * Creates the main game Scene.
  312. * @param director a CAAT.Director instance.
  313. * @param gardenSize
  314. */
  315. create : function(director, gardenSize) {
  316. director.audioLoop('music');
  317. this.director= director;
  318. this.directorScene= director.createScene();
  319. var dw= director.width;
  320. var dh= director.height;
  321. var me= this;
  322. this.directorScene.activated= function() {
  323. me.prepareSound();
  324. };
  325. var imgb= director.getImage('background-2');
  326. this.directorScene.addChild(
  327. new CAAT.Actor().
  328. setBounds(0,0,dw,dh).
  329. setBackgroundImage(imgb)
  330. );
  331. ///////////// some clouds
  332. this.createClouds();
  333. ////////////// garden
  334. if ( gardenSize>0 ) {
  335. // fondo. jardin.
  336. this.directorScene.addChild(
  337. new HN.Garden().
  338. create().
  339. setBounds(0,0,dw,dh).
  340. initialize( director.ctx, gardenSize, dh*.5 )
  341. );
  342. }
  343. //////////// scores
  344. this.buttonImage= new CAAT.SpriteImage().initialize(
  345. director.getImage('buttons'), 7,3 );
  346. var bw= this.buttonImage.singleWidth;
  347. var bh= this.buttonImage.singleHeight;
  348. var numButtons= 4;
  349. var yGap= 10;
  350. var scores= null;
  351. if (false && CAAT.browser!=='iOS') {
  352. scores=new CAAT.Actor().
  353. setAsButton( this.buttonImage.getRef(), 18,19,20,18, function() {
  354. director.audioPlay('11');
  355. }).
  356. setBounds( dw-bw-10, dh-bh-10, bw, bh );
  357. }
  358. ////////////// sound controls
  359. this.soundControls(director);
  360. ////////////// level buttons
  361. this.createModeButtons();
  362. if ( false && CAAT.browser!=='iOS' ) {
  363. this.directorScene.addChild(scores);
  364. }
  365. ////////////// Sumon logo
  366. var logoi= director.getImage('logo');
  367. var logo= new CAAT.Actor().
  368. setBackgroundImage(logoi).
  369. enableEvents(true);
  370. logo.setLocation( (dw - logo.width)/2, -10 );
  371. if ( director.width<director.height ) {
  372. logo.
  373. setBackgroundImage(logoi, false).
  374. setSize( logoi.width*.8, logoi.height*.8 ).
  375. setImageTransformation( CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE );
  376. }
  377. logo.mouseClick= function( e ) {
  378. CreateLinksInGame('Math-Plus','menu','logo');
  379. };
  380. this.directorScene.addChild(logo);
  381. ///////// info & howto
  382. var info_howto_ci= new CAAT.SpriteImage().initialize( director.getImage('info_howto'), 2, 3 );
  383. var howto= this.createHowtoButton(info_howto_ci);
  384. this.directorScene.addChild(howto.howto);
  385. this.directorScene.addChild(howto.howtod);
  386. if ( director.width<director.height ) {
  387. CAAT.modules.LayoutUtils.row(
  388. this.directorScene,
  389. [howto.howto],
  390. {
  391. padding_left: 195,
  392. padding_right: 195,
  393. top: director.height/2+100
  394. });
  395. }
  396. return this;
  397. },
  398. soundControls : function(director) {
  399. var ci= new CAAT.SpriteImage().initialize( director.getImage('sound'), 2,3 );
  400. var dw= director.width;
  401. var dh= director.height;
  402. var music= new CAAT.Actor().
  403. setAsButton( ci.getRef(),0,1,0,0, function(button) {
  404. director.setMusicEnabled( !director.audioManager.isMusicEnabled() );
  405. if ( director.isMusicEnabled() ) {
  406. button.setButtonImageIndex(0,1,0,0);
  407. } else {
  408. button.setButtonImageIndex(2,2,2,2);
  409. }
  410. }).
  411. setBounds( dw-ci.singleWidth-2, 2, ci.singleWidth, ci.singleHeight );
  412. var sound= new CAAT.Actor().
  413. setAsButton( ci.getRef(),3,4,3,3, function(button) {
  414. director.setSoundEffectsEnabled( !director.audioManager.isSoundEffectsEnabled() );
  415. if ( director.isSoundEffectsEnabled() ) {
  416. button.setButtonImageIndex(3,4,3,3);
  417. } else {
  418. button.setButtonImageIndex(5,5,5,5);
  419. }
  420. }).
  421. setBounds( dw-ci.singleWidth-2, 2+2+ci.singleHeight, ci.singleWidth, ci.singleHeight );
  422. music.prepare= function() {
  423. if ( director.audioManager.isMusicEnabled() ) {
  424. this.setButtonImageIndex(0,1,0,0);
  425. } else {
  426. this.setButtonImageIndex(2,2,2,2);
  427. }
  428. }
  429. sound.prepare= function() {
  430. if ( director.audioManager.isSoundEffectsEnabled() ) {
  431. this.setButtonImageIndex(3,4,3,3);
  432. } else {
  433. this.setButtonImageIndex(5,5,5,5);
  434. }
  435. }
  436. this.directorScene.addChild(sound);
  437. this.directorScene.addChild(music);
  438. if ( director.width<director.height ) {
  439. CAAT.modules.LayoutUtils.row(
  440. this.directorScene,
  441. [
  442. music,
  443. sound
  444. ],
  445. {
  446. padding_left: 195,
  447. padding_right: 195,
  448. top: director.height/2+150
  449. });
  450. }
  451. this.music= music;
  452. this.sound= sound;
  453. },
  454. prepareSound : function() {
  455. try {
  456. this.sound.prepare();
  457. this.music.prepare();
  458. }
  459. catch(e) {
  460. }
  461. },
  462. startGame : function(director,level,gameMode) {
  463. this.gameScene.setDifficulty(level);
  464. this.gameScene.prepareSceneIn(gameMode);
  465. director.easeInOut(
  466. 1,
  467. CAAT.Scene.EASE_TRANSLATE,
  468. CAAT.Actor.prototype.ANCHOR_TOP,
  469. 0,
  470. CAAT.Scene.EASE_TRANSLATE,
  471. CAAT.Actor.prototype.ANCHOR_BOTTOM,
  472. 1000,
  473. false,
  474. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false),
  475. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) );
  476. },
  477. /**
  478. * gameScene listener.
  479. * @param type {string}
  480. * @param data {object}
  481. */
  482. gameEvent : function( type, data ) {
  483. if ( CAAT.browser!=='iOS' ) {
  484. this.scores.addScore( data.score, data.level, data.gameMode );
  485. }
  486. }
  487. };
  488. })();