Vou tentar dar um exemplo de como você pode projetar seu arsenal e seu arsenal.
Nosso objetivo é desacoplar entidades, portanto, a arma deve ser uma interface.
interface Weapon {
public int getDamage();
}
Suponhamos que cada jogador possua apenas uma arma; podemos usá-la Strategy pattern
para trocar de arma facilmente.
class Knife implements Weapon {
private int damage = 10;
@Override
public int getDamage() {
return this.damage;
}
}
class Sword implements Weapon {
private int damage = 40;
@Override
public int getDamage() {
return this.damage;
}
}
Outro padrão útil seria o Padrão de Objeto Nulo, caso o jogador esteja desarmado.
class Weaponless implements Weapon {
private int damage = 0;
@Override
public int getDamage() {
return this.damage;
}
}
Quanto ao arsenal, podemos usar vários equipamentos de defesa.
// Defence classes,interfaces
interface Armor {
public int defend();
}
class Defenseless implements Armor {
@Override
public int defend() {
return 0;
}
}
abstract class Armory implements Armor {
private Armor armory;
protected int defence;
public Armory() {
this(new Defenseless());
}
public Armory(Armor force) {
this.armory = force;
}
@Override
public int defend() {
return this.armory.defend() + this.defence;
}
}
// Defence implementations
class Helmet extends Armory {
{
this.defence = 30;
}
}
class Gloves extends Armory {
{
this.defence = 10;
}
}
class Boots extends Armory {
{
this.defence = 10;
}
}
Para dissociar, criei uma interface para o defensor.
interface Defender {
int getDefended();
}
E a Player
turma.
class Player implements Defender {
private String title;
private int health = 100;
private Weapon weapon = new Weaponless();
private List<Armor> armory = new ArrayList<Armor>(){{ new Defenseless(); }};
public Player(String name) {
this.title = name;
}
public Player() {
this("John Doe");
}
public String getName() {
return this.title;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Player enemy) {
System.out.println(this.getName() + " attacked " + enemy.getName());
int attack = enemy.getDefended() + enemy.getHealth()- this.weapon.getDamage();
int health = Math.min(enemy.getHealth(),attack);
System.out.println("After attack " + enemy.getName() + " health is " + health);
enemy.setHealth(health);
}
public int getHealth() {
return health;
}
private void setHealth(int health) {
/* Check for die */
this.health = health;
}
public void addArmory(Armor armor) {
this.armory.add(armor);
}
@Override
public int getDefended() {
int defence = this.armory.stream().mapToInt(armor -> armor.defend()).sum();
System.out.println(this.getName() + " defended , armory points are " + defence);
return defence;
}
}
Vamos adicionar um pouco de jogabilidade.
public class Game {
public static void main(String[] args) {
Player yannis = new Player("yannis");
Player sven = new Player("sven");
yannis.setWeapon(new Knife());
sven.setWeapon(new Sword());
sven.addArmory(new Helmet());
sven.addArmory(new Boots());
yannis.attack(sven);
sven.attack(yannis);
}
}
Voila!