Advertisement
Guest User

PianoTiles

a guest
Aug 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.aadvarkstudios.babygames.musicaldrum.pianotiles
  2. {
  3.     import starling.display.DisplayObject;
  4.     import starling.display.Image;
  5.     import starling.display.Quad;
  6.     import starling.display.Sprite;
  7.     import starling.events.EnterFrameEvent;
  8.     import starling.events.Touch;
  9.     import starling.events.TouchEvent;
  10.     import starling.events.TouchPhase;
  11.     import starling.text.TextField;
  12.     import starling.text.TextFormat;
  13.     import starling.textures.Texture;
  14.     import starling.utils.Align;
  15.     import starling.utils.deg2rad;
  16.  
  17.     import com.aadvarkstudios.babygames.Config;
  18.     import com.aadvarkstudios.babygames.Root;
  19.     import com.aadvarkstudios.babygames.Screen;
  20.     import com.aadvarkstudios.babygames.musicaldrum.MusicGameSelector;
  21.     import com.aadvarkstudios.babygames.musicaldrum.NotePlayer;
  22.     import com.aadvarkstudios.babygames.musicaldrum.SongManager;
  23.     import com.aadvarkstudios.babygames.ui.SimpleBtn;
  24.     import com.aadvarkstudios.babygames.utils.DrawUtils;
  25.     import com.greensock.TweenLite;
  26.     import com.greensock.TweenMax;
  27.     import com.greensock.easing.Back;
  28.     import com.greensock.easing.Linear;
  29.  
  30.     import flash.geom.Rectangle;
  31.     import flash.system.System;
  32.     import flash.utils.setTimeout;
  33.      
  34.  
  35.     /**
  36.      * @author Administrator
  37.      */
  38.     public class PianoTiles extends Screen
  39.     {
  40.         //TRANSLATIONS
  41.         public static const START_AGAIN         : String = "startAgain";
  42.         public static const PLAY_NEXT_SONG      : String = "playNextSong";
  43.         public static const WELL_DONE           : String = "wellDone";
  44.         public static const EXCELLENT           : String = "excellent";
  45.         public static const GOOD_JOB            : String = "goodJob";
  46.        
  47.         //SFX
  48.         public static const WRONG_TILE          : String = "low_knock";
  49.         public static const ERROR_HIT           : String = "error4";
  50.         public static const WRONG_BLACK_TILE    : String = "error4";
  51.         public static const SONG_FINISHED       : String = "tada1";
  52.         public static const FIREWORK            : String = "firework";
  53.        
  54.         private var speedIncrease               : Number = .02;// on iPad
  55.         private var speed                       : Number = 8; //on iPad;
  56.         private var speedLevelAddon             : Number = 2;
  57.        
  58.         private var noteHei                     : uint;
  59.         private var noteWid                     : uint;
  60.         private var content_rect                : Rectangle;
  61.         public var songManager                  : SongManager;
  62.         private var player                      : NotePlayer;
  63.         private var notesLevel                  : Sprite;
  64.         private var blackKeys_arr               : Array;       
  65.         private var pos_Y_0                     : uint;        
  66.         private var pos_Y_1                     : uint;        
  67.         private var pos_Y_2                     : uint;        
  68.         private var pos_Y_3                     : uint;        
  69.         private var pianoNotes_arr              : Array;
  70.         private var pianoNotes_dic              : Array;
  71.         private var columPos_dic                : Array;
  72.         private var startBtn                    : SimpleBtn;
  73.         private var trackedTile                 : TileBtn;
  74.         private var notesTotal                  : uint;
  75.         private var songTitle_txt               : TextField;
  76.         private var songTitleLevel              : Sprite;
  77.         private var oldBlackTilePos             : int = -1;
  78.         private var songCompleteDialog          : SongCompleteDialog;
  79.         private var bg                          : Quad;
  80.  
  81.         public function PianoTiles()
  82.         {
  83.             super();
  84.             init();
  85.         }
  86.  
  87.         private function init() : void
  88.         {
  89.             songManager = SongManager.getInstance();
  90.             player      = NotePlayer.getInstance();
  91.             songManager.setTilesMode();
  92.            
  93.             var margin : uint = hei*0.1;
  94.             var bgHeight : uint = hei-margin*2;
  95.            
  96.             //create blcak BG for testing
  97.             bg = this.addChild(DrawUtils.rect2Quad(new Rectangle(0, 0, wid, hei), 0x000055, 1)) as Quad;
  98.             bg.addEventListener(TouchEvent.TOUCH, onBgTouched);
  99.            
  100.             this.addChild(DrawUtils.rect2Quad(new Rectangle(0, margin, wid, bgHeight), 0xEFEFEF, 1));
  101.            
  102.             notesLevel = new Sprite();
  103.             this.addChild(notesLevel);
  104.            
  105.             notesLevel.touchable = false;
  106.                
  107.             //add plank
  108.             var plankTex : Texture = assets.getTexture("plank");
  109.             var plankGFX : Image = this.addChild(new Image(plankTex)) as Image;
  110.             plankGFX.width = wid;
  111.             plankGFX.scaleY = plankGFX.scaleX;
  112.             plankGFX.y = margin;
  113.             plankGFX.color = 0x999999;
  114.            
  115.             var plankHei : uint = plankGFX.height;
  116.            
  117.             plankGFX  = this.addChild(new Image(plankTex)) as Image;
  118.             plankGFX.width = wid;  
  119.             plankGFX.height = plankHei;    
  120.             plankGFX.y = margin + bgHeight - plankHei;
  121.             plankGFX.color = 0x999999;
  122.            
  123.             var contentHei : uint = bgHeight - plankHei*2;
  124.             content_rect = new Rectangle(0,margin + plankHei,wid,contentHei);
  125.            
  126.             noteHei = content_rect.height *.25;
  127.             noteWid = wid *.2;
  128.             var lineHei : int = 1;
  129.             //create lines
  130.             pos_Y_0                     = content_rect.y;
  131.             var lineRect : Rectangle    = new Rectangle(0,0,wid,lineHei);
  132.             var line : Quad             = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
  133.             pos_Y_1                     = content_rect.y+ noteHei;
  134.             line.y                      = pos_Y_1;
  135.             //pos_Y_1+=lineHei;
  136.            
  137.             line = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
  138.             pos_Y_2                     = pos_Y_1 + noteHei;
  139.             line.y                      = pos_Y_2;
  140.             //pos_Y_2+=lineHei;
  141.            
  142.             line = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
  143.             pos_Y_3                     = pos_Y_2 + noteHei ;
  144.             line.y                      = pos_Y_3;
  145.             //pos_Y_3+=lineHei;
  146.            
  147.             columPos_dic                = [];
  148.             columPos_dic[0]             = pos_Y_0;
  149.             columPos_dic[1]             = pos_Y_1;
  150.             columPos_dic[2]             = pos_Y_2;
  151.             columPos_dic[3]             = pos_Y_3;
  152.            
  153.  
  154.  
  155.            
  156.             //init keys and positions
  157.             pianoNotes_arr = ["c1","cis1","d1","dis1","e1","f1","fis1","g1","gis1","a1","ais1","h1","c2","cis2","d2","e2","dis2"]; //17
  158.             pianoNotes_dic = [];
  159.            
  160.             for (var i : int = 0; i < pianoNotes_arr.length; i++)
  161.             {
  162.                 var noteID : String = pianoNotes_arr[i];
  163.                 pianoNotes_dic[noteID] = i;                
  164.             }
  165.             addNotes();
  166.            
  167.            
  168.            
  169.             //add start button
  170.             var startBtn_rect : Rectangle = new Rectangle(0.8*content_rect.width,content_rect.y + content_rect.height*.9, content_rect.height*.8,40*scaleFactor);
  171.             startBtn = new SimpleBtn(startBtn_rect, getTrans(Config.START), startRolling,0xFF55FF);
  172.             startBtn.rotation = deg2rad(-90);
  173.             this.addChild(startBtn);
  174.            
  175.             loadSFX();
  176.            
  177.             //song title
  178.             songTitleLevel = this.addChild(new Sprite()) as Sprite;
  179.             var title_rect : Rectangle = new Rectangle(0.56*content_rect.width,content_rect.y + content_rect.height*1, content_rect.height,96*scaleFactor);
  180.            
  181.             var  titleTextBG : Quad = songTitleLevel.addChild(DrawUtils.rect2Quad(title_rect,0xFF55FF,1)) as Quad;
  182.             titleTextBG.rotation = deg2rad(-90);
  183.             titleTextBG.touchable = false;
  184.             var offset : Number = title_rect.height *.05;
  185.  
  186.             var fmt : TextFormat = new TextFormat(Config.getInstance().titleFont, 28*scaleFactor, 0xFFFFFF,Align.CENTER,Align.CENTER);
  187.             fmt.leading = -10 * scaleFactor;
  188.             songTitle_txt = new TextField(title_rect.width-offset*2, title_rect.height-offset*2, songManager.activeSong.name, fmt);
  189.             songTitle_txt.touchable = false;
  190.             ///textField_txt.border = true;
  191.             songTitle_txt.rotation = deg2rad(-90);
  192.             songTitle_txt.x = title_rect.x + offset;
  193.             songTitle_txt.y = title_rect.y - offset;
  194.             songTitle_txt.wordWrap = true;
  195.             songTitleLevel.addChild(songTitle_txt);
  196.            
  197.            
  198.             notesLevel.addEventListener(TouchEvent.TOUCH, onTouch);
  199.  
  200.             // going back
  201.             rootInst.onBackButtonPressed = loadMusicGameSelector;
  202.             createBackArrowButton(0,0,loadMusicGameSelector);
  203.         }
  204.  
  205.  
  206.         private function onTouch(event : TouchEvent) : void
  207.         {
  208.              var touch:Touch = event.getTouch(notesLevel, TouchPhase.BEGAN);
  209.              
  210.              if (touch)
  211.              {
  212.                  var touchedObj : DisplayObject = touch.target;
  213.                  
  214.                  if (touchedObj)
  215.                  {
  216.                     if (touchedObj is TileBtn)
  217.                     {
  218.                         onTilePressed(touchedObj as TileBtn);  
  219.                     }
  220.                  }
  221.              }
  222.         }
  223.  
  224.         private function loadSFX() : void
  225.         {
  226.             assets.enqueue(appDir.resolvePath("includes/musical_drum/tiles_sfx/"));
  227.             assets.loadQueue(function (ratio : Number) : void
  228.             {
  229.                 if (ratio == 1)
  230.                 {
  231.                     System.pauseForGCIfCollectionImminent(0);
  232.                     System.gc();
  233.                     //levelWon();
  234.                 }
  235.             });
  236.         }
  237.        
  238.  
  239.         private function onTilePressed(pressedTile : TileBtn) : void
  240.         {
  241.             var isBlack : Boolean = pressedTile.isBlack;
  242.            
  243.            
  244.             var activeNoteIDX : uint = songManager.getActiveNoteIDX();
  245.            
  246.             trace("black:"+isBlack+" pressed note: "+pressedTile.noteID + " | idx"+pressedTile.noteIDX+ " | activeNoteIDX: "+activeNoteIDX);
  247.            
  248.             if (isBlack)
  249.             {
  250.                 //if (pressedTile.noteID  == trackedTile.noteID)
  251.                 if (activeNoteIDX == pressedTile.noteIDX)
  252.                 {
  253.                     pressedTile.fadeIn();
  254.                     songManager.playSongNote();
  255.                     var deletedTile : TileBtn = blackKeys_arr.pop() as TileBtn;
  256.                     trace("Deleting "+deletedTile.noteIDX+"\n");
  257.                    
  258.                     if (difficulty == Root.DIFF_LOW)
  259.                     {
  260.                         var nextX : int = notesLevel.x+noteWid;
  261.                         TweenLite.to(notesLevel,.6,{x:nextX});
  262.                     }
  263.                     else
  264.                     {
  265.                         setTrackingTile();
  266.                     }
  267.                    
  268.                     if (pressedTile.noteIDX == (notesTotal - 1))
  269.                     {
  270.                         notesLevel.touchable = false;
  271.                         setTimeout(levelWon, 500);
  272.                     }
  273.                 }
  274.                 else
  275.                 {
  276.                     playSoundFX(WRONG_BLACK_TILE);
  277.                 }
  278.             }
  279.             else
  280.             {
  281.                 pressedTile.fadeIn();
  282.                 playSoundFX(WRONG_TILE);
  283.             }
  284.         }
  285.        
  286.        
  287.         private var paused :Boolean = false;
  288.          
  289.         private function onBgTouched(event : TouchEvent) : void
  290.         {
  291.              var touch:Touch = event.getTouch(bg, TouchPhase.BEGAN);
  292.              
  293.              if (touch)
  294.              {
  295.                 paused = !paused;
  296.                 toggleRoll(paused);
  297.              }
  298.         }
  299.  
  300.        
  301.  
  302.         private function toggleRoll(pause : Boolean) : void
  303.         {
  304.             if (pause)
  305.             {
  306.                 this.addEventListener(EnterFrameEvent.ENTER_FRAME, roll);
  307.             }
  308.             else
  309.             {
  310.                 this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
  311.             }
  312.         }
  313.  
  314.         private function addNotes() : void
  315.         {
  316.             blackKeys_arr               = [];
  317.             oldBlackTilePos             = -1;
  318.             var isBlack     : Boolean   = false;
  319.             var tile        : TileBtn;
  320.             var nextNoteID  : String    = "";
  321.             var noteID      : String    = "";
  322.            
  323.             var songNotes_arr   : Array = songManager.songNotes_arr;
  324.            
  325.             var noteRect        : Rectangle = new Rectangle(0, 0, noteWid, noteHei);
  326.             var nextX           : int = -songNotes_arr.length * noteWid + noteWid*3;
  327.             var blackTileIDX    : uint = 0;
  328.             notesTotal = songNotes_arr.length;
  329.            
  330.             for (var i : int = notesTotal-1; i >= 0; i--)
  331.             {
  332.                 noteID = songNotes_arr[i];
  333.                 nextNoteID = songNotes_arr[(i+1)];
  334.  
  335.                 blackTileIDX = positionBlackTile(noteID,nextNoteID,i);
  336.  
  337.                 for (var j : int = 0; j < 4; j++)
  338.                 {
  339.                    
  340.                     noteRect.x  = nextX;
  341.                     noteRect.y  = columPos_dic[j];
  342.                     isBlack     = blackTileIDX==j;
  343.                     tile        = new TileBtn(noteID, i,noteRect, isBlack);
  344.                     notesLevel.addChild(tile);
  345.  
  346.                     if (isBlack)
  347.                     {
  348.                         blackKeys_arr.push(tile);
  349.                         trace("Adding black tile at "+i);
  350.                     }
  351.                 }
  352.                 nextX+=noteWid + 1;
  353.             }
  354.         }
  355.  
  356.         private function positionBlackTile(noteID : String, nextNoteID : String = "", i : int=0) : uint
  357.         {
  358.             if ((oldBlackTilePos == -1))
  359.             {
  360.                 // trace("random shit");
  361.                 var blackTilePos : int = Math.floor(Math.random() * 4);
  362.  
  363.                 if (blackTilePos == oldBlackTilePos)
  364.                 {
  365.                     // trace("random shit recursed");
  366.                     blackTilePos = Math.floor(Math.random() * 4);
  367.                 }
  368.             }
  369.             else
  370.             {
  371.                 if ((pianoNotes_dic[noteID] == pianoNotes_dic[nextNoteID])) // stayingOnTone
  372.                 {
  373.                     // trace("Stay")
  374.                     blackTilePos = oldBlackTilePos;
  375.                 }
  376.                 else
  377.                 {
  378.                     var goingUp : Boolean = pianoNotes_dic[noteID] < pianoNotes_dic[nextNoteID] ;
  379.                     if (goingUp)
  380.                     {
  381.                         blackTilePos = oldBlackTilePos + 1;
  382.                         if (blackTilePos == 4) blackTilePos = 0;
  383.                     }
  384.                     else
  385.                     {
  386.                         blackTilePos = oldBlackTilePos - 1;
  387.                         if (blackTilePos == -1) blackTilePos = 3;
  388.                     }
  389.                 }
  390.             }
  391.  
  392.             oldBlackTilePos = blackTilePos;
  393.  
  394.             /*switch (blackTilePos)
  395.             {
  396.                 case 3:
  397.                     trace(StringUtils.padLeft(i.toString(),"0", 2)+" |" + noteID + "|  |  |  |");
  398.                     break;
  399.                 case 2:
  400.                     trace(StringUtils.padLeft(i.toString(),"0", 2)+" |  |" + noteID + "|  |  |");
  401.                     break;
  402.                 case 1:
  403.                     trace(StringUtils.padLeft(i.toString(),"0", 2)+" |  |  |" + noteID + "|  |");
  404.                     break;
  405.                 case 0:
  406.                     trace(StringUtils.padLeft(i.toString(),"0", 2)+" |  |  |  |" + noteID + "|");
  407.                     break;
  408.                 default :
  409.                     trace("WTF ? " + blackTilePos);
  410.                     break;
  411.             }*/
  412.  
  413.             return blackTilePos;
  414.         }
  415.        
  416.        
  417.        
  418.        
  419.        
  420.  
  421.         private function levelWon() : void
  422.         {
  423.             this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
  424.             songCompleteDialog = this.addChild(new SongCompleteDialog(onPlayNextSong, onCancel)) as SongCompleteDialog;
  425.         }
  426.  
  427.         private function onPlayNextSong() : void
  428.         {
  429.             if (songCompleteDialog)
  430.             {
  431.                 if (this.contains(songCompleteDialog)) this.removeChild(songCompleteDialog);
  432.             }
  433.             TweenMax.to(startBtn,.5,{autoAlpha:1,ease:Linear.easeOut});
  434.             TweenMax.to(songTitleLevel,.2,{autoAlpha:1,ease:Linear.easeOut});
  435.             songTitle_txt.text = songManager.activeSong.name;
  436.             startBtn.touchable = true;
  437.             startBtn.text = getTrans(Config.START);
  438.             startBtn.onSelected = startRolling;
  439.             notesLevel.x = 0;
  440.             notesLevel.touchable = false;
  441.             notesLevel.removeChildren(0,-1,true);
  442.             songManager.notesPos = 0;
  443.             addNotes();
  444.             speedLevelAddon++;
  445.         }
  446.        
  447.         private function setSpeed() : void
  448.         {
  449.             if (onDesktop)
  450.             {
  451.                 var amplifier : Number = 0.5;
  452.             }
  453.             else
  454.             {
  455.                 amplifier = scaleFactor *.8; //.3  .5   .6 = faster
  456.             }
  457.            
  458.             speed = int(5*amplifier) + speedLevelAddon;
  459.             speedIncrease = .02 * amplifier*.5;
  460.            
  461.             //speed = int(2*amplifier) + speedLevelAddon;
  462.             //speedIncrease = .01 * amplifier*.2;
  463.         }
  464.        
  465.        
  466.        
  467.  
  468.         private function onCancel() : void
  469.         {
  470.             loadScreen(MusicGameSelector);
  471.         }
  472.  
  473.         private function setTrackingTile() : void
  474.         {
  475.             trackedTile = blackKeys_arr[blackKeys_arr.length-1];
  476.             //if (trackedTile) trace("trackedTile "+trackedTile.noteID);
  477.         }
  478.  
  479.         private function startRolling() : void
  480.         {
  481.             if (difficulty == Root.DIFF_LOW)
  482.             {
  483.                
  484.             }
  485.             else
  486.             {
  487.                 setSpeed();
  488.                 this.addEventListener(EnterFrameEvent.ENTER_FRAME, roll);
  489.                 setTrackingTile();
  490.  
  491.             }
  492.            
  493.             TweenMax.to(startBtn,.7,{autoAlpha:0,ease:Linear.easeOut});
  494.             TweenMax.to(songTitleLevel,.2,{autoAlpha:0,ease:Linear.easeOut});
  495.             startBtn.touchable = false;
  496.             notesLevel.touchable = true;
  497.         }
  498.  
  499.         private function roll(event : EnterFrameEvent) : void
  500.         {
  501.             notesLevel.x+=speed;   
  502.            
  503.             if (trackedTile)
  504.             {
  505.                 var trackedPos : Number = (notesLevel.x + trackedTile.x + noteWid) - wid;
  506.                 if (trackedPos > 0) onTileCrash();
  507.             }
  508.             speed+=speedIncrease;
  509.         }
  510.  
  511.         private function onTileCrash() : void
  512.         {
  513.             this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
  514.             playSoundFX(ERROR_HIT);
  515.             trackedTile.showHitAnim();
  516.            
  517.             TweenMax.to(startBtn,.7,{autoAlpha:1,ease:Linear.easeOut, delay:1.5});
  518.             startBtn.touchable = true;
  519.             startBtn.text = getTrans(START_AGAIN);
  520.             startBtn.onSelected = resetAndStartAgain;  
  521.             TweenMax.to(notesLevel,.7,{x:0, ease:Back.easeOut, delay:1.5});
  522.             notesLevel.touchable = false;      
  523.         }
  524.  
  525.         private function resetAndStartAgain() : void
  526.         {
  527.             notesLevel.removeChildren(0,-1,true);
  528.             songManager.notesPos = 0;
  529.             addNotes();
  530.             startRolling();
  531.         }
  532.     }
  533. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement