Skip to content

Instantly share code, notes, and snippets.

@yohfee
Created August 24, 2011 12:05
Show Gist options
  • Select an option

  • Save yohfee/1167919 to your computer and use it in GitHub Desktop.

Select an option

Save yohfee/1167919 to your computer and use it in GitHub Desktop.
package org.yohfee.kantoroid;
import org.yohfee.kantoroid.GameEventListener;
public class Game {
private final GameEventListener listener;
private long score;
private float bonus;
private float prevX;
private float prevY;
private float prevZ;
private int count;
public Game(GameEventListener listener) {
this.listener = listener;
score = 0;
bonus = 1;
count = 0;
prevX = 0;
prevY = 0;
prevZ = 0;
}
public void send(float x, float y, float z) {
if (x == prevX) bonus += 0.1;
if (y == prevY) bonus += 0.5;
if (z == prevZ) bonus += 0.2;
if (x != prevX || y != prevY || z != prevZ) bonus = 1;
score += Math.abs(Math.tan(Math.toRadians(y))) * (90 - Math.abs(z)) * bonus;
listener.onSent();
prevX = x;
prevY = y;
prevZ = z;
if (++count == 200) listener.onFinish();
}
public long getScore() {
return score;
}
public String getResult() {
if (score < 1000) return "竿燈力…たったの" + score + "か…ゴミめ…";
if (score < 5000) return "ほう…竿燈力" + score + "。こんなやつもいたのか。";
if (score < 10000) return score + "…バカめ!その程度の竿燈力でオレたちにはむかうつもりか…!?";
if (score < 30000) return "竿燈力" + score + "だと!そいつは何かのマチガイだ…";
if (score < 50000) return "竿燈力" + score + "…!!バカな…!!";
if (score < 100000) return "これはすごい!竿燈力" + score + "まであがりましたよ。わたしの竿燈力は530000です。";
return "竿燈力" + score + "…(ボンッ!!)";
}
}
package org.yohfee.kantoroid;
public interface GameEventListener {
public void onSent();
public void onFinish();
}
package org.yohfee.kantoroid;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import org.yohfee.kantoroid.Game;
import org.yohfee.kantoroid.GameEventListener;
public class KantoroidActivity extends Activity
implements SensorEventListener, OnClickListener, GameEventListener {
private SensorManager manager;
private TextView score;
private Button button;
private Game game;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
manager = (SensorManager)getSystemService(SENSOR_SERVICE);
score = (TextView)findViewById(R.id.score);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() == 0) {
new AlertDialog.Builder(this)
.setMessage("傾きセンサーついてないよ!")
.setPositiveButton("OK", null)
.show();
finish();
} else {
manager.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
public void onStop() {
manager.unregisterListener(this);
super.onStop();
}
public void onClick(View v) {
button.setEnabled(false);
game = new Game(this);
}
public void onSent() {
score.setText("竿燈力" + game.getScore());
}
public void onFinish() {
new AlertDialog.Builder(this)
.setTitle(R.string.app_name)
.setMessage(game.getResult())
.setPositiveButton("OK", null)
.show();
button.setEnabled(true);
game = null;
}
public void onAccuracyChanged(final Sensor sensor, int accuracy) {
}
public void onSensorChanged(final SensorEvent event) {
if (game != null && event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
Log.d("KANTOROID", "ORIENTATION\t" + event.values[0] + "\t" + event.values[1] + "\t" + event.values[2]);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
game.send(x, y, z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment