mirror of
https://github.com/etlegacy/JoyStick.git
synced 2024-11-10 06:42:15 +00:00
small improvements
This commit is contained in:
parent
af997e84d2
commit
47643184eb
3 changed files with 31 additions and 26 deletions
|
@ -44,10 +44,10 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
|
|||
RectF rectF;
|
||||
float rotate;
|
||||
|
||||
double radians;
|
||||
double angle;
|
||||
double power;
|
||||
|
||||
double radians2;
|
||||
double angle2;
|
||||
|
||||
Vector<Star> stars;
|
||||
|
||||
|
@ -83,15 +83,15 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
|
|||
}
|
||||
}
|
||||
|
||||
posX -= Math.cos(radians) * (power/2);
|
||||
posY += Math.sin(-radians) * (power/2);
|
||||
posX -= Math.cos(angle) * (power/2);
|
||||
posY += Math.sin(-angle) * (power/2);
|
||||
if (posX > width - radius) posX = width - radius;
|
||||
if (posX < radius) posX = radius;
|
||||
if (posY > height - radius) posY = height - radius;
|
||||
if (posY < radius) posY = radius;
|
||||
|
||||
if (radians2 == 0) rotate = 0;
|
||||
else rotate = (float) Math.toDegrees(radians2) - 90;
|
||||
if (angle2 == 0) rotate = 0;
|
||||
else rotate = (float) Math.toDegrees(angle2) - 90;
|
||||
canvas.rotate(rotate, posX, posY);
|
||||
rectF.set(posX - radius, posY - radius, posX + radius, posY + radius);
|
||||
canvas.drawBitmap(droid, null, rectF, paint);
|
||||
|
@ -143,12 +143,12 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
|
|||
gameLoop = null;
|
||||
}
|
||||
|
||||
public void move(double radians, double power) {
|
||||
this.radians = radians;
|
||||
public void move(double angle, double power) {
|
||||
this.angle = angle;
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
public void rotate(double radians) {
|
||||
this.radians2 = radians;
|
||||
public void rotate(double angle2) {
|
||||
this.angle2 = angle2;
|
||||
}
|
||||
}
|
|
@ -18,17 +18,18 @@ public class MainActivity extends AppCompatActivity implements JoyStick.JoyStick
|
|||
((JoyStick)findViewById(R.id.joy1)).setListener(this);
|
||||
((JoyStick)findViewById(R.id.joy1)).setPadColor(Color.parseColor("#55ffffff"));
|
||||
((JoyStick)findViewById(R.id.joy1)).setButtonColor(Color.parseColor("#55ff0000"));
|
||||
|
||||
((JoyStick)findViewById(R.id.joy2)).setListener(new JoyStick.JoyStickListener() {
|
||||
@Override
|
||||
public void onMove(double radians, double power) {
|
||||
gameView.rotate(radians);
|
||||
}
|
||||
});
|
||||
((JoyStick)findViewById(R.id.joy2)).setListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMove(double radians, double power) {
|
||||
gameView.move(radians, power);
|
||||
public void onMove(JoyStick joyStick, double angle, double power) {
|
||||
switch (joyStick.getId()) {
|
||||
case R.id.joy1:
|
||||
gameView.move(angle, power);
|
||||
break;
|
||||
case R.id.joy2:
|
||||
gameView.rotate(angle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,14 +40,14 @@ public class JoyStick extends View {
|
|||
float radius;
|
||||
float buttonRadius;
|
||||
double power;
|
||||
double radians;
|
||||
double angle;
|
||||
int padColor;
|
||||
int buttonColor;
|
||||
Paint paint;
|
||||
JoyStickListener listener;
|
||||
|
||||
public interface JoyStickListener {
|
||||
void onMove(double radians, double power);
|
||||
void onMove(JoyStick joyStick, double angle, double power);
|
||||
}
|
||||
|
||||
public JoyStick(Context context) {
|
||||
|
@ -116,7 +116,7 @@ public class JoyStick extends View {
|
|||
posY = ((posY - centerY) * radius / abs + centerY);
|
||||
}
|
||||
|
||||
radians = Math.atan2(centerY - posY, centerX - posX);
|
||||
angle = Math.atan2(centerY - posY, centerX - posX);
|
||||
|
||||
power = (100 * Math.sqrt((posX - centerX)
|
||||
* (posX - centerX) + (posY - centerY)
|
||||
|
@ -128,14 +128,14 @@ public class JoyStick extends View {
|
|||
case MotionEvent.ACTION_CANCEL:
|
||||
posX = centerX;
|
||||
posY = centerY;
|
||||
radians = 0;
|
||||
angle = 0;
|
||||
power = 0;
|
||||
invalidate();
|
||||
break;
|
||||
}
|
||||
|
||||
if (listener != null) {
|
||||
listener.onMove(radians, power);
|
||||
listener.onMove(this, angle, power);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -158,7 +158,11 @@ public class JoyStick extends View {
|
|||
return power;
|
||||
}
|
||||
|
||||
public double getRadians() {
|
||||
return radians;
|
||||
public double getAngle() {
|
||||
return angle;
|
||||
}
|
||||
|
||||
public double getAngleDegrees() {
|
||||
return Math.toDegrees(angle);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue