Finalize refactoring
This commit is contained in:
		@@ -7,7 +7,6 @@
 | 
			
		||||
 | 
			
		||||
package com.example.flappybird;
 | 
			
		||||
 | 
			
		||||
import com.example.flappybird.states.GameState;
 | 
			
		||||
import java.awt.*;
 | 
			
		||||
import java.awt.geom.AffineTransform;
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
@@ -33,6 +32,7 @@ public class Bird extends HasPosition {
 | 
			
		||||
    // Bird sprites
 | 
			
		||||
	private final BufferedImage[] sprites;
 | 
			
		||||
 | 
			
		||||
	// Track current frame
 | 
			
		||||
	private static double currentFrame = 0;
 | 
			
		||||
 | 
			
		||||
	public Bird (int x, int y) {
 | 
			
		||||
@@ -150,7 +150,6 @@ public class Bird extends HasPosition {
 | 
			
		||||
 | 
			
		||||
		// Create bird animation and pass in rotation angle
 | 
			
		||||
		animateBird(g, rotation);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void animateBird(Graphics g, double angle) {
 | 
			
		||||
@@ -173,7 +172,9 @@ public class Bird extends HasPosition {
 | 
			
		||||
		// Switch animation frames
 | 
			
		||||
		if (currentFrame >= count - 1) {
 | 
			
		||||
			currentFrame = 0;
 | 
			
		||||
		} else currentFrame += 0.09;
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			currentFrame += 0.09;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -45,18 +45,21 @@ public class FlappyBird implements ActionListener {
 | 
			
		||||
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 | 
			
		||||
		frame.setSize(WIDTH, HEIGHT);
 | 
			
		||||
 | 
			
		||||
		String fontPath = "/res/fonts/flappy-font.ttf";
 | 
			
		||||
 | 
			
		||||
		// Try to load ttf file
 | 
			
		||||
		try {
 | 
			
		||||
			InputStream is = new BufferedInputStream(this.getClass().getResourceAsStream("/res/fonts/flappy-font.ttf"));
 | 
			
		||||
			InputStream fontStream = this.getClass().getResourceAsStream(fontPath);
 | 
			
		||||
            assert fontStream != null;
 | 
			
		||||
 | 
			
		||||
            InputStream is = new BufferedInputStream(fontStream);
 | 
			
		||||
			flappyFontBase = Font.createFont(Font.TRUETYPE_FONT, is);
 | 
			
		||||
 | 
			
		||||
			// Header and sub-header fonts
 | 
			
		||||
			flappyScoreFont = flappyFontBase.deriveFont(Font.PLAIN, 50);
 | 
			
		||||
			flappyFontReal  = flappyFontBase.deriveFont(Font.PLAIN, 20);
 | 
			
		||||
			flappyMiniFont  = flappyFontBase.deriveFont(Font.PLAIN, 15);
 | 
			
		||||
 | 
			
		||||
		} catch (Exception ex) {
 | 
			
		||||
 | 
			
		||||
			// Exit is font cannot be loaded
 | 
			
		||||
			ex.printStackTrace();
 | 
			
		||||
			System.err.println("Could not load Flappy Font!");
 | 
			
		||||
 
 | 
			
		||||
@@ -78,10 +78,6 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
 | 
			
		||||
	// Mouse actions //
 | 
			
		||||
	///////////////////
 | 
			
		||||
 | 
			
		||||
	public Point getClickedPoint() {
 | 
			
		||||
		return clickedPoint;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Checks if point is in rectangle
 | 
			
		||||
	 *
 | 
			
		||||
 
 | 
			
		||||
@@ -12,11 +12,17 @@ public class Pipe extends HasPosition {
 | 
			
		||||
	String location;
 | 
			
		||||
 | 
			
		||||
	// Pipe constants
 | 
			
		||||
	public static final int WIDTH         = 67;
 | 
			
		||||
	public static final int HEIGHT        = 416;
 | 
			
		||||
	public static final int PIPE_DISTANCE = 150;          // Horizontal distance between pipes
 | 
			
		||||
	public static final int PIPE_SPACING  = HEIGHT + 170; // Vertical distance between pipes
 | 
			
		||||
	private static final int SPEED        = -2;
 | 
			
		||||
	public static final int WIDTH = 67;
 | 
			
		||||
	public static final int HEIGHT = 416;
 | 
			
		||||
 | 
			
		||||
	// Horizontal distance between pipes
 | 
			
		||||
	public static final int PIPE_DISTANCE = 150;
 | 
			
		||||
 | 
			
		||||
	// Vertical distance between pipes
 | 
			
		||||
	public static final int PIPE_SPACING = HEIGHT + 200;
 | 
			
		||||
 | 
			
		||||
	// Speed of the bird.
 | 
			
		||||
	private static final int SPEED = -2;
 | 
			
		||||
 | 
			
		||||
	// If the bird can get a point passing this pipe
 | 
			
		||||
	public boolean canAwardPoint = true;
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,6 @@ public class Texture extends HasPosition {
 | 
			
		||||
	private final BufferedImage image;
 | 
			
		||||
	private final Rectangle rect;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	public Texture (BufferedImage image, int x, int y) {
 | 
			
		||||
		super(x, y);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ public abstract class GameState {
 | 
			
		||||
	// Game variables //
 | 
			
		||||
	////////////////////
 | 
			
		||||
 | 
			
		||||
	// Background
 | 
			
		||||
	protected static final int[] baseCoords = { 0, 435 };
 | 
			
		||||
 | 
			
		||||
	// Player score
 | 
			
		||||
@@ -31,8 +32,10 @@ public abstract class GameState {
 | 
			
		||||
	// Arraylist of Pipe objects
 | 
			
		||||
	public static ArrayList<Pipe> pipes;
 | 
			
		||||
 | 
			
		||||
	// Game bird
 | 
			
		||||
	protected static Bird gameBird;
 | 
			
		||||
 | 
			
		||||
	// Game panel
 | 
			
		||||
	public GamePanel gamePanel;
 | 
			
		||||
 | 
			
		||||
	public GameState(GamePanel panel) {
 | 
			
		||||
@@ -45,9 +48,9 @@ public abstract class GameState {
 | 
			
		||||
	public void restart () {
 | 
			
		||||
 | 
			
		||||
		// Reset game statistics
 | 
			
		||||
		score            = 0;
 | 
			
		||||
		pipeDistTracker  = 0;
 | 
			
		||||
		scoreWasGreater  = false;
 | 
			
		||||
		score = 0;
 | 
			
		||||
		pipeDistTracker = 0;
 | 
			
		||||
		scoreWasGreater = false;
 | 
			
		||||
 | 
			
		||||
		// Game bird
 | 
			
		||||
		gameBird = new Bird(172, 250);
 | 
			
		||||
@@ -63,13 +66,11 @@ public abstract class GameState {
 | 
			
		||||
 | 
			
		||||
		// Only move screen if bird is alive
 | 
			
		||||
		if (gameBird.isAlive()) {
 | 
			
		||||
 | 
			
		||||
			// Move base
 | 
			
		||||
			// Moving base effect
 | 
			
		||||
			int baseSpeed = 2;
 | 
			
		||||
			baseCoords[0] = baseCoords[0] - baseSpeed < -435 ? 435 : baseCoords[0] - baseSpeed;
 | 
			
		||||
			baseCoords[1] = baseCoords[1] - baseSpeed < -435 ? 435 : baseCoords[1] - baseSpeed;
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Background
 | 
			
		||||
@@ -96,4 +97,3 @@ public abstract class GameState {
 | 
			
		||||
	public abstract void handleKeyboardEvent(KeyEvent e);
 | 
			
		||||
	public abstract void handleMouseEvent(MouseEvent e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -142,7 +142,6 @@ public class PlayState extends GameState {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Move and draw each pipe
 | 
			
		||||
 | 
			
		||||
        for (Pipe p : pipes) {
 | 
			
		||||
 | 
			
		||||
            // Move the pipe
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								refactoring/flappy-bird/uml/class-diagram.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								refactoring/flappy-bird/uml/class-diagram.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 189 KiB  | 
							
								
								
									
										133
									
								
								refactoring/flappy-bird/uml/class-diagram.puml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								refactoring/flappy-bird/uml/class-diagram.puml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,133 @@
 | 
			
		||||
@startuml
 | 
			
		||||
 | 
			
		||||
!theme plain
 | 
			
		||||
top to bottom direction
 | 
			
		||||
skinparam linetype ortho
 | 
			
		||||
 | 
			
		||||
class Audio {
 | 
			
		||||
  + Audio(): 
 | 
			
		||||
  - playSound(String): void
 | 
			
		||||
  + jump(): void
 | 
			
		||||
  + point(): void
 | 
			
		||||
  + hit(): void
 | 
			
		||||
}
 | 
			
		||||
class Bird {
 | 
			
		||||
  + Bird(int, int): 
 | 
			
		||||
  - isAlive: boolean
 | 
			
		||||
  + renderBird(Graphics): void
 | 
			
		||||
  + jump(): void
 | 
			
		||||
  + killBird(): void
 | 
			
		||||
  + menuFloat(): void
 | 
			
		||||
  + inGameBorders(): void
 | 
			
		||||
  - animateBird(Graphics, double): void
 | 
			
		||||
  + setGameStartPos(): void
 | 
			
		||||
   isAlive: boolean
 | 
			
		||||
}
 | 
			
		||||
class FlappyBird {
 | 
			
		||||
  + FlappyBird(): 
 | 
			
		||||
  + actionPerformed(ActionEvent): void
 | 
			
		||||
  + main(String[]): void
 | 
			
		||||
}
 | 
			
		||||
class GamePanel {
 | 
			
		||||
  + GamePanel(): 
 | 
			
		||||
  - gameState: GameState
 | 
			
		||||
  # clickedPoint: Point
 | 
			
		||||
  + keyReleased(KeyEvent): void
 | 
			
		||||
  + mouseEntered(MouseEvent): void
 | 
			
		||||
  + keyPressed(KeyEvent): void
 | 
			
		||||
  + mousePressed(MouseEvent): void
 | 
			
		||||
  + paintComponent(Graphics): void
 | 
			
		||||
  + keyTyped(KeyEvent): void
 | 
			
		||||
  + mouseClicked(MouseEvent): void
 | 
			
		||||
  + mouseReleased(MouseEvent): void
 | 
			
		||||
  + isTouching(Rectangle): boolean
 | 
			
		||||
  + addNotify(): void
 | 
			
		||||
  + mouseExited(MouseEvent): void
 | 
			
		||||
   gameState: GameState
 | 
			
		||||
   clickedPoint: Point
 | 
			
		||||
}
 | 
			
		||||
class GameState {
 | 
			
		||||
  + GameState(GamePanel): 
 | 
			
		||||
  + prepareScreen(Graphics): void
 | 
			
		||||
  + renderGameState(Graphics): void
 | 
			
		||||
  + handleMouseEvent(MouseEvent): void
 | 
			
		||||
  + handleKeyboardEvent(KeyEvent): void
 | 
			
		||||
  + restart(): void
 | 
			
		||||
  + drawBase(Graphics): void
 | 
			
		||||
}
 | 
			
		||||
class HasPosition {
 | 
			
		||||
  + HasPosition(int, int): 
 | 
			
		||||
  # y: int
 | 
			
		||||
  # x: int
 | 
			
		||||
   y: int
 | 
			
		||||
   x: int
 | 
			
		||||
}
 | 
			
		||||
class Highscore {
 | 
			
		||||
  + Highscore(): 
 | 
			
		||||
  + bestScore(): int
 | 
			
		||||
   newBest: int
 | 
			
		||||
}
 | 
			
		||||
class MenuState {
 | 
			
		||||
  + MenuState(GamePanel): 
 | 
			
		||||
  + handleKeyboardEvent(KeyEvent): void
 | 
			
		||||
  # drawMenu(Graphics): void
 | 
			
		||||
  + drawCentered(String, int, int, int, Graphics): void
 | 
			
		||||
  + handleMouseEvent(MouseEvent): void
 | 
			
		||||
  + renderGameState(Graphics): void
 | 
			
		||||
  + openReviewUrl(): void
 | 
			
		||||
}
 | 
			
		||||
class Pipe {
 | 
			
		||||
  + Pipe(String): 
 | 
			
		||||
  + move(): void
 | 
			
		||||
  + reset(): void
 | 
			
		||||
  + collide(Bird): boolean
 | 
			
		||||
   y: int
 | 
			
		||||
}
 | 
			
		||||
class PlayState {
 | 
			
		||||
  + PlayState(GamePanel): 
 | 
			
		||||
  + drawScore(Graphics, int, boolean, int, int): void
 | 
			
		||||
  + startGameScreen(Graphics): void
 | 
			
		||||
  + pipeHandler(Graphics): void
 | 
			
		||||
  + handleMouseEvent(MouseEvent): void
 | 
			
		||||
  + gameOver(Graphics): void
 | 
			
		||||
  + renderGameState(Graphics): void
 | 
			
		||||
  + handleKeyboardEvent(KeyEvent): void
 | 
			
		||||
}
 | 
			
		||||
class Sprites {
 | 
			
		||||
  + Sprites(): 
 | 
			
		||||
  - putTexture(String, int, int, int, int, int, int): void
 | 
			
		||||
  - resize(BufferedImage): BufferedImage
 | 
			
		||||
   gameTextures: HashMap<String, Texture>
 | 
			
		||||
}
 | 
			
		||||
class Texture {
 | 
			
		||||
  + Texture(BufferedImage, int, int): 
 | 
			
		||||
  - image: BufferedImage
 | 
			
		||||
  - rect: Rectangle
 | 
			
		||||
   image: BufferedImage
 | 
			
		||||
   rect: Rectangle
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Bird         -[#000082,plain]-^  HasPosition 
 | 
			
		||||
FlappyBird   -[#595959,dashed]->  Audio       : "«create»"
 | 
			
		||||
FlappyBird  "1" *-[#595959,plain]-> "audio\n1" Audio       
 | 
			
		||||
FlappyBird   -[#595959,dashed]->  GamePanel   : "«create»"
 | 
			
		||||
FlappyBird  "1" *-[#595959,plain]-> "game\n1" GamePanel   
 | 
			
		||||
FlappyBird   -[#595959,dashed]->  Sprites     : "«create»"
 | 
			
		||||
FlappyBird  "1" *-[#595959,plain]-> "textures\n*" Texture     
 | 
			
		||||
GamePanel   "1" *-[#595959,plain]-> "gameState\n1" GameState   
 | 
			
		||||
GamePanel    -[#595959,dashed]->  MenuState   : "«create»"
 | 
			
		||||
GameState    -[#595959,dashed]->  Bird        : "«create»"
 | 
			
		||||
GameState   "1" *-[#595959,plain]-> "gameBird\n1" Bird        
 | 
			
		||||
GameState   "1" *-[#595959,plain]-> "gamePanel\n1" GamePanel   
 | 
			
		||||
GameState   "1" *-[#595959,plain]-> "pipes\n*" Pipe        
 | 
			
		||||
MenuState    -[#000082,plain]-^  GameState   
 | 
			
		||||
MenuState    -[#595959,dashed]->  PlayState   : "«create»"
 | 
			
		||||
Pipe         -[#000082,plain]-^  HasPosition 
 | 
			
		||||
PlayState    -[#000082,plain]-^  GameState   
 | 
			
		||||
PlayState    -[#595959,dashed]->  Highscore   : "«create»"
 | 
			
		||||
PlayState   "1" *-[#595959,plain]-> "highscore\n1" Highscore   
 | 
			
		||||
PlayState    -[#595959,dashed]->  Pipe        : "«create»"
 | 
			
		||||
Sprites     "1" *-[#595959,plain]-> "textures\n*" Texture     
 | 
			
		||||
Sprites      -[#595959,dashed]->  Texture     : "«create»"
 | 
			
		||||
Texture      -[#000082,plain]-^  HasPosition 
 | 
			
		||||
@enduml
 | 
			
		||||
		Reference in New Issue
	
	Block a user