Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aadvarkstudios.babygames.musicaldrum.pianotiles
- {
- import starling.display.DisplayObject;
- import starling.display.Image;
- import starling.display.Quad;
- import starling.display.Sprite;
- import starling.events.EnterFrameEvent;
- import starling.events.Touch;
- import starling.events.TouchEvent;
- import starling.events.TouchPhase;
- import starling.text.TextField;
- import starling.text.TextFormat;
- import starling.textures.Texture;
- import starling.utils.Align;
- import starling.utils.deg2rad;
- import com.aadvarkstudios.babygames.Config;
- import com.aadvarkstudios.babygames.Root;
- import com.aadvarkstudios.babygames.Screen;
- import com.aadvarkstudios.babygames.musicaldrum.MusicGameSelector;
- import com.aadvarkstudios.babygames.musicaldrum.NotePlayer;
- import com.aadvarkstudios.babygames.musicaldrum.SongManager;
- import com.aadvarkstudios.babygames.ui.SimpleBtn;
- import com.aadvarkstudios.babygames.utils.DrawUtils;
- import com.greensock.TweenLite;
- import com.greensock.TweenMax;
- import com.greensock.easing.Back;
- import com.greensock.easing.Linear;
- import flash.geom.Rectangle;
- import flash.system.System;
- import flash.utils.setTimeout;
- /**
- * @author Administrator
- */
- public class PianoTiles extends Screen
- {
- //TRANSLATIONS
- public static const START_AGAIN : String = "startAgain";
- public static const PLAY_NEXT_SONG : String = "playNextSong";
- public static const WELL_DONE : String = "wellDone";
- public static const EXCELLENT : String = "excellent";
- public static const GOOD_JOB : String = "goodJob";
- //SFX
- public static const WRONG_TILE : String = "low_knock";
- public static const ERROR_HIT : String = "error4";
- public static const WRONG_BLACK_TILE : String = "error4";
- public static const SONG_FINISHED : String = "tada1";
- public static const FIREWORK : String = "firework";
- private var speedIncrease : Number = .02;// on iPad
- private var speed : Number = 8; //on iPad;
- private var speedLevelAddon : Number = 2;
- private var noteHei : uint;
- private var noteWid : uint;
- private var content_rect : Rectangle;
- public var songManager : SongManager;
- private var player : NotePlayer;
- private var notesLevel : Sprite;
- private var blackKeys_arr : Array;
- private var pos_Y_0 : uint;
- private var pos_Y_1 : uint;
- private var pos_Y_2 : uint;
- private var pos_Y_3 : uint;
- private var pianoNotes_arr : Array;
- private var pianoNotes_dic : Array;
- private var columPos_dic : Array;
- private var startBtn : SimpleBtn;
- private var trackedTile : TileBtn;
- private var notesTotal : uint;
- private var songTitle_txt : TextField;
- private var songTitleLevel : Sprite;
- private var oldBlackTilePos : int = -1;
- private var songCompleteDialog : SongCompleteDialog;
- private var bg : Quad;
- public function PianoTiles()
- {
- super();
- init();
- }
- private function init() : void
- {
- songManager = SongManager.getInstance();
- player = NotePlayer.getInstance();
- songManager.setTilesMode();
- var margin : uint = hei*0.1;
- var bgHeight : uint = hei-margin*2;
- //create blcak BG for testing
- bg = this.addChild(DrawUtils.rect2Quad(new Rectangle(0, 0, wid, hei), 0x000055, 1)) as Quad;
- bg.addEventListener(TouchEvent.TOUCH, onBgTouched);
- this.addChild(DrawUtils.rect2Quad(new Rectangle(0, margin, wid, bgHeight), 0xEFEFEF, 1));
- notesLevel = new Sprite();
- this.addChild(notesLevel);
- notesLevel.touchable = false;
- //add plank
- var plankTex : Texture = assets.getTexture("plank");
- var plankGFX : Image = this.addChild(new Image(plankTex)) as Image;
- plankGFX.width = wid;
- plankGFX.scaleY = plankGFX.scaleX;
- plankGFX.y = margin;
- plankGFX.color = 0x999999;
- var plankHei : uint = plankGFX.height;
- plankGFX = this.addChild(new Image(plankTex)) as Image;
- plankGFX.width = wid;
- plankGFX.height = plankHei;
- plankGFX.y = margin + bgHeight - plankHei;
- plankGFX.color = 0x999999;
- var contentHei : uint = bgHeight - plankHei*2;
- content_rect = new Rectangle(0,margin + plankHei,wid,contentHei);
- noteHei = content_rect.height *.25;
- noteWid = wid *.2;
- var lineHei : int = 1;
- //create lines
- pos_Y_0 = content_rect.y;
- var lineRect : Rectangle = new Rectangle(0,0,wid,lineHei);
- var line : Quad = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
- pos_Y_1 = content_rect.y+ noteHei;
- line.y = pos_Y_1;
- //pos_Y_1+=lineHei;
- line = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
- pos_Y_2 = pos_Y_1 + noteHei;
- line.y = pos_Y_2;
- //pos_Y_2+=lineHei;
- line = this.addChild(DrawUtils.rect2Quad(lineRect,0,1)) as Quad;
- pos_Y_3 = pos_Y_2 + noteHei ;
- line.y = pos_Y_3;
- //pos_Y_3+=lineHei;
- columPos_dic = [];
- columPos_dic[0] = pos_Y_0;
- columPos_dic[1] = pos_Y_1;
- columPos_dic[2] = pos_Y_2;
- columPos_dic[3] = pos_Y_3;
- //init keys and positions
- pianoNotes_arr = ["c1","cis1","d1","dis1","e1","f1","fis1","g1","gis1","a1","ais1","h1","c2","cis2","d2","e2","dis2"]; //17
- pianoNotes_dic = [];
- for (var i : int = 0; i < pianoNotes_arr.length; i++)
- {
- var noteID : String = pianoNotes_arr[i];
- pianoNotes_dic[noteID] = i;
- }
- addNotes();
- //add start button
- var startBtn_rect : Rectangle = new Rectangle(0.8*content_rect.width,content_rect.y + content_rect.height*.9, content_rect.height*.8,40*scaleFactor);
- startBtn = new SimpleBtn(startBtn_rect, getTrans(Config.START), startRolling,0xFF55FF);
- startBtn.rotation = deg2rad(-90);
- this.addChild(startBtn);
- loadSFX();
- //song title
- songTitleLevel = this.addChild(new Sprite()) as Sprite;
- var title_rect : Rectangle = new Rectangle(0.56*content_rect.width,content_rect.y + content_rect.height*1, content_rect.height,96*scaleFactor);
- var titleTextBG : Quad = songTitleLevel.addChild(DrawUtils.rect2Quad(title_rect,0xFF55FF,1)) as Quad;
- titleTextBG.rotation = deg2rad(-90);
- titleTextBG.touchable = false;
- var offset : Number = title_rect.height *.05;
- var fmt : TextFormat = new TextFormat(Config.getInstance().titleFont, 28*scaleFactor, 0xFFFFFF,Align.CENTER,Align.CENTER);
- fmt.leading = -10 * scaleFactor;
- songTitle_txt = new TextField(title_rect.width-offset*2, title_rect.height-offset*2, songManager.activeSong.name, fmt);
- songTitle_txt.touchable = false;
- ///textField_txt.border = true;
- songTitle_txt.rotation = deg2rad(-90);
- songTitle_txt.x = title_rect.x + offset;
- songTitle_txt.y = title_rect.y - offset;
- songTitle_txt.wordWrap = true;
- songTitleLevel.addChild(songTitle_txt);
- notesLevel.addEventListener(TouchEvent.TOUCH, onTouch);
- // going back
- rootInst.onBackButtonPressed = loadMusicGameSelector;
- createBackArrowButton(0,0,loadMusicGameSelector);
- }
- private function onTouch(event : TouchEvent) : void
- {
- var touch:Touch = event.getTouch(notesLevel, TouchPhase.BEGAN);
- if (touch)
- {
- var touchedObj : DisplayObject = touch.target;
- if (touchedObj)
- {
- if (touchedObj is TileBtn)
- {
- onTilePressed(touchedObj as TileBtn);
- }
- }
- }
- }
- private function loadSFX() : void
- {
- assets.enqueue(appDir.resolvePath("includes/musical_drum/tiles_sfx/"));
- assets.loadQueue(function (ratio : Number) : void
- {
- if (ratio == 1)
- {
- System.pauseForGCIfCollectionImminent(0);
- System.gc();
- //levelWon();
- }
- });
- }
- private function onTilePressed(pressedTile : TileBtn) : void
- {
- var isBlack : Boolean = pressedTile.isBlack;
- var activeNoteIDX : uint = songManager.getActiveNoteIDX();
- trace("black:"+isBlack+" pressed note: "+pressedTile.noteID + " | idx"+pressedTile.noteIDX+ " | activeNoteIDX: "+activeNoteIDX);
- if (isBlack)
- {
- //if (pressedTile.noteID == trackedTile.noteID)
- if (activeNoteIDX == pressedTile.noteIDX)
- {
- pressedTile.fadeIn();
- songManager.playSongNote();
- var deletedTile : TileBtn = blackKeys_arr.pop() as TileBtn;
- trace("Deleting "+deletedTile.noteIDX+"\n");
- if (difficulty == Root.DIFF_LOW)
- {
- var nextX : int = notesLevel.x+noteWid;
- TweenLite.to(notesLevel,.6,{x:nextX});
- }
- else
- {
- setTrackingTile();
- }
- if (pressedTile.noteIDX == (notesTotal - 1))
- {
- notesLevel.touchable = false;
- setTimeout(levelWon, 500);
- }
- }
- else
- {
- playSoundFX(WRONG_BLACK_TILE);
- }
- }
- else
- {
- pressedTile.fadeIn();
- playSoundFX(WRONG_TILE);
- }
- }
- private var paused :Boolean = false;
- private function onBgTouched(event : TouchEvent) : void
- {
- var touch:Touch = event.getTouch(bg, TouchPhase.BEGAN);
- if (touch)
- {
- paused = !paused;
- toggleRoll(paused);
- }
- }
- private function toggleRoll(pause : Boolean) : void
- {
- if (pause)
- {
- this.addEventListener(EnterFrameEvent.ENTER_FRAME, roll);
- }
- else
- {
- this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
- }
- }
- private function addNotes() : void
- {
- blackKeys_arr = [];
- oldBlackTilePos = -1;
- var isBlack : Boolean = false;
- var tile : TileBtn;
- var nextNoteID : String = "";
- var noteID : String = "";
- var songNotes_arr : Array = songManager.songNotes_arr;
- var noteRect : Rectangle = new Rectangle(0, 0, noteWid, noteHei);
- var nextX : int = -songNotes_arr.length * noteWid + noteWid*3;
- var blackTileIDX : uint = 0;
- notesTotal = songNotes_arr.length;
- for (var i : int = notesTotal-1; i >= 0; i--)
- {
- noteID = songNotes_arr[i];
- nextNoteID = songNotes_arr[(i+1)];
- blackTileIDX = positionBlackTile(noteID,nextNoteID,i);
- for (var j : int = 0; j < 4; j++)
- {
- noteRect.x = nextX;
- noteRect.y = columPos_dic[j];
- isBlack = blackTileIDX==j;
- tile = new TileBtn(noteID, i,noteRect, isBlack);
- notesLevel.addChild(tile);
- if (isBlack)
- {
- blackKeys_arr.push(tile);
- trace("Adding black tile at "+i);
- }
- }
- nextX+=noteWid + 1;
- }
- }
- private function positionBlackTile(noteID : String, nextNoteID : String = "", i : int=0) : uint
- {
- if ((oldBlackTilePos == -1))
- {
- // trace("random shit");
- var blackTilePos : int = Math.floor(Math.random() * 4);
- if (blackTilePos == oldBlackTilePos)
- {
- // trace("random shit recursed");
- blackTilePos = Math.floor(Math.random() * 4);
- }
- }
- else
- {
- if ((pianoNotes_dic[noteID] == pianoNotes_dic[nextNoteID])) // stayingOnTone
- {
- // trace("Stay")
- blackTilePos = oldBlackTilePos;
- }
- else
- {
- var goingUp : Boolean = pianoNotes_dic[noteID] < pianoNotes_dic[nextNoteID] ;
- if (goingUp)
- {
- blackTilePos = oldBlackTilePos + 1;
- if (blackTilePos == 4) blackTilePos = 0;
- }
- else
- {
- blackTilePos = oldBlackTilePos - 1;
- if (blackTilePos == -1) blackTilePos = 3;
- }
- }
- }
- oldBlackTilePos = blackTilePos;
- /*switch (blackTilePos)
- {
- case 3:
- trace(StringUtils.padLeft(i.toString(),"0", 2)+" |" + noteID + "| | | |");
- break;
- case 2:
- trace(StringUtils.padLeft(i.toString(),"0", 2)+" | |" + noteID + "| | |");
- break;
- case 1:
- trace(StringUtils.padLeft(i.toString(),"0", 2)+" | | |" + noteID + "| |");
- break;
- case 0:
- trace(StringUtils.padLeft(i.toString(),"0", 2)+" | | | |" + noteID + "|");
- break;
- default :
- trace("WTF ? " + blackTilePos);
- break;
- }*/
- return blackTilePos;
- }
- private function levelWon() : void
- {
- this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
- songCompleteDialog = this.addChild(new SongCompleteDialog(onPlayNextSong, onCancel)) as SongCompleteDialog;
- }
- private function onPlayNextSong() : void
- {
- if (songCompleteDialog)
- {
- if (this.contains(songCompleteDialog)) this.removeChild(songCompleteDialog);
- }
- TweenMax.to(startBtn,.5,{autoAlpha:1,ease:Linear.easeOut});
- TweenMax.to(songTitleLevel,.2,{autoAlpha:1,ease:Linear.easeOut});
- songTitle_txt.text = songManager.activeSong.name;
- startBtn.touchable = true;
- startBtn.text = getTrans(Config.START);
- startBtn.onSelected = startRolling;
- notesLevel.x = 0;
- notesLevel.touchable = false;
- notesLevel.removeChildren(0,-1,true);
- songManager.notesPos = 0;
- addNotes();
- speedLevelAddon++;
- }
- private function setSpeed() : void
- {
- if (onDesktop)
- {
- var amplifier : Number = 0.5;
- }
- else
- {
- amplifier = scaleFactor *.8; //.3 .5 .6 = faster
- }
- speed = int(5*amplifier) + speedLevelAddon;
- speedIncrease = .02 * amplifier*.5;
- //speed = int(2*amplifier) + speedLevelAddon;
- //speedIncrease = .01 * amplifier*.2;
- }
- private function onCancel() : void
- {
- loadScreen(MusicGameSelector);
- }
- private function setTrackingTile() : void
- {
- trackedTile = blackKeys_arr[blackKeys_arr.length-1];
- //if (trackedTile) trace("trackedTile "+trackedTile.noteID);
- }
- private function startRolling() : void
- {
- if (difficulty == Root.DIFF_LOW)
- {
- }
- else
- {
- setSpeed();
- this.addEventListener(EnterFrameEvent.ENTER_FRAME, roll);
- setTrackingTile();
- }
- TweenMax.to(startBtn,.7,{autoAlpha:0,ease:Linear.easeOut});
- TweenMax.to(songTitleLevel,.2,{autoAlpha:0,ease:Linear.easeOut});
- startBtn.touchable = false;
- notesLevel.touchable = true;
- }
- private function roll(event : EnterFrameEvent) : void
- {
- notesLevel.x+=speed;
- if (trackedTile)
- {
- var trackedPos : Number = (notesLevel.x + trackedTile.x + noteWid) - wid;
- if (trackedPos > 0) onTileCrash();
- }
- speed+=speedIncrease;
- }
- private function onTileCrash() : void
- {
- this.removeEventListener(EnterFrameEvent.ENTER_FRAME, roll);
- playSoundFX(ERROR_HIT);
- trackedTile.showHitAnim();
- TweenMax.to(startBtn,.7,{autoAlpha:1,ease:Linear.easeOut, delay:1.5});
- startBtn.touchable = true;
- startBtn.text = getTrans(START_AGAIN);
- startBtn.onSelected = resetAndStartAgain;
- TweenMax.to(notesLevel,.7,{x:0, ease:Back.easeOut, delay:1.5});
- notesLevel.touchable = false;
- }
- private function resetAndStartAgain() : void
- {
- notesLevel.removeChildren(0,-1,true);
- songManager.notesPos = 0;
- addNotes();
- startRolling();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement