summaryrefslogtreecommitdiff
path: root/java/org/neocities/autoart/ballbounce/MoveBall.java
blob: cc44fc421436fb85ca624cce5a2a7c22f63297d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.neocities.autoart.ballbounce;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;

import java.util.Date;

public class MoveBall implements Runnable
{
    public CanvasView cv;
    public double[] ballVel;
    public double gravity = 1;
    public boolean running = true;
    public long lastBounce;
    public double a;
    public double b;
    public int t = 0;

    @Override
    public void run()
    {




        if (cv.w == 0 || !running)
        {
            cv.handler.postDelayed(this, 10);
            return;
        }

        if (Game.MODE != Game.CRAZY_GRAVITY_MODE && Game.MODE != Game.FLIP_MODE)
            gravity = 1;
        else if (Game.MODE == Game.CRAZY_GRAVITY_MODE)
            gravity = Math.abs(cv.w*(t*a*Math.sin(b*t)));
        else if (Game.MODE == Game.FLIP_MODE && (t/500) % 2 == 0)
            gravity = 1;
        else if (Game.MODE == Game.FLIP_MODE)
            gravity = -1;

        cv.currentBallPosition[0] += ballVel[0];
        cv.currentBallPosition[1] += ballVel[1];


        if (cv.currentBallPosition[1] > cv.h || cv.currentBallPosition[1] < 0 || cv.currentBallPosition[0] > cv.w || cv.currentBallPosition[0] < 0)
        {
            running = false;
            new AlertDialog.Builder(CanvasView.activity)
                    .setTitle("You lost!")
                    .setMessage("Score: " + CanvasView.score)
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            CanvasView.started = false;
                            Intent intent = new Intent(CanvasView.activity, Menu.class);
                            CanvasView.activity.startActivity(intent);
                        }
                    }).create().show();
        }

        if (Math.abs(cv.currentBallPosition[0]-cv.paddlePosition[0]) < cv.paddleSize/2 &&
                Math.abs(cv.currentBallPosition[1]-cv.paddlePosition[1]) < 5+cv.ballSize)
        {
            Date d = new Date();

            if (d.getTime()-lastBounce < 100)
            {
                ballVel[1] += gravity;
                cv.ballPositions.add(cv.currentBallPosition.clone());
                cv.invalidate();
                cv.handler.postDelayed(this, 10);
                return;
            }

            ballVel[0] += cv.w*0.00005 * (cv.currentBallPosition[0]-cv.paddlePosition[0]) + Math.random()*cv.w*0.0005-cv.w*0.00025;
            ballVel[1] = -gravity*cv.w*0.02;
            CanvasView.score++;

            lastBounce = d.getTime();
        }


        ballVel[1] += gravity;


        cv.ballPositions.add(cv.currentBallPosition.clone());

        cv.invalidate();

        t++;

        cv.handler.postDelayed(this, 10);


    }
}