diff --git a/src/main/java/dev/dvorakmortonxd/Game.java b/src/main/java/dev/dvorakmortonxd/Game.java index 62c3ec7..2002624 100644 --- a/src/main/java/dev/dvorakmortonxd/Game.java +++ b/src/main/java/dev/dvorakmortonxd/Game.java @@ -11,14 +11,14 @@ public class Game extends JPanel implements ActionListener { // defines the window dimensions public final int windowWidth = 900; public final int windowHeight = 550; - // defines the star count for the main menu and also creates the arrays for the star positions + + // defines the star count for the main menu, predefines the size of the stars and also creates the arrays for the star positions public final int MENU_STAR_COUNT = 96; public final int[] starX = new int[MENU_STAR_COUNT]; public final int[] starY = new int[MENU_STAR_COUNT]; + public int starSize; - public int starSize = 1; - - // Font + // font public static Font caveat; public static Font titleCaveat; @@ -37,7 +37,7 @@ public class Game extends JPanel implements ActionListener { public static BufferedImage landingPlatform; public static BufferedImage sun; - // predefines all the player sprites (tabbed because its technically a part of the last comment lol) + // predefines all the player sprites (tabbed because it's technically a part of the last comment lol) public static BufferedImage playerForward0; public static BufferedImage playerForward1; public static BufferedImage playerForward2; @@ -51,24 +51,41 @@ public class Game extends JPanel implements ActionListener { public static BufferedImage playerRight1; public static BufferedImage playerRight2; - public final Timer timer = new Timer(16, this); // 60 FPS + // sets the FPS up (its 16 because 1000/60 is around 16.666 but it only accepts integers so its just rounded to 60) + public final Timer timer = new Timer(16, this); // 60 FPS (actually more so around 62.5 but thats kinda irrelevant) + // predefines the mouse position variables public int mouseX, mouseY; + /* defines the level which is used to determine what exactly shows (for example main menu is level 1), + levelProgress which is only used for the animation from level 1 to level 2 + and fadeAnimation which is also used for the animation from level 1 to level 2 */ public int level = 1; public boolean levelProgress = false; // this is used to make the transition from the Game menu to the game level look cool public int fadeAnimation = 0; - public int hoverYOffset = 0; + /* defines variables which make the hovering animation work (for example the planet on the main menu), + hoverYOffset is by how much the planetY changes (it works by adding the hoverYOffset ontop of planetYPre), + hoverModifier is by how many the hoverYOffset changes every tick */ + public int hoverYOffset; public String hoverDirection = "up"; // this makes the cool floating animation on the Game menu - public int planetX = 450; public int planetYPre = 140; + public boolean hoveringOverPlanet = false; + int hoverModifier; + + // defines planet variables (position, size and collision box) + public int planetX = 450; public int planetY = planetYPre + hoverYOffset /6; public int planetSize = 128*3; public Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize); - public boolean hoveringOverPlanet = false; - int hoverModifier; + + /* defines by how much every tick the opacity of "glowing" objects changes + (like rocket exhaust and the sun) and the initial opacity value. */ double opacityModifier; + public float opacityAnimation = 0.03f; + + /* button variables (position, size, collision box and a variable that checks if the user is hovering over the button, + which is usually used to change the sprite of the button to a highlighted version) */ public int playButtonX = 70; public int quitButtonX = 70; public int playButtonY = 210; @@ -80,203 +97,29 @@ public class Game extends JPanel implements ActionListener { public boolean hoveringOverPlayButton = false; public boolean hoveringOverQuitButton = false; + /* defines rocket variables (Y position of the rocket itself and its exhaust, + the position of the bottom part of the rocket which falls off) */ public int rocketYPre = windowHeight/2-125; public int rocketY = windowHeight/2-125; public int rocketExhaustY = rocketY; public int rocketBottomX; public int rocketBottomY; - public float opacityAnimation = 0.03f; public int actionTimer = 0; + + // Starts update.java which is basically a class that contains all the stuff that happens every tick @Override public void actionPerformed(ActionEvent e) { // update step - Random rand = new Random(); - starSize = rand.nextInt(2,4); - - hoveringOverPlanet = planetCollision.contains(mouseX, mouseY); - hoveringOverPlayButton = playButtonCollision.contains(mouseX, mouseY); - hoveringOverQuitButton = quitButtonCollision.contains(mouseX, mouseY); - if (hoverDirection == "up") { - hoverModifier = 1; - opacityModifier = 0.01f; - if (hoverYOffset > 90) { hoverDirection = "down"; } - } else { - hoverModifier = -1; - opacityModifier = -0.01f; - if (hoverYOffset <= 0) { hoverDirection = "up"; } - } - hoverYOffset += hoverModifier; - opacityAnimation = (float) Math.max(0.01, Math.min(1.0, opacityAnimation + opacityModifier)); // I've replaced this used-to-be if statement with Math.min/max function which makes it so it never goes below 0.01 and above 1.00 - if (level == 1) { - planetY = planetYPre + hoverYOffset / 6; - if (levelProgress) { - fadeAnimation += 20; - } - if (windowWidth - fadeAnimation <= 0) { level = 2; } - } - if (level == 2) { - if (actionTimer < 1200) { - opacityAnimation = 0.01f; - } - rocketY = rocketYPre + hoverYOffset / 3; - rocketExhaustY = rocketYPre + 150 + hoverYOffset / 2; - actionTimer++; - fadeAnimation += 20; - if (actionTimer > 1760) { - level = 3; - } - } - repaint(); // triggers render + update.performPerTick(this); } + // Renders the game (draws everything on the screen) @Override protected void paintComponent(Graphics g) { // render step super.paintComponent(g); // this clears the previous frame - if (level == 1) { - g.setColor(Color.WHITE); - for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars - g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize); - } - g.setColor(Color.GRAY); - g.setFont(titleCaveat); - g.drawString("Very Cool Space Sim", 72, 100); - - if (planetZX311 != null) { - if (!hoveringOverPlanet) { - g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this); - } else { - g.drawImage(planetZX311H, planetX, planetY, planetSize, planetSize, this); - } - } - - if (button != null) { - if (!hoveringOverPlayButton) { - g.drawImage(button, playButtonX, playButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.BLACK); - g.setFont(caveat.deriveFont(Font.BOLD, 48f)); - g.drawString("Start the game", playButtonX +25, playButtonY+67); - } else { - g.drawImage(buttonH, playButtonX, playButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.WHITE); - g.setFont(caveat.deriveFont(Font.BOLD, 48f)); - g.drawString("Start the game", playButtonX +25, playButtonY+67); - } - if (!hoveringOverQuitButton) { - g.drawImage(button, quitButtonX, quitButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.BLACK); - g.setFont(caveat.deriveFont(Font.BOLD, 40f)); - g.drawString("Quit (pretty lame)", quitButtonX +24, quitButtonY+67); - } else { - g.drawImage(buttonH, quitButtonX, quitButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.WHITE); - g.setFont(caveat.deriveFont(Font.BOLD, 40f)); - g.drawString("Quit (pretty lame)", quitButtonX +24, quitButtonY+67); - } - } - if (levelProgress) { - g.setColor(Color.BLACK); - g.fillRect(windowWidth - fadeAnimation, 0, windowWidth, windowHeight); - } - } - else if (level == 2) { - if (actionTimer < 260) { - g.setColor(Color.GRAY); - for (int i = 0; i < 16; i++) { // drawing the stars - g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize); - } - Graphics2D g2 = (Graphics2D) g; - g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacityAnimation)); - g2.drawImage(rocketExhaust, windowWidth / 2 - 25, rocketExhaustY, 50, 100, this); - g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent - g.drawImage(rocket, windowWidth / 2 - 73, rocketY, 147, 234, this); - } else { - if (actionTimer > 320) { - if ((actionTimer -320) / 4 <= 255) { - g.setColor(new Color((actionTimer - 320) / 8, (actionTimer - 320) / 8, (actionTimer - 320) / 4)); - } else { - g.setColor(new Color(127, 127,255)); - } - g.fillRect(0,0,windowWidth,windowHeight); - if (actionTimer < 1000) { - g.setColor(Color.GRAY); - for (int i = 0; i < 16; i++) { // drawing the stars - g.fillOval(starX[i] - starSize, starY[i] - starSize, starSize, starSize); - } - } - if ((rocketYPre + (actionTimer -321)/5) <= 300){ - g.drawImage(parachute, windowWidth / 2 - 73, rocketY - hoverYOffset / 3 - 42 + (actionTimer -321)/5, 147, 90, this); - g.drawImage(rocketTop, windowWidth / 2 - 73, rocketY - hoverYOffset / 3 + (actionTimer -321)/5, 147, 84, this); - } else { - Graphics2D g2 = (Graphics2D) g; - g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacityAnimation)); - g2.drawImage(sun, 600, -80, 384, 384, this); - g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent - if (actionTimer < 1200) { - g.drawImage(parachute, windowWidth / 2 - 73, 258, 147, 90, this); - g.drawImage(rocketTop, windowWidth / 2 - 73, 300, 147, 84, this); - } else if (1400 > actionTimer && actionTimer >= 1200) { - g.drawImage(parachute, windowWidth / 2 - 73, 258, 147, 90, this); - g.drawImage(rocketLanding0, windowWidth / 2 - 73, 300, 147, 111, this); - } else if (1400 <= actionTimer && actionTimer < 1550) {; - g.drawImage(parachute, windowWidth / 2 - 73, 258, 147, 90, this); - g.drawImage(rocketLanding1, windowWidth / 2 - 73, 300, 147, 111, this); - } else { - g.drawImage(parachute, windowWidth / 2 - 73, 258+(actionTimer -1550)*3/2, 147, 90, this); // parachute is falling! - g.drawImage(rocketLanding1, windowWidth / 2 - 73, 300, 147, 111, this); - } - - // (windowHeight-210) is the desired height - if ((1100-(actionTimer / 2 )) >= 340) { - g.drawImage(landingPlatform, 0, 1100-(actionTimer /2), windowWidth, windowWidth/128*30, this); - } else { - g.drawImage(landingPlatform, 0, 340, windowWidth, windowWidth/128*30, this); - } - } - - } else { - g.setColor(Color.GRAY); - for (int i = 0; i < 16; i++) { // drawing the stars - g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize); - } - g.drawImage(rocketTop, windowWidth / 2 - 73, rocketY - hoverYOffset / 3, 147, 84, this); - } - - Graphics2D g2 = (Graphics2D) g; - rocketBottomX = windowWidth / 2 - 73; - rocketBottomY = rocketY - hoverYOffset / 3 + 64 + (actionTimer - 260) * 2; - g2.rotate(Math.toRadians((actionTimer -260)/3), rocketBottomX + 147 / 2, rocketBottomY + 171 / 2); - g2.drawImage(rocketBottom, rocketBottomX, rocketBottomY, 147, 171, this); - } - - if (levelProgress) { - g.setColor(Color.BLACK); - g.fillRect(windowWidth-fadeAnimation, 0, windowWidth, windowHeight); - } - } - else if (level == 3) { - g.setColor(new Color(127, 127,255)); - g.fillRect(0,0,windowWidth,windowHeight); - Graphics2D g2 = (Graphics2D) g; - g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacityAnimation)); - g2.drawImage(sun, 600, -80, 384, 384, this); - g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent - g.drawImage(landingPlatform, 0, 340, windowWidth, windowWidth/128*30, this); - g.drawImage(rocketLanding1, windowWidth / 2 - 73, 300, 147, 111, this); - } - else { - g.setColor(Color.BLACK); - g.fillRect(0,0,windowWidth,windowHeight); - g.setColor(Color.WHITE); - g.setFont(caveat.deriveFont(Font.BOLD, 56f)); - g.drawString("Sorry, this is as far as I got with", 30, windowHeight/2-160); - g.drawString("this project, It's still a work in", 30, windowHeight/2-160+70); - g.drawString("progress so I'll definitely add more", 30, windowHeight/2-160+140); - g.drawString("content but I just wanted to make sure", 30, windowHeight/2-160+210); - g.drawString("that I could ship it for now so I", 30, windowHeight/2-160+280); - g.drawString("could be safe from ratticus (I'm scared)", 30, windowHeight/2-160+350); - } - Toolkit.getDefaultToolkit().sync(); + update.paint(this, g); } + // constructor which sets the window up and starts up everything public Game() { // sets the window size setPreferredSize(new Dimension(windowWidth,windowHeight)); @@ -295,6 +138,7 @@ public class Game extends JPanel implements ActionListener { timer.start(); } + // what runs when it starts, sets up the window public static void main(String[] args) { JFrame frame = new JFrame("My cool space sim"); frame.setResizable(false); // makes it so gamers cant just resize the window and leave black parts of the window diff --git a/src/main/java/dev/dvorakmortonxd/update.java b/src/main/java/dev/dvorakmortonxd/update.java index f14ca96..cbdae29 100644 --- a/src/main/java/dev/dvorakmortonxd/update.java +++ b/src/main/java/dev/dvorakmortonxd/update.java @@ -1,4 +1,193 @@ package dev.dvorakmortonxd; +import java.awt.*; +import java.util.Random; + public class update { + static void performPerTick(Game m) { + Random rand = new Random(); + m.starSize = rand.nextInt(2,4); + + m.hoveringOverPlanet = m.planetCollision.contains(m.mouseX, m.mouseY); + m.hoveringOverPlayButton = m.playButtonCollision.contains(m.mouseX, m.mouseY); + m.hoveringOverQuitButton = m.quitButtonCollision.contains(m.mouseX, m.mouseY); + if (m.hoverDirection.equals("up")) { + m.hoverModifier = 1; + m.opacityModifier = 0.007f; + if (m.hoverYOffset > 90) { m.hoverDirection = "down"; } + } else { + m.hoverModifier = -1; + m.opacityModifier = -0.007f; + if (m.hoverYOffset <= 0) { m.hoverDirection = "up"; } + } + m.hoverYOffset += m.hoverModifier; + m.opacityAnimation = (float) Math.max(0.3, Math.min(1.0, m.opacityAnimation + m.opacityModifier)); // I've replaced this used-to-be if statement with Math.min/max function which makes it so it never goes below 0.01 and above 1.00 + if (m.level == 1) { + m.planetY = m.planetYPre + m.hoverYOffset / 6; + if (m.levelProgress) { + m.fadeAnimation += 20; + } + if (m.windowWidth - m.fadeAnimation <= 0) { m.level = 2; } + } + if (m.level == 2) { + if (m.actionTimer < 1000 && m.actionTimer > 950) { + m.opacityAnimation = 0.01f; + } + m.rocketY = m.rocketYPre + m.hoverYOffset / 3; + m.rocketExhaustY = m.rocketYPre + 150 + m.hoverYOffset / 2; + m.actionTimer++; + m.fadeAnimation += 20; + if (m.actionTimer > 1760) { + m.level = 3; + } + } + // triggers render + m.repaint(); + } + static void paint(Game m, Graphics g) { + if (m.level == 1) { + g.setColor(Color.WHITE); + for (int i = 0; i < m.MENU_STAR_COUNT; i++) { // drawing the stars + g.fillOval(m.starX[i]-m.starSize, m.starY[i]-m.starSize, m.starSize, m.starSize); + } + g.setColor(Color.GRAY); + g.setFont(m.titleCaveat); + g.drawString("Very Cool Space Sim", 72, 100); + + if (m.planetZX311 != null) { + if (!m.hoveringOverPlanet) { + g.drawImage(m.planetZX311, m.planetX, m.planetY, m.planetSize, m.planetSize, m); + } else { + g.drawImage(m.planetZX311H, m.planetX, m.planetY, m.planetSize, m.planetSize, m); + } + } + + if (m.button != null) { + if (!m.hoveringOverPlayButton) { + g.drawImage(m.button, m.playButtonX, m.playButtonY, m.buttonWidth, m.buttonHeight, m); + g.setColor(Color.BLACK); + g.setFont(m.caveat.deriveFont(Font.BOLD, 48f)); + g.drawString("Start the game", m.playButtonX +25, m.playButtonY+67); + } else { + g.drawImage(m.buttonH, m.playButtonX, m.playButtonY, m.buttonWidth, m.buttonHeight, m); + g.setColor(Color.WHITE); + g.setFont(m.caveat.deriveFont(Font.BOLD, 48f)); + g.drawString("Start the game", m.playButtonX +25, m.playButtonY+67); + } + if (!m.hoveringOverQuitButton) { + g.drawImage(m.button, m.quitButtonX, m.quitButtonY, m.buttonWidth, m.buttonHeight, m); + g.setColor(Color.BLACK); + g.setFont(m.caveat.deriveFont(Font.BOLD, 40f)); + g.drawString("Quit (pretty lame)", m.quitButtonX +24, m.quitButtonY+67); + } else { + g.drawImage(m.buttonH, m.quitButtonX, m.quitButtonY, m.buttonWidth, m.buttonHeight, m); + g.setColor(Color.WHITE); + g.setFont(m.caveat.deriveFont(Font.BOLD, 40f)); + g.drawString("Quit (pretty lame)", m.quitButtonX +24, m.quitButtonY+67); + } + } + if (m.levelProgress) { + g.setColor(Color.BLACK); + g.fillRect(m.windowWidth - m.fadeAnimation, 0, m.windowWidth, m.windowHeight); + } + } + else if (m.level == 2) { + if (m.actionTimer < 260) { + g.setColor(Color.GRAY); + for (int i = 0; i < 16; i++) { // drawing the stars + g.fillOval(m.starX[i]-m.starSize, m.starY[i]-m.starSize, m.starSize, m.starSize); + } + Graphics2D g2 = (Graphics2D) g; + g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, m.opacityAnimation)); + g2.drawImage(m.rocketExhaust, m.windowWidth / 2 - 25, m.rocketExhaustY, 50, 100, m); + g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent + g.drawImage(m.rocket, m.windowWidth / 2 - 73, m.rocketY, 147, 234, m); + } else { + if (m.actionTimer > 320) { + if ((m.actionTimer -320) / 4 <= 255) { + g.setColor(new Color((m.actionTimer - 320) / 8, (m.actionTimer - 320) / 8, (m.actionTimer - 320) / 4)); + } else { + g.setColor(new Color(127, 127,255)); + } + g.fillRect(0,0,m.windowWidth,m.windowHeight); + if (m.actionTimer < 1000) { + g.setColor(Color.GRAY); + for (int i = 0; i < 16; i++) { // drawing the stars + g.fillOval(m.starX[i] - m.starSize, m.starY[i] - m.starSize, m.starSize, m.starSize); + } + } + if ((m.rocketYPre + (m.actionTimer -321)/5) <= 300){ + g.drawImage(m.parachute, m.windowWidth / 2 - 73, m.rocketY - m.hoverYOffset / 3 - 42 + (m.actionTimer -321)/5, 147, 90, m); + g.drawImage(m.rocketTop, m.windowWidth / 2 - 73, m.rocketY - m.hoverYOffset / 3 + (m.actionTimer -321)/5, 147, 84, m); + } else { + Graphics2D g2 = (Graphics2D) g; + g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, m.opacityAnimation)); + g2.drawImage(m.sun, 600, -80, 384, 384, m); + g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent + if (m.actionTimer < 1200) { + g.drawImage(m.parachute, m.windowWidth / 2 - 73, 258, 147, 90, m); + g.drawImage(m.rocketTop, m.windowWidth / 2 - 73, 300, 147, 84, m); + } else if (1400 > m.actionTimer && m.actionTimer >= 1200) { + g.drawImage(m.parachute, m.windowWidth / 2 - 73, 258, 147, 90, m); + g.drawImage(m.rocketLanding0, m.windowWidth / 2 - 73, 300, 147, 111, m); + } else if (1400 <= m.actionTimer && m.actionTimer < 1550) {; + g.drawImage(m.parachute, m.windowWidth / 2 - 73, 258, 147, 90, m); + g.drawImage(m.rocketLanding1, m.windowWidth / 2 - 73, 300, 147, 111, m); + } else { + g.drawImage(m.parachute, m.windowWidth / 2 - 73, 258+(m.actionTimer -1550)*3/2, 147, 90, m); // parachute is falling! + g.drawImage(m.rocketLanding1, m.windowWidth / 2 - 73, 300, 147, 111, m); + } + + // (windowHeight-210) is the desired height + if ((1100-(m.actionTimer / 2 )) >= 340) { + g.drawImage(m.landingPlatform, 0, 1100-(m.actionTimer /2), m.windowWidth, m.windowWidth/128*30, m); + } else { + g.drawImage(m.landingPlatform, 0, 340, m.windowWidth, m.windowWidth/128*30, m); + } + } + + } else { + g.setColor(Color.GRAY); + for (int i = 0; i < 16; i++) { // drawing the stars + g.fillOval(m.starX[i]-m.starSize, m.starY[i]-m.starSize, m.starSize, m.starSize); + } + g.drawImage(m.rocketTop, m.windowWidth / 2 - 73, m.rocketY - m.hoverYOffset / 3, 147, 84, m); + } + + Graphics2D g2 = (Graphics2D) g; + m.rocketBottomX = m.windowWidth / 2 - 73; + m.rocketBottomY = m.rocketY - m.hoverYOffset / 3 + 64 + (m.actionTimer - 260) * 2; + g2.rotate(Math.toRadians((m.actionTimer -260)/3), m.rocketBottomX + 147 / 2, m.rocketBottomY + 171 / 2); + g2.drawImage(m.rocketBottom, m.rocketBottomX, m.rocketBottomY, 147, 171, m); + } + + if (m.levelProgress) { + g.setColor(Color.BLACK); + g.fillRect(m.windowWidth-m.fadeAnimation, 0, m.windowWidth, m.windowHeight); + } + } + else if (m.level == 3) { + g.setColor(new Color(127, 127,255)); + g.fillRect(0,0,m.windowWidth,m.windowHeight); + Graphics2D g2 = (Graphics2D) g; + g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, m.opacityAnimation)); + g2.drawImage(m.sun, 600, -80, 384, 384, m); + g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent + g.drawImage(m.landingPlatform, 0, 340, m.windowWidth, m.windowWidth/128*30, m); + g.drawImage(m.rocketLanding1, m.windowWidth / 2 - 73, 300, 147, 111, m); + } + else { + g.setColor(Color.BLACK); + g.fillRect(0,0,m.windowWidth,m.windowHeight); + g.setColor(Color.WHITE); + g.setFont(m.caveat.deriveFont(Font.BOLD, 56f)); + g.drawString("Sorry, this is as far as I got with", 30, m.windowHeight/2-160); + g.drawString("this project, It's still a work in", 30, m.windowHeight/2-160+70); + g.drawString("progress so I'll definitely add more", 30, m.windowHeight/2-160+140); + g.drawString("content but I just wanted to make sure", 30, m.windowHeight/2-160+210); + g.drawString("that I could ship it for now so I", 30, m.windowHeight/2-160+280); + g.drawString("could be safe from ratticus (I'm scared)", 30, m.windowHeight/2-160+350); + } + Toolkit.getDefaultToolkit().sync(); + } }