Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.perski.kotlarek.projectgame.Views;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.Rect;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import java.util.Collections;
- import java.util.Comparator;
- import pl.perski.kotlarek.projectgame.Act.GameActivity;
- import pl.perski.kotlarek.projectgame.Act.MainActivity;
- import pl.perski.kotlarek.projectgame.Objects.Enemy;
- import pl.perski.kotlarek.projectgame.Objects.Player;
- /**
- * Created by Lukasz on 2018-04-18.
- */
- public class GameView extends SurfaceView implements Runnable {
- public static boolean playing;
- public Context context;
- Paint paint;
- private Thread gameThread = null;
- private Canvas canvas;
- private SurfaceHolder surfaceHolder;
- private Enemy[] enemies;
- private int enemyCount;
- private Rect touchRect;
- private int bitCounter;
- private boolean touchFlag = false;
- private int centerY;
- private int centerX;
- private int screenX;
- private int screenY;
- private int boomRadius;
- private int acceleration;
- private boolean gameOver;
- //Class constructor
- public GameView(Context context, int screenX, int screenY) {
- super(context);
- this.context = context;
- surfaceHolder = getHolder();
- paint = new Paint();
- bitCounter = MainActivity.getName().equals("Perski") ? 555 : 0;
- centerY = screenY / 2;
- centerX = screenX / 2;
- this.screenX = screenX;
- this.screenY = screenY;
- boomRadius = MainActivity.getName().equals("Perski") ? 0 : screenX / 10;
- acceleration = MainActivity.getName().equals("test_game") ? 100 : 10;
- enemyCount = 3;
- enemies = new Enemy[enemyCount];
- for (int i = 0; i < enemyCount; i++) {
- enemies[i] = new Enemy(context, screenX, screenY, acceleration);
- }
- touchRect = new Rect(0, 0, 0, 0);
- }
- @Override
- public void run() {
- while (playing) {
- //to update the frame
- update();
- //to draw the frame
- draw();
- //to control
- control();
- }
- }
- private void update() {
- for (int i = 0; i < enemyCount; i++) {
- enemies[i].update();
- boolean isEnemyKilled = rectangle_collision(touchRect.left, touchRect.top, touchRect.right, touchRect.bottom,
- enemies[i].getDetectCollision().left, enemies[i].getDetectCollision().top, enemies[i].getDetectCollision().right, enemies[i].getDetectCollision().bottom);
- if (isEnemyKilled && touchFlag) {
- bitCounter++;
- enemies[i].setX(-200);
- touchFlag = false;
- } else if (
- (enemies[i].getX() <= centerX + boomRadius * 0.8) &&
- (enemies[i].getY() <= centerY + boomRadius) && // ograniczenie z dołu
- (enemies[i].getY() + enemies[i].getBitmap().getHeight() >= centerY - boomRadius)) // ograniczenie z góry
- {
- if (boomRadius < 300) {
- boomRadius += centerY / 60;
- } else if (boomRadius < 600) {
- boomRadius += centerY / 80;
- } else if (boomRadius < 800) {
- boomRadius += centerY / 100;
- } else {
- boomRadius += centerY / 30;
- }
- //Enemy się cofa poza ekran i wraca po chwili
- enemies[i].setX(-200);
- }
- }
- //koniec gry
- if (boomRadius >= screenX * 0.485) { gameOver(); }
- }
- private void draw() {
- if (surfaceHolder.getSurface().isValid()) {
- canvas = surfaceHolder.lockCanvas();
- canvas.drawColor(Color.BLACK); // czyszczenie pola gry
- for (int i = 0; i < enemyCount; i++) {
- canvas.drawBitmap(
- enemies[i].getBitmap(),
- enemies[i].getX(),
- enemies[i].getY(),
- paint);
- }
- paint.setColor(Color.WHITE);
- canvas.drawCircle(screenX / 2, screenY / 2, boomRadius, paint);
- //paint.setColor(Color.WHITE); //prostokąt pokazujący miejsce dotyku
- //canvas.drawRect(touchRect,paint);
- if (!playing && gameOver) {
- paint.setTextSize(150);
- paint.setTextAlign(Paint.Align.CENTER);
- paint.setColor(Color.BLACK);
- canvas.drawText("Game Over", screenX / 2, centerY, paint);
- paint.setTextSize(100);
- canvas.drawText("Score: " + String.valueOf(bitCounter), canvas.getWidth() / 2, (float) (centerY * 1.2), paint);
- paint.setTextSize(50);
- if (!MainActivity.Players.isEmpty()){
- canvas.drawText("Best: " + String.valueOf(MainActivity.Players.get(0).getScore()), canvas.getWidth() / 2, (float) (centerY * 1.3), paint);}
- }
- surfaceHolder.unlockCanvasAndPost(canvas);
- }
- }
- private void control() {
- //spowolnienie wykonywania gry
- try {
- Thread.sleep(17);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void pause() {
- playing = false;
- try {
- //stopowanie wątku
- gameThread.join();
- } catch (InterruptedException e) {
- }
- }
- public void resume() {
- // wznawianie wątku
- playing = true;
- gameThread = new Thread(this);
- gameThread.start();
- }
- @Override
- public boolean onTouchEvent(MotionEvent motionEvent) {
- //obsługa dotyku
- if ((motionEvent.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
- int rectsize = 60;
- touchRect.left = (int) motionEvent.getX() - rectsize;
- touchRect.top = (int) motionEvent.getY() - rectsize;
- touchRect.right = (int) motionEvent.getX() + rectsize;
- touchRect.bottom = (int) motionEvent.getY() + rectsize;
- touchFlag = true;
- }
- if (!playing) {
- if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setMessage("Play again?")
- .setCancelable(false)
- .setPositiveButton("Yes, do it!", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- Intent myIntent = new Intent(context, GameActivity.class);
- context.startActivity(myIntent);
- Activity activity = (Activity) getContext();
- activity.finish();
- }
- })
- .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
- }
- return true;
- }
- void gameOver() {
- playing = false;
- gameOver = true;
- if (bitCounter > 0)
- {
- MainActivity.Players.add(new Player(MainActivity.getName(), bitCounter));
- Collections.sort(MainActivity.Players, Player.COMPARE_BY_SCORE);
- }
- }
- //via https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other/4098512
- 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) {
- 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));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement