Переглянути джерело

By gameoverscreen able to typ a name

Remco 10 роки тому
батько
коміт
336a3051ff

+ 1 - 1
data/io/SQLConnector.java

@@ -8,7 +8,7 @@ import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.List;
 
-import model.objects.Highscore;
+import model.objects.highscore.Highscore;
 import audio.Song;
 import audio.SongInstance;
 

+ 48 - 32
model/gameState/GameOverState.java

@@ -13,6 +13,7 @@ import java.awt.image.VolatileImage;
 
 import model.SongHandler;
 import model.objects.InfoPanel;
+import model.objects.highscore.HighscoreName;
 import control.GameStateManager;
 import control.GameStateManager.State;
 import control.button.ButtonEvent;
@@ -24,15 +25,15 @@ public class GameOverState extends GameState {
     BufferedImage gameOver = Images.getImage(ImageType.gameover);
     VolatileImage background;
 	Font textFont = new Font("OCR A Extended", Font.BOLD, 70);
-	
-	
-	private String endScore = "";
+	HighscoreName hsn;
+	int image_x = 0;		
 	
     int frame;
 
 	public GameOverState(GameStateManager gsm, SongHandler sh, SQLConnector sql) {
 		super(gsm, sh, sql);
 		createBackground();
+		hsn = new HighscoreName(640,717,5,textFont);
 	}
 
 	@Override
@@ -41,44 +42,20 @@ public class GameOverState extends GameState {
 
 	@Override
 	public void update(float factor) {
-
+		image_x = ((frame / 5) % 5) * 40;
         frame++;
 	}
 
 	@Override
 	public void draw(Graphics2D g2) {
-		g2.drawImage(background, 0, 0, 1280, 1024, null);
-		int image_x = ((frame / 5) % 5) * 40;
-		g2.drawImage(gameOver.getSubimage(image_x, 0, 40, 26),  640-122, 512, 245, 130, null);
-		
-		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-	    g2.setRenderingHints(rh);
-		g2.setColor(new Color(1,1,1, 0.3f));
-		g2.fillRect(0,0,1280,1024);
-		
-		g2.setColor(new Color(0,1,0, 0.7f));
-		g2.fillRect(0,0,100,1024);
-		g2.fillRect(1180,0,100,1024);
-		
-		g2.setColor(new Color(1,1,0, 0.7f));
-		g2.fillRect(100,0,100,1024);
-		g2.fillRect(1080,0,100,1024);
-		
-		g2.setColor(new Color(1,0,0, 0.7f));
-		g2.fillRect(200,0,100,1024);
-		g2.fillRect(980,0,100,1024);
-		
-		g2.setColor(Color.BLACK);
-		g2.setFont(textFont);
-		g2.drawString("High Score", 385, 212);
-		g2.drawString(InfoPanel.getTotalHighscore(), 390, 342);
-		g2.dispose();
-		background.createGraphics();
+		g2.drawImage(background, 0, 0, 1280, 1024, null);		
+		g2.drawImage(gameOver.getSubimage(image_x, 0, 40, 26),  640-122, 400, 245, 130, null);
+		hsn.drawName(g2);
 	}
 	
 	@Override
 	public void buttonPressed(ButtonEvent e) {
-		
+		System.out.println("Name: "+hsn.getName());
 		switch(e.getButton().getButtonID()){
 		case 0:
 			gsm.setState(State.MENU_STATE);
@@ -93,10 +70,49 @@ public class GameOverState extends GameState {
 	}
 	@Override
 	public void onJoystickMoved(JoystickEvent e) {
+		switch(e.getJoystick().getPos()){		
+		case DOWN:
+			hsn.down();
+			break;			
+		case LEFT:
+			hsn.left();
+			break;
+		case RIGHT:
+			hsn.right();
+			break;
+		case UP:
+			hsn.up();
+			break;		
+		default:
+			break;	
+		}
 	}
 	
 	public void createBackground(){
 		background = Images.initVolatileImage(1280, 1024, Transparency.OPAQUE);
 		Graphics2D g2 = background.createGraphics();
+
+		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+	    g2.setRenderingHints(rh);
+		g2.setColor(new Color(1,1,1, 0.3f));
+		g2.fillRect(0,0,1280,1024);
+		
+		g2.setColor(new Color(0,1,0, 0.7f));
+		g2.fillRect(0,0,100,1024);
+		g2.fillRect(1180,0,100,1024);
+		
+		g2.setColor(new Color(1,1,0, 0.7f));
+		g2.fillRect(100,0,100,1024);
+		g2.fillRect(1080,0,100,1024);
+		
+		g2.setColor(new Color(1,0,0, 0.7f));
+		g2.fillRect(200,0,100,1024);
+		g2.fillRect(980,0,100,1024);
+		
+		g2.setColor(Color.BLACK);
+		g2.setFont(textFont);
+		g2.drawString("High Score", 385, 212);
+		g2.drawString(InfoPanel.getTotalHighscore(), 390, 342);
+		g2.dispose();
 	}
 }

+ 3 - 0
model/gameState/PlayState.java

@@ -169,6 +169,9 @@ public class PlayState extends GameState {
 
 	@Override
 	public void buttonPressed(ButtonEvent e) {
+		if(e.getButton().getButtonID() == 0){
+			lifePoints = 0;
+		}
 		boolean notHit = true;
 		Iterator<Enemy> enemysInPath = area.paths.get(player.getIndex()).getEnemysInPath().iterator();
 		while (enemysInPath.hasNext()) {

+ 1 - 1
model/objects/Highscore.java → model/objects/highscore/Highscore.java

@@ -1,4 +1,4 @@
-package model.objects;
+package model.objects.highscore;
 
 import java.util.Date;
 

+ 39 - 0
model/objects/highscore/HighscoreLetter.java

@@ -0,0 +1,39 @@
+package model.objects.highscore;
+
+import java.awt.Graphics2D;
+
+public class HighscoreLetter {
+	
+	public static int charLength = 46;
+	private char[] letters;
+	private int x,y,index = 0;
+		
+	public HighscoreLetter(char[] letters, int x, int y) {
+		super();
+		this.letters = letters;
+		this.x = x;
+		this.y = y;
+	}
+	
+	public void draw(Graphics2D g2){
+		charLength = g2.getFontMetrics().stringWidth(letters[index]+"");
+		g2.drawString(letters[index]+"", x, y);
+		g2.drawLine(x, y, x+charLength, y);
+	}
+	
+	public void up(){
+		index++;
+		index %= letters.length;
+	}
+	
+	public void down(){
+		index--;
+		if(index < 0){
+			index = letters.length-1;
+		}
+	}
+	
+	public String getCurrentString(){
+		return letters[index]+"";
+	}
+}

+ 66 - 0
model/objects/highscore/HighscoreName.java

@@ -0,0 +1,66 @@
+package model.objects.highscore;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Graphics2D;
+
+public class HighscoreName {
+
+	private final char[] characters = " abcdefghijklmnopqrstuvwxyz0123456789†".toUpperCase().toCharArray();
+	private int spacing = 10;
+	private HighscoreLetter[] letters;
+	private Font font;
+	private int index = 0;
+	
+	public HighscoreName(int x, int y,int nameLength,Font font) {
+		this.font = font;
+		letters = new HighscoreLetter[nameLength];
+		int marge = (int) (Math.floor(nameLength/2)-1)*spacing;
+		x -= (double)HighscoreLetter.charLength*(nameLength/2.0) + marge;
+		
+		System.out.println(x);
+		for(int i = 0; i < letters.length; i++){			
+				letters[i] = new HighscoreLetter(characters, x+(HighscoreLetter.charLength*i)+(spacing*i), y);		
+		}
+	}
+	
+	public void drawName(Graphics2D g2){
+		g2.setFont(font);
+		for(int i = 0; i < letters.length; i++){
+			if(i == index){
+				g2.setColor(Color.RED);
+			}else{
+				g2.setColor(Color.BLACK);
+			}
+			letters[i].draw(g2);			
+		}
+	}	
+	
+	public void left(){
+		index--;
+		if(index < 0){
+			index = letters.length-1;
+		}
+	}
+	
+	public void right(){
+		index++;
+		index %= letters.length;
+	}
+	
+	public void up(){
+		letters[index].up();
+	}
+	
+	public void down(){
+		letters[index].down();
+	}
+	
+	public String getName(){
+		String name = "";
+		for(int i = 0; i < letters.length; i++){
+			name += letters[i].getCurrentString();
+		}
+		return name;
+	}
+}