Currently I am making a small type game, which in my player class I have far too many imports and dependencies.
So I have decided to separate the switch statements in its own individual class to be called in a method.
For context the player code
import Accessories.BasicSight;
import Accessories.BasicSilencer;
import Accessories.BasicStock;
import Accessories.GoodSight;
import Accessories.GoodSilencer;
import Accessories.GoodStock;
import Accessories.GreatSight;
import Accessories.GreatSilencer;
import Accessories.GreatStock;
import PlayerState.AliveState;
import PlayerState.PlayerState;
import Guns.Weapon;
import Guns.*;
import java.util.Scanner;
public class PlayerSingleton {
private static PlayerSingleton player;
Scanner scanner = new Scanner(System.in);
private String playerName;
private int lives;
private Integer health;
public static Integer MAX_PLAYER_HEALTH = 500;
public static Integer DEFAULT_PLAYER_LIVES = 2;
private PlayerState playerState;
private Weapon weapon;
private PlayerSingleton(Weapon weapon, String pName) {
this.weapon = weapon;
this.playerName = pName;
this.lives = DEFAULT_PLAYER_LIVES;
setState(new AliveState(this));
}
public static PlayerSingleton getInstance(String choice, String name) {
System.out.println("Choose Weapon to play the with: ");
Weapon weapon = PlayerSingleton.chooseWeapon(choice);
weapon.getDescription();
if (player == null) {
player = new PlayerSingleton(weapon, name);
}
return player;
}
public void sufferDamage(int damage) {
playerState.takeDamage(damage);
}
public void respawn() {
playerState.respawn();
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = DEFAULT_PLAYER_LIVES;
}
public int getHealth() {
return health;
}
public void setHealth(Integer health) {
this.health = health;
}
public void setState(PlayerState playerState) {
this.playerState = playerState;
}
public void chosenWeapon() {
System.out.println("Player Info: " + playerName + " " + "Has: " + health + " health and " + lives + " lives");
System.out.println(weapon.getDescription() + ":" + " base damage: " + weapon.damage());
}
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 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 getBasicAttachment(String attachment) {
}
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!");
}
}
}
So the code above is my Player class, I would like to put the addBasic/Good/Great weapon attachments in another separate class file, to then allow better decoupled code. I am not sure how to do this with a switch statement.
Any ideas on how to approach this?
Edit:
The switch methods are being called form the main:
import Accessories.*;
import Player.PlayerSingleton;
import Guns.Weapon;
import Guns.MP5Weapon;
import java.util.Scanner;
public class FireingRange {
Weapon weapon;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println(name + " to begin the game, first type a weapon you would like to play with:");
System.out.println();
System.out.println("Choose Weapon to play the with: ");
System.out.println("'Shotgun' for Shotgun");
System.out.println("'Sniper' for Sniper");
System.out.println("'MP5' for MP5");
String chooseWeapon = scanner.next().toUpperCase();
PlayerSingleton player = PlayerSingleton.getInstance(chooseWeapon, name);
System.out.println(player.getHealth());
System.out.println(player.getLives());
System.out.println();
player.chosenWeapon();
System.out.println("Are you ready to play the game?");
}
The Abstract accessory class:
import Guns.Weapon;
public abstract class AccessoryDecorator extends Weapon{
public abstract String getDescription();
}
An accessory extending AccessoryDecorator:
public class BasicSight extends AccessoryDecorator {
Weapon weapon;
public BasicSight(Weapon weapon) {
this.weapon = weapon;
}
@Override
public String getDescription() {
return weapon.getDescription() + ", Basic Sight";
}
@Override
public double weight() {
return 7 + weapon.weight();
}
@Override
public double damage() {
return 7 + weapon.damage();
}
So the objective is to have:
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!");
}
}
In a class of its own, to then be able to call the switch with result, which will then add to the values of the gun selected.
I cant seem to think how to return the accessory depending on what accessory is created. Can anyone help me, I have spent a few days on this issue.
Aucun commentaire:
Enregistrer un commentaire