summaryrefslogtreecommitdiff
path: root/java/org/neocities/autoart/ballbounce/CanvasView.java
blob: e0f3653fe8e85e4662901813d8827ea6ea4a8056 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.neocities.autoart.ballbounce;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class CanvasView extends View
{
    public static Game activity;
    public static int score = 0;

    public ArrayList<double[]> ballPositions;
    public double[] currentBallPosition;
    public double[] paddlePosition;

    public double ballSize;
    public double paddleSize;

    public int w;
    public int h;


    public static boolean started = false;


    public ArrayList<int[]> colours;

    public Handler handler = new Handler();

    MoveBall moveBall = new MoveBall();

    public Bitmap bmp;
    public Canvas bmpCanvas;


    public CanvasView(Context context)
    {
        super(context);
    }

    public CanvasView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public CanvasView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public double dist(double x1, double y1, double x2, double y2)
    {
        return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    }

    public void init()
    {
        moveBall.cv = this;
        w = getWidth();
        h = getHeight();
        moveBall.t = 0;
        moveBall.a = Math.random()*0.00001;
        moveBall.b = Math.random()*0.002;



        colours = new ArrayList<>();

        score = 0;
        moveBall.running = true;

        currentBallPosition = new double[2];
        paddlePosition = new double[2];


        currentBallPosition[0] = -w;
        currentBallPosition[1] = -h;

        paddlePosition[0] = w/2;
        paddlePosition[1] = h/2;

        ballSize = w*0.05;
        paddleSize = w*0.2;

        ballPositions = new ArrayList<>();


        moveBall.ballVel = new double[2];
        moveBall.ballVel[0] = 0;
        moveBall.ballVel[1] = 0;


        setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                int x = (int)event.getX();
                int y = (int)event.getY();
                paddlePosition[0] = x;
                paddlePosition[1] = y;

                return true;
            }
        });



        handler.postDelayed(moveBall, 50);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);

        if (started)
            return;

        started = true;

        w = getWidth();
        h = getHeight();

        currentBallPosition = new double[2];
        paddlePosition = new double[2];

        currentBallPosition[0] = w/2;
        currentBallPosition[1] = 0;

        paddlePosition[0] = -w;
        paddlePosition[1] = -h;

        ballSize = w*0.05;
        paddleSize = w*0.2;

        bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        bmpCanvas = new Canvas(bmp);


    }

    @Override
    public void onDraw(Canvas canvas)
    {

        if (w == 0)
            return;

        Paint paint = new Paint();


        int i = moveBall.t;

        if (colours.size() <= Math.ceil((double)i/100))
        {
            int[] c = new int[3];
            c[0] = (int)(Math.random()*255);
            c[1] = (int)(Math.random()*255);
            c[2] = (int)(Math.random()*255);

            colours.add(c);
        }

        paint.setStyle(Paint.Style.FILL);
        if (Game.MODE == Game.NORMAL_MODE || Game.MODE == Game.CRAZY_GRAVITY_MODE || Game.MODE == Game.FLIP_MODE)
        {
            paint.setARGB(255, 0, 0, 0);
        }
        else if (Game.MODE == Game.COLOUR_CHANGING_MODE)
        {
            paint.setARGB(255, colours.get(i / 100)[0], colours.get(i / 100)[1], colours.get(i / 100)[2]);
        }
        else if (Game.MODE == Game.OSCILLATION_MODE)
        {
            int val = (int)(128*Math.sin(0.001*i)+127);
            paint.setARGB(255, val, val, val);
        }
        else if (Game.MODE == Game.BW_MODE)
        {
            int val = 255*((i/500)%2);
            paint.setARGB(255, val, val, val);
        }
        bmpCanvas.drawCircle((float) currentBallPosition[0], (float) currentBallPosition[1], (float) ballSize, paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setARGB(255, 0, 0, 255);
        paint.setStrokeWidth(10);

        canvas.drawBitmap(bmp, 0, 0, null);
        canvas.drawLine((float) (paddlePosition[0] - paddleSize / 2), (float) (paddlePosition[1]), (float) (paddlePosition[0] + paddleSize / 2), (float) (paddlePosition[1]), paint);

    }
}