Bomb Direction line added

master
Chara1236 2022-06-11 20:02:41 -04:00
parent 189b029bbc
commit deed563819
4 changed files with 108 additions and 14 deletions

View File

@ -0,0 +1,20 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class BombDirectionShow extends StickyBomb{
public BombDirectionShow(int x, int y, int xVelocity, int yVelocity) {
super(x, y, xVelocity, yVelocity, null, null);
}
public void draw(Graphics g){
isMove = true;
int loopCounter = 0;
while(isMove&&loopCounter<10) {
super.move();
if(isMove) {
g.drawRect(x - GamePanel.camera.x + Player.PLAYER_WIDTH / 2, y + Player.PLAYER_HEIGHT / 2, 2, 2);
}
loopCounter++;
}
}
}

View File

@ -38,6 +38,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public boolean isPaused; public boolean isPaused;
public PauseMenu pauseMenu; public PauseMenu pauseMenu;
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
public static BufferedImage[] explosionArray = new BufferedImage[9]; public static BufferedImage[] explosionArray = new BufferedImage[9];
@ -51,6 +52,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static ArrayList<StickyBomb>bombs = new ArrayList<>(); public static ArrayList<StickyBomb>bombs = new ArrayList<>();
public BombDirectionShow bombDir = null;
public static ArrayList<Particle>particles = new ArrayList<Particle>(); public static ArrayList<Particle>particles = new ArrayList<Particle>();
public static Camera camera; public static Camera camera;
@ -125,9 +127,20 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
player.mouseReleased(e); player.mouseReleased(e);
} }
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
player.mouseDragged(e);
}
}); });
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time! //make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this); gameThread = new Thread(this);
gameThread.start(); gameThread.start();
@ -185,6 +198,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
} }
} }
if(player.leftMouseDown){
bombDir = new BombDirectionShow(GamePanel.player.x + GamePanel.camera.x + WIDTH/2, GamePanel.player.y+HEIGHT/2,
(player.mouseX - GamePanel.player.x) / 20, (player.mouseY - GamePanel.player.y) / 10);
bombDir.draw(g);
}
g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length)+" "+player.leftMouseDown,100,100); g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length)+" "+player.leftMouseDown,100,100);
if (isPaused) { if (isPaused) {

View File

@ -22,7 +22,12 @@ public class Player extends GenericSprite {
public boolean alive; public boolean alive;
private final Sound jump; private final Sound jump;
public static int mouseX;
public static int mouseY;
public boolean leftMouseDown; public boolean leftMouseDown;
// sA[0] is -x, -y // sA[0] is -x, -y
// sA[1] is x, -y // sA[1] is x, -y
// sA[2] is -x, y // sA[2] is -x, y
@ -114,7 +119,8 @@ public class Player extends GenericSprite {
return canUpdate; return canUpdate;
} }
public void move() throws IOException { public void move() throws IOException {
// mouseX = MouseInfo.getPointerInfo().getLocation().x;
// mouseY = MouseInfo.getPointerInfo().getLocation().y;
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
GamePanel.camera.x += -Math.signum(xVelocity); GamePanel.camera.x += -Math.signum(xVelocity);
} }
@ -203,18 +209,28 @@ public class Player extends GenericSprite {
} }
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
if(e.getButton()==MouseEvent.BUTTON1) { if(e.getButton()==MouseEvent.BUTTON1) {
leftMouseDown = true; leftMouseDown = true;
} }
}
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
} }
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
int xx = e.getX(); mouseX = e.getX();
int yy = e.getY(); mouseY = e.getY();
if(e.getButton()==MouseEvent.BUTTON1) { if(e.getButton()==MouseEvent.BUTTON1) {
leftMouseDown = false; leftMouseDown = false;
if (GamePanel.bombs.size() < 3) { if (GamePanel.bombs.size() < 3) {
GamePanel.bombs.add(new StickyBomb(GamePanel.player.x + GamePanel.camera.x, GamePanel.player.y, 25, GamePanel.bombs.add(new StickyBomb(GamePanel.player.x + GamePanel.camera.x + WIDTH/2, GamePanel.player.y+HEIGHT/2,
(xx - GamePanel.player.x) / 20, (yy - GamePanel.player.y) / 10, GamePanel.bomb, GamePanel.explosionArray)); (mouseX - GamePanel.player.x) / 20, (mouseY - GamePanel.player.y) / 10, GamePanel.bomb, GamePanel.explosionArray));
} }
} }
} }
@ -225,6 +241,7 @@ public class Player extends GenericSprite {
} }
} }
public int draw(Graphics g, int frame) { public int draw(Graphics g, int frame) {
g.drawString(mouseX+" "+mouseY, 300, 300);
frame %= spriteArray[0][0].length; frame %= spriteArray[0][0].length;
if (!upPressed && !downPressed && !leftPressed && !rightPressed) { if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-10, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-10, y, null);
@ -236,5 +253,7 @@ public class Player extends GenericSprite {
g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-10, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-10, y, null);
return 1; return 1;
} }
} }
} }

