Trataré de darle un ejemplo de cómo puede diseñar su arsenal y su arsenal.
Nuestro objetivo es desacoplar entidades, por lo tanto, el arma debe ser una interfaz.
interface Weapon {
public int getDamage();
}
Supongamos que cada jugador solo puede poseer un arma, podemos usarla Strategy pattern
para cambiar las armas fácilmente.
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;
}
}
Otro patrón útil sería el Patrón de objeto nulo en caso de que el jugador esté desarmado.
class Weaponless implements Weapon {
private int damage = 0;
@Override
public int getDamage() {
return this.damage;
}
}
En cuanto a la armería, podemos usar múltiples equipos de defensa.
// 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 desacoplar, creé una interfaz para el defensor.
interface Defender {
int getDefended();
}
Y la Player
clase
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;
}
}
Agreguemos algo de juego.
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!