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 PauseMenu pauseMenu;
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
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<StickyBomb>bombs = new ArrayList<>();
public BombDirectionShow bombDir = null;
public static ArrayList<Particle>particles = new ArrayList<Particle>();
public static Camera camera;
@ -125,9 +127,20 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public void mouseReleased(MouseEvent e) {
player.mouseReleased(e);
}
});
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
player.mouseDragged(e);
}
});
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!
gameThread = new Thread(this);
gameThread.start();
@ -172,7 +185,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
if (bombs.get(i).erase) {
bombs.remove(i);
} else {
bombs.get(i).draw(g);
bombs.get(i).draw(g);
}
}
}
@ -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);
if (isPaused) {

View File

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

View File

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