using UnityEngine;
using System.Collections;

public class PlayerCollision : MonoBehaviour {

	public GameObject[] SpaceMan;
	public Rect[] rect;

	int life;
	BoxCollider2D bCollider;

	// Use this for initialization
	void Start () {
		if(bCollider==null){
			bCollider = (BoxCollider2D)collider2D;
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void Initialize(){
		life = 3;
		for(int i =0;i<3;i++){
			SpaceMan[i].SetActive(true);
		}
		gameObject.SetActive (true);
		if(bCollider==null){
			bCollider = (BoxCollider2D)collider2D;
		}
		bCollider.center = rect [2].position;
		bCollider.size = rect [2].size;
	}

	void SpaceManDead(){
		life--;
		switch(life){
		case 2:
			SpaceMan[2].SetActive(false);
			bCollider.center = rect[1].position;
			bCollider.size = rect[1].size;
			break;
		case 1:
			SpaceMan[1].SetActive(false);
			bCollider.center = rect[0].position;
			bCollider.size = rect[0].size;
			break;
		case 0:
			PlayerController.instance.transform.parent = null;
			SpaceMan[0].SetActive(false);
			gameObject.SetActive(false);
			GameManager.instance.GameOver();
			break;
		}
	}

	void OnCollisionEnter2D(Collision2D coll){
		if(coll.gameObject.tag=="astroid"){
			PlayerController.instance.CollisionHappen(coll);
			SpaceManDead();
		}
	}
	/*
	void OnCollisionExit2D(Collision2D coll){
		if(coll.gameObject.tag=="astroid"){
			transform.localPosition = Vector3.zero;
		}
	}
	*/
}
