Skip to content

Commit

Permalink
Merge branch 'main' into sistemaUsuario
Browse files Browse the repository at this point in the history
  • Loading branch information
delfi-fenoy authored Nov 15, 2024
2 parents 45753c2 + bfe1702 commit 8157ea3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 6 deletions.
Binary file added res/option_background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/gameState/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public enum GameState {

PLAYING, MENU, OPTIONS, RANKING, QUIT, REGISTER, LOGIN;

public static GameState state = LOGIN; // Estado por default.
public static GameState state = MENU; // Estado por default.

}
171 changes: 171 additions & 0 deletions src/gameState/Option.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package gameState;

import exceptions.InvalidUsernameOrPasswordException;
import exceptions.NonexistentUserException;
import main.Game;
import users.User;
import utilz.LoadSave;

import javax.swing.*;
import java.awt.*;

import static utilz.LoadSave.OPTION_BACKGROUD;

public class Option extends UserAccount {
// ====================> ATRIBUTOS <====================
// Botones
JButton confirmUsernameButton;
JButton confirmPasswordButton;

// Contenedores
JTextField userIDField;
JPasswordField userPasswordField;

// Texto
JLabel userIDLabel;
JLabel userPasswordLabel;

// ====================> CONSTRUCTOR <====================
public Option(Game game) {
super(game);
initUI();
addEventListeners();
}

// ====================> SET/GET <====================
public void setShowMessage(int showMessage) {
this.showMessage = showMessage;
}

// ====================> METODOS <====================
@Override
public void initUI(){
super.initUI();
// Instanciar
confirmUsernameButton = new JButton("Confirm");
confirmPasswordButton = new JButton("Confirm");
userIDField = new JTextField();
userPasswordField = new JPasswordField();
userIDLabel = new JLabel("");
userPasswordLabel = new JLabel("");

backButton.setText("To Menu");

// Botones
confirmUsernameButton.setBackground(new Color(82, 110, 72));
confirmUsernameButton.setForeground(new Color(158, 223, 156));
confirmUsernameButton.setBounds(193, 330, 100, 25);

confirmPasswordButton.setBackground(new Color(82, 110, 72));
confirmPasswordButton.setForeground(new Color(158, 223, 156));
confirmPasswordButton.setBounds(193, 450 , 100, 25);

//Labels
userIDLabel.setBounds(140, 270, 75, 25);
userPasswordLabel.setBounds(140, 370, 75, 25);

//Fields
userIDField.setBackground(Color.LIGHT_GRAY);
userIDField.setForeground(Color.DARK_GRAY);
userPasswordField.setBackground(Color.LIGHT_GRAY);
userPasswordField.setForeground(Color.DARK_GRAY);
userIDField.setBounds(140, 300, 200, 25);
userPasswordField.setBounds(140, 420, 200, 25);
}

@Override
public void addComponents(){
super.addComponents();
panel.setLayout(null);
panel.add(confirmUsernameButton);
panel.add(confirmPasswordButton);
panel.add(userIDField);
panel.add(userPasswordField);
panel.add(userIDLabel);
panel.add(userPasswordLabel);
}

@Override
public void addEventListeners(){
confirmUsernameButton.addActionListener(e -> login());
confirmPasswordButton.addActionListener(e -> login());

backButton.addActionListener(e ->{ // Register button
game.getGamePanel().removeAll();
flagAddComponents = false;
clearFields();
GameState.state = GameState.MENU;
});
}

public void login(){
String name = userIDField.getText();
String password = new String(userPasswordField.getPassword());

try {
if(name.isBlank() || password.isBlank() || name.length() >20 || password.length() >20) { // Si esta vacio o es >20
throw new InvalidUsernameOrPasswordException();
} else if(!game.getJsonUserManager().verifyUserInfo(new User(name, password))){
throw new NonexistentUserException();
}

game.getGamePanel().removeAll();
flagAddComponents = false;
game.setUserInGame(new User(name, password));
GameState.state = GameState.MENU;

} catch (InvalidUsernameOrPasswordException e){ // Excepcion si name o password estan vacios o >20
e.getMessage();
e.printStackTrace();
showMessage = 1;

} catch (NonexistentUserException e){ // Excepcion si el nombre de usuario y/o contraseña son incorrectos
e.getMessage();
e.printStackTrace();
showMessage = 2;

} finally {
clearFields();
}
}

@Override
public void clearFields(){
userIDField.setText("");
userPasswordField.setText("");
}

// ====================> METODOS INTERFACE <====================
@Override
public void update() {
super.update();

if(showMessage != 0){
messageCounter(this);
}
}

@Override
public void draw(Graphics g) {
// Fondo y Titulo
LoadSave.drawTitleBackgroud(g,OPTION_BACKGROUD);

if(showMessage != 0){
g.setColor(Color.DARK_GRAY);
g.fillRect(40, 550, 410, 25); // Rectangulo Negro
g.setFont(new Font("Console", Font.BOLD, 12));
g.setColor(Color.RED);

switch (showMessage) {
case 1 -> g.drawString("Nombre de usuario y/o contraseña inválidos.", 120, 567);
case 2 -> g.drawString("Nombre de usuario y/o contraseña incorrectos.", 120, 567);
}
}

// Textos
g.setColor(Color.WHITE);
g.setFont(new Font("Console", Font.BOLD, 25));
g.drawString("New Username", 150, 290);
g.drawString("New Password", 150, 410);
}
}
17 changes: 12 additions & 5 deletions src/main/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Game implements Runnable, IRenderable {
private Menu menu;
private Register register;
private Login login;
private Option option;
protected User userInGame;

// JSON
Expand Down Expand Up @@ -90,6 +91,7 @@ private void initClasses(){
playing = new Playing(this);
register = new Register(this);
login = new Login(this);
option = new Option(this);
userInGame = new User();

jsonUserManager = new JSONUserManager();
Expand All @@ -106,18 +108,20 @@ private void startGameLoop(){
public void update(){
switch (GameState.state){ // Llamar method update() según el gameState
case REGISTER:
register.update();
register.update();
break;
case LOGIN:
login.update();
login.update();
break;
case MENU:
menu.update();
break;
case PLAYING:
playing.update();
break;
case OPTIONS: //option.update() [no existe aun] break;
case OPTIONS:
option.update();
break;
case RANKING: //ranking.update() [no existe aun] break;
case QUIT:
default:
Expand All @@ -131,17 +135,20 @@ public void update(){
public void draw(Graphics g){
switch (GameState.state){
case REGISTER:
register.draw(g);
register.draw(g);
break;
case LOGIN:
login.draw(g);
login.draw(g);
break;
case MENU:
menu.draw(g);
break;
case PLAYING:
playing.draw(g);
break;
case OPTIONS:
option.draw(g);
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/utilz/LoadSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class LoadSave {
public static final String LOGIN_BACKGROUD = "login_background.jpg";
public static final String REGISTER_BACKGROUD = "register_background.jpg";
public static final String MENU_BACKGROUD = "menu_background.jpg";
public static final String OPTION_BACKGROUD = "option_background.jpg";
public static final String TITLE_GAME = "501_title.png";
public static final String PAUSE_MENU = "pause_menu.png";

Expand Down

0 comments on commit 8157ea3

Please sign in to comment.