Ι akan mencoba memberi Anda contoh tentang bagaimana Anda dapat merancang gudang senjata dan gudang senjata Anda.
Tujuan kami adalah untuk memisahkan entitas, oleh karena itu senjata harus menjadi antarmuka.
interface Weapon {
public int getDamage();
}
Menganggap bahwa setiap pemain hanya dapat memiliki satu senjata, kita dapat menggunakan Strategy pattern
untuk mengubah senjata dengan mudah.
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;
}
}
Pola lain yang bermanfaat adalah Pola Objek Null jika pemain tidak bersenjata.
class Weaponless implements Weapon {
private int damage = 0;
@Override
public int getDamage() {
return this.damage;
}
}
Sedangkan untuk gudang senjata, kita bisa memakai beberapa peralatan pertahanan.
// 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;
}
}
Untuk decoupling, saya membuat antarmuka untuk bek.
interface Defender {
int getDefended();
}
Dan Player
kelasnya.
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;
}
}
Mari kita tambahkan beberapa gameplay.
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!