-
Notifications
You must be signed in to change notification settings - Fork 0
/
BallView
261 lines (211 loc) · 50.3 KB
/
BallView
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
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;
/**
* 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 canDraw = false;
public int scores;
// 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;
// Timer time = new Timer();
private TextView time;
// int time = 1000;
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>();
this.setFocusableInTouchMode(true);
}
/* new CountDownTimer(30000, 1000); {
public void onTick(long millisUntilFinished) {
time.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("done!");
}
}.start();
*/
// Called back to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Bitmap temp;
// 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.DKGRAY);
for(Obstacle o : obsList) {
o.draw(canvas, painto);
}
canvas.drawText("Scores: "+scores ,1000, 30, paintb );
canvas.drawText("TimeLeft: " + time, 950, 50, paintb);
// 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
}
// Detect collision and update the position of the ball.
private void update() {
// Get new (x,y) position
ballX += ballSpeedX;
ballY += ballSpeedY;
// 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;
}
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");
scores++;
obsList.clear();
this.onSizeChanged(xMax+1, yMax+1, 0, 0);
}
}
private int checkObsCollision(float ballX, float ballY, float ballRadius) {
for (int i = 0; i < obsList.size(); i++) {
if (obsList.get(i).contact(ballX, ballY, ballRadius) != 0) {
return obsList.get(i).contact(ballX, ballY, ballRadius);
}
}
return 0;
}
/* 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)));
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)));
obsList.add(o);
}
}
if (Math.random() > .4) {
// obsList.add(new Obstacle(Math.random() * xMax, Math.random() * yMax, squareSide, squareSide));
obsList.add( new Obstacle( (float) (new Float(Math.random() * xMax)),(float) (new Float(Math.random() * yMax)), (float) (new Float(squareSide)), (float)( new Float(squareSide))));
}
}
// Touch-input handler
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
float deltaX, deltaY;
float scalingFactor = 10.0f / ((xMax > yMax) ? yMax : xMax);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// Modify rotational angles according to movement
deltaX = currentX - previousX;
deltaY = currentY - previousY;
ballSpeedX += deltaX * scalingFactor;
ballSpeedY += deltaY * scalingFactor;
}
// Save current x, y
previousX = currentX;
previousY = currentY;
return true; // Event handled
}
}