Advertisement
luk_per

Untitled

Jul 29th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. package pl.perski.kotlarek.projectgame.Views;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.graphics.Point;
  12. import android.graphics.Rect;
  13. import android.view.MotionEvent;
  14. import android.view.SurfaceHolder;
  15. import android.view.SurfaceView;
  16.  
  17.  
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20.  
  21. import pl.perski.kotlarek.projectgame.Act.GameActivity;
  22. import pl.perski.kotlarek.projectgame.Act.MainActivity;
  23. import pl.perski.kotlarek.projectgame.Objects.Enemy;
  24. import pl.perski.kotlarek.projectgame.Objects.Player;
  25.  
  26. /**
  27. * Created by Lukasz on 2018-04-18.
  28. */
  29.  
  30. public class GameView extends SurfaceView implements Runnable {
  31.  
  32. public static boolean playing;
  33. public Context context;
  34. Paint paint;
  35. private Thread gameThread = null;
  36. private Canvas canvas;
  37. private SurfaceHolder surfaceHolder;
  38. private Enemy[] enemies;
  39. private int enemyCount;
  40. private Rect touchRect;
  41. private int bitCounter;
  42. private boolean touchFlag = false;
  43. private int centerY;
  44. private int centerX;
  45. private int screenX;
  46. private int screenY;
  47. private int boomRadius;
  48. private int acceleration;
  49. private boolean gameOver;
  50.  
  51. //Class constructor
  52. public GameView(Context context, int screenX, int screenY) {
  53. super(context);
  54. this.context = context;
  55. surfaceHolder = getHolder();
  56. paint = new Paint();
  57. bitCounter = MainActivity.getName().equals("Perski") ? 555 : 0;
  58. centerY = screenY / 2;
  59. centerX = screenX / 2;
  60. this.screenX = screenX;
  61. this.screenY = screenY;
  62. boomRadius = MainActivity.getName().equals("Perski") ? 0 : screenX / 10;
  63. acceleration = MainActivity.getName().equals("test_game") ? 100 : 10;
  64. enemyCount = 3;
  65. enemies = new Enemy[enemyCount];
  66. for (int i = 0; i < enemyCount; i++) {
  67. enemies[i] = new Enemy(context, screenX, screenY, acceleration);
  68. }
  69. touchRect = new Rect(0, 0, 0, 0);
  70. }
  71.  
  72. @Override
  73. public void run() {
  74. while (playing) {
  75. //to update the frame
  76. update();
  77.  
  78. //to draw the frame
  79. draw();
  80.  
  81. //to control
  82. control();
  83. }
  84. }
  85.  
  86. private void update() {
  87. for (int i = 0; i < enemyCount; i++) {
  88. enemies[i].update();
  89. boolean isEnemyKilled = rectangle_collision(touchRect.left, touchRect.top, touchRect.right, touchRect.bottom,
  90. enemies[i].getDetectCollision().left, enemies[i].getDetectCollision().top, enemies[i].getDetectCollision().right, enemies[i].getDetectCollision().bottom);
  91.  
  92. if (isEnemyKilled && touchFlag) {
  93. bitCounter++;
  94. enemies[i].setX(-200);
  95. touchFlag = false;
  96. } else if (
  97. (enemies[i].getX() <= centerX + boomRadius * 0.8) &&
  98. (enemies[i].getY() <= centerY + boomRadius) && // ograniczenie z dołu
  99. (enemies[i].getY() + enemies[i].getBitmap().getHeight() >= centerY - boomRadius)) // ograniczenie z góry
  100. {
  101. if (boomRadius < 300) {
  102. boomRadius += centerY / 60;
  103. } else if (boomRadius < 600) {
  104. boomRadius += centerY / 80;
  105. } else if (boomRadius < 800) {
  106. boomRadius += centerY / 100;
  107. } else {
  108. boomRadius += centerY / 30;
  109. }
  110. //Enemy się cofa poza ekran i wraca po chwili
  111. enemies[i].setX(-200);
  112. }
  113. }
  114.  
  115. //koniec gry
  116. if (boomRadius >= screenX * 0.485) { gameOver(); }
  117. }
  118.  
  119. private void draw() {
  120. if (surfaceHolder.getSurface().isValid()) {
  121. canvas = surfaceHolder.lockCanvas();
  122. canvas.drawColor(Color.BLACK); // czyszczenie pola gry
  123. for (int i = 0; i < enemyCount; i++) {
  124. canvas.drawBitmap(
  125. enemies[i].getBitmap(),
  126. enemies[i].getX(),
  127. enemies[i].getY(),
  128. paint);
  129. }
  130. paint.setColor(Color.WHITE);
  131. canvas.drawCircle(screenX / 2, screenY / 2, boomRadius, paint);
  132.  
  133. //paint.setColor(Color.WHITE); //prostokąt pokazujący miejsce dotyku
  134. //canvas.drawRect(touchRect,paint);
  135.  
  136. if (!playing && gameOver) {
  137. paint.setTextSize(150);
  138. paint.setTextAlign(Paint.Align.CENTER);
  139. paint.setColor(Color.BLACK);
  140. canvas.drawText("Game Over", screenX / 2, centerY, paint);
  141. paint.setTextSize(100);
  142. canvas.drawText("Score: " + String.valueOf(bitCounter), canvas.getWidth() / 2, (float) (centerY * 1.2), paint);
  143. paint.setTextSize(50);
  144. if (!MainActivity.Players.isEmpty()){
  145. canvas.drawText("Best: " + String.valueOf(MainActivity.Players.get(0).getScore()), canvas.getWidth() / 2, (float) (centerY * 1.3), paint);}
  146.  
  147. }
  148. surfaceHolder.unlockCanvasAndPost(canvas);
  149. }
  150. }
  151.  
  152. private void control() {
  153. //spowolnienie wykonywania gry
  154. try {
  155. Thread.sleep(17);
  156. } catch (InterruptedException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160.  
  161.  
  162. public void pause() {
  163. playing = false;
  164. try {
  165. //stopowanie wątku
  166. gameThread.join();
  167. } catch (InterruptedException e) {
  168. }
  169. }
  170.  
  171. public void resume() {
  172. // wznawianie wątku
  173. playing = true;
  174. gameThread = new Thread(this);
  175. gameThread.start();
  176. }
  177.  
  178.  
  179. @Override
  180. public boolean onTouchEvent(MotionEvent motionEvent) {
  181. //obsługa dotyku
  182. if ((motionEvent.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
  183. int rectsize = 60;
  184. touchRect.left = (int) motionEvent.getX() - rectsize;
  185. touchRect.top = (int) motionEvent.getY() - rectsize;
  186. touchRect.right = (int) motionEvent.getX() + rectsize;
  187. touchRect.bottom = (int) motionEvent.getY() + rectsize;
  188. touchFlag = true;
  189. }
  190.  
  191. if (!playing) {
  192. if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
  193. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  194. builder.setMessage("Play again?")
  195. .setCancelable(false)
  196. .setPositiveButton("Yes, do it!", new DialogInterface.OnClickListener() {
  197. public void onClick(DialogInterface dialog, int id) {
  198. Intent myIntent = new Intent(context, GameActivity.class);
  199. context.startActivity(myIntent);
  200. Activity activity = (Activity) getContext();
  201. activity.finish();
  202. }
  203. })
  204. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  205. public void onClick(DialogInterface dialog, int id) {
  206. dialog.cancel();
  207. }
  208. });
  209. AlertDialog alert = builder.create();
  210. alert.show();
  211. }
  212. }
  213. return true;
  214. }
  215.  
  216. void gameOver() {
  217. playing = false;
  218. gameOver = true;
  219. if (bitCounter > 0)
  220. {
  221. MainActivity.Players.add(new Player(MainActivity.getName(), bitCounter));
  222. Collections.sort(MainActivity.Players, Player.COMPARE_BY_SCORE);
  223. }
  224. }
  225. //via https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other/4098512
  226. boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) {
  227. return !((x_1 > x_2 + width_2 || x_1 + width_1 < x_2 || y_1 > y_2 + height_2 || y_1 + height_1 < y_2));
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement