package se.nekotronic.satelliterushtest; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; class Brick { public int x, y; public int radius; public int color; } public class RushView extends View { // private ShapeDrawable my_drawable; private ArrayList bricks = new ArrayList(); public int MAX_BRICKS = 100; Timer ticker = new Timer(); public final int NOT_READY= 1, RUNNING = 2, STOPPED = 3; private int status = NOT_READY; private void init(Context context) { general_thing_size = 10; // setFocusable(true); } // init public RushView(Context context) { super(context); init(context); } public RushView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public RushView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private int n = 0; private int width_pixels; private int height_pixels; private float general_thing_size; // The size of a 1-centimeter thing, in pixels private float ball_x; // Measured in SCREENS private float ball_y;// Measured in SCREENS private float ball_dx; // Measured in SCREENS PER SECOND private float ball_dy; // Measured in SCREENS PER SECOND private float gravity; protected void onDraw(Canvas canvas) { // my_drawable.draw(canvas); super.onDraw(canvas); int h = canvas.getHeight(); int w = canvas.getWidth(); height_pixels = h; width_pixels = w; Paint p = new Paint(); p.setColor(Color.RED); canvas.drawText("n = " + ++n + ", x = " + roundDouble(ball_x, 2) + ", y = " + roundDouble(ball_y, 2) + ", dx = " + roundDouble(ball_dx, 2) + ", dy = " + roundDouble(ball_dy, 2), 20.0f, 20.0f, p); p.setColor(Color.BLUE); canvas.drawCircle(ball_x * width_pixels, ball_y * height_pixels, general_thing_size, p); } public void pause() { if (ticker != null) { ticker.cancel(); ticker = null; } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { width_pixels = w; height_pixels = h; // Remember: These are measured in SCREENS and SCREENS PER SECOND ball_x = 0.5f; ball_y = 0.5f; ball_dx = 0.1f; ball_dy = 0.1f; gravity = 0.02f; } private void start_ticker() { ticker = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { update_simulation(); Runnable r = new Runnable() { @Override public void run() { // print("Tick!"); invalidate(); } }; getHandler().post(r); } }; ticker.schedule(task, 10, 10); } /* @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub // return super.onKeyDown(keyCode, event); ball_x = 0.5f; ball_y = 0.5f; return false; // So the rest works } */ @Override public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getX(); int y = (int)event.getY(); ball_x = 1.0f*x/width_pixels; ball_y = 1.0f*y/height_pixels; return true; } public void resume() { if (status == RUNNING) start_ticker(); } // resume private long previous_nanos = -1; // -1 is not guaranteed to never happen, but we ignore that private void update_simulation() { long now_nanos = java.lang.System.nanoTime(); if (previous_nanos == -1 || now_nanos < previous_nanos) { // First time, or overflow, so don't update the game previous_nanos = now_nanos; return; } long nanos = previous_nanos - now_nanos; previous_nanos = now_nanos; double seconds = nanos / 1e9; ball_dy += gravity * seconds; ball_x += ball_dx * seconds; ball_y += ball_dy * seconds; if (ball_x < 0) { ball_x = 0; ball_dx = -ball_dx; } if (ball_x >= 1) { ball_x = 1; ball_dx = -ball_dx; } if (ball_y < 0) { ball_y = 0; ball_dy = -ball_dy; } if (ball_y >= 1) { ball_y = 1; ball_dy = -ball_dy; } } // update_simulation public void start() { status = RUNNING; resume(); } public static final double roundDouble(double d, int places) { return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10, (double) places); } } // class RushView