package com.darkdimension.ritle_run;

import java.util.Random;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class MenuGame extends Menu
{
    //Important Values
    private Random randomizer;
	private Paint font;
	private boolean paused;
	private int gameRunning;
	public boolean hardReset, hardRestart;
	private DiAnimation aStart;
	private String sScore;

	//Camera
	private int cameraX;
	private int speed;
	private int highScore, score;

    //Ground
	private int[] groundLevel;
    private Ground[] ground;
    private int lastGround;

	//Trees
    private DiPoint[] tree;
    private DiPoint lastTree0, lastTree1;

    //Pick ups & enemies
    private DiPoint[] pickUp;
    private DiPoint[] enemy;
	private DiAnimation doubleScoreTimer;

    //Character
    private Character character;

    //Crystal Show - UI
    private DiPoint diCrystal;
    private int dCrystals;
    private DiAnimation aCrystal, aCrystalDelay, tutorialDelay;
	private DiPoint buttonRestart, buttonBack, buttonResume, buttonMute;

    //Results
    private DiAnimation aResults;
    private int dResults;
    private int shownCrystals;
    private int shownScore;
    private int shownTotal;

	//Pause Menu
	private String sPaused, sRestart, sResume, sMainMenu;

	//Results Texts
	private String sResults, sResultsCrystals, sResultsScore, sResultsTotal, sResultsHighScore, sResultsNewHighScore, strTouchAnywhere;

    public MenuGame(Resources resources, int gHighScore, boolean gHardRestart)
    {
		//Super Constructor
        super();

        //Important Variables
			randomizer = new Random(System.currentTimeMillis());

			font = new Paint();
			font.setColor(Color.argb(255, 143, 231, 134));
			font.setTextSize(20 *GameSurface.density);
			font.setTextAlign(Paint.Align.CENTER);
			font.setAntiAlias(true);
			font.setShadowLayer(2, 1, 1, Color.BLACK);

			paused = false;
			gameRunning = -1;
			hardReset = false;
			hardRestart = gHardRestart;

			aStart = new DiAnimation(50);
			aStart.start();

			sScore = resources.getString(R.string.score);

		//Camera
			cameraX = 0;
			speed = (int) ((4 *GameSurface.density) *4);
			highScore = gHighScore;
			score = 0;

		//Grounds
			//Ground Levels
			groundLevel = new int[4];
			for (int iii = 0; iii < 4; iii++)
				groundLevel[iii] = GameSurface.height *(iii+1) /(groundLevel.length +1);

			//First Ground
			int N = GameSurface.width /(DiPoint.getWidth(14)*2) +2;
			ground = new Ground[N];

			ground[0] = new Ground();
			ground[0].setWidth(GameSurface.width /ground[0].getWidth() +2);
			ground[0].setCenterX(GameSurface.width/2);
			ground[0].setBottom(GameSurface.height);

			//Rest Grounds
			for (int iii = 1; iii < N; iii++)
			{
				//Init ground
				ground[iii] = new Ground();
				ground[iii].setWidth(randomizer.nextInt(5) +3);
				ground[iii].setLeft(ground[iii-1].getRight());

				//Randomize one of the ground levels except the last
				ground[iii].setTop( groundLevel[ randomizer.nextInt(3) ] -DiPoint.getHeight(14)/4 );

				//If it matches the previous
				if (ground[iii].getTop() == ground[iii -1].getTop())
					//Give it the last one
					ground[iii].setTop( groundLevel[3] -DiPoint.getHeight(14)/4 );
			}
			lastGround = N -1;

		//Trees
			tree = new DiPoint[N/2];

			//First two trees are fixed
			tree[0] = new DiPoint(5);
			tree[0].setCenterX(GameSurface.width /4);
			tree[0].setBottom (ground[0].getGroundLevel());

			tree[1] = new DiPoint(6);
			tree[1].setCenterX(GameSurface.width *5/6);
			tree[1].setBottom (ground[0].getGroundLevel());

			//Rest are on invalid state (for reseting)
			for (int iii = 2; iii < N/2; iii++)
			{
				tree[iii] = new DiPoint(5);
				tree[iii].setRight(0);
			}

			//Last two remain invalid until game stops
			lastTree0 = new DiPoint(5);
			lastTree0.setRight(0);

			lastTree1 = new DiPoint(6);
			lastTree1.setRight(0);

		//Pick ups & Enemies
			pickUp = new DiPoint[N];
			for (int iii = 0; iii < N; iii++)
			{
				pickUp[iii] = new DiPoint(21);
				pickUp[iii].setRight(0);
			}
			doubleScoreTimer = new DiAnimation(250);

			enemy = new DiPoint[N];
			for (int iii = 0; iii < N; iii++)
			{
				enemy[iii] = new DiPoint(25);
				enemy[iii].setRight(0);
			}

		//Character
			character = new Character();
			character.setRight(0);
			character.setBottom(ground[0].getGroundLevel() -1);

		//Crystals - UI
			diCrystal = new DiPoint(21);
			diCrystal.setLeft(0);
			diCrystal.setTop(GameSurface.height);

			dCrystals = 0;

			aCrystal = new DiAnimation(16);
			aCrystalDelay = new DiAnimation(40);

			buttonRestart = new DiPoint(8);
			buttonRestart.setCenterX(GameSurface.width    /4);
			buttonRestart.setCenterY(GameSurface.height *2/3);

			buttonResume = new DiPoint(8);
			buttonResume.setCenterX(GameSurface.width  *2/4);
			buttonResume.setCenterY(GameSurface.height *2/3);

			buttonBack = new DiPoint(8);
			buttonBack   .setCenterX(GameSurface.width  *3/4);
			buttonBack   .setCenterY(GameSurface.height *2/3);

			tutorialDelay = new DiAnimation(120);
			tutorialDelay.start();

			buttonMute = new DiPoint(GameSurface.bSound ? 26 : 27);
			buttonMute.setRight (GameSurface.width  -buttonMute.getWidth ()/2);
			buttonMute.setBottom(GameSurface.height -buttonMute.getHeight()/2);

        //Results
			aResults = new DiAnimation(30);

			dResults = 0;
			shownCrystals = 0;
			shownScore = 0;
			shownTotal = 0;

		//Pause Menu texts
		sPaused = resources.getString(R.string.paused);
		sRestart = resources.getString(R.string.button_restart);
		sResume = resources.getString(R.string.button_resume);
		sMainMenu = resources.getString(R.string.button_main_menu);

		//Results texts
		sResults = resources.getString(R.string.results);
		sResultsCrystals = resources.getString(R.string.results_crystals);
		sResultsScore = resources.getString(R.string.results_score);
		sResultsTotal = resources.getString(R.string.results_total);
		sResultsHighScore = resources.getString(R.string.results_high_score);
		sResultsNewHighScore = resources.getString(R.string.results_new_high_score);
		strTouchAnywhere = resources.getString(R.string.touch_anywhere);
	}

    public Action update()
    {
        super.update();

        //Configure Input
        if (bTouched)
        {
            //Cancel input
            bTouched = false;

            //Skip results
            if (aResults.isRunning())
            {
                switch (dResults)
                {
                    case 1: shownCrystals = dCrystals; break;
                    case 2: shownScore = (score /100) -1; break;
                    case 3: shownTotal = shownCrystals +shownScore -1; break;
                    case 4:
                        if (buttonRestart.isTouched(touchX, touchY))
                        {
                        	nMenu = Action.TO_GAME;
                            aBasic.setMax(10);
                            aBasic.startFromEnd();

							if (gameRunning > 0)
								hardReset = true;
                        } else
                        if (buttonBack.isTouched(touchX, touchY))
                        {
                        	nMenu = Action.TO_MAIN_MENU;
                            aBasic.setMax(10);
                            aBasic.startFromEnd();

							if (gameRunning > 0)
								hardReset = true;
                        }
                }
                aResults.setCounter(aResults.getMax() -1);
            }
            else
			//Pause Menu
            if (paused)
            {
				if (buttonResume.isTouched(touchX, touchY))
					paused = !paused; else
                if (buttonRestart.isTouched(touchX, touchY))
                {
                    nMenu = Action.TO_GAME;
                    aBasic.setMax(10);
                    aBasic.startFromEnd();

					hardReset = true;
                } else
                if (buttonBack.isTouched(touchX, touchY))
                {
                    nMenu = Action.TO_MAIN_MENU;
                    aBasic.setMax(10);
                    aBasic.startFromEnd();

					hardReset = true;
                } else
				if (buttonMute.isTouched(touchX, touchY))
				{
					if (GameSurface.bSound)
					{
						GameSurface.bSound = false;
						buttonMute.setFrame(27);
					}
					else
					{
						GameSurface.bSound = true;
						buttonMute.setFrame(26);
					}
				}
            }
            else
                character.jump();
        }

		if (paused); else
		//Game is Running
        if (gameRunning != 0)
        {
			//Advance Camera
			cameraX += speed;

			//Check Background Props

            //Background.Materialize(cameraX);
			Background.checkLimits(randomizer, cameraX);

            //Ground
			int N = ground.length;
            for (int iii = 0; iii < N; iii++)
            {
                //Reset Ground
                if (ground[iii].getRight() -cameraX/4 <= 0
                &&  gameRunning == -1)
                {
                    //Width
                    if (score < 4000)	//5, 6, 7
                        ground[iii].setWidth(randomizer.nextInt(3) +5); else
                    if (score < 9000)	//4, 5
                        ground[iii].setWidth(randomizer.nextInt(2) +4); else
                    if (score < 15000)	//3, 4
                        ground[iii].setWidth(randomizer.nextInt(2) +3);
					else				//2, 3
                        ground[iii].setWidth(randomizer.nextInt(2) +2);

                    //Coordinates
                    if (score < 2000)
                        ground[iii].setLeft(ground[lastGround].getRight()); else
                    if (score < 6500)
                        ground[iii].setLeft(ground[lastGround].getRight() +DiPoint.getWidth(14)); else
                    if (score < 12000)
                        ground[iii].setLeft(ground[lastGround].getRight() +DiPoint.getWidth(14) +DiPoint.getWidth(14)/2); else
					if (score < 17000)
						ground[iii].setLeft(ground[lastGround].getRight() +DiPoint.getWidth(14) *2);
                    else
                        ground[iii].setLeft(ground[lastGround].getRight() +DiPoint.getWidth(14) *3);

                    ground[iii].setTop( groundLevel[ randomizer.nextInt(3) ] -DiPoint.getHeight(14)/4 );

                    //Same ground level
                    if (ground[iii].getTop() == ground[lastGround].getTop())
                        ground[iii].setTop( groundLevel[3] -DiPoint.getHeight(14)/4 );

                    lastGround = iii;

                    //Items
                    int item = randomizer.nextInt(100);
					if (item < 50) item = 0; else
					if (item < 60) item = 1; else
					if (item < 64) item = 3; else
					if (item < 67) item = 2;
					else item = -1;

                    if (item != -1)
                        //Cycle pickups
                        for (int jjj = 0; jjj < pickUp.length; jjj++)
                            if (pickUp[jjj].getRight() <= 0)
                            {
                                //Create this pickUp
                                pickUp[jjj].setFrame(21 +item);
                                pickUp[jjj].setLeft(ground[iii].getLeft() +randomizer.nextInt(ground[iii].getWidth() -pickUp[jjj].getWidth()));
                                pickUp[jjj].setBottom(ground[iii].getGroundLevel());

                                jjj = pickUp.length;
                            }

                    //Enemies
                    item = -1;
                    if (randomizer.nextInt(100) < 25 && ground[iii].getBlockWidth() > 2)
                        item = 0;
                    else
                    if (ground[iii].getBlockWidth() < 6 && randomizer.nextInt(100) < 60 && score > 3000)
                        ground[iii].setSelfDestruct(true);

                    if (item != -1)
                        //Cycle Enemies
                        for (int jjj = 0; jjj < enemy.length; jjj++)
                            if (enemy[jjj].getRight() -cameraX/4 <= 0)
                            {
                                //Create Enemy
                                enemy[jjj].setCenterX(ground[iii].getLeft() +ground[iii].getWidth() /3 *(randomizer.nextInt(2) +1));
                                enemy[jjj].setBottom(ground[iii].getGroundLevel());

                                jjj = enemy.length;
                            }

                    //Chance for trees
                    if (ground[iii].getBlockWidth() >= 3 && !ground[iii].isSelfDestruct())
                        if (randomizer.nextInt(100) < 35)
                            for (int jjj = 0; jjj < tree.length; jjj++)
                                if (tree[jjj].getRight() -cameraX/4 <= 0)
                                {
                                    tree[jjj].setFrame(randomizer.nextBoolean() ? 5 : 6);
                                    tree[jjj].setBottom(ground[iii].getGroundLevel());
                                    tree[jjj].setCenterX(ground[iii].getLeft() +(ground[iii].getWidth() *(randomizer.nextInt(4) +1)/5 ));

                                    jjj = tree.length;
                                }
                }

				//Update ground -Self Destruct-
				ground[iii].update();
            }

			//Opening animation
			if (aStart.update())
				character.setRight(aStart.getDistance(character.getWidth() *2));

            //Check if character moved to next ground
            if (character.getCenterX() > ground[character.walkingOn].getRight() -cameraX/4)
                if (++character.walkingOn >= ground.length) character.walkingOn = 0;

            //Character Update
            character.update(
					(character.getCenterX() > ground[character.walkingOn].getLeft() -cameraX/4 || aBasic.isRunning()) && gameRunning == -1 ?
                            ground[character.walkingOn].getGroundLevel() : -GameSurface.height*2,
                    groundLevel[1]);

            //Activate ground selfDestruct if there is
            if (character.justLanded())
                ground[character.walkingOn].selfDestruct();

            //PickUps
			for (DiPoint cPickUp : pickUp)
				if (cPickUp.getRight() -cameraX/4 > 0)
					//if Character picked up something
					if (isColliding(character.getSquare(), cPickUp.getSquare(-cameraX / 4, 0))
					&& gameRunning == -1)
					{
						switch (cPickUp.getFrame() - 21)
						{
							case 0: //Memory Crystal
								dCrystals++;
								if (!aCrystal.isPositive())
									aCrystal.start();
								if (aCrystalDelay.isRunning())
									aCrystalDelay.startFromBeginning();
								GameSurface.playSound(2);
								break;
							case 1: //Magic Mana
								character.addManaLevel();
								break;
							case 2: //Shield
								character.giveShield();
								GameSurface.playSound(6);
								break;
							case 3: //Double Score
								doubleScoreTimer.startFromBeginning();
								GameSurface.playSound(5);
								break;
						}

						//Remove pickUp
						cPickUp.setRight(0);
					}

            //Enemies
			for (DiPoint cEnemy : enemy)
				if (cEnemy.getRight() -cameraX/4 > 0)
				{
					if (character.justLanded())
						if (character.getCenterX() > cEnemy.getLeft () - cameraX/4
						&&  character.getCenterX() < cEnemy.getRight() - cameraX/4)
						{
							if (character.hasShield())
								character.loseShield();
							else if (character.hasMana())
								character.loseMana();
							else
							{
								aStart.startFromEnd();
								character.jump();
								destroy(Action.TO_MAIN_MENU);
							}

							GameSurface.playSound(7);
						}
				}

			//Game Over
			if (character.getTop() > GameSurface.height && gameRunning == -1)
			{
				GameSurface.playSound(7);
				destroy(Action.TO_MAIN_MENU);
			}else
            //if game is ending, and it got to the final frame
            if (gameRunning > 0)
                if (gameRunning -cameraX/4 < 0)
                {
                    //End game
                    gameRunning = 0;

                    //Fix camera so it has same angle as in main menu
                    cameraX = (ground[lastGround].getCenterX() -GameSurface.width/2) *4;
                }

			//Survived this frame - award score
			//If game not starting && if game not ending
			if (!aBasic.isRunning() && gameRunning == -1)
				if (doubleScoreTimer.update())
					score += 2;
				else
					score++;
        }//End game is running
        else
        //Game is not running and its not paused (maybe inside a menu)
			Background.animate(randomizer, cameraX);

        //If game is ended
        if (aResults.update())
        {
            //Game over updated
            switch (dResults)
            {
                case 0: //Menu comes in play
                    break;
                case 1: //Count crystals
                    if (dCrystals > 0)
                        shownCrystals++;
                    break;
                case 2: //Count Score
                    if (score /100 > 0)
                        shownScore++;
                    break;
                case 3: //Count Total
                    if (shownCrystals +shownScore > 0)
                        shownTotal++;
                    break;
                case 4: //Wait for input
                    break;
            }

            //Stop updating and start next menu
            if (aResults.isAtEnd())
            {
                boolean changed = false;
                if (dResults < 4)
                {
                    dResults++;
                    changed = true;
                }
                switch (dResults)
                {
                    case 1:
                        aResults.start( dCrystals > 0 ? dCrystals : 10);
                        break;
                    case 2:
                        aResults.start(score /100 > 0 ? score /100 : 10);
                        break;
                    case 3:
                        aResults.start(shownCrystals +shownScore > 0 ? shownCrystals +shownScore : 10);
                        break;
                    case 4:
                        aResults.start(50);
                        break;
                }
                if (changed && dResults == 4) return Action.SHOW_AD;
            }
        }

		//Crystal Display
		if (aCrystalDelay.isRunning())
			if (!paused)
			{
				//Advance Delay
				aCrystalDelay.update();

				//Action when it's over
				if (aCrystalDelay.isAtEnd())
					aCrystal.startFromEnd();
			}
		//else
		if (aCrystal.update())
		{
			diCrystal.setTop(GameSurface.height - aCrystal.getDistance(diCrystal.getHeight()));

			if (aCrystal.isAtEnd())
				aCrystalDelay.startFromBeginning();
		}

		//Redraw
        bDraw = true;

        //Menu destroyed
        if (aBasic.isAtStart() && !aBasic.isPositive())
		{
			Background.Materialize(cameraX);
			return nMenu;
		}

        return Action.NO_ACTION;
    }

    public void draw(Canvas canvas)
    {
        super.draw(canvas);

        //Background
        Background.Draw(canvas, cameraX);

		int N;

		//Hard Reset - Restart
		Paint paint = null;
		if (aBasic.isPositive() && hardRestart
		|| !aBasic.isPositive() && hardReset)
			paint = invisible;

		//Ground
		N = ground.length;
		for (int iii = 0; iii < N; iii++)
			ground[iii].draw(canvas, -cameraX/4, 0, paint);

		//Trees
		N = tree.length;
		for (int iii = 0; iii < N; iii++)
			tree[iii].draw(canvas, -cameraX/4, 0, paint);

		lastTree0.draw(canvas, -cameraX/4, 0, paint);
		lastTree1.draw(canvas, -cameraX/4, 0, paint);

		N = enemy.length;
		for (int iii = 0; iii < N; iii++)
			enemy[iii].draw(canvas, -cameraX/4, 0, paint);

		N = pickUp.length;
		for (int iii = 0; iii < N; iii++)
			if (pickUp[iii].getRight() > 0)
				pickUp[iii].draw(canvas, -cameraX/4, 0, paint);

		//Character
		character.draw(canvas, paint);

		//Crystals
		diCrystal.draw(canvas, paint);
		if (!aCrystal.isAtStart())
		{
			canvas.drawText("x ", diCrystal.getRight(), diCrystal.getCenterY() + (diCrystal.getHeight() / 4), GameSurface.font);
			canvas.drawText(String.valueOf(dCrystals), diCrystal.getCenterX() *3, diCrystal.getCenterY() + (diCrystal.getHeight() / 4), GameSurface.font);
		}

        //Score
		canvas.drawText(sScore, GameSurface.width /2, (24 + (GameSurface.isPhone ? 0 : 12)) * GameSurface.density, GameSurface.font);

		if (doubleScoreTimer.isRunning() && gameRunning == -1)
			canvas.drawText(String.valueOf(score), GameSurface.width /2, (44 + (GameSurface.isPhone ? 0 : 12)) * GameSurface.density, font);
		else
			canvas.drawText(String.valueOf(score), GameSurface.width /2, (44 + (GameSurface.isPhone ? 0 : 12)) * GameSurface.density, GameSurface.font);

        //Draw Results
        if (aResults.isRunning())
        {
            canvas.drawText(sResults, GameSurface.width/2, (20 *GameSurface.density) *4, GameSurface.font);

            canvas.drawText(sResultsCrystals	, GameSurface.width/3, (20 *GameSurface.density) *5, GameSurface.font);
            canvas.drawText(sResultsScore		, GameSurface.width/3, (20 *GameSurface.density) *6, GameSurface.font);
            canvas.drawText(sResultsTotal   	, GameSurface.width/3, (20 *GameSurface.density) *7, GameSurface.font);
			canvas.drawText(sResultsHighScore   , GameSurface.width/3, (20 *GameSurface.density) *8, GameSurface.font);

			if (dResults == 1) canvas.drawText(String.valueOf(shownCrystals), GameSurface.width*2/3, (20 *GameSurface.density) *5, font);
			else 			   canvas.drawText(String.valueOf(shownCrystals), GameSurface.width*2/3, (20 *GameSurface.density) *5, GameSurface.font);

			if (dResults == 2) canvas.drawText(String.valueOf(shownScore)	 , GameSurface.width*2/3, (20 *GameSurface.density) *6, font);
			else			   canvas.drawText(String.valueOf(shownScore)	 , GameSurface.width*2/3, (20 *GameSurface.density) *6, GameSurface.font);

			if (dResults == 3) canvas.drawText(String.valueOf(shownTotal)	 , GameSurface.width*2/3, (20 *GameSurface.density) *7, font);
			else			   canvas.drawText(String.valueOf(shownTotal)	 , GameSurface.width*2/3, (20 *GameSurface.density) *7, GameSurface.font);

			canvas.drawText(String.valueOf(highScore), GameSurface.width*2/3, (20 *GameSurface.density) *8, GameSurface.font);
            if (shownTotal > highScore)
            	canvas.drawText(sResultsNewHighScore, GameSurface.width/2, (20 *GameSurface.density) *9, GameSurface.font);

            if (dResults == 4)
            {
                buttonRestart.draw(canvas, invisible);
                canvas.drawText(sRestart, buttonRestart.getCenterX(), buttonRestart.getCenterY() +(10 *GameSurface.density), GameSurface.font);
                buttonBack.draw(canvas, invisible);
                canvas.drawText(sMainMenu, buttonBack.getCenterX(), buttonBack.getCenterY() +(10 *GameSurface.density), GameSurface.font);
            }
			else
				canvas.drawText(strTouchAnywhere, GameSurface.width/2, GameSurface.height -(20 *GameSurface.density), GameSurface.font);
        }

        //Pause Menu
        if (paused)
        {
            canvas.drawText(sPaused, GameSurface.width/2, GameSurface.height/2 +(10 *GameSurface.density), GameSurface.font);

			//Resume Button
			buttonResume.draw(canvas, invisible);
			canvas.drawText(sResume, buttonResume.getCenterX(), buttonResume.getCenterY() +(10 *GameSurface.density), GameSurface.font);

			//Restart Button
            buttonRestart.draw(canvas, invisible);
            canvas.drawText(sRestart, buttonRestart.getCenterX(), buttonRestart.getCenterY() +(10 *GameSurface.density), GameSurface.font);

            //Back Button
            buttonBack.draw(canvas, invisible);
            canvas.drawText(sMainMenu, buttonBack.getCenterX(), buttonBack.getCenterY() +(10 *GameSurface.density), GameSurface.font);

			//Mute Button
			buttonMute.draw(canvas, invisible);
        }
    }

    //Actions
    @Override
    public void destroy(Action action)
    {
        if (gameRunning == -1)
        {
            nMenu = action;

            //Find first not visible ground
            //Create Main Menu pattern there
            //Make the rest not visible
            int iii = character.walkingOn;
            int startingPoint = -1;
            do
            {
                //if ground is right from screen
                if (ground[iii].getLeft() -cameraX/4 > GameSurface.width)
                    //First one becomes main menu pattern
                    if (startingPoint == -1)
                    {
                        //Create Main Menu Patter here
                        ground[iii].setWidth(GameSurface.width /DiPoint.getWidth(14) +2);
                        ground[iii].setBottom(GameSurface.height);
                        ground[iii].setSelfDestruct(false);
                        startingPoint = ground[iii].getCenterX() -GameSurface.width/2;

                        lastTree0.setCenterX(startingPoint +GameSurface.width   /4);
                        lastTree0.setBottom(ground[iii].getGroundLevel());

                        lastTree1.setCenterX(startingPoint +GameSurface.width *5/6);
                        lastTree1.setBottom(ground[iii].getGroundLevel());

                        lastGround = iii;
                    }
                    //The rest of them are gone
                    else
                        ground[iii].setTop(GameSurface.height*2);

                iii++;
                if (iii == ground.length)
                    iii = 0;
            } while (iii != character.walkingOn);

			int N;

            //Remove unnecessary items
			N = pickUp.length;
            for (int jjj = 0; jjj < N; jjj++)
                if (pickUp[jjj].getLeft() -cameraX/4 > GameSurface.width)
                    pickUp[jjj].setRight(0);

            //Remove unnecessary enemies(int) (startingPoint -(cameraX/4) /(4 *GameSurface.density *speed));
			N = enemy.length;
            for (int jjj = 0; jjj < N; jjj++)
                if (enemy[jjj].getLeft() -cameraX/4 > GameSurface.width)
                    enemy[jjj].setRight(0);

			N = tree.length;
            for (int jjj = 0; jjj < N; jjj++)
                if (tree[jjj].getLeft() -cameraX/4 > GameSurface.width)
                    tree[jjj].setRight(0);

            //Create game countdown
            gameRunning = startingPoint;

            //Start showing results
            aResults.setMax(30);
            aResults.startFromBeginning();
            dResults = 0;
        }
    }

    public boolean hasNewHighScore() {return shownTotal > highScore;}

    public int getHighScore() {return shownTotal;}

    public void pause()
    {
        //If inside ending screen, go to main menu
        if (dResults == 4
        && gameRunning != -1)
        {
            nMenu = Action.TO_MAIN_MENU;
            aBasic.setMax(10);
            aBasic.startFromEnd();

			if (gameRunning > 0)
				hardReset = true;
        } else
        //else pause/un-pause game
        if (gameRunning == -1)
		{
			paused = !paused;

			if (!aCrystal.isPositive())
				aCrystal.start();
			else
			if (aCrystalDelay.isRunning())
				aCrystalDelay.startFromBeginning();
		}
    }

    private boolean isColliding(int[] object1, int[] object2)
    {
        return  (object1[2] -object2[0] > 0)
			!=  (object1[0] -object2[2] > 0)
			&&  (object1[3] -object2[1] > 0)
			!=  (object1[1] -object2[3] > 0);
    }
}