package com.backfire.Animation;

import net.yeeyo.doudizhu.feiyu.R;
import android.graphics.Bitmap;
import android.graphics.Rect;

import com.backfire.doudizhu.common.BitmapManager;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.graphics.PCanvas;
import com.backfire.room.data.ConfigCenter;

/**
 * @Title: AnimationAirplane.java
 * @Package com.backfire.Animation
 * @Description: TODO(用一句话描述该文件做什么)
 * @author Potter
 * @date 2013-11-26 下午8:21:34
 * @version V2.0
 */
public class AnimationAirplane extends Animation {

	private final int[] textures = { R.drawable.plane_anim_01,
			R.drawable.plane_anim_02, R.drawable.plane_anim_03,
			R.drawable.plane_anim_04, R.drawable.plane_anim_05,
			R.drawable.plane_anim_06, R.drawable.plane_anim_07,
			R.drawable.plane_anim_08, R.drawable.plane_anim_09,
			R.drawable.plane_anim_10, R.drawable.plane_anim_11,
			R.drawable.plane_anim_12, R.drawable.plane_anim_13,
			R.drawable.plane_anim_14 };
	private int index;

	private Rect rect1, rect2;
	float statetime = 0.0f;
	private final int textureBmpWindth = ConfigCenter.plane_anim_smoke_W;
	private final int textureBmpHeight = ConfigCenter.plane_anim_smoke_H;
	private final int bmpWindth = ConfigCenter.plane_anim_body_W;
	private final int bmpHeight = ConfigCenter.plane_anim_body_H;
	private final int relocateY_H = ConfigCenter.plane_anim_relacate_Y_H;
	private final int planePathLength=ConfigCenter.plane_anim_x_path_length;
	private int planeStepLength=ConfigCenter.plane_anim_x_step_lenght;

	private final int x = (DdzApplication.getApplication().screenWidth-planePathLength) / 2
			+ planePathLength-(bmpWindth+textureBmpWindth);
	private final int y = DdzApplication.getApplication().screenHeight / 2
			- textureBmpHeight;

	public AnimationAirplane() {
		rect1 = new Rect();
		rect2 = new Rect();
		if(textures.length>0){
			planeStepLength=(planePathLength-(bmpWindth+textureBmpWindth))/textures.length;
		}
	}

	@Override
	public void exec(PCanvas canvas, long sleep) {
		super.exec(canvas, sleep);
		statetime += this.sleep;
		index = getCurrentBitmap(statetime, textures);
		BitmapManager bpool = DdzApplication.getApplication().getBmpPool();
		Bitmap textureBmp = bpool.getBitmap(textures[index], 0, 0);
		Bitmap bmp = bpool.getBitmap(R.drawable.plane_anim_body, 0, 0);
		int tmpY = index % 2 == 0 ? (y+relocateY_H) : (y-relocateY_H);
		if (textureBmp != null && bmp != null) {
			rect2.set(x - index * planeStepLength, tmpY, x - index * planeStepLength
					+ bmpWindth, tmpY + bmpHeight);
			rect1.set(rect2.right, rect2.top, rect2.right + textureBmpWindth,
					rect2.top + textureBmpHeight);
			canvas.drawBitmap(bmp, null, rect2, null);
			canvas.drawBitmap(textureBmp, null, rect1, null);
		}
		if (index == textures.length - 1) {
			index = 0;
			this.deleteFlag = true;
		} 
	}

	@Override
	public void unload() {
		// TODO Auto-generated method stub

	}

	@Override
	public void overstatue() {
		this.deleteFlag = true;
	}
}