package cn.bunny.pattern2.demo3;


import lombok.Setter;

public class GameCharacter {
    public static void main(String[] args) {
        Character soldier = new Soldier();
        soldier.fight();

        Character king = new King();
        king.setPrimaryWeapon(new AxeWeapon());
        king.fight();
    }

    // 武器接口
    interface Weapon {
        String function();
    }

    // 各种武器实现
    public static class SwordWeapon implements Weapon {
        @Override
        public String function() {
            return "⚔️ ⚔️ ⚔️";
        }
    }

    public static class AxeWeapon implements Weapon {
        @Override
        public String function() {
            return "🪓 🪓 🪓";
        }
    }

    public static class KnifeWeapon implements Weapon {
        @Override
        public String function() {
            return "🔪 🔪 🔪";
        }
    }

    // 抽象角色类
    public abstract static class Character {
        String name;
        @Setter
        private Weapon primaryWeapon;
        @Setter
        private Weapon subWeapon;

        public Character(String name) {
            this.name = name;
            // 默认武器配置
            this.primaryWeapon = new SwordWeapon();
            this.subWeapon = new KnifeWeapon();
        }

        void fight() {
            if (primaryWeapon != null && subWeapon != null) {
                String primary = primaryWeapon.function();
                String sub = subWeapon.function();
                System.out.println(name + "：" + primary + "和" + sub);
            } else {
                System.out.println(name + "没有武器！");
            }
        }
    }

    // 国王角色
    public static class King extends Character {
        public King() {
            super("King");
            setPrimaryWeapon(new SwordWeapon());
            setSubWeapon(new KnifeWeapon());
        }
    }

    // 士兵角色
    public static class Soldier extends Character {
        public Soldier() {
            super("Soldier");
            setPrimaryWeapon(new AxeWeapon());
            setSubWeapon(new SwordWeapon());
        }
    }
}

