jeudi 10 décembre 2020

True State Pattern with Singleton Player class

I have an application that I have been working on for a little while, I understand a little of Java.

The scope of the application is combine multiple design patterns in a way that allows reusability, which code can be edited without having to scroll through hundreds of lines of code.

I have implemented a true Singleton Player class. I have also implemented a decorator weapon class.

I am not looking to add a state pattern for the player class, an example of this would be AliveState and DeadState. Something simple so I understand the workings of it all.

For the sake of this I will post the full PlayerSingleton class:

public class PlayerSingleton{

    private static PlayerSingleton player;
    Scanner scanner = new Scanner(System.in);

    private String playerName;
    private Integer playerHealth;

    private Weapon weapon;
    
    private PlayerSingleton(Weapon weapon, String pName, int pHealth) {
        this.weapon = weapon;
        playerName = pName;
        playerHealth = pHealth;
    }

    public static Weapon chooseWeapon(String choice) {
        switch (choice) {
            case "MP5":
                System.out.println("You have chosen MP5!");
                return new MP5Weapon();
            
            case "SNIPER":
                System.out.println("You have chosen Sniper!");
                return new SniperRifleWeapon();
            case "SHOTGUN":
                System.out.println("You have chosen Shotgun!");
                return new ShotgunWeapon();
            default:
                System.out.println("No gun by that name found!");
                return null;
        }
    }

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

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

    public void getWeaponDamage(Weapon damage) { 
        System.out.println("Damage of weapon: " + weapon.damage());       
    }

    public void attackState(double damage) {
        damage = player.weapon.damage();
        
    }  
//    @Override
//    public void aliveState() {
//        if(playerHealth >= 1){
//            
//        }
//    }
//    @Override
//    public void deadState() {
//        if(playerHealth ==0){
//          System.out.println("You are dead");
//            System.out.println("GameOver");
//        }      
//    }

    public void chosenWeapon() {
        System.out.println("Player Info: " + playerName + " " + "Has: " + playerHealth + " health");
        System.out.println(weapon.getDescription() + ":" + " 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!");
        }
    }

I've tried to implement this with help of Head first design patterns (State) but using the Singleton Pattern on the player class means that I cannot call the object from another class.

public class DeadState implements PlayerState{
PlayerSingleton player;

    public DeadState(PlayerSingleton player){
        this.player = player;
    }
    @Override
    public void deadState() {
        System.out.println("You are Dead!");
    }
    @Override
    public void aliveState() {
        System.out.println("You are Dead!");  
    }
    
}

Above is a test on making a DeadState implementing from a PlayerState interface.

Is there any way to do this with separate classes using the state pattern with PlayerSingleton?

Seriously any help would be amazing!

Also if you could explain the answer so I understand better.

Aucun commentaire:

Enregistrer un commentaire