gamescene.js 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  1. /**
  2. * See LICENSE file.
  3. *
  4. * Play scene..
  5. */
  6. (function() {
  7. HN.BrickActor= function() {
  8. HN.BrickActor.superclass.constructor.call(this);
  9. this.sb= new CAAT.ScaleBehavior().
  10. setFrameTime(-1,0).
  11. setValues(1,1);
  12. this.addBehavior(this.sb);
  13. this.rb= new CAAT.RotateBehavior().
  14. setFrameTime(-1,0).
  15. setValues(0,0)
  16. this.addBehavior(this.rb);
  17. this.pb= new CAAT.PathBehavior().
  18. setFrameTime(-1,0)
  19. this.addBehavior(this.pb);
  20. this.ab= new CAAT.AlphaBehavior().
  21. setFrameTime(-1,0).
  22. setValues(1)
  23. this.addBehavior(this.ab);
  24. return this;
  25. };
  26. HN.BrickActor.status= {
  27. REGULAR: 0, // en tablero.
  28. FALLING: 1, // cayendo. (rearrange)
  29. CLEARED: 2 // eliminado, no disponible (cayendo y eliminado)
  30. }
  31. HN.BrickActor.prototype= {
  32. timeOver: 250,
  33. timeSelection: 1000,
  34. timeRespawn: 1500,
  35. timeRearrange: 1500,
  36. timeOverflow: 200,
  37. timeCleared: 800,
  38. brick: null,
  39. status: 0, // 0: regular state, 1: falling, 2: cleared.
  40. ab: null,
  41. sb: null,
  42. rb: null,
  43. pb: null,
  44. /**
  45. *
  46. * @param numberImage
  47. * @param brick a HN.Brick instance.
  48. */
  49. initialize : function( numberImage, brick ) {
  50. var tw= numberImage.singleWidth;
  51. var th= numberImage.singleHeight;
  52. this.setSize(tw,th);
  53. this.setBackgroundImage(numberImage.getRef(),true);
  54. this.brick= brick;
  55. var me= this;
  56. brick.delegate = function() {
  57. me.setSpriteIndex( me.brick.value + numberImage.columns*me.brick.color );
  58. }
  59. return this;
  60. },
  61. /**
  62. * Make initialize animation.
  63. * @param brickPosition
  64. */
  65. initializeForPlay: function( brickPosition, animationStartTime, animationDuration ) {
  66. var brickActor= this;
  67. brickActor.
  68. setLocation( brickPosition.x, brickPosition.y ).
  69. setScale( .01, .01 );
  70. brickActor.sb.
  71. setValues( .01,1, .01, 1).
  72. setInterpolator(
  73. new CAAT.Interpolator().createElasticOutInterpolator(1.1, 0.1, false) ).
  74. setFrameTime(animationStartTime, animationDuration );
  75. },
  76. setStatus : function(st) {
  77. this.status= st;
  78. return this;
  79. },
  80. mouseEnter : function(mouseEvent) {
  81. if ( this.brick.selected ) {
  82. return;
  83. }
  84. this.parent.setZOrder( this, Number.MAX_VALUE );
  85. this.sb.setFrameTime( this.time, this.timeOver ).
  86. setValues( 1, 1.2, 1, 1.2 ).
  87. setPingPong();
  88. if ( CAAT.browser!=='iOS' ) {
  89. CAAT.setCursor('pointer');
  90. }
  91. },
  92. mouseExit : function(mouseEvent) {
  93. if ( CAAT.browser!=='iOS' ) {
  94. CAAT.setCursor('default');
  95. }
  96. },
  97. mouseDown : function(mouseEvent) {
  98. this.brick.changeSelection();
  99. },
  100. toString : function() {
  101. return 'HN.Brick '+this.brick.row+','+this.brick.column;
  102. },
  103. /**
  104. * brick deselected.
  105. */
  106. reset : function() {
  107. this.resetAlpha().
  108. resetRotate().
  109. resetScale().
  110. resetTransform();
  111. this.alpha=1;
  112. return this;
  113. },
  114. resetBehavior : function(b,p1,p2) {
  115. b.emptyListenerList();
  116. b.setCycle(false);
  117. b.setInterpolator( new CAAT.Interpolator().createLinearInterpolator() );
  118. b.setFrameTime(-1,0);
  119. if ( p1&& p2 ) {
  120. b.setValues(p1,p2);
  121. }
  122. },
  123. resetAlpha: function() {
  124. this.resetBehavior(this.ab,1,1);
  125. return this;
  126. },
  127. resetScale: function() {
  128. this.resetBehavior(this.sb,1,1);
  129. return this;
  130. },
  131. resetRotate: function() {
  132. this.resetBehavior(this.rb,0,2*Math.PI);
  133. return this;
  134. },
  135. resetPath : function() {
  136. this.resetBehavior(this.pb);
  137. return this;
  138. },
  139. resetBehaviors : function() {
  140. this.resetAlpha();
  141. this.resetScale();
  142. this.resetRotate();
  143. this.resetPath();
  144. return this;
  145. },
  146. /**
  147. * brick selected.
  148. */
  149. setSelected : function() {
  150. this.sb.
  151. setValues( 1, .65, 1, .65 ).
  152. setFrameTime( this.time, this.timeSelection ).
  153. setPingPong().
  154. setCycle(true);
  155. this.ab.
  156. setValues( .75, .25 ).
  157. setFrameTime( this.time, this.timeSelection ).
  158. setPingPong().
  159. setCycle(true);
  160. },
  161. /**
  162. * game just started.
  163. */
  164. set: function() {
  165. this.status= HN.BrickActor.status.REGULAR;
  166. this.enableEvents(true);
  167. this.reset();
  168. },
  169. /**
  170. * actors entering game upon respawn timeout.
  171. * @param x
  172. * @param y
  173. */
  174. respawn: function(x,y) {
  175. this.
  176. reset().
  177. enableEvents(true).
  178. setFrameTime(this.time,Number.MAX_VALUE).
  179. resetTransform().
  180. setAlpha(1).
  181. setStatus(HN.BrickActor.status.FALLING)
  182. ;
  183. this.pb.
  184. emptyListenerList().
  185. setFrameTime(this.time, this.timeRespawn+this.brick.row*50).
  186. setPath( new CAAT.LinearPath().
  187. setInitialPosition(x, -this.height-((Math.random()*100)>>0) ).
  188. setFinalPosition(x, y)).
  189. setInterpolator(
  190. new CAAT.Interpolator().createBounceOutInterpolator()
  191. ).
  192. addListener(
  193. {
  194. behaviorApplied : function() {
  195. },
  196. // re-enable events on actor after moving to rearrange position.
  197. behaviorExpired : function(behavior, time, actor) {
  198. actor.setStatus(HN.BrickActor.status.REGULAR);
  199. }
  200. }
  201. );
  202. },
  203. /**
  204. * brick's been rearranged, i.e. moved position in context.
  205. * @param x
  206. * @param y
  207. */
  208. rearrange: function(x,y) {
  209. if ( this.status===HN.BrickActor.status.CLEARED ) {
  210. return;
  211. }
  212. this.setStatus(HN.BrickActor.status.FALLING);
  213. this.pb.
  214. emptyListenerList().
  215. setFrameTime(this.time + this.brick.column*50, this.timeRearrange).
  216. setPath( new CAAT.LinearPath().
  217. setInitialPosition(this.x, this.y).
  218. setFinalPosition(x, y)).
  219. setInterpolator(
  220. new CAAT.Interpolator().createBounceOutInterpolator()
  221. ).
  222. addListener({
  223. behaviorApplied : function() {
  224. },
  225. // re-enable events on actor after moving to rearrange position.
  226. behaviorExpired : function(behavior, time, actor) {
  227. actor.setStatus(HN.BrickActor.status.REGULAR);
  228. }
  229. });
  230. },
  231. /**
  232. * selection excedes suggested number.
  233. * @param callback
  234. */
  235. selectionOverflow : function() {
  236. // ladrillos que estn cayendo, no hacen el path de error.
  237. //if ( !this.falling ) {
  238. if ( this.status===HN.BrickActor.status.REGULAR ) {
  239. var signo= Math.random()<.5 ? 1: -1;
  240. this.pb.
  241. emptyListenerList().
  242. setFrameTime(this.time, this.timeOverflow).
  243. setPath(
  244. new CAAT.Path().
  245. beginPath(this.x,this.y).
  246. addLineTo(this.x + signo*(5+5*Math.random()),this.y).
  247. addLineTo(this.x - signo*(5+5*Math.random()),this.y).
  248. endPath() ).
  249. setPingPong();
  250. }
  251. },
  252. /**
  253. * this brick belongs to valid selection which sums up the suggested number.
  254. * @param scene
  255. * @param maxHeight
  256. */
  257. selectionCleared : function(scene, maxHeight) {
  258. this.setStatus(HN.BrickActor.status.CLEARED);
  259. var signo= Math.random()<.5 ? 1 : -1;
  260. var offset= 50+Math.random()*30;
  261. var actor= this;
  262. this.enableEvents(false).setScale( 1.5, 1.5 );
  263. this.pb.
  264. emptyListenerList().
  265. setFrameTime( this.time, this.timeCleared ).
  266. setPath(
  267. new CAAT.Path().
  268. beginPath( this.x, this.y ).
  269. addQuadricTo(
  270. this.x+offset*signo, this.y-300,
  271. this.x+offset*signo*2, this.y+maxHeight+20).
  272. endPath() );
  273. if ( !HN.LOWQUALITY ) {
  274. this.pb.addListener( {
  275. behaviorExpired : function(behavior, time, actor) {
  276. actor.setExpired(true);
  277. },
  278. behaviorApplied : function(behavior, time, normalizedTime, actor, value) {
  279. for( var i=0; i< (CAAT.browser==='iOS' ? 1 : 3); i++ ) {
  280. var offset0= Math.random()*10*(Math.random()<.5?1:-1);
  281. var offset1= Math.random()*10*(Math.random()<.5?1:-1);
  282. if ( scene.fallingStarCache.length>0 ) {
  283. var actor2= scene.fallingStarCache.shift();
  284. // check for no parent, that is, no active actor.
  285. if ( !actor2.domParent ) {
  286. actor2.
  287. setAnimationImageIndex( [(Math.random()*6)>>0] ).
  288. setFrameTime(actor.time, 400).
  289. setLocation(
  290. offset0+actor.x+actor.width/2,
  291. offset1+actor.y);
  292. actor2.__parent.addChild(actor2);
  293. actor2.getBehavior( '__alpha' ).setFrameTime( actor.time, 400 );
  294. }
  295. }
  296. }
  297. }
  298. });
  299. }
  300. this.pb.setInterpolator( new CAAT.Interpolator().createLinearInterpolator(false,false) );
  301. this.rb.
  302. setFrameTime( this.time, this.timeCleared ).
  303. setValues( 0, (Math.PI + Math.random()*Math.PI*2)*(Math.random()<.5?1:-1) );
  304. this.ab.
  305. setFrameTime( this.time, this.timeCleared ).
  306. setValues( 1, .25 );
  307. }
  308. };
  309. extend( HN.BrickActor, CAAT.Actor);
  310. })();
  311. (function() {
  312. HN.RespawnClockActor= function() {
  313. HN.RespawnClockActor.superclass.constructor.call(this);
  314. this.ticks= [];
  315. return this;
  316. };
  317. HN.RespawnClockActor.prototype= {
  318. ticks: null,
  319. respawnTime: 10000,
  320. scene: null,
  321. context: null,
  322. arrow: null,
  323. enabled: false,
  324. initialize: function(director, scene, context) {
  325. this.scene= scene;
  326. this.context= context;
  327. var itick= director.getImage('rclock-tick');
  328. var itickci;
  329. itickci= new CAAT.SpriteImage().initialize( itick, 16, 1 );
  330. var ibg= director.getImage('rclock-bg');
  331. var iarrow= director.getImage('rclock-arrow');
  332. var me= this;
  333. this.setSize(64,64);
  334. var bg= new CAAT.Actor().
  335. setBackgroundImage( ibg, true ).
  336. setLocation((this.width-ibg.width)/2, (this.height-ibg.height)/2);
  337. this.addChild(bg);
  338. var NTicks= 12;
  339. for( var i=0; i<NTicks; i++ ) {
  340. var tick= new CAAT.Actor().
  341. setBackgroundImage( itickci.getRef(), true ).
  342. setLocation(
  343. this.width/2 + 29*Math.cos( -Math.PI/2 + i*2*Math.PI/NTicks ) - itick.width/2,
  344. this.height/2 + 29*Math.sin( -Math.PI/2 + i*2*Math.PI/NTicks ) - itick.width/2
  345. );
  346. this.addChild(tick);
  347. this.ticks.push(tick);
  348. var cb= new CAAT.ContainerBehavior().
  349. setOutOfFrameTime().
  350. addBehavior(
  351. new CAAT.ScaleBehavior().
  352. setFrameTime( i*this.respawnTime/NTicks, 300 ).
  353. setValues( 1,3, 1,3 )
  354. );
  355. cb.addBehavior(
  356. new CAAT.GenericBehavior().
  357. setFrameTime( i*this.respawnTime/NTicks, 300 ).
  358. setValues( 1, 0, null, null, function(value, target, actor ) {
  359. actor.setAnimationImageIndex( [15-((value*15)>>0)] );
  360. })
  361. );
  362. tick.addBehavior(cb);
  363. }
  364. var flecha= new CAAT.Actor().setBackgroundImage(iarrow, true);
  365. flecha.setLocation(
  366. (this.width-flecha.width)/2, this.height/2-23-.5
  367. );
  368. this.addChild(flecha);
  369. flecha.addBehavior(
  370. new CAAT.RotateBehavior().
  371. setOutOfFrameTime().
  372. setValues(0, 2*Math.PI, .50, (23/flecha.height*100)/100 ).
  373. addListener({
  374. behaviorExpired : function(behavior, time, actor) {
  375. me.resetTimer();
  376. me.context.respawn();
  377. },
  378. behaviorApplied : function(behavior, time, normalizedTime, actor, value) {}
  379. })
  380. );
  381. this.arrow= flecha;
  382. return this;
  383. },
  384. resetTimer : function() {
  385. var NTicks= this.ticks.length;
  386. for( var i=0; i<NTicks; i++ ) {
  387. this.ticks[i].resetTransform().setAnimationImageIndex([0]);
  388. this.ticks[i].behaviorList[0].setFrameTime(this.scene.time, this.respawnTime);
  389. this.ticks[i].behaviorList[0].behaviors[0].setFrameTime( i*this.respawnTime/NTicks, 300 );
  390. this.ticks[i].behaviorList[0].behaviors[1].setFrameTime( i*this.respawnTime/NTicks, 300 );
  391. }
  392. this.arrow.behaviorList[0].setFrameTime(this.scene.time, this.respawnTime);
  393. },
  394. stopTimer : function() {
  395. for( var i=0; i<this.ticks.length; i++ ) {
  396. this.ticks[i].resetTransform();
  397. this.ticks[i].behaviorList[0].setOutOfFrameTime();
  398. }
  399. this.arrow.behaviorList[0].setOutOfFrameTime();
  400. },
  401. contextEvent : function( event ) {
  402. if ( !this.enabled ) {
  403. return;
  404. }
  405. if ( event.event=='status') {
  406. if ( event.params==HN.Context.prototype.ST_RUNNNING ) {
  407. this.resetTimer();
  408. } else {
  409. this.stopTimer();
  410. }
  411. }
  412. },
  413. enable : function( enabled, respawnTime ) {
  414. this.respawnTime= respawnTime;
  415. this.enabled= enabled;
  416. if ( enabled ) {
  417. this.setFrameTime(0,Number.MAX_VALUE);
  418. } else {
  419. this.setOutOfFrameTime();
  420. }
  421. }
  422. };
  423. extend(HN.RespawnClockActor, CAAT.ActorContainer);
  424. })();
  425. (function() {
  426. HN.GuessNumberActor= function() {
  427. HN.GuessNumberActor.superclass.constructor.call(this);
  428. this.actors= [];
  429. this.setGlobalAlpha(true);
  430. return this;
  431. };
  432. HN.GuessNumberActor.prototype= {
  433. guessNumber: 0,
  434. numbersImage: null,
  435. offsetX: 0,
  436. offsetY: 0,
  437. numbers: null,
  438. tmpnumbers: null,
  439. container: null,
  440. actors: null,
  441. setNumbersImage : function( image, bg ) {
  442. this.setBackgroundImage(bg);
  443. this.numbersImage= image;
  444. this.container= new CAAT.ActorContainer()
  445. .setBounds( 10,0,this.width,this.height);
  446. this.addChild( this.container );
  447. this.offsetX= (this.width - 2 * (image.singleWidth-30)) / 2;
  448. this.offsetY= 10 + (this.height - image.singleHeight) / 2;
  449. for( var i=0; i<2; i++ ) {
  450. var digit=
  451. new CAAT.Actor()
  452. .setBackgroundImage(image.getRef(), true)
  453. .setLocation(this.offsetX,this.offsetY)
  454. .setVisible(false)
  455. ;
  456. this.actors.push(digit);
  457. this.container.addChild(digit).setGlobalAlpha(true);
  458. }
  459. return this;
  460. },
  461. contextEvent : function( event ) {
  462. var i;
  463. if ( event.source=='context' ) {
  464. if ( event.event=='guessnumber' ) {
  465. var me= this;
  466. me.guessNumber= event.params.guessNumber;
  467. me.numbers= [];
  468. var snumber= me.guessNumber.toString();
  469. if ( snumber.length===1 ) {
  470. snumber='0'+snumber;
  471. }
  472. me.offsetX= 10;
  473. me.offsetY= (me.height - me.numbersImage.singleHeight)/2;
  474. var i;
  475. for( i=0; i<snumber.length; i++ ) {
  476. me.numbers[i]= parseInt(snumber.charAt(i));
  477. this.actors[i].setLocation(
  478. me.offsetX+i*(me.numbersImage.singleWidth-30),
  479. this.actors[i].y );
  480. this.actors[i].setVisible(true);
  481. }
  482. if ( null==me.tmpnumbers ) {
  483. for( i=0; i<snumber.length; i++ ) {
  484. this.actors[i].setAnimationImageIndex([this.numbers[i]]);
  485. }
  486. this.container.emptyBehaviorList();
  487. this.container.addBehavior(
  488. new CAAT.AlphaBehavior().
  489. setFrameTime( this.time, 250 ).
  490. setValues(0,1).
  491. setId(1000)
  492. );
  493. me.tmpnumbers= me.numbers;
  494. } else {
  495. this.container.emptyBehaviorList();
  496. this.container.addBehavior(
  497. new CAAT.AlphaBehavior().
  498. setFrameTime( this.time, 250 ).
  499. setValues(1,0).
  500. addListener( {
  501. behaviorExpired : function(behavior, time, actor) {
  502. for( i=0; i<snumber.length; i++ ) {
  503. me.actors[i].setAnimationImageIndex([me.numbers[i]]);
  504. }
  505. actor.emptyBehaviorList();
  506. actor.addBehavior(
  507. new CAAT.AlphaBehavior().
  508. setFrameTime( me.time, 250 ).
  509. setValues(0,1));
  510. },
  511. behaviorApplied : function(behavior, time, normalizedTime, actor, value) {}
  512. })
  513. );
  514. }
  515. } else if ( event.event=='status') {
  516. if ( event.params!=HN.Context.prototype.ST_RUNNNING ) {
  517. this.numbers= null;
  518. this.tmpnumbers= null;
  519. }
  520. }
  521. }
  522. }
  523. };
  524. extend( HN.GuessNumberActor, CAAT.ActorContainer, null);
  525. })();
  526. (function() {
  527. HN.Chrono= function() {
  528. HN.Chrono.superclass.constructor.call(this);
  529. this.actorventana= new CAAT.Actor();
  530. this.actorcrono= new CAAT.Actor().setLocation(14,18);
  531. this.addChild( this.actorcrono );
  532. this.addChild( this.actorventana );
  533. return this;
  534. };
  535. HN.Chrono.prototype= {
  536. maxTime: 0,
  537. elapsedTime:0,
  538. actorventana: null,
  539. actorcrono: null,
  540. progressHole: 160,
  541. setImages : function( background, progress ){
  542. this.actorventana.setBackgroundImage(background, true);
  543. this.actorcrono.setBackgroundImage(progress, true);
  544. this.setSize( this.actorventana.width, this.actorventana.height );
  545. this.actorcrono.setImageTransformation( CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE );
  546. return this;
  547. },
  548. animate : function(director, time) {
  549. var size=
  550. this.maxTime!=0 ?
  551. this.elapsedTime/this.maxTime * this.progressHole :
  552. 0;
  553. // make sure this actor is marked as dirty by calling setSize and not .width=new_size.
  554. this.actorcrono.setSize( this.progressHole-size, this.actorcrono.height );
  555. return HN.Chrono.superclass.animate.call(this,director,time);
  556. },
  557. tick : function( iElapsedTime, maxTime ) {
  558. this.maxTime= maxTime;
  559. this.elapsedTime= iElapsedTime;
  560. },
  561. contextEvent : function(event) {
  562. if ( event.source=='context' && event.event=='status') {
  563. if ( event.params==HN.Context.prototype.ST_ENDGAME ) {
  564. this.maxTime=0;
  565. this.elapsedTime= 1000;
  566. }
  567. }
  568. }
  569. };
  570. extend( HN.Chrono, CAAT.ActorContainer);
  571. })();
  572. (function() {
  573. HN.SelectionPath= function(director) {
  574. HN.SelectionPath.superclass.constructor.call(this);
  575. this.coords= [];
  576. this.particles= [];
  577. this.fillStyle= null;
  578. this.bolasImage=
  579. new CAAT.SpriteImage().initialize(
  580. director.getImage('bolas'),1,8);
  581. this.director= director;
  582. return this;
  583. };
  584. HN.SelectionPath.prototype= {
  585. coords: null, // an array of 2D positions on screen.
  586. path: null,
  587. pathMeasure: null,
  588. particles: null, // an array of random time to position on path.
  589. particlesPerSegment: 10,
  590. traversingPathTime: 3000,
  591. context: null,
  592. bolasImage: null,
  593. director: null,
  594. /**
  595. *
  596. * @param bolas {CAAT.SpriteImage}
  597. */
  598. initialize : function() {
  599. this.coords= [];
  600. this.path= null;
  601. this.pathMeasure= null;
  602. },
  603. setup : function( context, brickActors ) {
  604. var i;
  605. this.context= context;
  606. this.brickActors= brickActors;
  607. this.coords= [];
  608. // no bricks, no path
  609. if ( 0==context.selectedList.length ) {
  610. this.initialize();
  611. return;
  612. }
  613. var expectedParticleCount= this.particlesPerSegment*(context.selectedList.length-1);
  614. if ( this.particles.length> expectedParticleCount ) {
  615. this.particles.splice( expectedParticleCount, this.particles.length-expectedParticleCount );
  616. } else {
  617. while( this.particles.length<expectedParticleCount ) {
  618. this.particles.push( (context.selectedList.length)*this.traversingPathTime + this.traversingPathTime*Math.random() );
  619. }
  620. }
  621. },
  622. setupPath : function() {
  623. this.coords= [];
  624. if ( this.context.selectedList.length ) {
  625. var numberWidth= this.brickActors[0][0].width;
  626. var numberHeight= this.brickActors[0][0].height;
  627. var offsetX= (this.context.columns-this.context.currentColumns)/2*numberWidth;
  628. var offsetY= (this.context.rows-this.context.currentRows)/2*numberHeight;
  629. // get selected bricks screen coords.
  630. for( i=0; i<this.context.selectedList.length; i++ ) {
  631. var brick= this.context.selectedList[i];
  632. var brickActor= this.brickActors[brick.row][brick.column];
  633. this.coords.push(
  634. {
  635. x: brickActor.x + brickActor.width/2,// + offsetX,
  636. y: brickActor.y + brickActor.height/2// + offsetY
  637. });
  638. }
  639. // setup a path for the coordinates.
  640. this.path= new CAAT.Path();
  641. this.path.beginPath( this.coords[0].x, this.coords[0].y );
  642. for( i=1; i<this.context.selectedList.length; i++ ) {
  643. this.path.addLineTo( this.coords[i].x, this.coords[i].y );
  644. }
  645. this.path.endPath();
  646. this.pathMeasure= new CAAT.PathBehavior().
  647. setPath(this.path).
  648. setFrameTime(0, this.traversingPathTime*this.context.selectedList.length).
  649. setCycle(true);
  650. }
  651. },
  652. paint : function(director, time) {
  653. if ( null!=this.context && 0!=this.context.selectedList.length ) {
  654. var i;
  655. this.setupPath();
  656. var ctx= director.ctx;
  657. ctx.beginPath();
  658. var i;
  659. for( i=0; i<this.coords.length; i++ ) {
  660. ctx.lineTo( this.coords[i].x, this.coords[i].y );
  661. }
  662. ctx.strokeStyle= '#ffff00';
  663. ctx.lineCap= 'round';
  664. ctx.lineJoin= 'round';
  665. for( i=2; i<=(CAAT.browser==='iOS' ? 2 : 8); i+=2 ) {
  666. ctx.lineWidth= i;
  667. ctx.globalAlpha= .5 - i/8/3;
  668. ctx.stroke();
  669. }
  670. if ( this.pathMeasure ) {
  671. var pos;
  672. for(i=0; i<this.particles.length; i++) {
  673. pos= this.pathMeasure.positionOnTime( (this.particles[i]+time)*(1+(i%3)*.33) );
  674. this.bolasImage.setSpriteIndex(i%8);
  675. this.bolasImage.paint( director, 0, pos.x-4, pos.y-4 );
  676. }
  677. }
  678. }
  679. },
  680. contextEvent : function( event ) {
  681. },
  682. paintActorGL : function(director,time) {
  683. if ( null!=this.context && this.context.selectedList.length>1 ) {
  684. this.setupPath();
  685. if ( null===this.coords || 0===this.coords.length ) {
  686. return;
  687. }
  688. director.glFlush();
  689. var i,
  690. pos=0,
  691. z= 0,
  692. point= new CAAT.Point(),
  693. m= this.worldModelViewMatrix,
  694. cc= director.coords,
  695. ccthis= this.coords,
  696. pa= this.particles;
  697. for( i=0; i<ccthis.length; i++ ) {
  698. point.set(ccthis[i].x, ccthis[i].y,0);
  699. m.transformCoord(point);
  700. cc[pos++]= point.x;
  701. cc[pos++]= point.y;
  702. cc[pos++]= z;
  703. }
  704. for( i=2; i<=8; i+=2 ) {
  705. director.glTextureProgram.drawPolylines(cc, ccthis.length, 1,1,0,.5 - i/8/3, i);
  706. }
  707. //
  708. // setup particles
  709. //
  710. pos=0;
  711. for(i=0; i<pa.length; i++) {
  712. ppos= this.pathMeasure.positionOnTime( (pa[i]+time)*(1+(i%3)*.33) );
  713. point.set(ppos.x, ppos.y,0);
  714. m.transformCoord(point);
  715. cc[pos++]= point.x-3;
  716. cc[pos++]= point.y-3;
  717. cc[pos++]= z;
  718. cc[pos++]= point.x+3;
  719. cc[pos++]= point.y+3;
  720. cc[pos++]= z;
  721. }
  722. director.glTextureProgram.drawLines(cc, pa.length, 1,1,1,.3, 7);
  723. }
  724. }
  725. };
  726. extend( HN.SelectionPath, CAAT.Actor);
  727. })();
  728. (function() {
  729. HN.ScoreActor= function() {
  730. HN.ScoreActor.superclass.constructor.call(this);
  731. return this;
  732. };
  733. HN.ScoreActor.prototype= {
  734. numDigits: 6,
  735. incrementScore: 0,
  736. maxScore: 0,
  737. minScore: 0,
  738. currentScore: 0,
  739. numbers: null,
  740. startTime: 0,
  741. interpolator: null,
  742. scoreDuration: 2000,
  743. font: null,
  744. FONT_CORRECTION: .6,
  745. reset : function() {
  746. this.currentScore= 0;
  747. this.maxScore= 0;
  748. this.minScore= 0;
  749. this.currentScore=0;
  750. this.setScore();
  751. },
  752. initialize : function(font, background) {
  753. var i;
  754. this.font= font;
  755. this.interpolator= new CAAT.Interpolator().createExponentialInOutInterpolator(2,false);
  756. this.setBackgroundImage(background, true);
  757. for( i=0; i<this.numDigits; i++ ) {
  758. var actor= new CAAT.Actor().
  759. setBackgroundImage(font.getRef(), true).
  760. setLocation(
  761. (((this.width-this.numDigits*this.font.singleWidth*this.FONT_CORRECTION)/2)>>0) +
  762. (i*this.font.singleWidth*this.FONT_CORRECTION) - 5,
  763. 12
  764. ).
  765. setScale( this.FONT_CORRECTION, this.FONT_CORRECTION );
  766. this.addChild(actor);
  767. }
  768. return this;
  769. },
  770. contextEvent : function( event ) {
  771. if ( event.source=='context' ) {
  772. if ( event.event=='score' ) {
  773. this.maxScore= event.params.score;
  774. this.minScore= this.currentScore;
  775. this.incrementScore= this.maxScore- this.minScore;
  776. window.score=this.maxScore;
  777. this.startTime= this.time;
  778. } else if ( event.event=='status') {
  779. if ( event.params==HN.Context.prototype.ST_STARTGAME ) {
  780. this.reset();
  781. }
  782. }
  783. }
  784. },
  785. setScore: function(director) {
  786. this.currentScore>>=0;
  787. var str= ''+this.currentScore;
  788. while( str.length<6 ) {
  789. str='0'+str;
  790. }
  791. this.numbers= [];
  792. var i=0;
  793. for( i=0; i<str.length; i++ ) {
  794. this.numbers[i]= parseInt(str.charAt(i));
  795. this.childrenList[i].setAnimationImageIndex([this.numbers[i]]);
  796. }
  797. },
  798. animate : function(director, time) {
  799. if ( time>= this.startTime && time<this.startTime+this.scoreDuration ) {
  800. this.currentScore=
  801. this.minScore +
  802. this.incrementScore *
  803. this.interpolator.getPosition( (time-this.startTime)/this.scoreDuration ).y;
  804. this.setScore(director);
  805. } else {
  806. if ( this.currentScore!=this.maxScore ) {
  807. this.currentScore= this.maxScore;
  808. this.setScore(director);
  809. }
  810. }
  811. return HN.ScoreActor.superclass.animate.call(this,director,time);
  812. }
  813. };
  814. extend( HN.ScoreActor, CAAT.ActorContainer);
  815. })();
  816. (function() {
  817. HN.AnimatedBackground= function() {
  818. HN.AnimatedBackground.superclass.constructor.call(this);
  819. return this;
  820. };
  821. HN.AnimatedBackground.prototype= {
  822. timer: null,
  823. context: null,
  824. scene: null,
  825. altitude: .05,
  826. altitudeMeterByIncrement: 2,
  827. currentAltitude: 0,
  828. initialOffset: 0,
  829. currentOffset: 0,
  830. setData : function(scene, gameContext) {
  831. this.context= gameContext;
  832. this.scene= scene;
  833. return this;
  834. },
  835. contextEvent : function( event ) {
  836. var me= this;
  837. if ( event.source=='context' ) {
  838. if ( event.event=='status') {
  839. if ( event.params==HN.Context.prototype.ST_ENDGAME ) {
  840. if ( this.timer!=null ) {
  841. this.timer.cancel();
  842. this.timer= null;
  843. this.currentOffset= this.backgroundImage.offsetY;
  844. this.addBehavior(
  845. new CAAT.GenericBehavior().
  846. setFrameTime( this.scene.time, 1000 ).
  847. setValues(this.currentOffset, this.initialOffset, null, null,
  848. function(value,target,actor) {
  849. me.setBackgroundImageOffset(0,value);
  850. }).
  851. setInterpolator( new CAAT.Interpolator().
  852. createBounceOutInterpolator(false) )
  853. );
  854. me.currentAltitude= me.initialOffset;
  855. }
  856. } else if ( event.params==HN.Context.prototype.ST_LEVEL_RESULT ) {
  857. this.timer.cancel();
  858. }
  859. } else if ( event.event=='levelchange') {
  860. this.startTimer();
  861. }
  862. }
  863. },
  864. startTimer : function() {
  865. var me= this;
  866. if ( !this.timer ) {
  867. this.timer= this.scene.createTimer(
  868. me.scene.time,
  869. 200,
  870. function timeout(sceneTime, time, timerTask) {
  871. me.currentAltitude+= me.altitude;
  872. if ( me.currentAltitude>0 ) {
  873. me.currentAltitude=0;
  874. }
  875. me.setBackgroundImageOffset( 0, me.currentAltitude );
  876. timerTask.reset( me.scene.time );
  877. me.context.incrementAltitude( me.altitudeMeterByIncrement );
  878. },
  879. null,
  880. null );
  881. }
  882. },
  883. setInitialOffset : function( offset ) {
  884. this.setBackgroundImageOffset( 0, offset );
  885. this.initialOffset= offset;
  886. this.currentAltitude= offset;
  887. return this;
  888. },
  889. caer : function(time) {
  890. this.setBackgroundImageOffset( 0, this.currentOffset + (this.initialOffset-this.currentOffset)*time );
  891. }
  892. };
  893. extend( HN.AnimatedBackground, CAAT.Actor);
  894. })();
  895. (function() {
  896. HN.BackgroundImage= function() {
  897. HN.BackgroundImage.superclass.constructor.call(this);
  898. return this;
  899. };
  900. HN.BackgroundImage.prototype= {
  901. setupBehavior : function(director, bFirstTime) {
  902. var is_bg= Math.random()<.4;
  903. this.setBackgroundImage( director.getImage('cloud'+(is_bg ? 'b' : '')+ ((4*Math.random())>>0) ), true );
  904. var t= (30000*(is_bg?1.5:1) + 5000*Math.random()*2);
  905. var me= this;
  906. var ix0, ix1, iy0, iy1;
  907. var dw= director.width;
  908. var dh= director.height-200;
  909. var ih= this.backgroundImage.height;
  910. var iw= this.backgroundImage.width;
  911. if ( bFirstTime ) {
  912. ix0= dw*Math.random();
  913. iy0= dh*Math.random();
  914. t= (dw-ix0)/dw*t;
  915. } else {
  916. ix0= -iw-iw*Math.random();
  917. iy0= dh*Math.random();
  918. }
  919. ix1= dw+iw*Math.random();
  920. iy1= iy0 + Math.random()*30;
  921. this.emptyBehaviorList();
  922. this.addBehavior(
  923. new CAAT.PathBehavior().
  924. setFrameTime( this.time, t ).
  925. setPath(
  926. new CAAT.Path().setLinear(ix0, iy0, ix1, iy1)
  927. ).
  928. addListener( {
  929. behaviorExpired : function(behavior, time, actor) {
  930. me.setupBehavior(director, false);
  931. },
  932. behaviorApplied : function(actor,time,normalizedTime,value) {
  933. }
  934. })
  935. );
  936. return this;
  937. }
  938. };
  939. extend( HN.BackgroundImage, CAAT.Actor);
  940. })();
  941. (function() {
  942. HN.LevelActor= function() {
  943. HN.LevelActor.superclass.constructor.call(this);
  944. this.numbers= [];
  945. return this;
  946. };
  947. HN.LevelActor.prototype= {
  948. font: null,
  949. numbers: null,
  950. initialize : function(font, background) {
  951. this.font= font;
  952. for( var i=0; i<2; i++ ) {
  953. var digit= new CAAT.Actor().
  954. setBackgroundImage(font.getRef(), true).
  955. setVisible(false);
  956. this.numbers.push(digit);
  957. this.addChild(digit);
  958. }
  959. this.setBackgroundImage(background, true);
  960. return this;
  961. },
  962. contextEvent : function(event) {
  963. if ( event.source=='context' ) {
  964. if ( event.event=='levelchange') {
  965. this.level= event.params;
  966. var snumber= this.level.toString();
  967. var i, number;
  968. var correction= this.font.singleWidth*.8;
  969. for( i=0; i<snumber.length; i++ ) {
  970. number= parseInt(snumber.charAt(i));
  971. this.numbers[i].
  972. setSpriteIndex(number).
  973. setLocation(
  974. (this.width - snumber.length*correction)/2 + i*correction, 24 ).
  975. setVisible(true);
  976. }
  977. for( ;i<this.numbers.length; i++ ) {
  978. this.numbers[i].setVisible(false);
  979. }
  980. }
  981. }
  982. }
  983. };
  984. extend(HN.LevelActor, CAAT.ActorContainer);
  985. })();
  986. (function() {
  987. HN.MultiplierActor= function() {
  988. HN.MultiplierActor.superclass.constructor.call(this);
  989. this.actorx= new CAAT.Actor().setVisible(false);
  990. this.actornum= new CAAT.Actor();
  991. this.addChild(this.actorx);
  992. this.addChild(this.actornum);
  993. return this;
  994. };
  995. HN.MultiplierActor.prototype= {
  996. actorx: null,
  997. actornum: null,
  998. multiplier: 0,
  999. setImages : function( font, x ) {
  1000. this.actorx.setBackgroundImage(x,true);
  1001. this.actornum.setBackgroundImage(font,true).setVisible(false);
  1002. var xoffset= (this.width-x.width-font.singleWidth)/2 + 10;
  1003. this.actorx.setLocation( xoffset, this.height-x.height+5 );
  1004. this.actornum.setLocation( xoffset+x.width, 0 );
  1005. return this;
  1006. },
  1007. hideMultiplier : function() {
  1008. this.multiplier=0;
  1009. this.actornum.setVisible(false);
  1010. this.actorx.setVisible(false);
  1011. },
  1012. b1 : function(actor) {
  1013. actor.emptyBehaviorList();
  1014. var cb= new CAAT.ContainerBehavior().
  1015. setFrameTime(this.time,1000).
  1016. setCycle(true);
  1017. var ab= new CAAT.AlphaBehavior().
  1018. setFrameTime(0,1000).
  1019. setValues(.6,.8).
  1020. setPingPong();
  1021. cb.addBehavior(ab);
  1022. actor.addBehavior(cb);
  1023. },
  1024. b2 : function(actor) {
  1025. var me= this;
  1026. actor.emptyBehaviorList();
  1027. var ab= new CAAT.AlphaBehavior().
  1028. setFrameTime(this.time,300).
  1029. setValues( this.alpha, 0 ).
  1030. addListener( {
  1031. behaviorExpired : function(behavior, time, actor) {
  1032. me.hideMultiplier();
  1033. },
  1034. behaviorApplied : function(actor,time,normalizedTime,value) {}
  1035. });
  1036. actor.addBehavior(ab);
  1037. },
  1038. contextEvent : function( event ) {
  1039. if ( event.source == 'context' ) {
  1040. if ( event.event=='multiplier' ) {
  1041. if ( event.params.multiplier>1 ) {
  1042. this.multiplier = event.params.multiplier;
  1043. this.actornum.setVisible(true).setAnimationImageIndex([this.multiplier]);
  1044. this.actorx.setVisible(true);
  1045. this.emptyBehaviorList();
  1046. this.addBehavior(
  1047. new CAAT.ScaleBehavior().
  1048. setFrameTime(this.time,1000).
  1049. setValues(.9, 1.1, .9, 1.1 ).
  1050. setPingPong().
  1051. setCycle(true));
  1052. this.b1(this.actorx);
  1053. this.b1(this.actornum);
  1054. } else {
  1055. this.emptyBehaviorList();
  1056. this.b2(this.actorx);
  1057. this.b2(this.actornum);
  1058. }
  1059. } else if ( event.event=='status') {
  1060. if ( event.params==HN.Context.prototype.ST_ENDGAME ) {
  1061. this.hideMultiplier();
  1062. }
  1063. }
  1064. }
  1065. }
  1066. };
  1067. extend( HN.MultiplierActor, CAAT.ActorContainer);
  1068. })();
  1069. (function() {
  1070. HN.MultiplierActorS= function() {
  1071. HN.MultiplierActorS.superclass.constructor.call(this);
  1072. this.star= new CAAT.Actor();
  1073. this.container= new CAAT.ActorContainer();
  1074. this.actorx= new CAAT.Actor();
  1075. this.actornum= new CAAT.Actor();
  1076. this.addChild(this.star);
  1077. this.addChild(this.container);
  1078. this.container.addChild(this.actorx);
  1079. this.container.addChild(this.actornum);
  1080. this.container.setGlobalAlpha(true);
  1081. return this;
  1082. };
  1083. HN.MultiplierActorS.prototype= {
  1084. actorx: null,
  1085. actornum: null,
  1086. star: null,
  1087. multiplier: 0,
  1088. setImages : function( font, x, star, scene ) {
  1089. this.scene= scene;
  1090. this.setOutOfFrameTime();
  1091. this.star.setBackgroundImage(star);
  1092. var S= this.star.width/2;
  1093. this.actorx.setBackgroundImage(x,false).
  1094. setBounds(0, (this.star.height-S)/2, S, S).
  1095. setImageTransformation( CAAT.SpriteImage.prototype.TR_FIXED_TO_SIZE );
  1096. this.actornum.setBackgroundImage(font,true).
  1097. setScale( .4,.4 ).
  1098. setLocation( 0, -20 );
  1099. this.setSize( this.star.width, this.star.height);
  1100. this.container.setSize( this.star.width, this.star.height);
  1101. this.container.setRotation( Math.PI/16 );
  1102. this.star.setLocation( 0,0 );
  1103. return this;
  1104. },
  1105. hideMultiplier : function() {
  1106. this.multiplier=0;
  1107. this.setOutOfFrameTime();
  1108. },
  1109. b1 : function(actor) {
  1110. actor.emptyBehaviorList();
  1111. var cb= new CAAT.ContainerBehavior().
  1112. setFrameTime(this.scene.time,1000).
  1113. setCycle(true);
  1114. var ab= new CAAT.AlphaBehavior().
  1115. setFrameTime(0,1000).
  1116. setValues(.8,1).
  1117. setPingPong();
  1118. var sb= new CAAT.ScaleBehavior().
  1119. setFrameTime(0,1000).
  1120. setValues(.8, 1, .8, 1).
  1121. setPingPong();
  1122. cb.addBehavior(ab);
  1123. cb.addBehavior(sb);
  1124. actor.addBehavior(cb);
  1125. },
  1126. b2 : function(actor) {
  1127. var me= this;
  1128. actor.emptyBehaviorList();
  1129. var ab= new CAAT.AlphaBehavior().
  1130. setFrameTime(this.time,300).
  1131. setValues( this.alpha, 0 ).
  1132. addListener( {
  1133. behaviorExpired : function(behavior, time, actor) {
  1134. me.hideMultiplier();
  1135. },
  1136. behaviorApplied : function(actor,time,normalizedTime,value) {}
  1137. });
  1138. actor.addBehavior(ab);
  1139. },
  1140. contextEvent : function( event ) {
  1141. if ( event.source == 'context' ) {
  1142. if ( event.event=='multiplier' ) {
  1143. if ( event.params.multiplier>1 ) {
  1144. this.multiplier = event.params.multiplier;
  1145. this.actornum.setAnimationImageIndex([this.multiplier]);
  1146. this.setFrameTime(0, Number.MAX_VALUE);
  1147. this.emptyBehaviorList();
  1148. this.star.addBehavior(
  1149. new CAAT.RotateBehavior().
  1150. setFrameTime(this.time,1000).
  1151. setValues(0, Math.PI*2 ).
  1152. setCycle(true));
  1153. this.b1(this.container);
  1154. } else {
  1155. this.emptyBehaviorList();
  1156. this.b2(this.container);
  1157. }
  1158. } else if ( event.event=='status') {
  1159. if ( event.params==HN.Context.prototype.ST_ENDGAME ) {
  1160. this.hideMultiplier();
  1161. }
  1162. }
  1163. }
  1164. }
  1165. };
  1166. extend( HN.MultiplierActorS, CAAT.ActorContainer);
  1167. })();
  1168. (function() {
  1169. HN.GameScene= function() {
  1170. this.gameListener= [];
  1171. return this;
  1172. };
  1173. HN.GameScene.prototype= {
  1174. gameRows: 15,
  1175. gameColumns: 20,
  1176. context: null,
  1177. directorScene: null,
  1178. selectionPath: null,
  1179. bricksContainer: null,
  1180. brickActors: null,
  1181. particleContainer: null,
  1182. selectionStarCache: null,
  1183. fallingStarCache: null,
  1184. brickWidth: 0,
  1185. brickHeight: 0,
  1186. buttonImage: null,
  1187. starsImage: null,
  1188. numbersImage: null,
  1189. numbersImageSmall: null,
  1190. levelActor: null,
  1191. chronoActor: null,
  1192. timer: null,
  1193. scrollTimer: null,
  1194. scoreActor: null,
  1195. respawnClock: null,
  1196. scoreActorEG: null,
  1197. levelActorEG: null,
  1198. endGameActor: null,
  1199. endLevelActor: null,
  1200. endLevelMessage: null,
  1201. director: null,
  1202. actorInitializationCount: 0, // flag indicating how many actors have finished initializing.
  1203. backgroundContainer: null,
  1204. music: null,
  1205. sound: null,
  1206. gameListener: null,
  1207. /**
  1208. * Creates the main game Scene.
  1209. * @param director a CAAT.Director instance.
  1210. */
  1211. create : function(director, gameMode ) {
  1212. var me= this;
  1213. var i,j;
  1214. this.director= director;
  1215. this.bricksImageAll= new CAAT.SpriteImage().initialize(
  1216. director.getImage('bricks'), 9, 10 );
  1217. this.brickWidth= this.bricksImageAll.singleWidth;
  1218. this.brickHeight= this.bricksImageAll.singleHeight;
  1219. this.buttonImage= new CAAT.SpriteImage().initialize(
  1220. director.getImage('buttons'), 7,3 );
  1221. this.starsImage= new CAAT.SpriteImage().initialize(
  1222. director.getImage('stars'), 24,6 );
  1223. this.numbersImage= new CAAT.SpriteImage().initialize(
  1224. director.getImage('numbers'), 1,10 );
  1225. this.numbersImageSmall= new CAAT.SpriteImage().initialize(
  1226. director.getImage('numberssmall'), 1,10 );
  1227. this.context= new HN.Context().
  1228. create( 8,8, 9 ).
  1229. addContextListener(this);
  1230. this.gameRows= this.context.rows;
  1231. this.gameColumns= this.context.columns;
  1232. this.directorScene= director.createScene();
  1233. this.directorScene.activated= function() {
  1234. // cada vez que la escena se prepare para empezar partida, inicializar el contexto.
  1235. //me.context.initialize();
  1236. me.context.setGameMode(me.gameMode);
  1237. me.prepareSound();
  1238. };
  1239. var dw= director.width;
  1240. var dh= director.height;
  1241. //////////////////////// animated background
  1242. this.backgroundContainer= new HN.AnimatedBackground().
  1243. setBounds(0,0,dw,dh).
  1244. setBackgroundImage( director.getImage('background-1') ).
  1245. setInitialOffset( -director.getImage('background-1').height+dh ).
  1246. setData(this.directorScene, this.context);
  1247. this.directorScene.addChild( this.backgroundContainer );
  1248. this.context.addContextListener(this.backgroundContainer);
  1249. this.brickActors= [];
  1250. ///////////// some clouds
  1251. for( i=0; i<4; i++ ) {
  1252. var cl= new HN.BackgroundImage().setupBehavior(director,true);
  1253. this.directorScene.addChild(cl);
  1254. }
  1255. //////////////////////// Number Bricks
  1256. this.bricksContainer= new CAAT.ActorContainer().
  1257. create().
  1258. setSize(
  1259. this.gameColumns*this.brickWidth,
  1260. this.gameRows*this.brickHeight )
  1261. ;
  1262. var TT= 15;
  1263. var bricksCY= dw>dh ?
  1264. 10:
  1265. (dh-this.brickHeight*this.gameRows) - 105; // vertically set space for banner.
  1266. var bricksCX= dw>dh ?
  1267. bricksCY + TT :
  1268. (dw - (this.gameColumns * this.brickWidth )) / 2; // center horizontally
  1269. this.bricksContainer.setLocation( bricksCX, bricksCY );
  1270. this.directorScene.addChild(this.bricksContainer);
  1271. for( i=0; i<this.gameRows; i++ ) {
  1272. this.brickActors.push([]);
  1273. for( j=0; j<this.gameColumns; j++ ) {
  1274. var brick= new HN.BrickActor().
  1275. initialize( this.bricksImageAll, this.context.getBrick(i,j) ).
  1276. setLocation(-100,-100);
  1277. this.brickActors[i].push( brick );
  1278. this.bricksContainer.addChild(brick);
  1279. }
  1280. }
  1281. var HH=55;
  1282. var WW=10;
  1283. /////////////////////// game indicators.
  1284. var controls= new CAAT.ActorContainer();
  1285. if ( dw>dh ) {
  1286. controls.setBounds(
  1287. this.bricksContainer.x + this.bricksContainer.width + bricksCX + 10 ,
  1288. -15,
  1289. dw - bricksCX - (this.bricksContainer.x + this.bricksContainer.width) - bricksCX,
  1290. this.director.height-40 );
  1291. } else {
  1292. controls.setBounds(0,0,dw, this.bricksContainer.y-5);
  1293. }
  1294. this.directorScene.addChild( controls );
  1295. /////////////////////// initialize button
  1296. var restart= new CAAT.Actor();
  1297. if ( dw>dh ) {
  1298. restart.setAsButton( this.buttonImage.getRef(), 9,10,11,9,
  1299. function(button) {
  1300. me.context.timeUp();
  1301. }).
  1302. setLocation(
  1303. (controls.width-this.buttonImage.singleWidth)/2 - 15,
  1304. controls.height - this.buttonImage.singleHeight - 15 );
  1305. } else {
  1306. restart.
  1307. setAsButton(
  1308. new CAAT.SpriteImage().initialize(director.getImage('boton-salir'), 1, 3),
  1309. 0,2,0,0,
  1310. function(button) {
  1311. me.context.timeUp();
  1312. }).
  1313. setLocation( 0,0 );
  1314. }
  1315. restart.contextEvent= function(event) {
  1316. if ( event.source=='context' ) {
  1317. if ( event.event=='levelchange') {
  1318. restart.enableEvents(true);
  1319. } else if ( event.event=='status') {
  1320. if ( event.params==HN.Context.prototype.ST_STARTGAME ) {
  1321. restart.enableEvents(true);
  1322. } else if ( event.params==HN.Context.prototype.ST_ENDGAME || event.params==HN.Context.prototype.ST_LEVEL_RESULT ) {
  1323. restart.enableEvents(false);
  1324. }
  1325. }
  1326. }
  1327. };
  1328. this.context.addContextListener(restart);
  1329. controls.addChild(restart);
  1330. ///////////////////// Guess Number
  1331. var guess= new HN.GuessNumberActor().
  1332. setNumbersImage( this.numbersImage, director.getImage('target-number') );
  1333. if ( dw>dh ) {
  1334. guess.setLocation( -15, 20, controls.width, 70 );
  1335. } else {
  1336. guess.setLocation( WW, HH, controls.width, 70 );
  1337. }
  1338. this.context.addContextListener(guess);
  1339. controls.addChild(guess);
  1340. ///////////////////// score
  1341. this.scoreActor= new HN.ScoreActor().
  1342. initialize( this.numbersImageSmall, director.getImage('points') );
  1343. if ( dw>dh ) {
  1344. this.scoreActor.setLocation( 0, 250 );
  1345. } else {
  1346. this.scoreActor.setLocation( dw-this.scoreActor.width-WW, HH );
  1347. }
  1348. controls.addChild( this.scoreActor );
  1349. this.context.addContextListener(this.scoreActor);
  1350. ///////////////////// chronometer
  1351. this.chronoActor= new HN.Chrono().
  1352. setImages( director.getImage('time'), director.getImage('timeprogress'));
  1353. if ( dw>dh ) {
  1354. this.chronoActor.setLocation( 0, 310 );
  1355. } else {
  1356. this.chronoActor.setLocation( dw-this.chronoActor.width-WW, guess.y+guess.height-this.chronoActor.height-15 );
  1357. }
  1358. this.context.addContextListener(this.chronoActor);
  1359. controls.addChild(this.chronoActor);
  1360. ///////////////////// Level indicator
  1361. this.levelActor= new HN.LevelActor().
  1362. initialize( this.numbersImageSmall, dw>dh ? director.getImage('level') : director.getImage('level-small') );
  1363. if ( dw>dh ) {
  1364. this.levelActor.setLocation( 0, 170 );
  1365. } else {
  1366. this.levelActor.setLocation(
  1367. // espacio entre target number y score actor
  1368. ((this.scoreActor.x- (guess.x+guess.width)) - this.levelActor.width)/2 + (guess.x+guess.width),
  1369. guess.y+guess.height - this.levelActor.height - 10 );
  1370. }
  1371. this.context.addContextListener(this.levelActor);
  1372. controls.addChild(this.levelActor);
  1373. ////////////////////// Multiplier
  1374. var multiplier;
  1375. var star= director.getImage('multiplier-star');
  1376. multiplier= new HN.MultiplierActorS().
  1377. setLocation(
  1378. this.scoreActor.x + this.scoreActor.width - star.width*.8,
  1379. this.scoreActor.y - star.height*.3 ).
  1380. setImages( this.numbersImage, director.getImage('multiplier'), star, this.directorScene );
  1381. controls.addChild( multiplier );
  1382. this.context.addContextListener(multiplier);
  1383. var sitelogo= new CAAT.Actor().
  1384. setBackgroundImage( director.getImage('sitelogo'), true).
  1385. enableEvents(true);
  1386. sitelogo.setLocation( (director.width-sitelogo.width)/2, 10 );
  1387. this.directorScene.addChild(sitelogo);
  1388. sitelogo.mouseClick= function( e ) {
  1389. CreateLinksInGame('Math-Plus','game','logo');
  1390. };
  1391. /////////////////////// particle container
  1392. // just to accelerate events delivery
  1393. if ( !HN.LOWQUALITY ) {
  1394. this.particleContainer= new CAAT.ActorContainer().
  1395. create().
  1396. //setBounds(0,0,dw,dh).
  1397. setBounds( this.bricksContainer.x, this.bricksContainer.y, dw, dh ).
  1398. enableEvents(false);
  1399. this.directorScene.addChild( this.particleContainer );
  1400. this.create_cache();
  1401. }
  1402. /////////////////////// initialize selection path
  1403. /// create this one as the last actor so when gl active, no extra drawtris call needed.
  1404. this.selectionPath= new HN.SelectionPath(director).
  1405. setBounds(
  1406. this.bricksContainer.x,
  1407. this.bricksContainer.y,
  1408. this.gameColumns*this.brickWidth,
  1409. this.gameRows*this.brickHeight);
  1410. this.selectionPath.enableEvents(false);
  1411. this.directorScene.addChild(this.selectionPath);
  1412. this.context.addContextListener(this.selectionPath);
  1413. ////////////// respawn
  1414. this.create_respawntimer(director);
  1415. if ( dw>dh ) {
  1416. this.respawnClock.setLocation(2,2);
  1417. } else {
  1418. this.respawnClock.setLocation(
  1419. this.levelActor.x + (this.levelActor.width - this.respawnClock.width)/2,
  1420. this.levelActor.y - this.respawnClock.height );
  1421. }
  1422. ////////////////////////////////////////////////
  1423. this.create_EndGame(director);
  1424. this.create_EndLevel(director);
  1425. this.soundControls( director );
  1426. return this;
  1427. },
  1428. createCachedStar : function(director) {
  1429. var iindex= (Math.random()*this.starsImage.columns)>>0;
  1430. var actor= new CAAT.Actor();
  1431. actor.__imageIndex= iindex;
  1432. actor.
  1433. setBackgroundImage( this.starsImage.getRef().setAnimationImageIndex( [iindex] ), true ).
  1434. enableEvents(false).
  1435. setDiscardable(true).
  1436. setOutOfFrameTime().
  1437. addBehavior(
  1438. this.director.getRenderType()==='CSS' ?
  1439. new CAAT.AlphaBehavior().
  1440. setValues( 1, .1 ).
  1441. setId('__alpha'):
  1442. new CAAT.GenericBehavior().
  1443. setValues( 1, .1, null, null, function(value,target,actor) {
  1444. actor.backgroundImage.setAnimationImageIndex( [
  1445. actor.__imageIndex+(23-((23*value)>>0))*actor.backgroundImage.getColumns()
  1446. ] );
  1447. }).
  1448. setId('__alpha') );
  1449. return actor;
  1450. },
  1451. create_cache: function() {
  1452. this.selectionStarCache= [];
  1453. this.fallingStarCache= [];
  1454. var i,actor,me;
  1455. me= this;
  1456. for( i=0; i<16*4; i++ ) {
  1457. actor= this.createSelectionStarCache();
  1458. actor.addListener( {
  1459. actorLifeCycleEvent : function(actor, event, time) {
  1460. if (event === 'destroyed') {
  1461. me.selectionStarCache.push(actor);
  1462. }
  1463. }
  1464. });
  1465. actor.__parent= this.particleContainer;
  1466. this.selectionStarCache.push(actor);
  1467. }
  1468. for( i=0; i<384; i++ ) {
  1469. var actor= this.createCachedStar();
  1470. actor.addListener( {
  1471. actorLifeCycleEvent : function(actor, event, time) {
  1472. if (event === 'destroyed') {
  1473. me.fallingStarCache.push(actor);
  1474. }
  1475. }
  1476. });
  1477. actor.__parent= this.particleContainer;
  1478. this.fallingStarCache.push(actor);
  1479. }
  1480. },
  1481. createSelectionStarCache : function() {
  1482. var actor= this.createCachedStar();
  1483. var trb=
  1484. new CAAT.PathBehavior().
  1485. setFrameTime(this.directorScene.time,0).
  1486. setPath(
  1487. new CAAT.LinearPath().
  1488. setInitialPosition(0,0).
  1489. setFinalPosition(0,0)
  1490. ).
  1491. setInterpolator(
  1492. new CAAT.Interpolator().createExponentialOutInterpolator(
  1493. 2,
  1494. false)
  1495. );
  1496. actor.__trb= trb;
  1497. actor.addBehavior(trb);
  1498. var ab= null;
  1499. if ( this.director.getRenderType()==='CSS' ) {
  1500. ab= new CAAT.AlphaBehavior().setValues( 1, .1 );
  1501. } else {
  1502. ab= new CAAT.GenericBehavior().
  1503. setValues( 1, .1, null, null, function(value,target,actor) {
  1504. actor.backgroundImage.setAnimationImageIndex( [
  1505. actor.__imageIndex+(23-((23*value)>>0))*actor.backgroundImage.getColumns()
  1506. ] );
  1507. });
  1508. }
  1509. actor.__ab= ab;
  1510. actor.addBehavior(ab);
  1511. return actor;
  1512. },
  1513. create_respawntimer: function(director) {
  1514. var clock= new HN.RespawnClockActor().create().initialize(director, this.directorScene, this.context);
  1515. this.directorScene.addChild( clock );
  1516. this.context.addContextListener(clock);
  1517. this.respawnClock= clock;
  1518. this.respawnClock.setOutOfFrameTime();
  1519. },
  1520. create_EndLevel : function( director ) {
  1521. this.endLevelActor= new CAAT.ActorContainer().
  1522. setBackgroundImage( director.getImage('levelclear'), true);
  1523. var me= this;
  1524. var me_endLevel= this.endLevelActor;
  1525. var continueButton= new CAAT.Actor().
  1526. setAsButton( this.buttonImage.getRef(), 12,13,14,12,
  1527. function(button) {
  1528. director.audioPlay('11');
  1529. me.removeGameEvent( me.endLevelActor, function() {
  1530. me.context.nextLevel();
  1531. });
  1532. });
  1533. continueButton.setLocation(
  1534. (this.endLevelActor.width-continueButton.width)/2,
  1535. this.endLevelActor.height-continueButton.height-50 );
  1536. this.endLevelMessage= new CAAT.Actor().
  1537. setBackgroundImage( director.getImage('msg1'), true );
  1538. this.endLevelActor.addChild(continueButton);
  1539. this.endLevelActor.addChild(this.endLevelMessage);
  1540. this.endLevelActor.setOutOfFrameTime();
  1541. this.directorScene.addChild(this.endLevelActor);
  1542. },
  1543. create_EndGame : function(director, go_to_menu_callback ) {
  1544. var me= this;
  1545. this.endGameActor= new CAAT.ActorContainer().
  1546. setBackgroundImage( director.getImage('background_op'), true );
  1547. var menu= new CAAT.Actor().
  1548. setAsButton( this.buttonImage.getRef(), 0,1,2,0,
  1549. function(button) {
  1550. director.audioPlay('11');
  1551. me.endGameActor.enableEvents(false);
  1552. var a0;
  1553. var a1;
  1554. a0= CAAT.Actor.prototype.ANCHOR_BOTTOM;
  1555. a1= CAAT.Actor.prototype.ANCHOR_TOP;
  1556. director.easeInOut(
  1557. 0,
  1558. CAAT.Scene.EASE_TRANSLATE,
  1559. a0,
  1560. 1,
  1561. CAAT.Scene.EASE_TRANSLATE,
  1562. a1,
  1563. 1000,
  1564. false,
  1565. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false),
  1566. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) );
  1567. });
  1568. var me_endGame= this.endGameActor;
  1569. var restart= new CAAT.Actor().
  1570. setAsButton( this.buttonImage.getRef(), 3,4,5,3,
  1571. function(button) {
  1572. director.audioPlay('11');
  1573. me.removeGameEvent( me.endGameActor, function() {
  1574. me.prepareSceneIn(me.context.gameMode);
  1575. me.context.initialize();
  1576. })
  1577. });
  1578. var x= 45;
  1579. var y= this.endGameActor.height-35-menu.height;
  1580. menu.setLocation( x, y );
  1581. restart.setLocation( x+menu.width+10, y );
  1582. this.endGameActor.addChild(menu);
  1583. this.endGameActor.addChild(restart);
  1584. var __buttons= [ menu, restart ];
  1585. CAAT.modules.LayoutUtils.row(
  1586. this.endGameActor,
  1587. __buttons,
  1588. {
  1589. padding_left: 50,
  1590. padding_right: 50,
  1591. top: y
  1592. });
  1593. //////////////////////// info de partida
  1594. this.levelActorEG= new HN.LevelActor().
  1595. initialize(this.numbersImageSmall, director.getImage('level'));
  1596. this.levelActorEG.
  1597. setBounds(
  1598. (this.endGameActor.width-this.levelActorEG.width)/2,
  1599. 265,
  1600. this.levelActorEG.width,
  1601. this.levelActorEG.height );
  1602. this.endGameActor.addChild(this.levelActorEG);
  1603. this.context.addContextListener(this.levelActorEG);
  1604. console.log( this.levelActorEG);
  1605. ///////////////////// score
  1606. this.scoreActorEG= new HN.ScoreActor().
  1607. create().
  1608. setBounds(
  1609. ((this.endGameActor.width-this.scoreActor.width)/2)|0,
  1610. 335,
  1611. this.scoreActor.width,
  1612. this.scoreActor.height ).
  1613. initialize( this.numbersImageSmall, director.getImage('points') );
  1614. this.endGameActor.addChild( this.scoreActorEG );
  1615. this.context.addContextListener(this.scoreActorEG);
  1616. this.endGameActor.setOutOfFrameTime();
  1617. this.directorScene.addChild(this.endGameActor); console.log(this.scoreActorEG);
  1618. },
  1619. getBrickPosition : function(row,column) {
  1620. return {
  1621. x: (this.context.columns-this.context.currentColumns)/2*this.brickWidth+ column*this.brickWidth,
  1622. y: (this.context.rows-this.context.currentRows)/2*this.brickHeight+ row*this.brickHeight
  1623. };
  1624. },
  1625. uninitializeActors : function() {
  1626. this.selectionPath.initialize();
  1627. var i, j;
  1628. var radius= Math.max(this.director.width,this.director.height );
  1629. var angle= Math.PI*2*Math.random();
  1630. var me= this;
  1631. var p0= Math.random()*this.director.width;
  1632. var p1= Math.random()*this.director.height;
  1633. var p2= Math.random()*this.director.width;
  1634. var p3= Math.random()*this.director.height;
  1635. for( i=0; i<this.gameRows; i++ ) {
  1636. for( j=0; j<this.gameColumns; j++ ) {
  1637. var brickActor= this.brickActors[i][j];
  1638. if ( brickActor.brick.removed ) {
  1639. continue;
  1640. }
  1641. var random= Math.random()*1000;
  1642. brickActor.pb.
  1643. emptyListenerList().
  1644. setPath(
  1645. new CAAT.CurvePath().
  1646. setCubic(
  1647. brickActor.x, brickActor.y,
  1648. p0, p1,
  1649. p2, p3,
  1650. radius/2 + Math.cos(angle)*radius,
  1651. radius/2 + Math.sin(angle)*radius
  1652. )
  1653. ).
  1654. setInterpolator(
  1655. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) );
  1656. if (!HN.LOWQUALITY) {
  1657. brickActor.pb.
  1658. setFrameTime(this.directorScene.time, 1000+random );
  1659. } else {
  1660. brickActor.pb.
  1661. setFrameTime(this.directorScene.time + i*200, 700+random/3 );
  1662. }
  1663. if (!HN.LOWQUALITY) {
  1664. brickActor.sb.
  1665. emptyListenerList().
  1666. setFrameTime(this.directorScene.time , 1000+random).
  1667. setValues( 1, .1, 1 , .1).
  1668. setInterpolator(
  1669. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) );
  1670. }
  1671. brickActor.
  1672. enableEvents(false).
  1673. setAlpha(1).
  1674. resetTransform();
  1675. }
  1676. }
  1677. },
  1678. initializeActors : function() {
  1679. this.selectionPath.initialize();
  1680. var i, j;
  1681. var maxt= 0;
  1682. var animationDuration;
  1683. var animationStartTime;
  1684. var stepTime= 30;
  1685. var brickPosition;
  1686. var context;
  1687. var radius= Math.max(this.director.width,this.director.height );
  1688. var angle= Math.PI*2*Math.random();
  1689. var me= this;
  1690. var p0= Math.random()*this.director.width;
  1691. var p1= Math.random()*this.director.height;
  1692. var p2= Math.random()*this.director.width;
  1693. var p3= Math.random()*this.director.height;
  1694. for( i=0; i<this.gameRows; i++ ) {
  1695. for( j=0; j<this.gameColumns; j++ ) {
  1696. var brickActor= this.brickActors[i][j];
  1697. if ( brickActor.brick.removed ) {
  1698. brickActor.setOutOfFrameTime();
  1699. } else {
  1700. brickActor.
  1701. setFrameTime( this.directorScene.time, Number.MAX_VALUE ).
  1702. setAlpha(1).
  1703. enableEvents(true).
  1704. reset();
  1705. var random= (Math.random()*1000)>>0;
  1706. var brickPosition= this.getBrickPosition(i,j);
  1707. brickActor.pb.
  1708. emptyListenerList().
  1709. setPath(
  1710. new CAAT.CurvePath().
  1711. setCubic(
  1712. radius/2 + Math.cos(angle)*radius,
  1713. radius/2 + Math.sin(angle)*radius,
  1714. p0, p1, p2, p3,
  1715. brickPosition.x,
  1716. brickPosition.y)
  1717. ).
  1718. setInterpolator(
  1719. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) ).
  1720. setFrameTime(this.directorScene.time, 1000+random);
  1721. brickActor.sb.
  1722. emptyListenerList().
  1723. setValues( .1, 1, .1 , 1).
  1724. setInterpolator(
  1725. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) ).
  1726. setFrameTime(this.directorScene.time , 1000+random);
  1727. brickActor.
  1728. enableEvents(false);
  1729. var actorCount=0;
  1730. brickActor.pb.addListener( {
  1731. behaviorExpired : function( behavior, time, actor ) {
  1732. actorCount++;
  1733. if ( actorCount==me.context.getLevelActiveBricks() ) {
  1734. brickActor.pb.emptyListenerList();
  1735. if ( me.context.status==me.context.ST_INITIALIZING ) {
  1736. me.context.setStatus( me.context.ST_RUNNNING );
  1737. }
  1738. }
  1739. }
  1740. });
  1741. }
  1742. }
  1743. }
  1744. context= this.context;
  1745. this.directorScene.createTimer(
  1746. this.directorScene.time,
  1747. maxt,
  1748. function timeout() {
  1749. if ( context.status==context.ST_INITIALIZING ) {
  1750. context.setStatus( context.ST_RUNNNING );
  1751. }
  1752. }
  1753. );
  1754. this.actorInitializationCount=0;
  1755. },
  1756. contextEvent : function( event ) {
  1757. var i, j;
  1758. var brickActor;
  1759. var me= this;
  1760. if ( event.source=='context' ) {
  1761. if ( event.event=='levelchange') {
  1762. this.bricksContainer.enableEvents(true);
  1763. } else if ( event.event=='status') {
  1764. if ( event.params==this.context.ST_INITIALIZING ) {
  1765. this.director.audioPlay( 'mostrarpanel' );
  1766. this.initializeActors();
  1767. } else if ( event.params==this.context.ST_RUNNNING) {
  1768. for( i=0; i<this.gameRows; i++ ) {
  1769. for( j=0; j<this.gameColumns; j++ ) {
  1770. brickActor= this.brickActors[i][j].set();
  1771. }
  1772. }
  1773. this.cancelTimer();
  1774. this.enableTimer();
  1775. } else if ( event.params==this.context.ST_LEVEL_RESULT ) {
  1776. this.director.audioPlay('10');
  1777. this.cancelTimer();
  1778. var me= this;
  1779. // wait 1 sec
  1780. var timer= this.directorScene.createTimer(
  1781. this.directorScene.time,
  1782. 1000,
  1783. function (sceneTime, time, timerTask) {
  1784. me.endLevel();
  1785. },
  1786. null
  1787. );
  1788. } else if ( event.params==this.context.ST_ENDGAME ) {
  1789. this.director.audioPlay('01');
  1790. this.endGame();
  1791. }
  1792. }
  1793. } else if ( event.source==='brick' ) {
  1794. if ( event.event==='selection' ) { // des/marcar un elemento.
  1795. this.director.audioPlay( event.params.selected ? '11' : 'deseleccionar' );
  1796. this.brickSelectionEvent(event);
  1797. } else if ( event.event==='selectionoverflow') { // seleccion error.
  1798. this.director.audioPlay( 'sumamal' );
  1799. this.selectionOverflowEvent(event);
  1800. } else if ( event.event==='selection-cleared') {
  1801. CAAT.setCursor('default');
  1802. this.director.audioPlay('12');
  1803. this.selectionClearedEvent(event);
  1804. } else if ( event.event==='rearranged' ) {
  1805. this.rearrange( event );
  1806. } else if ( event.event==='respawn' ) {
  1807. this.respawn(event);
  1808. }
  1809. // rebuild selection path
  1810. this.selectionPath.setup(
  1811. this.context,
  1812. this.brickActors);
  1813. }
  1814. },
  1815. respawn : function(event) {
  1816. var respawnData= event.params;
  1817. for( var i=0; i<respawnData.length; i++ ) {
  1818. var row= respawnData[i].row;
  1819. var col= respawnData[i].column;
  1820. var brickPos= this.getBrickPosition(row,col);
  1821. this.brickActors[row][col].respawn(brickPos.x, brickPos.y);
  1822. }
  1823. },
  1824. rearrange : function(event) {
  1825. var fromRow= event.params.fromRow;
  1826. var toRow= event.params.toRow;
  1827. var column= event.params.column;
  1828. var tmp= this.brickActors[fromRow][column];
  1829. this.brickActors[fromRow][column]= this.brickActors[toRow][column];
  1830. this.brickActors[toRow][column]= tmp;
  1831. // el actor de posicion row, column, no tiene que ser el que esperabamos
  1832. // porque se ha reorganizado la cuadricula del modelo.
  1833. var brickActor= this.brickActors[toRow][column];
  1834. var brickPos= this.getBrickPosition(toRow,column);
  1835. brickActor.rearrange( brickPos.x, brickPos.y );
  1836. },
  1837. brickSelectionEvent : function(event) {
  1838. var me= this;
  1839. var brick= event.params;
  1840. var brickActor= this.brickActors[brick.row][brick.column];
  1841. if ( brick.selected ) {
  1842. if ( !HN.LOWQUALITY ) {
  1843. // dibujar un grupo de estrellas desde el centro del ladrillo haciendo fade-out.
  1844. var x0= brickActor.x+brickActor.width/2-this.starsImage.singleWidth/2;
  1845. var y0= brickActor.y+brickActor.height/2-this.starsImage.singleHeight/2;
  1846. var R= Math.sqrt( brickActor.width*brickActor.width + brickActor.height*brickActor.height )/2;
  1847. var N= 16;
  1848. var i;
  1849. var rad= Math.PI/N*2;
  1850. var T= 300;
  1851. for( i=0; i<N; i++ ) {
  1852. var x1= x0+ R*Math.cos( i*rad );
  1853. var y1= y0+ R*Math.sin( i*rad );
  1854. if ( this.selectionStarCache.length ) {
  1855. var actor= this.selectionStarCache.shift();
  1856. actor.setFrameTime(this.directorScene.time, T);
  1857. actor.backgroundImage.setAnimationImageIndex( [(Math.random()*6)>>0] );
  1858. actor.__trb.setFrameTime(this.directorScene.time, T);
  1859. actor.__trb.path.setInitialPosition(x0,y0).setFinalPosition(x1,y1);
  1860. actor.__ab.setFrameTime(this.directorScene.time, T);
  1861. actor.__parent.addChild(actor);
  1862. }
  1863. }
  1864. }
  1865. brickActor.setSelected();
  1866. }
  1867. else {
  1868. brickActor.reset();
  1869. }
  1870. },
  1871. selectionOverflowEvent : function(event) {
  1872. var i,j;
  1873. var selectedContextBricks= event.params;
  1874. var actor;
  1875. for( i=0; i<selectedContextBricks.length; i++ ) {
  1876. this.brickActors[ selectedContextBricks[i].row ][ selectedContextBricks[i].column ].reset();
  1877. }
  1878. this.bricksContainer.enableEvents(false);
  1879. // get all active actors on board
  1880. var activeActors= [];
  1881. for( i=0; i<this.gameRows; i++ ) {
  1882. for( j=0; j<this.gameColumns; j++ ) {
  1883. actor= this.brickActors[i][j];
  1884. if ( !actor.brick.removed ) {
  1885. activeActors.push(actor);
  1886. }
  1887. }
  1888. }
  1889. var me= this;
  1890. // for each active actor, play a wrong-path.
  1891. for( i=0; i<activeActors.length; i++ ) {
  1892. activeActors[i].selectionOverflow();
  1893. }
  1894. var ttimer;
  1895. ttimer= this.directorScene.createTimer(
  1896. this.directorScene.time,
  1897. HN.BrickActor.prototype.timeOverflow+100,
  1898. function timeout(sceneTime, time, timerTask) {
  1899. me.bricksContainer.enableEvents(true);
  1900. },
  1901. function tick(sceneTime, time, timerTask) {
  1902. });
  1903. },
  1904. selectionClearedEvent : function(event) {
  1905. var selectedContextBricks= event.params;
  1906. var i;
  1907. for( i=0; i<selectedContextBricks.length; i++ ) {
  1908. var actor= this.brickActors[ selectedContextBricks[i].row ][ selectedContextBricks[i].column ];
  1909. actor.parent.setZOrder(actor,Number.MAX_VALUE);
  1910. actor.selectionCleared(this, this.director.height);
  1911. }
  1912. this.timer.reset(this.directorScene.time);
  1913. },
  1914. showLevelInfo : function() {
  1915. },
  1916. prepareSceneIn : function(gameMode) {
  1917. // setup de actores
  1918. var i,j;
  1919. this.gameMode= gameMode;
  1920. if ( this.gameMode.respawn ) {
  1921. this.respawnClock.enable(true,this.gameMode.respawn_time);
  1922. } else {
  1923. this.respawnClock.enable(false,this.gameMode.respawn_time);
  1924. }
  1925. //this.context.initialize()
  1926. this.bricksContainer.enableEvents(true);
  1927. // fuera de pantalla
  1928. for( i=0; i<this.gameRows; i++ ) {
  1929. for( j=0; j<this.gameColumns; j++ ) {
  1930. this.brickActors[i][j].setLocation(-100,-100);
  1931. }
  1932. }
  1933. this.selectionPath.initialize();
  1934. this.chronoActor.tick(0,0);
  1935. this.scoreActor.reset();
  1936. this.endGameActor.setFrameTime(-1,0);
  1937. },
  1938. endGame : function() {
  1939. this.fireEvent( 'end-game', {
  1940. score: this.context.score,
  1941. level: this.context.level,
  1942. gameMode: this.context.gameMode.name
  1943. })
  1944. this.showGameEvent( this.endGameActor );
  1945. },
  1946. addGameListener : function(gameListener) {
  1947. this.gameListener.push(gameListener);
  1948. },
  1949. fireEvent : function( type, data ) {
  1950. for( var i=0, l= this.gameListener.length; i<l; i++ ) {
  1951. this.gameListener[i].gameEvent(type, data);
  1952. }
  1953. },
  1954. setDifficulty : function(level) {
  1955. this.context.difficulty=level;
  1956. },
  1957. cancelTimer : function(){
  1958. if ( this.timer!=null ) {
  1959. this.timer.cancel();
  1960. }
  1961. this.timer= null;
  1962. },
  1963. enableTimer : function() {
  1964. var me= this;
  1965. this.timer= this.directorScene.createTimer(
  1966. this.directorScene.time,
  1967. this.context.turnTime,
  1968. function timeout(sceneTime, time, timerTask) {
  1969. me.context.timeUp();
  1970. },
  1971. function tick(sceneTime, time, timerTask) {
  1972. me.chronoActor.tick(time, timerTask.duration);
  1973. });
  1974. },
  1975. setGameMode : function(gameMode) {
  1976. this.context.setGameMode(gameMode);
  1977. },
  1978. endLevel : function() {
  1979. var level= this.context.level;
  1980. if ( level>7 ) {
  1981. level=7;
  1982. }
  1983. var image= this.director.getImage( 'msg'+level);
  1984. if ( null!=image ) {
  1985. this.endLevelMessage.setBackgroundImage( image, true );
  1986. this.endLevelMessage.setLocation(
  1987. (this.endLevelMessage.parent.width-image.width)/2,
  1988. this.endLevelMessage.parent.height/2 - 65
  1989. );
  1990. }
  1991. this.showGameEvent( this.endLevelActor );
  1992. window.level=level;
  1993. //updateShare(window.level,window.score);
  1994. //Play68.setRankingLevelScoreDesc(window.level,window.score);
  1995. console.log('level:'+window.level+'score:'+window.score);
  1996. },
  1997. removeGameEvent : function( actor, callback ) {
  1998. actor.enableEvents(false);
  1999. this.uninitializeActors();
  2000. var me= this;
  2001. actor.emptyBehaviorList();
  2002. actor.addBehavior(
  2003. new CAAT.PathBehavior().
  2004. setFrameTime( actor.time, 2000 ).
  2005. setPath(
  2006. new CAAT.LinearPath().
  2007. setInitialPosition( actor.x, actor.y ).
  2008. setFinalPosition( actor.x, -actor.height )
  2009. ).
  2010. setInterpolator(
  2011. new CAAT.Interpolator().createExponentialInInterpolator(2,false)
  2012. ).
  2013. addListener(
  2014. {
  2015. behaviorExpired : function(behavior, time, actor) {
  2016. actor.setOutOfFrameTime();
  2017. callback();
  2018. }
  2019. }
  2020. )
  2021. );
  2022. },
  2023. showGameEvent : function(actor) {
  2024. // parar y eliminar cronometro.
  2025. this.cancelTimer();
  2026. // quitar contorl de mouse.
  2027. this.bricksContainer.enableEvents(false);
  2028. // mostrar endgameactor.
  2029. var x= (this.directorScene.width - actor.width)/2;
  2030. var y= (this.directorScene.height - actor.height)/2 - 100;
  2031. var me_endGameActor= actor;
  2032. actor.emptyBehaviorList().
  2033. setFrameTime(this.directorScene.time, Number.MAX_VALUE).
  2034. enableEvents(false).
  2035. addBehavior(
  2036. new CAAT.PathBehavior().
  2037. setFrameTime( this.directorScene.time, 2000 ).
  2038. setPath(
  2039. new CAAT.LinearPath().
  2040. setInitialPosition( x, this.directorScene.height ).
  2041. setFinalPosition( x, y ) ).
  2042. setInterpolator(
  2043. new CAAT.Interpolator().createExponentialInOutInterpolator(3,false) ).
  2044. addListener( {
  2045. behaviorExpired : function(behavior, time, actor) {
  2046. me_endGameActor.enableEvents(true);
  2047. me_endGameActor.emptyBehaviorList();
  2048. },
  2049. behaviorApplied : function(behavior, time, normalizedTime, actor, value) {
  2050. }
  2051. } )
  2052. );
  2053. },
  2054. soundControls : function(director) {
  2055. var ci= new CAAT.SpriteImage().initialize( director.getImage('sound'), 2,3 );
  2056. var dw= director.width;
  2057. var dh= director.height;
  2058. var music= new CAAT.Actor().
  2059. setAsButton( ci.getRef(),0,1,0,0, function(button) {
  2060. director.audioManager.setMusicEnabled( !director.audioManager.isMusicEnabled() );
  2061. if ( director.audioManager.isMusicEnabled() ) {
  2062. button.setButtonImageIndex(0,1,0,0);
  2063. } else {
  2064. button.setButtonImageIndex(2,2,2,2);
  2065. }
  2066. }).
  2067. setBounds( dw-ci.singleWidth-2, 2, ci.singleWidth, ci.singleHeight );
  2068. var sound= new CAAT.Actor().
  2069. setAsButton( ci.getRef(),3,4,3,3, function(button) {
  2070. director.audioManager.setSoundEffectsEnabled( !director.audioManager.isSoundEffectsEnabled() );
  2071. if ( director.audioManager.isSoundEffectsEnabled() ) {
  2072. button.setButtonImageIndex(3,4,3,3);
  2073. } else {
  2074. button.setButtonImageIndex(5,5,5,5);
  2075. }
  2076. });
  2077. if ( director.width>director.height ) {
  2078. sound.setBounds( dw-ci.singleWidth-2, 2+2+ci.singleHeight, ci.singleWidth, ci.singleHeight );
  2079. } else {
  2080. sound.setBounds( dw-ci.singleWidth*2-2, 2+2, ci.singleWidth, ci.singleHeight );
  2081. }
  2082. music.prepare= function() {
  2083. if ( director.audioManager.isMusicEnabled() ) {
  2084. this.setButtonImageIndex(0,1,0,0);
  2085. } else {
  2086. this.setButtonImageIndex(2,2,2,2);
  2087. }
  2088. }
  2089. sound.prepare= function() {
  2090. if ( director.audioManager.isSoundEffectsEnabled() ) {
  2091. this.setButtonImageIndex(3,4,3,3);
  2092. } else {
  2093. this.setButtonImageIndex(5,5,5,5);
  2094. }
  2095. }
  2096. this.directorScene.addChild(sound);
  2097. this.directorScene.addChild(music);
  2098. this.music= music;
  2099. this.sound= sound;
  2100. },
  2101. prepareSound : function() {
  2102. try {
  2103. this.sound.prepare();
  2104. this.music.prepare();
  2105. }
  2106. catch(e) {
  2107. }
  2108. }
  2109. };
  2110. })();