mercredi 9 décembre 2020

How to change Singleton Pattern before initialisation

public class PlayerSingleton {

private static PlayerSingleton player;   
private String playerName;

private Weapon weapon;

Weapon stockMP5 = new MP5Weapon();        //Making a new weapon called stockMP5 from MP5weapon Class
Weapon sniper = new SniperRifleWeapon();  //Making a new weapon called sniper from SniperRifleWeapon Class
Weapon shotgun = new ShotgunWeapon();  //Making a new weapon called shotgun from Shotgun Class

private PlayerSingleton(Weapon weapon, String pN) {
    this.weapon = weapon;
    playerName = pN;
}

public void chooseWeapon(String choice) {
    switch (choice) {
        case "MP5":
            weapon = new MP5Weapon();
            break;
        case "Sniper":
            weapon = new SniperRifleWeapon();
            break;
        case "Shotgun":
            weapon = new ShotgunWeapon();
            break;
        default:
            System.out.println("No Attachment found!");
    }
}

public static PlayerSingleton getInstance(Weapon choice, String n) {
    System.out.println("Choose Weapon to play the with: ");

// HERE I am looking to allow the player to choose the weapon before the singleton class initiallises below. To let the player play with their gun and not change it throughout the game.

    if (player == null) {
        player = new PlayerSingleton(choice, n);
    }
    return player;
}

public void chosenWeapon() {
    System.out.println(weapon.getDescription()
            + " Weight of Player: " + playerName + " gun: " + weapon.weight() + " base damage: " + weapon.damage());
}

public void addBasicAttachment(String attachment) {
    switch (attachment) {
        case "sight":
            weapon = new BasicSight(weapon);
            break;
        case "silencer":
            weapon = new BasicSilencer(weapon);
            break;
        case "stock":
            weapon = new BasicStock(weapon);
            break;
        default:
            System.out.println("No Attachment found!");
    }
}

public void addGoodAttachment(String attachment) {
    switch (attachment) {
        case "sight":
            weapon = new GoodSight(weapon);
            break;
        case "silencer":
            weapon = new GoodSilencer(weapon);
            break;
        case "stock":
            weapon = new GoodStock(weapon);
            break;
        default:
            System.out.println("No Attachment found!");
    }
}

public void addGreatAttachment(String attachment) {
    switch (attachment) {
        case "sight":
            weapon = new GreatSight(weapon);
            break;
        case "silencer":
            weapon = new GreatSilencer(weapon);
            break;
        case "stock":
            weapon = new GreatStock(weapon);
            break;
        default:
            System.out.println("No Attachment found!");
    }
}

How do I go about allowing the player to choose the weapon before initialising the playerSingleton getInstance method?

Any help would be greatly appreciated.

Edited:

public static Weapon chooseWeapon(String choice) {
        switch (choice) {
            case "MP5":
                return new MP5Weapon();
            case "Sniper":
                return new SniperRifleWeapon();
            case "Shotgun":
                return new ShotgunWeapon();
            default:
                return null;
        }
    }
public static PlayerSingleton getInstance(String choice, String n) {
        System.out.println("Choose Weapon to play the with: ");

        Weapon weapon = PlayerSingleton.chooseWeapon(choice);
        if (player == null) {
            player = new PlayerSingleton(weapon, n);
        }
        return player;
    }

The main:

Scanner scanner = new Scanner(System.in);
        System.out.println(" ------------------------------------ ");
        System.out.println("       Text based Shooting Game       ");
        System.out.println(" ------------------------------------ ");
        System.out.println("Please Enter your name before Beginning*");
        String name = scanner.next();
        System.out.println("Thank you for joining this Adventure " +name);
   
        
        Weapon weapon = PlayerSingleton.chooseWeapon(name);   
        PlayerSingleton player = PlayerSingleton.getInstance("MP5",name);
        player.chosenWeapon();

Additional edit: The Weapon class

public abstract class Weapon {
    String description = "Unknown Weapon: ";
    
    
    public String getDescription(){
        return description;
    }
    //public abstract int bulletCount();
    public abstract double weight();
    public abstract double damage();
}

Aucun commentaire:

Enregistrer un commentaire