Commit b47377af authored by Derek Xu's avatar Derek Xu
Browse files

Projectile work and javadoc

rebase
parent eedff5c8
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -9,8 +9,11 @@ public class Background {
    int xPos = 0, yPos = 0;
    Bitmap background;

    /*
     * Creates background from 'background1.jpeg' in res/drawable directory
    /**
     * Creates a background
     * @param screenWidth Width of screen in pixels
     * @param screenHeight Height of screen in pixels
     * @param res Resource object
     */
    Background(int screenWidth, int screenHeight, Resources res) {
        background = BitmapFactory.decodeResource(res, R.drawable.background);
+10 −0
Original line number Diff line number Diff line
@@ -10,6 +10,10 @@ public class GameActivity extends AppCompatActivity {

    private GameView gameView;

    /**
     * Create an instance of the activity based on the saved state. Then initialize the gameview and display the gameview onto the screen
     * @param savedInstanceState The cached game state
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -26,12 +30,18 @@ public class GameActivity extends AppCompatActivity {
        setContentView(gameView);
    }

    /**
     * Pause the game
     */
    @Override
    protected void onPause() {
        super.onPause();
        gameView.pause();
    }

    /**
     * Resume the game
     */
    @Override
    protected void onResume() {
        super.onResume();
+13 −0
Original line number Diff line number Diff line
@@ -14,9 +14,18 @@ public class GameView extends SurfaceView implements Runnable{
    private float screenRatioWidth, screenRatioHeight;
    private Background background1, background2;
    private Paint paint;

    /** The {@link Joystick} */
    Joystick joystick;
    /** The {@link Player} */
    Player p1;

    /**
     * Create a {@link GameView} scaled based on the device size which includes the background, player, and joystick
     * @param context Context of the device
     * @param screenWidth Screen width in pixels
     * @param screenHeight Screen height in pixels
     */
    public GameView(Context context, int screenWidth, int screenHeight) {
        super(context);
        joystick = new Joystick(screenWidth/2,screenHeight*9/10,130,40);
@@ -39,6 +48,10 @@ public class GameView extends SurfaceView implements Runnable{
     * 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
     */

    /**
     * Runs the app by continuously calling private methods {@link Void update()} and {@link Void draw()} based on whether the app {@link Boolean isRunning}
     */
    @Override
    public void run() {

+22 −0
Original line number Diff line number Diff line
@@ -9,6 +9,10 @@ import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    /**
     * Displays the menu and checks for user input to click play
     * @param savedInstanceState The cached state of the game
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -35,31 +39,49 @@ public class MainActivity extends AppCompatActivity {
        });
    }

    /**
     * Start the game
     */
    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * Resume the game
     */
    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * Pause the game
     */
    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * Stop the game
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * Destroy the sprite
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * Restart the game
     */
    @Override
    protected void onRestart() {
        super.onRestart();
+50 −0
Original line number Diff line number Diff line
package com.example.a8_bitinvader;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Projectile {
    private int velocity;
    int xPos;
    int yPos;
    Bitmap bulletImage;
    private final int bulletHeight = 100, bulletWidth = 30;


    /**
     * Projectiles from player only go upward
     * @param velocity of the projectile
     */
    public Projectile(int xPos, int yPos, int velocity, Resources res) {
        this.xPos = xPos;
        this.yPos = yPos;
        bulletImage = BitmapFactory.decodeResource(res, R.drawable.bullet);
        bulletImage = Bitmap.createScaledBitmap(bulletImage, bulletHeight, bulletWidth, false);
        this.velocity = velocity;
    }

    /**
     *
     * @return Velocity of projectile
     */
    public int getVelocity() {
        return velocity;
    }

    /**
     * Checks if the
     * @param xx X coordinate to check collision
     * @param yy Y coordinate to check collision
     * @return True if the bullet has connected with a {@link Sprite}
     */
    public boolean checkForCollision(int xx, int yy)
    {
        if((xx < (xPos+bulletWidth) && xx > xPos) && (yy < (yPos+bulletHeight) && yy > yPos))
        {
            return true;
        }
        return false;
    }
}