Source

ld3k / GameWorld.as

package
{
	import hurts.*;
	
	import levels.*;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	import net.flashpunk.World;
	import net.flashpunk.debug.Console;
	import net.flashpunk.graphics.Backdrop;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	import objs.*;
	
	public class GameWorld extends World
	{
		public static var gworld:GameWorld;
		public static var player:Player;
		private static const INDI_PHASE:int = 0;
		private static const WAIT_PHASE:int = 1;
		private static const PLAY_PHASE:int = 2;
		private static const HOLD_PHASE:int = 3;
		private static const GAME_OVER:int  = 4;
		
		private var _bgplain:BgPlain;
		private var _bgstripe:Entity;
		private var _panel:Panel;
		private var _text:Entity;
		private var _playsfx:Sfx;
		private var _spawnsfx:Sfx;
		
		private var _phase:int;
		private var _framecnt:int;
		private var _obj:Obj;
		private var _oix:uint;
		private var _levellength:uint;
		private var _levelwaves:int;
		private var _curlevel:LevelBase;
		private var _curwave:int;
		private var _curolist:Vector.<Obj>;
		private var _levelix:int;
		
		private var _tline:Line; // temp pointer
		private var _textcnter:int; // flick timer
		
		override public function begin():void
		{
			player = new Player(47, 47);
			gworld = this;
			
			_bgplain = new BgPlain();
			_bgstripe = new Entity(0, 0, new Backdrop(Au.ImgBg));
			_panel = new Panel();
			_text = new Entity(12, 40, new Text("press SHIFT key\n    to return"));
			Text(_text.graphic).size = 8;
			_text.visible = false;
			_textcnter = 0;
			_playsfx = new Sfx(Au.SndPlay);
			_spawnsfx = new Sfx(Au.SndSpawn);
			
			add(_bgplain);
			add(_bgstripe);
			add(player);
			Indicator.staticInit(this);
			Hurter.staticInit(this);
			
			add(_text);
			add(_panel);
			
			// init
			_levelix = 0;
			switchLevel(new (Au.levelArr[_levelix]));
			switchPhase(INDI_PHASE);
			
			// debugs
			//switchLevel(new Grid);
//			FP.console.enable();
		}
	
		override public function update():void
		{
			if (_phase == INDI_PHASE) {
				if (_framecnt == Au.INDI_TIME) {
					_bgplain.waitStarts();
					switchPhase(WAIT_PHASE);
				} else if (_oix < _levellength && _framecnt == _curolist[_oix].frame) {
					if (_curolist[_oix] is RemoveLine) {
						// remove the line from list
						_tline = RemoveLine(_curolist[_oix]).line;
						_curolist[ _curolist.indexOf(_tline) ] = null;
//						trace(_curolist.indexOf(_tline));
//						delete _curolist[ _curolist.indexOf(_tline) ];
					}
					Indicator.handleObj(_curolist[_oix++]);
				}
			} else if (_phase == WAIT_PHASE) {
				if (_framecnt == Au.WAIT_TIME) {
					_playsfx.play();
					switchPhase(PLAY_PHASE);
				}
			} else if (_phase == PLAY_PHASE) {
				if (_framecnt == Au.PLAY_TIME) {
					Hurter.killAll();
					_bgplain.holdStarts();
					switchPhase(HOLD_PHASE);
				} else if (_oix < _levellength) {
					// still handle one at a frame
					// shouldn't differ a lot
					Hurter.handleObj(_curolist[_oix++]);
				}
			} else if (_phase == HOLD_PHASE) {
				if (_framecnt == Au.HOLD_TIME) {
					if (player.active == false) {
						_spawnsfx.play();
						player.reset(47, 47);
					}
					if (++_curwave < _levelwaves) {
						// if still have waves then change new wave
						_curolist = _curlevel.leveldata(_curwave);
						_levellength = _curolist.length;
//						trace(_curwave);
					} else {
						if (++	_levelix < Au.levelArr.length) {
							switchLevel(new (Au.levelArr[_levelix]));
						} else {
							// game is done!
//							FP.world = new MenuWorld();
							showPanel(1, true); // dirty dummy heart
							gameover();
							return; // to bypass switchPhase
						}
					}
					switchPhase(INDI_PHASE);
				}
			} else if (_phase == GAME_OVER) {
				if (_textcnter++ > 20) {
					_text.visible = !_text.visible;
					_textcnter = 0;
				}
				if (Input.pressed(Key.SHIFT)) {
					FP.world = new MenuWorld();
				}
			}
			
			// debugs
//			if (Input.pressed(Key.R)) {
//				FP.world = new GameWorld();
//			}
//			if (Input.pressed(Key.J)) {
//				FP.camera.x -= 5;
//			}
//			if (Input.pressed(Key.L)) {
//				FP.camera.x += 5;
//			}
//			if (Input.pressed(Key.I)) {
//				FP.camera.y -= 5;
//			}
//			if (Input.pressed(Key.K)) {
//				FP.camera.y += 5;
//			}
			
			// essential updates
			updateQuake();
			++_framecnt;
			super.update();
		}
		
		private function switchLevel(Level:LevelBase):void {
			_curlevel = Level;
			_curolist = _curlevel.leveldata(0);
			_levellength = _curolist.length;
			_levelwaves = Level.waves;
			_curwave = 0;
		}
		
		// should change phase use this func
		private function switchPhase(Phase:int):void {
			_phase = Phase;
			_framecnt = 0; // reset in swichPhase
			_oix = 0;
		}
		
		// aux funcs
		private var _qcnter:int;
		private var _intense:Number = 0.05;
		public function quake(Frames:int):void
		{
			_qcnter = Frames;	
		}
		
		private function updateQuake():void
		{
			if (_qcnter > 0) {
				if (--_qcnter <= 0) {
					FP.camera.x = 0;
					FP.camera.y = 0;
					return;
				}
				FP.camera.x = (Math.random()*_intense*Au.WIDTH*2-_intense*Au.WIDTH);
				FP.camera.y = (Math.random()*_intense*Au.HEIGHT*2-_intense*Au.HEIGHT);
			}	
		}
		
		public function showPanel(Hearts:int, Congrats:Boolean=false):void
		{
			if (Congrats) {
				_panel.show(8, true);
			} else {
				_panel.show(7 - Hearts, Hearts == 0 ? true: false);
			}
			if (Hearts == 0) gameover();
		}
		
		private function gameover():void
		{
			_phase = GAME_OVER;
			// leave player control able
			// TODO emits
//			if (player.active) {
//				player.active = false;
//				player.visible = false;
//			}
		}
	}
}