RPG多重状态效果

时间:2015-02-12 14:57:32

标签: java enums

我尝试实现的类将处理角色的不同状态效果。想想神奇宝贝,神奇宝贝都会瘫痪和中毒。

为此我首先考虑存储一个存储了所有不同状态效果的文件,所有这些都是public final static List<String> effects。然后我想尝试以bitfield代替它。在我尝试这是最好的方法之后不确定,但这是CodeReview而不是StackOverflow的问题。现在我希望这段代码能够工作。

public class CharacterStatus {

    public static final int EFFECTS_MAX = 7;

    public enum StatusFlags {
        DEAD        (1<<0),
        PARALYZED   (2<<0),
        SLEEPING    (3<<0),
        CONFUSED    (4<<0),
        POISONED    (5<<0),
        BURNING     (6<<0),
        FROZEN      (7<<0);

        private final long status;

        StatusFlags (long status) {
            this.status = status;
        }

        public long getStatus () {
            return status;
        }
    }

    public CharacterStatus () {
        // declare a Status Flag with all bits set to 0
    }

    public setBit (String effect) {
        // It should set the correct bit Based on DEAD, PARALYZED etc
    }

    public clearBit (String effect) {
        // It should clear the correct bit Based on DEAD, PARALYZED etc
    }

    public setBit (int b) {
        // It should set the correct bit Based on DEAD, PARALYZED etc
    }

    public clearBit (int b) {
        // It should clear the correct bit Based on DEAD, PARALYZED etc
    }

    public long getValue () {
        return statusFlags.getStatus();
    }

    public String getValueAsString (String d) {
        String s = "";
        d = "|";

        int currentEffect = EFFECTS_MAX;

        for (int i = EFFECTS_MAX; statusFlags.getStatus() > 0; i--) {
            if (statusFlags.getStatus() > Math.pow(2, i)) {
                // load effect #i based on the StatusFlags
                s+= EFFECT#i + d;
            }
        }

        s.substring(0, s.length()-d.length());

        return s;
    }
}

很多代码都是虚拟代码,例如s+= EFFECT#i + d;应该根据i的数字得到该标志的名称。

Character.java的迷你版

public abstract class Character
{
    // alive and well
    private final CharacterStatus characterStatus = new CharacterStatus ();

    /**
     * @return  the status of this character
     */
    public CharacterStatus getCharacterStatus () {
        return characterStatus;
    }
}

澄清:我想要工作的是为StatusFlags存储Character并能够操纵和打印不同的标志。例如,在战斗A中对B施放毒药攻击,这将导致B.getCharacterStatus().setBit("POISONED");

1 个答案:

答案 0 :(得分:2)

这是一个使用EnumSet而不是位标志的修改。修改为自包含(我在ideone中写道)。

import java.util.*;
import java.lang.*;
import java.io.*;

class CharacterStatus
{

    public  enum StatusFlags {
        DEAD,
        PARALYZED,
        SLEEPING,
        CONFUSED,
        POISONED,
        BURNING,
        FROZEN;
    }

    private EnumSet<StatusFlags> status; 

    public EnumSet<StatusFlags> getStatus () {
        return this.status;
        // you could return another format if you like, e.g., string array
    }

    public String toString() {
        String s = "";
        for (StatusFlags stat  : this.status){
            s+= stat.toString()+"\n";
        }
        return s;   
    }


    public CharacterStatus () {
        this.status = EnumSet.noneOf(StatusFlags.class);
        // declare a Status Flag with all bits set to 0
    }

    public void setBit(StatusFlags effect) {
        this.status.add(effect);
        // It should set the correct bit Based on DEAD, PARALYZED etc
    }

    public void clearBit (String effect) {
        this.status.remove(effect);
        // It should clear the correct bit Based on DEAD, PARALYZED etc
    }


    public static void main (String[] args) throws java.lang.Exception
    {
        CharacterStatus status = new CharacterStatus ();
        status.setBit (StatusFlags.CONFUSED);
        System.out.println(status.toString());
    }
}