KafunWarsActivity2

package org.yasrun.game.kafunwars;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class KafunWarsActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    MyView mView = new MyView(getApplication());

    setContentView(mView);

    mView.start();
  }
}

/**
* 描画用のクラス
*/
class MyView extends View implements Updatable {
  private static final int KAFUN_MAX = 100;
  private Paint myPaint = new Paint();
  private Bitmap bmpHana = null;
  private Bitmap bmpKafun = null;
  private List<Kafun> kafuns = null;
  private List<Laser> lasers = null;
  private UpdateHandler updateHandler = new UpdateHandler(this);
  private int touchStartX = 0;
  private int touchStartY = 0;
  private int power = 0;

  /**
   * コンストラクタ
   *
   * @param c
   */
  public MyView(Context c) {
    super(c);
    setFocusable(true);
    Resources res = this.getContext().getResources();
    bmpHana = BitmapFactory.decodeResource(res, R.drawable.hana);
    bmpKafun = BitmapFactory.decodeResource(res, R.drawable.kafun);
    kafuns = new ArrayList<Kafun>();
    lasers = new ArrayList<Laser>();
    myPaint.setColor(Color.CYAN);
    myPaint.setStyle(Style.FILL);
  }

  /**
   * 処理開始
   */
  public void start() {
    update();
  }

  /**
   * タッチイベント
   */
  public boolean onTouchEvent(MotionEvent event) {

    if (lasers.size() >= 5) {
      return true;
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      touchStartX = (int) (event.getX());
      touchStartY = (int) (event.getY());
      power = 1;
      break;
    case MotionEvent.ACTION_UP:
      int touchEndX = (int) (event.getX());
      int touchEndY = (int) (event.getY());
      lasers.add(new Laser(touchStartX, touchStartY, touchEndX, touchEndY));
      power = 0;
      break;
    case MotionEvent.ACTION_MOVE:
      if (power < 20) {
        power++;
      }
      break;
    }

    return true;
  }

  /**
   * メイン
   */
  public void update() {
    if (Math.random() < 0.3 && kafuns.size() < KAFUN_MAX) {
      kafuns.add(new Kafun((int) (Math.random() * 320), 0, (int) (Math.random() * 8) - 4, 8));
    }

    final List<Kafun> deadKafuns = new ArrayList<Kafun>();
    final List<Laser> deadLasers = new ArrayList<Laser>();

    for (Kafun kafun : kafuns) {
      kafun.move();
      kafun.judge();
      if (kafun.isDead()) {
        deadKafuns.add(kafun);
      }
    }

    for (Kafun kafun : deadKafuns) {
      kafuns.remove(kafun);
    }

    for (Laser laser : lasers) {
      laser.move();
      laser.judge();
      if (laser.isDead()) {
        deadLasers.add(laser);
      }
    }

    for (Laser laser : deadLasers) {
      lasers.remove(laser);
    }

    invalidate();

    updateHandler.sleep(200);
  }

  /**
   * 描画処理
   */
  protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);

    canvas.drawBitmap(bmpHana, 75, 340, myPaint);

    if (power > 0) {
      int r = power / 2;
      canvas.drawOval(new RectF(touchStartX - r, touchStartY - r, touchStartX + r, touchStartY + r), myPaint);
    }

    for (Kafun kafun : kafuns) {
      canvas.drawBitmap(bmpKafun, kafun.X, kafun.Y, myPaint);
    }

    for (Laser laser : lasers) {
      canvas.drawLine(laser.X1, laser.Y1, laser.X2, laser.Y2, myPaint);
    }
  }
}
最終更新:2011年03月07日 23:45