package tw.edu.hust.bn98107;

import java.util.Iterator;
import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Ball {
	private int x, y; // 圓心坐標
	private int r; // 半徑
	private int vx, vy; // v = (vx, vy); tan(x) = vy/vx;
	private int color; // 顏色

	// 球的初始資訊
	public Ball(int x, int y, int r, int vx, int vy, int cc) {
		this.x = x;
		this.y = y;
		this.r = r;
		this.vx = vx;
		this.vy = vy;
		this.color = cc;

	}

	// 畫球
	public void draw(Canvas canvas) {
		Paint paint = new Paint();// 新增畫筆
		paint.setColor(color);// 設定畫筆顏色
		canvas.drawCircle(x, y, r, paint);
	}

	// 移動
	public void move(int width, int height) {
		x += vx;
		y += vy;
		// 微調
		// 1. 左右
		if (x - r < 0) {
			x = r;
			vx = -vx;
		} else if (x + r > width) {
			x = width - r;
			vx = -vx;
		}
		// 2. 上下
		if (y - r < 0) {
			y = r;
			vy = -vy;
		} else if (y + r > height) {
			y = height - r;
			vy = -vy;
		}
	}
	public Rect getRect() {
		return new Rect(x - r, y - r, x + r, y + r);
	}
	// 移動
	public void move(int height, int width,Block bb,Bar bar) {
		x += vx;
		y += vy;
		// 微調
		// 1. 左右
		if (x - r < 0) {
			x = r;
			vx = -vx;
		} else if (x + r > width) {
			x = width - r;
			vx = -vx;
		}
		// 2. 上下
		if (y - r < 0) {
			y = r;
			vy = -vy;
		} else if (y + r > height) {
			y = height - r;
			vy = -vy;
		}
		//3.Block
		if (isCollision(bb) ==true ){
			vy =-vy;
		}
		//4.Bar
		if (isCollision(bar) ==true ){
			vy =-vy;
		}
	}

	private boolean isCollision(Block bb) {
		// TODO Auto-generated method stub
		Rect r1=this.getRect();
		Rect r2=bb.getRect();
		if (r1.intersect(r2)){
			return true;}
		return false;
		
	}
	private boolean isCollision(Bar bar) {
		// TODO Auto-generated method stub
		Rect r1=this.getRect();
		Rect r2=bar.getRect();
		if (r1.intersect(r2)){
			return true;}
		return false;
		
	}
}
