Modified:
trunk/src/android/com/abb/Avatar.java
trunk/src/android/com/abb/Enemy.java
trunk/src/android/com/abb/Game.java
trunk/src/android/com/abb/GameState.java
trunk/src/android/com/abb/GameView.java
Log:
Added touch screen controls.
Modified: trunk/src/android/com/abb/Avatar.java
==============================================================================
--- trunk/src/android/com/abb/Avatar.java (original)
+++ trunk/src/android/com/abb/Avatar.java Tue Jan 13 19:23:12 2009
@@ -14,6 +14,7 @@
import android.graphics.Rect;
import android.net.Uri;
import android.view.KeyEvent;
+import android.view.MotionEvent;
public class Avatar extends ArticulatedEntity {
@@ -34,8 +35,8 @@
mWeapon.x = x;
mWeapon.y = y;
- // Update the horizontal acceleration acceleration according to the
current
- // controls and the contact with the ground.
+ // Update the horizontal acceleration according to the current
controls and
+ // the contact with the ground.
if (ddx > 0 && has_ground_contact) {
ddx = +kGroundAcceleration;
} else if (ddx > 0 && !has_ground_contact) {
@@ -77,7 +78,7 @@
// Update the shooting mechanism. The choices for shot direction are
// specialized for each animation case: in the air, facing left,
right, and
- // considering the avatar's speed. TODO(barnes): Replace all of this
with an
+ // considering the avatar's speed. TODO: Replace all of this with an
// equivalent in Weapon.java.
mShotDelay -= time_step;
if (mShooting && mShotDelay < time_step) {
@@ -117,6 +118,17 @@
}
}
+ @Override
+ public void draw(Graphics graphics, float center_x, float center_y,
+ float zoom) {
+ // We intercept the draw method only to get the canvas dimensions. The
+ // drawing buffer dimensions are used to interpret the touch events.
+ mCanvasWidth = graphics.getWidth();
+ mCanvasHeight = graphics.getHeight();
+
+ super.draw(graphics, center_x, center_y, zoom);
+ }
+
public void setKeyState(int key_code, int state) {
if (key_code == kKeyLeft) {
ddx = -kGroundAcceleration * state;
@@ -124,11 +136,60 @@
ddx = +kGroundAcceleration * state;
} else if (key_code == kKeyJump && state == 1 && has_ground_contact) {
dy -= kJumpVelocity;
+ has_ground_contact = false;
} else if (key_code == kKeyShoot) {
mShooting = (state == 1);
}
}
+ public void onMotionEvent(MotionEvent motion_event) {
+ // We translate motion events into key events and then pass it onto
the key
+ // event handler. Note: Pressure and size measurements are also
available
+ // from the API, but aren't yet used here.
+ int action = motion_event.getAction();
+ if (action == MotionEvent.ACTION_DOWN ||
+ action == MotionEvent.ACTION_MOVE) {
+ // Handled below.
+ } else if (action == MotionEvent.ACTION_UP) {
+ setKeyState(kKeyLeft, 0);
+ setKeyState(kKeyRight, 0);
+ setKeyState(kKeyJump, 0);
+ setKeyState(kKeyShoot, 0);
+ return;
+ } else {
+ return;
+ }
+ int x = (int)motion_event.getX();
+
+ // The touch event was in the movement section of the display surface.
+ if (motion_event.getY() > mCanvasHeight - kTouchMovementHeight) {
+ setKeyState(kKeyJump, 0);
+ setKeyState(kKeyShoot, 0);
+ if (motion_event.getX() < mCanvasWidth / 2) {
+ setKeyState(kKeyRight, 0);
+ setKeyState(kKeyLeft, 1);
+ } else {
+ setKeyState(kKeyLeft, 0);
+ setKeyState(kKeyRight, 1);
+ }
+ }
+ // the touch event was in the action section. (Any area above the
movement
+ // section of the display surface.)
+ else {
+ setKeyState(kKeyLeft, 0);
+ setKeyState(kKeyRight, 0);
+ if (motion_event.getX() < mCanvasWidth / 2) {
+ setKeyState(kKeyShoot, 0);
+ setKeyState(kKeyJump, 1);
+ } else {
+ setKeyState(kKeyJump, 0);
+ setKeyState(kKeyShoot, 1);
+ }
+ }
+ }
+
+ private int mCanvasWidth;
+ private int mCanvasHeight;
private GameState mGameState;
private float mShotDelay;
private boolean mShooting;
@@ -155,4 +216,5 @@
private static final float kShotSpread = 15.0f * (float)Math.PI / 180.0f;
private static final float kShotVelocity = 60.0f;
private static final int kSpriteSize = 64;
+ private static final int kTouchMovementHeight = 30;
}
Modified: trunk/src/android/com/abb/Enemy.java
==============================================================================
--- trunk/src/android/com/abb/Enemy.java (original)
+++ trunk/src/android/com/abb/Enemy.java Tue Jan 13 19:23:12 2009
@@ -127,5 +127,5 @@
private static final String kParameterGravity = "gravity";
private static final String kParameterLife = "life";
private static final String kParameterRadius = "radius";
- private static final float kRange = 500.0f;
+ private static final float kRange = 1000.0f;
}
Modified: trunk/src/android/com/abb/Game.java
==============================================================================
--- trunk/src/android/com/abb/Game.java (original)
+++ trunk/src/android/com/abb/Game.java Tue Jan 13 19:23:12 2009
@@ -14,6 +14,7 @@
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
+import android.view.MotionEvent;
/** Simple interface which hides most of the Android specifics. All method
calls
@@ -22,7 +23,8 @@
void initializeGraphics(Graphics graphics);
void reset();
+ boolean onFrame(Graphics graphics, float time_step);
boolean onKeyDown(int key_code);
boolean onKeyUp(int key_code);
- boolean onFrame(Graphics graphics, float time_step);
+ void onMotionEvent(MotionEvent motion_event);
}
Modified: trunk/src/android/com/abb/GameState.java
==============================================================================
--- trunk/src/android/com/abb/GameState.java (original)
+++ trunk/src/android/com/abb/GameState.java Tue Jan 13 19:23:12 2009
@@ -21,6 +21,7 @@
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
+import android.view.MotionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
@@ -73,6 +74,10 @@
public boolean onKeyUp(int key_code) {
avatar.setKeyState(key_code, 0);
return false; // False to indicate not handled.
+ }
+
+ public void onMotionEvent(MotionEvent motion_event) {
+ avatar.onMotionEvent(motion_event);
}
public boolean onFrame(Graphics graphics, float time_step) {
Modified: trunk/src/android/com/abb/GameView.java
==============================================================================
--- trunk/src/android/com/abb/GameView.java (original)
+++ trunk/src/android/com/abb/GameView.java Tue Jan 13 19:23:12 2009
@@ -26,6 +26,7 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
+import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@@ -62,9 +63,9 @@
// Since our target platform is a mobile device, we should do what
we can
// to save power. In the case of a game like this, we should 1)
limit the
- // framerate to something "reasonable" and 2) pause the updates as
much as
- // possible. Here we define the maximum framerate which needs to
make the
- // trade off between graphics fluidity and power savings.
+ // frame rate to something "reasonable" and 2) pause the updates as
much
+ // as possible. Here we define the maximum frame rate which needs to
make
+ // the trade off between graphics fluidity and power savings.
final float kMaxFrameRate = 30.0f; // Frames / second.
final float kMinFrameRate = 6.0f; // Frames / second.
final float kMinTimeStep = 1.0f / kMaxFrameRate; // Seconds.
@@ -87,7 +88,7 @@
}
// Calculate the interval between this and the previous frame. See
note
- // above regarding system timers. If we have exceeded our framerate
+ // above regarding system timers. If we have exceeded our frame
rate
// budget, sleep.
long current_time = System.nanoTime();
float time_step = (float)(current_time - time) * 1.0e-9f;
@@ -215,6 +216,19 @@
if (mGameThread != null) {
mGameThread.pause(!window_has_focus);
}
+ }
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ if (!mTitleViewHidden) {
+ mTitleView.setText("");
+ mTitleViewHidden = true;
+ }
+
+ synchronized (mGame) {
+ mGame.onMotionEvent(event);
+ }
+ return true;
}
/** Callback invoked when the Surface has been created and is ready to be