2022-05-30 19:46:53 +01:00
|
|
|
/* PlayerBall class defines behaviours for the player-controlled ball
|
|
|
|
|
|
|
|
child of Rectangle because that makes it easy to draw and check for collision
|
|
|
|
|
|
|
|
In 2D GUI, basically everything is a rectangle even if it doesn't look like it!
|
|
|
|
*/
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
|
2022-05-31 18:19:23 +01:00
|
|
|
public class GenericSprite extends Rectangle{
|
2022-05-30 19:46:53 +01:00
|
|
|
|
2022-05-31 18:19:41 +01:00
|
|
|
public double yVelocity;
|
|
|
|
public double xVelocity;
|
|
|
|
public final double SPEED = 20; //movement speed of ball
|
2022-06-01 19:39:10 +01:00
|
|
|
public final double speedCap = 5; //Speed cap of ball
|
2022-06-03 22:33:21 +01:00
|
|
|
public int WIDTH; //size of ball
|
|
|
|
public int HEIGHT; //size of ball
|
2022-05-31 18:19:41 +01:00
|
|
|
public boolean rightPressed = false;
|
|
|
|
public boolean leftPressed = false;
|
|
|
|
public boolean upPressed= false;
|
|
|
|
public boolean downPressed = false;
|
|
|
|
public boolean isGrounded = false;
|
2022-05-30 19:46:53 +01:00
|
|
|
//constructor creates ball at given location with given dimensions
|
2022-06-03 22:33:21 +01:00
|
|
|
// TODO: reverse order of height and width
|
2022-05-31 18:19:23 +01:00
|
|
|
public GenericSprite(int x, int y, int height, int width){
|
2022-06-03 22:33:21 +01:00
|
|
|
super(x, y, width, height);
|
|
|
|
WIDTH = width;
|
|
|
|
HEIGHT = height;
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 18:12:48 +01:00
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
//called from GamePanel when any keyboard input is detected
|
|
|
|
//updates the direction of the ball based on user input
|
|
|
|
//if the keyboard input isn't any of the options (d, a, w, s), then nothing happens
|
|
|
|
public void keyPressed(KeyEvent e){
|
2022-06-02 17:22:53 +01:00
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//called from GamePanel when any key is released (no longer being pressed down)
|
|
|
|
//Makes the ball stop moving in that direction
|
|
|
|
public void keyReleased(KeyEvent e){
|
2022-06-02 18:21:29 +01:00
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//called from GamePanel whenever a mouse click is detected
|
|
|
|
//changes the current location of the ball to be wherever the mouse is located on the screen
|
|
|
|
public void mousePressed(MouseEvent e){
|
2022-06-03 22:33:21 +01:00
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-31 18:19:41 +01:00
|
|
|
public void move(){
|
2022-06-03 22:33:21 +01:00
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
2022-05-31 18:19:41 +01:00
|
|
|
public void capSpeed(){
|
|
|
|
if(xVelocity>speedCap){
|
|
|
|
xVelocity = speedCap;
|
|
|
|
} else if(xVelocity<-1*speedCap) {
|
|
|
|
xVelocity = -1*speedCap;
|
|
|
|
}
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 18:12:48 +01:00
|
|
|
private boolean collide(Tile tile, double x, double y){
|
|
|
|
if(!tile.collision){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(x+WIDTH>tile.x&&x<tile.x+Tile.length&&y-tile.y<Tile.length&&tile.y-y<HEIGHT){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public boolean canUpdate(double x, double y){
|
|
|
|
boolean canUpdate = true;
|
|
|
|
for(Tile i: GamePanel.map){
|
|
|
|
if(collide(i,this.x+x,this.y+y)){
|
|
|
|
canUpdate = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return canUpdate;
|
|
|
|
}
|
2022-05-30 19:46:53 +01:00
|
|
|
//called frequently from the GamePanel class
|
|
|
|
//draws the current location of the ball to the screen
|
|
|
|
public void draw(Graphics g){
|
2022-06-03 22:33:21 +01:00
|
|
|
|
|
|
|
|
2022-05-30 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|