package net.greencouchgames.ld27;

import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.util.color.Color;

import android.util.Log;

import tv.ouya.console.api.OuyaController;

public class s_Multi extends s_Game {
	Sprite spr_hand2;
	Sprite spr_player2;
	public Sprite spr_count;
	e_Player player2;
	
	int controller2;
	
	float timer = 10;
	int count = 9;
	public int score1 = 0;
	public int score2 = 0;
	public Text score1Text;
	public Text score2Text;
	
    public s_Multi(int c1, int c2) {
		super(c1);
		controller2 = c2;
		skiprest=true;
		
		layer_bg.detachChildren();
		Sprite spr_bg = new Sprite(0, 0, Main.spr_multi, Main.root.getVertexBufferObjectManager());
		layer_bg.attachChild(spr_bg);

		spr_hand.detachSelf();
		spr_hand.dispose();
		spr_player.detachSelf();
		spr_player.dispose();
		player = new e_Player(1);
		player.x -= 200;
		spr_hand = new Sprite(432, 352, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		spr_hand.setScaleCenter(8,8);
		spr_hand.setRotationCenter(8,8);
		spr_hand.setColor(player.hand);
		layer_play.attachChild(spr_hand);
		spr_player = new Sprite(408, 328, Main.spr_player1, Main.root.getVertexBufferObjectManager());
		spr_player.setScaleCenter(32,32);
		spr_player.setRotationCenter(32,32);
		layer_play.attachChild(spr_player);
		
		player2 = new e_Player(2);
		player2.x += 200;
		spr_hand2 = new Sprite(832, 352, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		spr_hand2.setScaleCenter(8,8);
		spr_hand2.setRotationCenter(8,8);
		spr_hand2.setColor(player2.hand);
		layer_play.attachChild(spr_hand2);
		spr_player2 = new Sprite(808, 328, Main.spr_player2, Main.root.getVertexBufferObjectManager());
		spr_player2.setScaleCenter(32,32);
		spr_player2.setRotationCenter(32,32);
		layer_play.attachChild(spr_player2);
		
		for (int i=0; i<layer_hud.getChildCount(); i++) layer_hud.getChildByIndex(i).dispose();
		layer_hud.detachChildren();

		Sprite spr_over = new Sprite(967, 0, Main.spr_bgscore, Main.root.getVertexBufferObjectManager());
		layer_hud.attachChild(spr_over);
		Text sL = new Text(1030, 32, Main.font1, "P1", Main.root.getVertexBufferObjectManager());
		sL.setPosition(1030-sL.getWidth(), 32);
		sL.setColor(new Color(0.3f,0.3f,0.3f));
		layer_hud.attachChild(sL);
		Text gL = new Text(1130, 32, Main.font1, "P2", Main.root.getVertexBufferObjectManager());
		gL.setPosition(1130-gL.getWidth(), 32);
		gL.setColor(new Color(0.3f,0.3f,0.3f));
		layer_hud.attachChild(gL);
		score1Text = new Text(1050, 24, Main.font2, "0", 3, Main.root.getVertexBufferObjectManager());
		score1Text.setColor(new Color(0,0,0));
		layer_hud.attachChild(score1Text);
		score2Text = new Text(1150, 24, Main.font2, "0", 3, Main.root.getVertexBufferObjectManager());
		score2Text.setColor(new Color(0,0,0));
		layer_hud.attachChild(score2Text);
		
		spr_count = new Sprite(608, 96, Main.spr_count10, Main.root.getVertexBufferObjectManager());
		spr_count.setScaleCenter(32,-128);
		spr_count.setScale(0);
		layer_hud.attachChild(spr_count);
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		if (paused) return;
		
		OuyaController c1 = OuyaController.getControllerByDeviceId(controller);
		OuyaController c2 = OuyaController.getControllerByDeviceId(controller2);
		
		if (fade!=null) {
			if (gotimer<=9) if (fade.getAlpha()>0.005) fade.setAlpha(fade.getAlpha()-0.005f);
			if (gameover.getX()>640-(float)gameover.getWidth()/(float)2) gameover.setPosition(gameover.getX()-10,gameover.getY());
			else gameover.setPosition(640-(float)gameover.getWidth()/(float)2,gameover.getY());
			gotimer-=pSecondsElapsed;
			if (gotimer<=8) {
				boolean getout = false;
				if (c1 != null) {
					if (c1.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
						getout=true;
						Main.select.play();
					}
				}
				if (c2 != null) {
					if (c2.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
						getout=true;
						Main.select.play();
					}
				}
				if (gotimer<=0) getout=true;
				
				if (getout) {
					for (int i=0; i<layer_bg.getChildCount(); i++) layer_bg.getChildByIndex(i).dispose();
					for (int i=0; i<layer_part.getChildCount(); i++) layer_part.getChildByIndex(i).dispose();
					for (int i=0; i<layer_proj.getChildCount(); i++) layer_proj.getChildByIndex(i).dispose();
					for (int i=0; i<layer_play.getChildCount(); i++) layer_play.getChildByIndex(i).dispose();
					for (int i=0; i<layer_tank.getChildCount(); i++) layer_tank.getChildByIndex(i).dispose();
					for (int i=0; i<layer_prot.getChildCount(); i++) layer_prot.getChildByIndex(i).dispose();
					for (int i=0; i<layer_hud.getChildCount(); i++) layer_hud.getChildByIndex(i).dispose();
					layer_bg.detachChildren();
					layer_part.detachChildren();
					layer_proj.detachChildren();
					layer_play.detachChildren();
					layer_tank.detachChildren();
					layer_prot.detachChildren();
					layer_hud.detachChildren();
					layer_bg.detachSelf(); layer_bg.dispose();
					layer_part.detachSelf(); layer_part.dispose();
					layer_proj.detachSelf(); layer_proj.dispose();
					layer_play.detachSelf(); layer_play.dispose();
					layer_tank.detachSelf(); layer_tank.dispose();
					layer_prot.detachSelf(); layer_prot.dispose();
					layer_hud.detachSelf(); layer_hud.dispose();
					u_Enemy.stop=false;
					Main.root.setCurrentScene(new s_Menu(true));
				}
			}
			return;
		}
		
		if (c1 != null) {
			player.update(pSecondsElapsed,c1,spr_player,spr_hand);
		}
		if (c2 != null) {
			player2.update(pSecondsElapsed,c2,spr_player2,spr_hand2);
		}
		
		if (u_Explosion.timer>0) {
			u_Explosion.timer-=pSecondsElapsed;
			if (u_Explosion.timer<=0) {
				Main.root.resetCamera();
				u_Explosion.timer=0;
			} else Main.root.shakeCamera();
		}

		score1Text.setText(""+score1);
		score2Text.setText(""+score2);
		if (score1>=10) {
			if (!u_Enemy.stop) {
				gameover();
				spr_player2.detachSelf(); spr_player2.dispose();
				spr_hand2.detachSelf(); spr_hand2.dispose();
				gameover.detachSelf();
				gameover.dispose();
				gameover = new Text(1280, 360, Main.font3, "P1 WINS!", Main.root.getVertexBufferObjectManager());
				gameover.setPosition(1280,360-(float)gameover.getHeight()/(float)2);
				gameover.setColor(new Color(0,0,0.5f));
				layer_hud.attachChild(gameover);
			}
		} else if (score2>=10) {
			if (!u_Enemy.stop) {
				gameover();
				spr_player2.detachSelf(); spr_player2.dispose();
				spr_hand2.detachSelf(); spr_hand2.dispose();
				gameover.detachSelf();
				gameover.dispose();
				gameover = new Text(1280, 360, Main.font3, "P2 WINS!", Main.root.getVertexBufferObjectManager());
				gameover.setPosition(1280,360-(float)gameover.getHeight()/(float)2);
				gameover.setColor(new Color(0.5f,0,0));
				layer_hud.attachChild(gameover);
			}
		}
		
		timer-=pSecondsElapsed;
		spr_count.setScale(Math.abs((timer%1)-1));
		if (timer<=0) {
			timer=10;
			count=10;
			e_Projectile p = u_Projectiles.addBakedGood(640, -19);
			float tempX = p.spr.getX();
			float tempY = p.spr.getY();
			float tempR = p.spr.getRotation();
			p.spr.detachSelf();
			p.spr.dispose();
			p.spr = new Sprite(tempX, tempY, Main.spr_baked1, Main.root.getVertexBufferObjectManager());
			p.sprite = Main.spr_baked1;
			p.rottype = 1;
			p.spr.setRotation(tempR);
			p.trail = new Color(0.78f,0.44f,0.22f);
			p.trailsize = 1.5f;
			p.xs = 0;
			p.ys = 360+240*(float)Math.random();
			layer_proj.attachChild(p.spr);
		}
		if (timer<=count) {
			spr_count.detachSelf();
			spr_count.dispose();
			if (count==10) spr_count = new Sprite(608, 96, Main.spr_count10, Main.root.getVertexBufferObjectManager());
			else if (count==9) spr_count = new Sprite(608, 96, Main.spr_count9, Main.root.getVertexBufferObjectManager());
			else if (count==8) spr_count = new Sprite(608, 96, Main.spr_count8, Main.root.getVertexBufferObjectManager());
			else if (count==7) spr_count = new Sprite(608, 96, Main.spr_count7, Main.root.getVertexBufferObjectManager());
			else if (count==6) spr_count = new Sprite(608, 96, Main.spr_count6, Main.root.getVertexBufferObjectManager());
			else if (count==5) spr_count = new Sprite(608, 96, Main.spr_count5, Main.root.getVertexBufferObjectManager());
			else if (count==4) spr_count = new Sprite(608, 96, Main.spr_count4, Main.root.getVertexBufferObjectManager());
			else if (count==3) spr_count = new Sprite(608, 96, Main.spr_count3, Main.root.getVertexBufferObjectManager());
			else if (count==2) spr_count = new Sprite(608, 96, Main.spr_count2, Main.root.getVertexBufferObjectManager());
			else spr_count = new Sprite(608, 96, Main.spr_count1, Main.root.getVertexBufferObjectManager());
			spr_count.setScaleCenter(32,-128);
			spr_count.setScale(0);
			layer_hud.attachChild(spr_count);
			count--;
		}
		
		OuyaController.startOfFrame();
	}
}
