PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LevelSelect.as

http://github.com/schonstal/shadowmonk
ActionScript | 275 lines | 188 code | 47 blank | 40 comment | 39 complexity | d3920faf1fe8bdda6e5fc0b1c529c62c MD5 | raw file
  1package
  2{
  3	import org.flixel.*;
  4
  5	public class LevelSelect extends FlxState
  6	{
  7        [Embed(source='../data/Level_select.png')] private var ImgGlow:Class;
  8
  9        private var levels:Array;
 10        private var _t:FlxText;
 11        private var _oneStar:FlxText;
 12        private var _twoStar:FlxText;
 13        private var _threeStar:FlxText;
 14        private var _stars:StarRating;
 15        
 16        //Colors for text
 17        private var _dark:Number = 0xff000000;
 18        private var _light:Number = 0xff682777;
 19
 20        private var _upArrow:ScrollArrow;
 21        private var _downArrow:ScrollArrow;
 22
 23        //Moving arrow timers (when scrolling list)
 24        private var _arrowTime:Number = 0;
 25        private var _arrowAmount:Number = 2;
 26        private var _arrowInterval:Number = 0.1;
 27
 28        //Hold-to-scroll hold amount
 29        private var _holdTimer:Number;
 30        private var _holdInterval:Number = 0.5;
 31
 32        //Hold-to-scroll interval
 33        private var _scrollTimer:Number;
 34        private var _scrollInterval:Number = 0.05;
 35		
 36		//Make sure the player can't select after a level has already been selected
 37		private var _selected:Boolean = false;
 38
 39        //First displayed item on the list
 40        private var _window:Number = 1;
 41        private var _windowOffset:Number = 9;
 42
 43        private function get _windowMax():Number {
 44            return _window + _windowOffset;
 45        }
 46
 47		public function LevelSelect()
 48		{
 49            SoundBank.music("level_select");
 50            levels = new Array();
 51
 52            var m:SelectScroll;
 53            m = new SelectScroll();
 54            add(m);
 55
 56            //Make sure the window is in the correct place
 57            if(FlxG.level > 10)
 58                _window = FlxG.level - 9;
 59
 60            //Add all the text to the list
 61            var level:int;
 62            for(level=1; level <= Starter.levels; level++) {
 63                var levelText:LevelText;
 64                levelText = new LevelText(27, 54 + (level * 12), level);
 65
 66                //Make sure the selected level is hilighted
 67                levelText.color = (FlxG.level==level?_light:_dark);
 68                levels[level-1] = levelText;
 69
 70                //Hide text outside the window
 71                setVisibility(level-1);
 72
 73                add(levelText);
 74            }
 75            
 76            var bestTimer:GameTimer = new GameTimer(SaveData.best);
 77
 78            //Total stars
 79            _t = new FlxText(203, 9, 92, "" + SaveData.totalStars);
 80			_t.alignment = "right";
 81            _t.setFormat("SNES");
 82            _t.color = 0xffffffff;
 83			add(_t);
 84
 85            //Best time
 86			_t = new FlxText(191, 49, 128, (SaveData.completed>0?bestTimer.render():"--:--.--"));
 87			_t.alignment = "center";
 88            _t.setFormat("SNES");
 89            _t.color = 0xffffffff;
 90			add(_t);
 91            
 92            //Star times
 93			_oneStar = new FlxText(191, 121, 128, "--:--.--");
 94			_oneStar.alignment = "center";
 95            _oneStar.setFormat("SNES");
 96            _oneStar.color = 0xffffffff;
 97			add(_oneStar);
 98			_twoStar = new FlxText(191, 155, 128, "--:--.--");
 99			_twoStar.alignment = "center";
100            _twoStar.setFormat("SNES");
101            _twoStar.color = 0xffffffff;
102			add(_twoStar);
103			_threeStar = new FlxText(191, 189, 128, "--:--.--");
104			_threeStar.alignment = "center";
105            _threeStar.setFormat("SNES");
106            _threeStar.color = 0xffffffff;
107			add(_threeStar);
108
109            //"Level Select"
110			var t:FlxText = new FlxText(27, 42, 158, "Level Select");
111			t.alignment = "center";
112            t.setFormat("SNES");
113            t.color = _dark;
114			add(t);
115            
116            //Arrows
117            _upArrow = new ScrollArrow(100, 54, "up");
118            _upArrow.visible = false;
119            add(_upArrow);
120            _downArrow = new ScrollArrow(100, 186, "down");
121            add(_downArrow);
122            
123            //Only show the arrows if you can scroll
124            setArrows();
125            
126            //Show the rating for the current level
127            _stars = new StarRating(115, 32, SaveData.stars);
128            add(_stars);
129		}
130
131		override public function update():void
132		{
133            //Reset the arrows if you haven't pressed up/down for a while
134            _arrowTime -= FlxG.elapsed;
135            if(_arrowTime <= 0) {
136                _upArrow.y = 54;
137                _downArrow.y = 186;
138            }
139
140			super.update();
141
142            //Allow the player to scroll if they haven't recently
143            _scrollTimer += FlxG.elapsed;
144
145			if(!_selected) {
146				//Check if the player is holding down or up
147				if(FlxG.keys.DOWN)
148					_holdTimer -= FlxG.elapsed;
149				if(FlxG.keys.UP)
150					_holdTimer += FlxG.elapsed;
151				if(!FlxG.keys.UP && !FlxG.keys.DOWN)
152					_holdTimer = 0;
153
154				//If you just pressed UP or if you've been holding it, scroll up
155				if(FlxG.keys.justPressed("UP") || 
156						_holdTimer > _holdInterval && 
157						_scrollTimer >= _scrollInterval) {
158					if(FlxG.level > 1) {
159						moveCursor(FlxG.level - 1);
160					//Don't loop the cursor when the key is held
161					} else if(_holdTimer < _holdInterval) {
162						moveCursor(Starter.levels);
163					}
164				//See if the player wants to go down
165				} else if(FlxG.keys.justPressed("DOWN") || 
166						_holdTimer < -_holdInterval && 
167						_scrollTimer >= _scrollInterval) {
168					if(FlxG.level < Starter.levels) {
169						moveCursor(FlxG.level + 1);
170					} else if(_holdTimer > -_holdInterval) {
171						moveCursor(1);
172					}
173				//Check if the player wants to start the game
174				} else if(FlxG.keys.justPressed("X") || FlxG.keys.justPressed("ENTER")) {
175					if (FlxG.level <= SaveData.completed + 1) {
176						_selected = true;
177						FlxG.music.fadeOut(0.5);
178						SoundBank.play("select");
179						FlxG.fade.start(0xff000000, 0.5, function():void { 
180							SoundBank.music("game");
181							Starter.startLevel(); 
182						});
183					} else {
184						SoundBank.play("nope");
185					}
186				}
187			}
188            
189		}
190        
191        private function moveCursor(newLevel:int):void {
192            //loop var
193            var i:int;
194
195            if(newLevel > FlxG.level)
196                SoundBank.play("down");
197            else
198                SoundBank.play("up");
199            
200            //Set the previously selected text back to default
201            levels[FlxG.level-1].color = _dark;
202            
203            //Reset the timer so they can't scroll again for a little while (useful for holding)
204            _scrollTimer = 0;
205
206            //Scroll the list up
207            if(newLevel < _window) {
208                //Animate the up arrow
209                if(_arrowTime <= 0)
210                    _upArrow.y -= _arrowAmount;
211                _arrowTime = _arrowInterval;
212
213                //Move the level text down
214                for(i = 0; i < levels.length; i++) {
215                    _window = newLevel;
216                    setVisibility(i);
217                }
218            }
219            
220            //Scroll the window down if necessary
221            if(newLevel > _windowMax) {
222                //Animate down arrow
223                if(_arrowTime <= 0)
224                    _downArrow.y += _arrowAmount;
225                _arrowTime = _arrowInterval;
226
227                //Move the level text up
228                for(i = 0; i < levels.length; i++) {
229                    _window = newLevel - 9;
230                    setVisibility(i);
231                }
232            }
233            
234            //Only show the arrow if you can scroll
235            setArrows();
236
237            //Update to the new level
238            FlxG.level = newLevel;
239            var bestTimer:GameTimer = new GameTimer(SaveData.best);
240            _stars.rating = SaveData.stars;
241            _t.text = (bestTimer.elapsed>5999?"--:--.--":bestTimer.render());
242            
243            //Hilight the selected text
244            levels[FlxG.level-1].color = _light;
245        }
246
247        //Sets visibility of a menu item depending on it's index relative to the window
248        private function setVisibility(i:int):void {
249            //Place the text in the correct position
250            levels[i].y = 66 + (i*12) - ((_window-1) * 12);
251
252            //Check if it's in the window, set visibility accordingly
253            if(i < _window - 1)
254                levels[i].visible = false;
255            else if(i < _windowMax)
256                levels[i].visible = true;
257            if(i >= _windowMax)
258                levels[i].visible = false;
259        }
260
261        //Hides the arrows if you can't scroll the list, shows them if you can
262        private function setArrows():void {
263            if(_window > 1)
264                _upArrow.visible = true;
265            else
266                _upArrow.visible = false;
267
268            if(_windowMax < Starter.levels)
269                _downArrow.visible = true;
270            else
271                _downArrow.visible = false;
272            
273        }
274	}
275}