-
Notifications
You must be signed in to change notification settings - Fork 0
/
BallView.java
359 lines (294 loc) · 78.8 KB
/
BallView.java
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.example.andre.gyrogolf;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.MotionEvent;
import android.graphics.*;
import android.content.Context;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import android.os.CountDownTimer;
import android.content.res.Resources;
import android.view.ViewGroup;
import android.widget.TextView;
import org.w3c.dom.Text;
import static android.support.v4.app.ActivityCompat.startActivity;
/**
* Created by andre on 5/25/16.
*/
public class BallView extends View {
//make sure hole and ball is not overlapping with obstacle
//make sure ball does not spawn outside of bounds
private int xMin = 0; // This view's bounds
private int xMax;
private int yMin = 0;
private int yMax;
boolean timeOver = false;
private int score;
private final float FRICTION = (float) (new Float(-.05));
// Bitmap mBitmap; //BitmapFactory.decodeFile("/Users/andre/AndroidStudioProjects/Gyrogolf/app/src/main/res/drawable/green.jpg");
//File f = new File("/data/data/com.example.andre.gyrogolf/res/drawable/green.jpg");
// Bitmap mBitmap;// = BitmapFactory.decodeResource(this.getResources(),R.drawable.green);
private float ballRadius = 80; // Ball's radius
private float ballX = ballRadius + 20; // Ball's center (x,y)
private float ballY = ballRadius + 40;
private float ballSpeedX = 5; // Ball's speed (x,y)
private float ballSpeedY = 3;
public float previousX;
public float previousY;
private float holeX;
private float holeY;
private float holeRadius = 100;
private RectF ballBounds; // Needed for Canvas.drawOval
private Paint paint;
private Paint paintb;
private Paint painto;
private RectF holeBounds;
private TextView t;
// private CountDownTimer time;
protected double time = 1500.0;
protected List<Integer> scores;
private List<Obstacle> obsList;
// The paint used for drawing
// Constructor
public BallView(Context context) {
super(context);
ballBounds = new RectF();
holeBounds = new RectF();
paint = new Paint();
paintb = new Paint();
painto = new Paint();
obsList = new ArrayList<Obstacle>();
scores = new ArrayList<Integer>();
this.setFocusableInTouchMode(true);
}
// Called back to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Bitmap temp;
Check();
// draw background
// System.out.println(mBitmap);
// temp = BitmapFactory.decodeResource(this.getResources(),R.drawable.greenb);
// mBitmap = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
//canvas.drawBitmap(mBitmap,0,0,null);
canvas.drawColor(Color.GREEN);
//canvas.drawColor(#009900);
//draw the hole
holeBounds.set(holeX - holeRadius, holeY - holeRadius, holeX + holeRadius, holeY + holeRadius);
paintb.setColor(Color.BLACK);
canvas.drawOval(holeBounds, paintb);
// Draw the ball
ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
paint.setColor(Color.WHITE);
canvas.drawOval(ballBounds, paint);
painto.setColor(Color.LTGRAY);
for (Obstacle o : obsList) {
o.draw(canvas, painto);
}
float textSize = paintb.getTextSize();
paintb.setTextSize(textSize * 5);
canvas.drawText("Score: " + score, 500, 100, paintb);
paintb.setTextSize(textSize);
//canvas.drawText("TimeLeft: " + time, 950, 50, paintb);
if(time>0){
time--;
float textSize1 = paintb.getTextSize();
paintb.setTextSize(textSize1 *5);
canvas.drawText("Time Left: " + time, 500, 50, paintb);
paintb.setTextSize(textSize1);
}
// Update the position of the ball, including collision detection and reaction.
update();
// Delay
try {
Thread.sleep(30);
} catch (InterruptedException e) {
}
invalidate(); // Force a re-draw
}
private void Check(){
if(time == 0.0){
timeOver = true;
scores.add(score);
Context context = getContext();
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
}
}
public ArrayList<Integer> getScores(){
return (ArrayList<Integer>) scores;
}
// Detect collision and update the position of the ball.
private void update() {
// Get new (x,y) position
ballX += ballSpeedX;
if(Math.abs(ballSpeedX) > FRICTION) {
if(ballSpeedX > 0)
ballSpeedX += FRICTION;
else
ballSpeedX -= FRICTION;
} else
ballSpeedX = 0;
ballY += ballSpeedY;
if(Math.abs(ballSpeedY) > FRICTION) {
if(ballSpeedY > 0)
ballSpeedY += FRICTION;
else
ballSpeedY -= FRICTION;
} else
ballSpeedY = 0;
// ballX += ballSpeedX;
//ballSpeedX += FRICTION;
//ballY += ballSpeedY;
// ballSpeedY += FRICTION;
// Detect collision and react
// this.checkObsCollision(ballX, ballY, ballRadius);
// if (checkObsCollision(ballX, ballY, ballRadius) == 1) {
// ballSpeedX = -ballSpeedX;
//} else if (checkObsCollision(ballX, ballY, ballRadius) == 2) {
// ballSpeedY = -ballSpeedY;
//}
Obstacle o = checkObsCollision(ballX, ballY, ballRadius);
if ( o != null) {
switch (o.getWhichSide()) {
case 1: ballSpeedX = -ballSpeedX;
ballX = o.getLeftVert() - ballRadius;
break;
case 2: ballSpeedX = -ballSpeedX;
ballX = o.getRightVert() + ballRadius;
break;
case 3: ballSpeedY = -ballSpeedY;
ballY = o.getTop() - ballRadius;
break;
case 4: ballSpeedY = -ballSpeedY;
ballY = o.getBottom() + ballRadius;
break;
}
}
o = null;
if (ballX + ballRadius > xMax) {
ballSpeedX = -ballSpeedX;
ballX = xMax - ballRadius;
} else if (ballX - ballRadius < xMin) {
ballSpeedX = -ballSpeedX;
ballX = xMin + ballRadius;
}
if (ballY + ballRadius > yMax) {
ballSpeedY = -ballSpeedY;
ballY = yMax - ballRadius;
} else if (ballY - ballRadius < yMin) {
ballSpeedY = -ballSpeedY;
ballY = yMin + ballRadius;
}
if (Math.abs(ballX - holeX) <= 20 && Math.abs(ballY - holeY) <= 20) {
System.out.println("match");
score++;
obsList.clear();
this.onSizeChanged(xMax + 1, yMax + 1, 0, 0);
}
}
private Obstacle checkObsCollision(float ballX, float ballY, float ballRadius) {
for (int i = 0; i < obsList.size(); i++) {
if (obsList.get(i).contact(ballX, ballY, ballRadius)) {
return obsList.get(i);
}
}
return null;
}
/* for(Obstacle o : obsList) {
if (o.contact(ballX, ballY, ballRadius) != 0) {
return o.contact(ballX, ballY, ballRadius);
}
}
return 0;
}
*/
// Called back when the view is first created or its size changes.
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
// Set the movement bounds for the ball
xMax = w - 1;
yMax = h - 1;
holeX = new Float(Math.random() * (xMax - 200) + 100);
holeY = new Float(Math.random() * (yMax - 200) + 100);
generateObstacles();
}
private void generateObstacles() {
int longSide = (int)(xMax / 2);
int shortSide = longSide / 4;
int squareSide = longSide / 2;
for (int i = 0; i < 2; i++) {
if (Math.random() > .5) {
Obstacle o = new Obstacle((float) (new Float(Math.random() * xMax)), (float) (new Float(Math.random() * yMax)),(float)( new Float(longSide)), (float) (new Float(shortSide)));
if(o.contact(ballX, ballY, ballRadius) || o.contact(holeX, holeY, holeRadius))
continue;
obsList.add(o);
}
if (Math.random() <= .5) {
Obstacle o = new Obstacle( (float) (new Float(Math.random() * xMax)),(float) (new Float(Math.random() * yMax)), (float) (new Float(shortSide)), (float)( new Float(longSide)));
if(o.contact(ballX, ballY, ballRadius) || o.contact(holeX, holeY, holeRadius))
continue;
obsList.add(o);
}
}
if (Math.random() > .4) {
// obsList.add(new Obstacle(Math.random() * xMax, Math.random() * yMax, squareSide, squareSide));
Obstacle a = new Obstacle( (float) (new Float(Math.random() * xMax)),(float) (new Float(Math.random() * yMax)), (float) (new Float(squareSide)), (float)( new Float(squareSide)));
if(a.contact(ballX, ballY, ballRadius) || a.contact(holeX, holeY, holeRadius)) {
return;
}
/* for (int i = 0; i < 2; i++) {
if (Math.random() > .5) {
Obstacle o = new Obstacle((float) (new Float(Math.random() * xMax)), (float) (new Float(Math.random() * yMax)),(float)( new Float(longSide)), (float) (new Float(shortSide)));
if(o.contact(ballX, ballY, ballRadius) != 0 && o.contact(holeX, holeY, holeRadius) !=0)
continue;
obsList.add(o);
}
if (Math.random() <= .5) {
Obstacle o = new Obstacle( (float) (new Float(Math.random() * xMax)),(float) (new Float(Math.random() * yMax)), (float) (new Float(shortSide)), (float)( new Float(longSide)));
if(o.contact(ballX, ballY, ballRadius) != 0 && o.contact(holeX, holeY, holeRadius)!=0)
continue;
obsList.add(o);
}
}
if (Math.random() > .4) {
// obsList.add(new Obstacle(Math.random() * xMax, Math.random() * yMax, squareSide, squareSide));
Obstacle a = new Obstacle( (float) (new Float(Math.random() * xMax)),(float) (new Float(Math.random() * yMax)), (float) (new Float(squareSide)), (float)( new Float(squareSide)));
if(a.contact(ballX, ballY, ballRadius) != 0 && a.contact(holeX, holeY, holeRadius)!=0) {
return;
}
*/
obsList.add(a);
} // Touch-input handler
} @Override
public boolean onTouchEvent(MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
float deltaX, deltaY;
float scalingFactor = 7.0f / ((xMax > yMax) ? yMax : xMax);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// Modify rotational angles according to movement
deltaX = currentX - previousX;
deltaY = currentY - previousY;
System.out.println("deltaX" + deltaX * scalingFactor);
System.out.println("deltaY" + deltaY * scalingFactor);
ballSpeedX += deltaX * scalingFactor;
ballSpeedY += deltaY * scalingFactor;
}
// Save current x, y
previousX = currentX;
previousY = currentY;
return true; // Event handled
}
}