Fix camera in main screen, and remove unnecessary sleep

master
John 2022-06-07 16:40:28 -04:00
parent cb6e8fcacf
commit b47b3cec95
3 changed files with 8 additions and 21 deletions

View File

@ -7,8 +7,9 @@ public class BackgroundImage {
public int width, height; public int width, height;
public int parallaxRatio; public int parallaxRatio;
public BufferedImage backgroundImage; public BufferedImage backgroundImage;
public Camera camera;
public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio) { public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio, Camera camera) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
@ -16,10 +17,12 @@ public class BackgroundImage {
this.backgroundImage = backgroundImage; this.backgroundImage = backgroundImage;
this.parallaxRatio = parallaxRatio; this.parallaxRatio = parallaxRatio;
this.camera = camera;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width, y, width, height, null); g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width, y, width, height, null);
// added to prevent the background image from disappearing // added to prevent the background image from disappearing
g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width + width - 1, y, width, height, null); g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width + width - 1, y, width, height, null);
} }
} }

View File

@ -55,7 +55,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
this.gameFrame = gameFrame; this.gameFrame = gameFrame;
camera = new Camera(0); camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
try { try {
// load player sprites from disk here // load player sprites from disk here
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {

View File

@ -49,7 +49,7 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
textBoxArray.add(enter); textBoxArray.add(enter);
textBoxArray.add(settings); textBoxArray.add(settings);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
// the height of 35 is set because it is half of the original tile height (i.e., 70px) // the height of 35 is set because it is half of the original tile height (i.e., 70px)
this.setFocusable(true); //make everything in this class appear on the screen this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
@ -132,28 +132,12 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
CardLayout cardLayout = (CardLayout) gameFrame.getLayout(); CardLayout cardLayout = (CardLayout) gameFrame.getLayout();
cardLayout.show(gameFrame, textBoxArray.get(currentBox).id); cardLayout.show(gameFrame, textBoxArray.get(currentBox).id);
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { } else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
// sleep to prevent excessively fast scrolling
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
currentBox = (currentBox + 1) % textBoxArray.size(); currentBox = (currentBox + 1) % textBoxArray.size();
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { } else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// if currentBox > 0, subtract one // if currentBox > 0, subtract one
// else, set to TOTAL_BOXES-1 // else, set to TOTAL_BOXES-1
currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1; currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) { } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// if currentBox > total box amount - 1, set to 0 // if currentBox > total box amount - 1, set to 0
// else, set to TOTAL_BOXES-1 // else, set to TOTAL_BOXES-1
currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0; currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;