Commit c7f5859f authored by Jiawei  Xiang's avatar Jiawei Xiang
Browse files

General function for player and Enemy

parent c524a53d
Loading
Loading
Loading
Loading
+78 −0
Original line number Original line Diff line number Diff line
package com.example.a8_bitinvader;

import java.util.LinkedList;
import java.util.Queue;

public class Player extends Sprite{
    public class Bullet{
        private final int  speed = 5;  //this can be changed ltr;
        private final int X;
        private int Y;
        public Bullet(int xx, int yy){
            X = xx;
            Y = yy;
        }
        public void goUp(){
            Y += speed;
        }
        public int[] getLocation() {
            return new int[]{X, Y};
        }
    }

    private int health = 3;

    private Queue<Bullet> Bullets = new LinkedList<>(); // max on the screen at the same time, can be change ltr;

    public Player() {
        super(0,0, 0); //this should be the image number for the player
        x = screenW/2;  //haven't implement it yet
        y = 9 * screenH/10; //haven't implement it yet
    }

    //get input from the joystick, and change the location

    // tbh I think this is goingto give us an error ltr, becuase we are going to call update and shoot constantly, I wonder what will happen if we write and deleting at the same time. We'll see tho
    public void update(int joyStickInputX, int joyStickInputY){
        x += joyStickInputX;
        y += joyStickInputY;
        for(Bullet ii : Bullets) {
            ii.goUp();
        }
    }

    @Override //the return doesn't really mean anything all we need is that when any of the point hit within the hit box of player, call the loose health function.
    public boolean touching(int xx, int yy)
    {
        if( super.touching(xx,yy) ) {
            looseHealth();
            return true;
        } else {
            return false;
        }
    }

    // when looseHealth drop the help to 0, call the destroy function. which we will override ltr
    public void looseHealth() {
        health -= 1;
        if (health == 0) {
            destroy();
        }
    }


    public void shoot() {
        Bullets.add(new Bullet(x,y));
        if(Bullets.size() == 5){
            Bullets.remove();   // this should remove the first one that go into the list. it is a Que, so first in first out.
        }
    }

    @Override
    public void destroy() {
        //call animation also.
        //this should call the pulse function for the game, and manu would pop up.
        //implement ltr
    }

}
Loading