-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDView.java
400 lines (333 loc) · 13.3 KB
/
TDView.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package com.blkxltng.tappydefender;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by firej on 1/18/2017.
*/
public class TDView extends SurfaceView implements Runnable {
//Thread related
volatile boolean playing;
Thread gameThread = null;
//Game objects
private PlayerShip mPlayer;
public EnemyShip mEnemyShip1, mEnemyShip2, mEnemyShip3, mEnemyShip4, mEnemyShip5;
//Make some dust
public ArrayList<SpaceDust> dustList = new ArrayList<SpaceDust>();
//For drawing to screen
private Paint mPaint;
private Canvas mCanvas;
private SurfaceHolder mHolder;
//Timing
private float distanceRemaining;
private long timeTaken;
private long timeStarted;
private long fastestTime;
//Resolution
private int screenX;
private int screenY;
private Context mContext;
private boolean gameEnded;
//For sounds
private SoundPool mSoundPool;
int sndStart = -1;
int sndBump = -1;
int sndDestroyed = -1;
int sndWin = -1;
//For saving data ie. scores
private SharedPreferences mPreferences;
private SharedPreferences.Editor mEditor;
private int respawnNum = -1000;
public TDView(Context context, int x, int y) {
super(context);
mContext = context;
//Load the sounds
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
try {
AssetManager assetManager = mContext.getAssets();
AssetFileDescriptor descriptor;
descriptor = assetManager.openFd("start.ogg");
sndStart = mSoundPool.load(descriptor, 0);
descriptor = assetManager.openFd("win.ogg");
sndWin = mSoundPool.load(descriptor, 0);
descriptor = assetManager.openFd("bump.ogg");
sndBump = mSoundPool.load(descriptor, 0);
descriptor = assetManager.openFd("destroyed.ogg");
sndDestroyed = mSoundPool.load(descriptor, 0);
} catch (IOException e) {
//Print an error
Log.e("Error", "failed to load sound files!");
}
//Get Resolution
screenX = x;
screenY = y;
//Initialize objects for drawing
mHolder = getHolder();
mPaint = new Paint();
// //Initialize the player
// mPlayer = new PlayerShip(context, x, y);
//
// //Initialize enemy ships
// mEnemyShip1 = new EnemyShip(context, x, y);
// mEnemyShip2 = new EnemyShip(context, x, y);
// mEnemyShip3 = new EnemyShip(context, x, y);
//
// //Initialize dust
// int numDust = 40;
// for(int i = 0; i < numDust; i++) {
// //Where to spawn?
// SpaceDust speck = new SpaceDust(x,y);
// dustList.add(speck);
// }
//Get a reference to a file called HighScores. It is created if the id doesn't exist
mPreferences = mContext.getSharedPreferences("HighScores", mContext.MODE_PRIVATE);
//Initialize the editor
mEditor = mPreferences.edit();
//Load the fastest time from a entry in the file labled "fastestTime" if none then score is 10000000
fastestTime = mPreferences.getLong("fastestTime", 1000000);
startGame();
}
@Override
public void run() {
while(playing) {
update();
draw();
control();
}
}
private void update() {
//Collision detection on new positions before move because we are testing the last frame's
//position which has just been drawn
boolean hitDetected = false;
//If you use images in excess of 100 pixels wide, increase the -100 value accordingly
if(Rect.intersects(mPlayer.getHitBox(), mEnemyShip1.getHitBox())) {
hitDetected = true;
mEnemyShip1.setX(respawnNum);
}
if(Rect.intersects(mPlayer.getHitBox(), mEnemyShip2.getHitBox())) {
hitDetected = true;
mEnemyShip2.setX(respawnNum);
}
if(Rect.intersects(mPlayer.getHitBox(), mEnemyShip3.getHitBox())) {
hitDetected = true;
mEnemyShip3.setX(respawnNum);
}
if(screenX > 1000) {
if(Rect.intersects(mPlayer.getHitBox(), mEnemyShip4.getHitBox())) {
hitDetected = true;
mEnemyShip4.setX(respawnNum);
}
}
if(screenX > 1200) {
if(Rect.intersects(mPlayer.getHitBox(), mEnemyShip5.getHitBox())) {
hitDetected = true;
mEnemyShip5.setX(-1000);
}
}
if(hitDetected) {
mSoundPool.play(sndBump, 1, 1, 0, 0, 1);
mPlayer.reduceShieldStrength();
if(mPlayer.getShieldStrength() < 0) {
//Game over
mSoundPool.play(sndDestroyed, 1, 1, 0, 0, 1);
gameEnded = true;
}
}
//Update the player
mPlayer.update();
//Update the enemies
mEnemyShip1.update(mPlayer.getSpeed());
mEnemyShip2.update(mPlayer.getSpeed());
mEnemyShip3.update(mPlayer.getSpeed());
if(screenX > 1000) {
mEnemyShip4.update(mPlayer.getSpeed());
}
if(screenX > 1200) {
mEnemyShip5.update(mPlayer.getSpeed());
}
//For the dust
for(SpaceDust spaceDust : dustList) {
spaceDust.update(mPlayer.getSpeed());
}
//Change the distance remaining
if(!gameEnded) {
//Subtract distance to the home planet based on the current speed
distanceRemaining -= mPlayer.getSpeed();
//How long has the player ben flying?
timeTaken = System.currentTimeMillis() - timeStarted;
}
//Finished the game
if(distanceRemaining < 0) {
mSoundPool.play(sndWin, 1, 1, 0, 0, 1);
//Check for a fastest time
if(timeTaken < fastestTime) {
//Save the score
mEditor.putLong("fastestTime", timeTaken);
mEditor.commit();
fastestTime = timeTaken;
}
//No negative numbers
distanceRemaining = 0;
//end the game
gameEnded = true;
}
}
private void draw() {
if(mHolder.getSurface().isValid()) {
//Lock the area of memory we will draw to
mCanvas = mHolder.lockCanvas();
//Clear the last frame
mCanvas.drawColor(Color.argb(255, 0, 0, 0));
// //FOR DEBUGGING
// mPaint.setColor(Color.argb(255, 255, 255, 255));
// //Draw hitboxes
// mCanvas.drawRect(mPlayer.getHitBox().left, mPlayer.getHitBox().top,
// mPlayer.getHitBox().right, mPlayer.getHitBox().bottom, mPaint);
// mCanvas.drawRect(mEnemyShip1.getHitBox().left, mEnemyShip1.getHitBox().top,
// mEnemyShip1.getHitBox().right, mEnemyShip1.getHitBox().bottom, mPaint);
// mCanvas.drawRect(mEnemyShip2.getHitBox().left, mEnemyShip2.getHitBox().top,
// mEnemyShip2.getHitBox().right, mEnemyShip2.getHitBox().bottom, mPaint);
// mCanvas.drawRect(mEnemyShip3.getHitBox().left, mEnemyShip3.getHitBox().top,
// mEnemyShip3.getHitBox().right, mEnemyShip3.getHitBox().bottom, mPaint);
// //DEBUGGING END
//Set white for dust
mPaint.setColor(Color.argb(255, 255, 255, 255));
//Draw the dust
for(SpaceDust spaceDust : dustList) {
mCanvas.drawPoint(spaceDust.getX(), spaceDust.getY(), mPaint);
}
//Draw the player
mCanvas.drawBitmap(mPlayer.getBitmap(), mPlayer.getX(), mPlayer.getY(), mPaint);
//Draw the enemies
mCanvas.drawBitmap(mEnemyShip1.getBitmap(), mEnemyShip1.getX(), mEnemyShip1.getY(), mPaint);
mCanvas.drawBitmap(mEnemyShip2.getBitmap(), mEnemyShip2.getX(), mEnemyShip2.getY(), mPaint);
mCanvas.drawBitmap(mEnemyShip3.getBitmap(), mEnemyShip3.getX(), mEnemyShip3.getY(), mPaint);
if (screenX > 1000) {
mCanvas.drawBitmap(mEnemyShip4.getBitmap(), mEnemyShip4.getX(), mEnemyShip4.getY(), mPaint);
}
if (screenX > 1200) {
mCanvas.drawBitmap(mEnemyShip5.getBitmap(), mEnemyShip5.getX(), mEnemyShip5.getY(), mPaint);
}
if(!gameEnded) {
//Draw the HUD
mPaint.setTextAlign(Paint.Align.LEFT);
mPaint.setColor(Color.argb(255, 255, 255, 255));
mPaint.setTextSize(25);
//mCanvas.drawText("Fastest: " + fastestTime + "s", 10, 20, mPaint);
mCanvas.drawText("Fastest: " + formatTime(fastestTime) + "s", 10, 20, mPaint);
//mCanvas.drawText("Time: " + timeTaken + "s", screenX/2, 20, mPaint);
mCanvas.drawText("Time: " + formatTime(timeTaken) + "s", screenX/2, 20, mPaint);
mCanvas.drawText("Distance: " + distanceRemaining / 1000 + " KM", screenX/3, screenY - 20, mPaint);
mCanvas.drawText("Shield: " + mPlayer.getShieldStrength(), 10, screenY-20, mPaint);
mCanvas.drawText("Speed: " + mPlayer.getSpeed() * 60 + " MPS", (screenX/3)*2, screenY-20, mPaint);
} else {
//What to do if the game has ended
//Show the pause screen
mPaint.setTextSize(80);
mPaint.setTextAlign(Paint.Align.CENTER);
mCanvas.drawText("Game Over", screenX/2, 100, mPaint);
mPaint.setTextSize(25);
//mCanvas.drawText("Fastest: " + fastestTime + "s", screenX/2, 160, mPaint);
mCanvas.drawText("Fastest: " + formatTime(fastestTime) + "s", screenX/2, 160, mPaint);
//mCanvas.drawText("Time: " + timeTaken + "s", screenX/2, 200, mPaint);
mCanvas.drawText("Time: " + formatTime(fastestTime) + "s", screenX/2, 200, mPaint);
mCanvas.drawText("Distance Remaining: " + distanceRemaining + "KM", screenX/2, 240, mPaint);
mPaint.setTextSize(80);
mCanvas.drawText("Tap to Replay!", screenX/2, 350, mPaint);
}
//Unlock and draw the scene
mHolder.unlockCanvasAndPost(mCanvas);
}
}
private void control() {
try {
gameThread.sleep(17);
} catch (InterruptedException e) {
//catch things here
}
}
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
//catch things here
}
}
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//return super.onTouchEvent(event);
switch(event.getAction()& MotionEvent.ACTION_MASK) {
//Is the player's finger up?
case MotionEvent.ACTION_UP:
mPlayer.stopBoosting();
break;
//Is the player's finger on the screen?
case MotionEvent.ACTION_DOWN:
mPlayer.setBoosting();
//Start the game again if it has ended
if(gameEnded) {
startGame();
}
break;
}
return true;
}
private void startGame() {
//Initialize the game objects
mPlayer = new PlayerShip(mContext, screenX, screenY);
mEnemyShip1 = new EnemyShip(mContext, screenX, screenY);
mEnemyShip2 = new EnemyShip(mContext, screenX, screenY);
mEnemyShip3 = new EnemyShip(mContext, screenX, screenY);
if(screenX > 1000) {
mEnemyShip4 = new EnemyShip(mContext, screenX, screenY);
}
if(screenX > 1200) {
mEnemyShip5 = new EnemyShip(mContext, screenX, screenY);
}
int numDust = 400;
for(int i = 0; i < numDust; i++) {
//Where to spawn?
SpaceDust speck = new SpaceDust(screenX,screenY);
dustList.add(speck);
}
//Reset the time and distance
distanceRemaining = 10000; //10 KM
timeTaken = 0;
//Get the start time
timeStarted = System.currentTimeMillis();
gameEnded = false;
mSoundPool.play(sndStart, 1, 1, 0, 0, 1);
}
private String formatTime(long time) {
long seconds = (time) / 1000;
long thousandths = (time) - (seconds * 1000);
String strthousanths = "" + thousandths;
if(thousandths < 100) {
strthousanths = "0" + thousandths;
}
if(thousandths < 10) {
strthousanths = "0" + strthousanths;
}
String stringTime = "" + seconds + "." + strthousanths;
return stringTime;
}
}