?? ball.java
字號:
import javax.microedition.lcdui.*;
public class Ball {
int x, y; //坐標
int spd; //速度
int Vx, Vy; //速度分量
boolean moving; //是否移動
int nx, ny; //下一次的坐標
byte arraySign; //球在數組中的編號
boolean onBag;//是否在袋上
boolean alive; //是否在桌上
int standForceX, standForceY;//外力的X,Y值
int hittedBalls;//碰到過的球
byte style;//球的樣式
byte ballSign;
public Ball(Ball ball) {
this.x = ball.x;
this.y = ball.y;
nx = ball.nx;
ny = ball.ny;
Vx = ball.Vx;
Vy = ball.Vy;
arraySign = ball.arraySign;
alive = ball.alive;
standForceX = ball.standForceX;
standForceY = ball.standForceY;
hittedBalls = ball.hittedBalls;
onBag = ball.onBag;
moving = ball.moving;
style = ball.style;
ballSign = ball.ballSign;
}
/**
* @param x int X坐標
* @param y int Y坐標
* @param s int 編號
* @param t int 樣式
*/
public Ball(int x, int y, int as, int bs) {
this.x = x * 100;
this.y = y * 100;
nx = x * 100;
ny = y * 100;
Vx = 0;
Vy = 0;
arraySign = (byte) as;
alive = true;
standForceX = 0;
standForceY = 0;
hittedBalls = 0x0;
onBag = false;
moving = false;
ballSign = (byte) bs;
switch(bs){
case 0: style = a.STYLE_WHITE;break;
case 8: style = a.STYLE_BLACK;break;
default:
if( bs < 8){
style = a.STYLE_SOLID;
}
else{
style = a.STYLE_STRIPE;
}
}
}
/**
* 畫球
* @param g Graphics
* @param ballImage Image
*/
public void drawBall(Graphics g, Image ballImage) {
g.drawImage(ballImage, x / 100, y / 100, g.HCENTER | g.VCENTER);
g.setColor(0xffffff);
g.setFont(Font.getFont(0, 0, Font.SIZE_SMALL));
g.drawString("" + arraySign, x / 100, y / 100 - 6, g.HCENTER | g.TOP);
if (onBag) {
// spd = 0;
// a.setBallsState(arraySign,false);
onBag = false;
}
}
/**
* 白球受力
* @param force int
* @param x1 int
* @param y1 int
*/
public void onHit(int force, int x1, int y1) {
a.setBallsState(0, true);
int s = a.sqrt( (x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
Vx = (x1 - x) * 10000 / s;
Vy = (y1 - y) * 10000 / s;
spd = force;
moving = true;
}
/**
* 移動一湞
*/
public void move() {
spd = spd * 97 / 100;
if (spd <= 70) {
a.setBallsState(arraySign, false);
spd = 0;
moving = false;
if (a.think) { //-------------think---------------------------------------
if (arraySign == 0) {
a.whiteBallStop = true;
}
else
if (arraySign == a.curHitSign) {
a.curBallStop = true;
}
} //------------------------end think--------------------------------
}
}
/**
* 移動一步
*/
public void moveStep() {
x = nx;
y = ny;
if (inBag()) {
if (a.think) { //---------think-----------------------------------------
if (this.arraySign == a.curHitSign) {
a.curBallIn = true;
}
else
if (this.arraySign == 0) {
a.whiteBallIn = true;
if (a.curTrackSign == 0) {
a.fail = true;
}
}
} //--------------------end think--------------------------------------
else{
if(!a.firstIn){
// a.firstIn = true;
switch(this.style){
case a.STYLE_WHITE:
break;
case a.STYLE_BLACK:
break;
case a.STYLE_SOLID:
a.setTargetBalls(a.isPlayerTurn,a.KIND_SOLID);
a.setTargetBalls(!a.isPlayerTurn,a.KIND_STRIPE);
break;
case a.STYLE_STRIPE:
a.setTargetBalls(a.isPlayerTurn,a.KIND_STRIPE);
a.setTargetBalls(!a.isPlayerTurn,a.KIND_SOLID);
break;
}
}
}
spd = 0;
a.setBallsState(arraySign, false);
alive = false;
}
resetData();
}
/**
* 復位一些數據
*/
public void resetData() {
hittedBalls = 0;
standForceX = 0;
standForceY = 0;
}
/**
* 計算和球的碰撞,得到一個新的狀態
*/
public void getNextPost() {
for (int i = 0; i < a.tempBalls.length; i++) {
if (i == arraySign
|| !a.tempBalls[i].alive
|| ( (hittedBalls >>> i) & 0x00000001) == 1
) {
continue;
}
if (isNear(a.tempBalls[i])) {
hit(a.tempBalls[i], true);
}
}
}
/**
* 得到下一個不和球碰撞的坐標,和邊碰撞的結果實現
*/
public void getNextSet() {
nx = x + spd * Vx / (a.t * 10000);
ny = y + spd * Vy / (a.t * 10000);
if (nx <= a.SIDE) {
if ( (ny > 1960 && ny < 9134) || ny > 10866 && ny < 18040) {
nx += (a.SIDE - nx) * 2;
Vx = Math.abs(Vx);
}
}
if (nx >= a.w - a.SIDE) {
if ( (ny > 1960 && ny < 9134) || ny > 10866 && ny < 18040) {
nx -= (nx - a.w + a.SIDE) * 2;
Vx = -Math.abs(Vx);
}
}
if (ny <= a.SIDE) {
if (nx > 1960 && nx < 13040) {
ny += (a.SIDE - ny) * 2;
Vy = Math.abs(Vy);
}
}
if (ny >= a.h - a.SIDE) {
if (nx > 1960 && nx < 13040) {
ny -= (ny - a.h + a.SIDE) * 2;
Vy = -Math.abs(Vy);
}
}
}
/**
* 判斷是否撞到了其他球
* @param ball Ball 被撞的球
* @return boolean 返回是否撞到
*/
private boolean isNear(Ball ball) {
int dtx = Math.abs(nx - ball.nx);
int dty = Math.abs(ny - ball.ny);
if (dtx <= a.ballD && dty <= a.ballD) {
if ( ( (dtx * dtx) + (dty * dty)) <= a.ballDD) {
return true;
}
}
return false;
}
/**
* 撞到其他的球
* @param ball Ball 撞到的球
* @param initiative boolean 是否是主動撞到的
*/
public void hit(Ball ball, boolean initiative) {
if (a.think) { //--------------think--------------------------------------------------------
if (a.curTrackSign == 0 && arraySign == 0) { //---------------當前目標球為白球
if (ball.arraySign == a.curHitSign) { //---------撞到的球是目標球
a.curTrackSign = a.curHitSign; //-------設置當前跟蹤目標球
a.curBallStop = false;
}
else { //----------------------------------撞到的球非目標球
if (a.PRINT) System.out.println("!!!!!!!!!!" + "應該碰:" + a.curHitSign +" 結果是" + ball.arraySign); ///////////////
a.fail = true; //-----------------------失敗
a.haveSet = false;
}
}
else
if (a.curTrackSign == a.curHitSign && arraySign == a.curHitSign) { //----------------------------------當前目標球為被碰撞球
if (ball.arraySign != 0) { //------------------撞到的不是白球
a.thinkStop = true; //----------------思考停止
if (a.PRINT) System.out.println(
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!碰到的球:" + ball.arraySign +" 跟蹤的球:" + a.curTrackSign);
}
}
if (ball.arraySign == 0) {
a.whiteBallStop = false;
}
} //------------------------end think-------------------------------------------------------
a.setBallsState(arraySign, true); //將自己的狀態改為動態
hittedBalls |= (0x00000001 << ball.arraySign);
boolean haveSpd = false;
if (moving) {
int c = 0, d = 0;
c = ball.nx - nx;
d = ball.ny - ny;
// if(a.haveSet && ball.arraySign == a.curHitSign){//-------AI打中正確的球時-------
if (a.haveSet) {
if (a.think) {
if (ball.arraySign == a.curHitSign && this.arraySign == 0) {
c = ball.nx - a.AIx;
d = ball.ny - a.AIy;
}
}
else {
if (ball.arraySign == (byte) a.dataLevel[a.hitLevel][6] && this.arraySign == 0) {
c = ball.nx - a.dataLevel[a.hitLevel][3];
d = ball.ny - a.dataLevel[a.hitLevel][4];
}
}
a.haveSet = false;
}
int m = spd * Vx / 10200,
n = spd * Vy / 10200;
int temp0 = c * m + d * n;
if (temp0 > 0) {
int temp1 = c * c + d * d;
int x1 = c * temp0 / temp1,
y1 = d * temp0 / temp1;
int x0 = m - x1,
y0 = n - y1;
ball.addForce(x1, y1);
this.addForce(x0, y0);
}
else {
haveSpd = true;
}
}
if (initiative) {
ball.hit(this, false);
composeForce(haveSpd);
}
else {
composeForce(haveSpd);
return;
}
}
/**
* 給球加一個力量
* @param forceX int X力量
* @param forceY int Y力量
*/
public void addForce(int forceX, int forceY) {
standForceX += forceX;
standForceY += forceY;
}
/**
* 碰撞后計算合力
* @param haveSpeed boolean 是否保留自己之前的速度
*/
public void composeForce(boolean haveSpeed) {
if (haveSpeed) {
standForceX += (spd * Vx / 10000);
standForceY += (spd * Vy / 10000);
}
spd = a.sqrt(standForceX * standForceX + standForceY * standForceY);
if (spd <= 80) {
spd = 0;
Vx = 0;
Vy = 0;
moving = false;
}
else {
Vx = standForceX * 10000 / spd;
Vy = standForceY * 10000 / spd;
moving = true;
}
standForceX = 0;
standForceY = 0;
}
/**
* 球進袋
* @return boolean 是否進袋
*/
public boolean inBag() {
int dtx, dty;
if (x <= 1960) {
if (y >= 9134 && y <= 10866) { //1
dtx = x - 500;
dty = y - 10000;
if ( (dtx * dtx + dty * dty) < 1000000) {
x = 1100;
y = 10000;
onBag = true;
return true;
}
}
else if (y <= 1960) { //0
dtx = x;
dty = y;
if ( (dtx * dtx + dty * dty) < 4840000) {
x = 1100;
y = 1100;
onBag = true;
return true;
}
}
else if (y >= 18040) { //2
dtx = x;
dty = y - 20000;
if ( (dtx * dtx + dty * dty) < 4840000) {
x = 1100;
y = 18900;
onBag = true;
return true;
}
}
}
else if (x >= 13040) {
if (y >= 9134 && y <= 10866) { //4
dtx = x - 14500;
dty = y - 10000;
if ( (dtx * dtx + dty * dty) < 1000000) {
x = 13900;
y = 10000;
onBag = true;
return true;
}
}
else if (y <= 1960) { //3
dtx = x - 15000;
dty = y;
if ( (dtx * dtx + dty * dty) < 4840000) {
x = 13900;
y = 1100;
onBag = true;
return true;
}
}
else if (y >= 18040) { //5
dtx = x - 15000;
dty = y - 20000;
if ( (dtx * dtx + dty * dty) < 4840000) {
x = 13900;
y = 18900;
onBag = true;
return true;
}
}
}
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -