Android Open Source - endlessJourney Main Game Panel






From Project

Back to project page endlessJourney.

License

The source code is released under:

GNU General Public License

If you think the Android project endlessJourney listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kambashi.endlessjourney;
//from  w w w. j  a  va 2 s .  c o  m
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.kambashi.endlessjourney.model.Bot;
import com.kambashi.endlessjourney.model.components.Speed;

public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

  private static final String TAG = MainGamePanel.class.getSimpleName();

  private MainThread thread;
  private Bot bot;

  public MainGamePanel(Context context) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    // create bot and load bitmap
    bot = new Bot(BitmapFactory.decodeResource(getResources(),
        R.drawable.bot_1), 50, 50);

    // create the game loop thread
    thread = new MainThread(getHolder(), this);

    // make the GamePanel focusable so it can handle events
    setFocusable(true);
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    // at this point the surface is created and
    // we can safely start the game loop
    thread.setRunning(true);
    thread.start();
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "Surface is being destroyed");
    // tell the thread to shut down and wait for it to finish
    // this is a clean shutdown
    boolean retry = true;
    while (retry) {
      try {
        thread.join();
        retry = false;
      } catch (InterruptedException e) {
        // try again shutting down the thread
      }
    }
    Log.d(TAG, "Thread was shut down cleanly");
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      // delegating event handling to the bot
      bot.handleActionDown((int) event.getX(), (int) event.getY());

      // check if in the lower part of the screen we exit
      if (event.getY() > getHeight() - 50) {
        thread.setRunning(false);
        ((Activity) getContext()).finish();
      } else {
        Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
      }
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
      // the gestures
      if (bot.isTouched()) {
        // the bot was picked up and is being dragged
        bot.setX((int) event.getX());
        bot.setY((int) event.getY());
      }
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
      // touch was released
      if (bot.isTouched()) {
        bot.setTouched(false);
      }
    }
    return true;
  }

  // the fps to be displayed
  private String avgFps;

  public void setAvgFps(String avgFps) {
    this.avgFps = avgFps;
  }

  public void render(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    bot.draw(canvas);
    // display fps
    displayFps(canvas, avgFps);
  }

  private void displayFps(Canvas canvas, String fps) {
    if (canvas != null && fps != null) {
      Paint paint = new Paint();
      paint.setARGB(255, 255, 255, 255);
      canvas.drawText(fps, this.getWidth() - 50, 20, paint);
    }
  }

  public void update() {
    // check collision with right wall if heading right
    if (bot.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
        && bot.getX() + bot.getBitmap().getWidth() / 2 >= getWidth()) {
      bot.getSpeed().toggleXDirection();
    }
    // check collision with left wall if heading left
    if (bot.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
        && bot.getX() - bot.getBitmap().getWidth() / 2 <= 0) {
      bot.getSpeed().toggleXDirection();
    }
    // check collision with bottom wall if heading down
    if (bot.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
        && bot.getY() + bot.getBitmap().getHeight() / 2 >= getHeight()) {
      bot.getSpeed().toggleYDirection();
    }
    // check collision with top wall if heading up
    if (bot.getSpeed().getyDirection() == Speed.DIRECTION_UP
        && bot.getY() - bot.getBitmap().getHeight() / 2 <= 0) {
      bot.getSpeed().toggleYDirection();
    }
    // Update the lone bot
    bot.update();
  }
}




Java Source Code List

com.kambashi.endlessjourney.EndlessActivity.java
com.kambashi.endlessjourney.MainGamePanel.java
com.kambashi.endlessjourney.MainThread.java
com.kambashi.endlessjourney.model.Bot.java
com.kambashi.endlessjourney.model.components.Speed.java