利用接口只暴露出两个函数出来,内部实现其功能.
1.接口
public interface Walker {
//显示人物
void show(Canvas canvas);
//目标坐标
void targetPostion(float x, float y);
}
2. 计算位置
/**
* 计算坐标位置, 方向.
* @author Administrator
*
*/
public class CalcWalk {
private boolean isFinish;
/**
*
* @param x 贴图位置x
* @param y 贴图位置y
* @param step 步长
*/
public CalcWalk(int x, int y, int step ) {
this.x = x;
this.y = y;
this.step = step;
direction = Direction.DOWN;
stepX = stepY = 0;
xTarget = yTarget= 0;
isFinish = true;
}
/**
* 得到目标位置坐标()
* @param f
* @param g
*/
public void targetXY(float f, float g) {
this.xTarget = f;
this.yTarget = g;
isFinish = false;
initData();
}
//得到下一步贴图位置
public void nextPostion() {
//System.out.println("dx="+(xTarget - x) +" sx="+stepX+ " dy="+(yTarget - y)+" sy"+stepY);
float tmX = x, tmY = y;
x = Math.abs(xTarget - x) > Math.abs(stepX) ? x+stepX : x;
y = Math.abs(yTarget - y) > Math.abs(stepY) ? y+stepY : y;
if (tmX == x && tmY == y) {
isFinish = true;
}
}
/**
* 初始化数据,对穿进来的目标位置进行处理.
* 得到偏移量,确定贴图行走的方向
* @param xTarget 目标坐标
* @param yTarget
*/
private void initData() {
float dx = xTarget - x;
float dy = yTarget - y;
float move;
if (Math.abs(dx) > Math.abs(dy)) {
stepX = Mat