Added fade, refactored SettingPanel

master
John 2022-06-13 13:42:05 -04:00
parent e698273bfa
commit a7500073e3
3 changed files with 14 additions and 96 deletions

View File

@ -342,7 +342,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public void updateEnemy(){
for(int i=0; i<enemy.size(); i++){
if(enemy.get(i).isDead){
enemy.get(i).fadeCounter--;
// enemy.get(i).fadeCounter--;
if(enemy.get(i).fadeCounter<=0){
enemy.remove(i);
}

View File

@ -20,7 +20,7 @@ public class NonPlayer extends GenericSprite {
public int health;
public int fadeCounter;
public double fadeCounter;
// private final Sound bump;
public BufferedImage[][][] spriteArray;
@ -36,7 +36,7 @@ public class NonPlayer extends GenericSprite {
HEIGHT = npcHeight;
xVelocity = 3;
fadeCounter = 100;;
fadeCounter = 1;
}
@ -92,7 +92,11 @@ public class NonPlayer extends GenericSprite {
g.drawImage(spriteArray[currentXDirection][currentYDirection][frame], x-GamePanel.camera.x, y, null);
return 1;
} else {
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)fadeCounter));
g.drawImage(spriteArray[currentXDirection][currentYDirection][spriteArray[0][0].length-1], x-GamePanel.camera.x, (int)(y+HEIGHT/1.7), null);
// reset composite to not affect other sprites being drawn
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
fadeCounter -= 0.01;
return 0;
}
}

View File

@ -18,19 +18,12 @@ import java.io.IOException;
import java.security.Key;
import java.util.ArrayList;
public class SettingPanel extends JPanel implements Runnable, KeyListener{
public class SettingPanel extends MenuPanel {
//dimensions of window
public static final int GAME_WIDTH = 1225;
public static final int GAME_HEIGHT = 630;
public CameraPanel gameFrame;
public static Camera camera;
public Thread gameThread;
public Image image;
public Graphics graphics;
public BackgroundImage background;
public TextBox title;
public TextBox up, down, left, right;
public ArrayList<TextBox> textBoxArray = new ArrayList<TextBox>();
@ -46,62 +39,19 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
public BufferedImage backgroundImage = GamePanel.getImage("img/backgrounds/pointyMountains.png");
public SettingPanel(CameraPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
this.gameFrame = gameFrame;
camera = gameFrame.camera;
super(gameFrame);
title = new TextBox(100, 400, 100, GAME_WIDTH, standardFont, "Settings", null);
up = new TextBox(300, 600, 50, GAME_WIDTH, smallFont, "Up", Integer.toString(KeyEvent.VK_W));
down = new TextBox(350, 600, 50, GAME_WIDTH, smallFont, "Down", Integer.toString(KeyEvent.VK_A));
left = new TextBox(400, 600, 50, GAME_WIDTH, smallFont, "Left", Integer.toString(KeyEvent.VK_S));
down = new TextBox(350, 600, 50, GAME_WIDTH, smallFont, "Down", Integer.toString(KeyEvent.VK_S));
left = new TextBox(400, 600, 50, GAME_WIDTH, smallFont, "Left", Integer.toString(KeyEvent.VK_A));
right = new TextBox(450, 600, 50, GAME_WIDTH, smallFont, "Right", Integer.toString(KeyEvent.VK_D));
textBoxArray.add(up);
textBoxArray.add(down);
textBoxArray.add(left);
textBoxArray.add(right);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
pauseMenu = new PauseMenu(GAME_HEIGHT/2, 0, 400, 400, GAME_WIDTH, smallFont, "Enter your key");
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
// request focus when the CardLayout selects this game
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent cEvt) {
Component src = (Component) cEvt.getSource();
src.requestFocusInWindow();
}
});
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (hoverCheck(e)) {
keyPressed(new KeyEvent(new Component() {
}, 0, 0, 0, KeyEvent.VK_ENTER));
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
hoverCheck(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();
}
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
public void paint(Graphics g){
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen
graphics = image.getGraphics();
draw(graphics, playerFrame, enemyFrame);//update the positions of everything on the screen
g.drawImage(image, 0, 0, this); //move the image on the screen
}
//call the draw methods in each class to update positions as things move
@ -135,34 +85,9 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
}
}
//call the move methods in other classes to update positions
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
public void move(){
}
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime();
double amountOfTicks = 60;
double ns = 1000000000/amountOfTicks;
double delta = 0;
long now;
while(true){ //this is the infinite game loop
now = System.nanoTime();
delta = delta + (now-lastTime)/ns;
lastTime = now;
//only update screen if enough time has passed
if(delta >= 1){
changeKeyBind();
move();
camera.x += 10;
repaint();
delta--;
}
}
// move is repurposed to change key bind
public void move() {
changeKeyBind();
}
public boolean hoverCheck(MouseEvent e) {
@ -212,15 +137,4 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
currentBox = UtilityFunction.processBox(e, currentBox, textBoxArray);
}
}
//if a key is released, we'll send it over to the Player class for processing
public void keyReleased(KeyEvent e){
}
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface
public void keyTyped(KeyEvent e){
}
}