View File

@ -4,15 +4,16 @@ import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class StickyBomb extends GenericSprite{ public class StickyBomb extends GenericSprite{
public int length;
public static final int spriteLength = 35; public static final int spriteLength = 35;
public static final int length = 25;
public int xVelocity; public int xVelocity;
public int yVelocity; public int yVelocity;
public boolean isMove; public boolean isMove;
public int realX; public int realX;
public static BufferedImage sprite ; public BufferedImage sprite ;
public static BufferedImage[] explosionSpriteArray; public BufferedImage[] explosionSpriteArray;
public int fuse; public int fuse;
public int explosionPixel = 0; public int explosionPixel = 0;
@ -22,9 +23,10 @@ public class StickyBomb extends GenericSprite{
public boolean erase; public boolean erase;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){
public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){
super(x,y,length,length); super(x,y,length,length);
this.length = length;
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
this.sprite = sprite; this.sprite = sprite;
@ -83,6 +85,7 @@ public class StickyBomb extends GenericSprite{
return false; return false;
} }
public void move(){ public void move(){
boolean checked = false;
if(yVelocity>50){yVelocity=50;} if(yVelocity>50){yVelocity=50;}
update(); update();
if(fuse>0) { if(fuse>0) {
@ -93,10 +96,40 @@ public class StickyBomb extends GenericSprite{
} }
if(isMove) { if(isMove) {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
checked = true;
x += -Math.signum(xVelocity); x += -Math.signum(xVelocity);
//isMove = false; isMove = false;
int updateAmount = 0;
if(xVelocity>0&&yVelocity>0){
while(canUpdate(updateAmount, updateAmount)){
updateAmount++;
}
x+=updateAmount;
y+=updateAmount;
} else if(xVelocity<0&&yVelocity>0){
while(canUpdate(-updateAmount, updateAmount)){
updateAmount++;
}
x-=updateAmount;
y+=updateAmount;
} else if(xVelocity>0&&yVelocity<0){
while(canUpdate(updateAmount, -updateAmount)){
updateAmount++;
}
x+=updateAmount;
y-=updateAmount;
} else if(xVelocity<0&&yVelocity<0){
while(canUpdate(-updateAmount, -updateAmount)){
updateAmount++;
}
x-=updateAmount;
y-=updateAmount;
}
xVelocity = 0;
yVelocity = 0;
} }
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
checked = true;
isMove = false; isMove = false;
int updateAmount = 0; int updateAmount = 0;
if(xVelocity>0){ if(xVelocity>0){
@ -113,6 +146,7 @@ public class StickyBomb extends GenericSprite{
xVelocity = 0; xVelocity = 0;
} }
if(!canUpdate(0, yVelocity)){ if(!canUpdate(0, yVelocity)){
checked = true;
isMove = false; isMove = false;
if(yVelocity>0){ if(yVelocity>0){
while(canUpdate(0,1)){ while(canUpdate(0,1)){
@ -126,7 +160,7 @@ public class StickyBomb extends GenericSprite{
} }
yVelocity = 0; yVelocity = 0;
} }
if(canUpdate(xVelocity, yVelocity)) { if(canUpdate(xVelocity, yVelocity)&&!checked) {
y = y + (int) yVelocity; y = y + (int) yVelocity;
x = x + (int) xVelocity; x = x + (int) xVelocity;
} else { } else {
@ -139,6 +173,7 @@ public class StickyBomb extends GenericSprite{
public void draw(Graphics g){ public void draw(Graphics g){
if (explosionCounter >= 2) { if (explosionCounter >= 2) {
explosionPixel += 1; explosionPixel += 1;
@ -146,6 +181,7 @@ public class StickyBomb extends GenericSprite{
} }
if(alive) { if(alive) {
g.drawImage(sprite, x - GamePanel.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null); g.drawImage(sprite, x - GamePanel.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null);
//g.drawRect(x-GamePanel.camera.x,y,length,length);
} else if (explosionPixel < explosionSpriteArray.length - 1) { } else if (explosionPixel < explosionSpriteArray.length - 1) {
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel, g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel,
y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null); y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null);