Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
GameView.java 4.32 KiB
package com.example.a8_bitinvader;

import android.content.Context;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.graphics.Canvas;
import android.graphics.Paint;             // necessary when drawing Bitmaps with Canvas objects

public class GameView extends SurfaceView implements Runnable{

    private Thread thread;
    private boolean isRunning;
    private int screenWidth, screenHeight;
    private float screenRatioWidth, screenRatioHeight;
    private Background background1, background2;
    private Paint paint;

    Joystick joystick = new Joystick(450,1300,100,40);
    //undecided location


    public GameView(Context context, int screenWidth, int screenHeight) {
        super(context);

        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

        screenRatioWidth = 1080f / screenWidth;
        screenRatioHeight = 1920f / screenHeight;

        background1 = new Background(screenWidth, screenHeight, getResources());
        background2 = new Background(screenWidth, screenHeight, getResources());

        background2.yPos = -screenHeight;

        paint = new Paint();
    }

    /*
     * Continuously loops through update(), draw(), and sleep() if the app is running
     * Overrides from Runnable class, which Thread automatically calls once resume() initializes said Thread
     */
    @Override
    public void run() {

        while(isRunning) {
            update();
            draw();

            // creates 30 millisecond delay in between draw() and update()
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*
     * updates position of backgrounds.
     * checks if either background is offscreen. If so, it places that background above the other
     * giving the illusion of an infinitely moving background.
     */
    private void update() {
        background1.yPos += (int) (5 * screenRatioHeight);
        background2.yPos += (int) (5 * screenRatioHeight);

        if(background1.yPos > screenHeight) {
            background1.yPos = -screenHeight;
        }

        if(background2.yPos > screenHeight) {
            background2.yPos = -screenHeight;
        }

        joystick.update(joystick);
    }

    /*
     * draws both backgrounds onto the screen given their initialized
     */
    private void draw() {
        if(getHolder().getSurface().isValid()){
            Canvas canvas = getHolder().lockCanvas();
            canvas.drawBitmap(background1.background, background1.xPos, background1.yPos, paint);
            canvas.drawBitmap(background2.background, background2.xPos, background2.yPos, paint);
<<<<<<< HEAD
            Player p1 = new Player(getResources());
            canvas.drawBitmap(p1.playerImg, p1.xPos, p1.yPos, paint);
=======
            joystick.draw(canvas);
>>>>>>> Joystick
            getHolder().unlockCanvasAndPost(canvas);
        }
    }

    /*
     * https://developer.android.com/reference/java/lang/Thread#start()
     * initializes a thread and starts it, automatically calling the overridden run() method above.
     */
    public void resume() {

        isRunning = true;
        thread = new Thread(this);
        thread.start();

    }

    /*
     * https://developer.android.com/reference/java/lang/Thread#join()
     * waits for thread to die if game is paused
     */
    public void pause() {

        try {
            isRunning = false;
            thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(joystick.isPressed((double)event.getX(), (double)event.getY())){
                    joystick.setIsPressed(true);
            }
                return true;
            case MotionEvent.ACTION_MOVE:
                if(joystick.getIsPressed()) {
                    joystick.setActuator((double)event.getX(), (double)event.getY());
                }
                return true;
            case MotionEvent.ACTION_UP:
                joystick.setIsPressed(false);
                joystick.resetActuator();
                return true;
        }

        return super.onTouchEvent(event);

    }

}