开发者

How do I make commands in a java game?

Basically I want to make it so when the user types into the textfield /attack animal it does the attack method and takes animal as a parameter. So if player typed /attack foo it do player.attack(foo). I already have methods that perform chat so I know which player is doing the chat. I just need to know how to reconize what comes after the /attack and take it as a parameter for player.attack() which takes a Player object as an argument. These are my methods that receive input:

public void actionPerformed(ActionEvent textBox) {
        String text = textField.getText();
        player.chat(text);
}

Which is in my Gui class, and:

public void chat(String chat){
    playerGui.printText(this.getName() + ": " + chat);
    playerGui.tex开发者_运维知识库tField.selectAll();
}

Which is in the player class. A gui instance is passed to Player() which creates the variable playerGui.


You'll have to tweak this to fit the syntax you want the user to use, but you would probably want to change your actionPerformed method to check for the different actions the user can perform, rather than just chatting.

public void actionPerformed(ActionEvent textBox) { 
    String text = textField.getText();
    String command = text.substring(0, text.indexOf(" "));
    String args = text.substring(text.indexOf(" ") + 1);
    switch (command) {
        case "chat":
            // Pass everything but the command
            player.chat(args);
            break;
        case "/attack":
            player.attack(args);
            break;
        default:
            // Handle bad user input
    }
}

You will still have to verify that you are getting the right kind of arguments for each command, and I didn't put in anything safeguard against null pointers or out-of-bounds with the string functions, but hopefully that will get you started.


"/attack arg1 arg2 arg3".split("\\s+") will give you an array equivalent to new String[]{"/attack", "arg1", "arg2", "arg3"} which you can then use for whatever you want.

For invoking it you can reflect into the object (see Class.getMethod for more details on that) if you really need to, but you're probably better off avoiding that if you can.


Every instance of your Player object should have its own name, a String that uniquely identifies it. Store each enemy Player in a Map<String, Player>, then use this to look up the intended target after parsing its name from the input (which @mange explained how to do).

The Player class:

public class Player {

   public final String name;

   public Player(String uniqueName) {
      name = uniqueName;
   }

   //additional code
}

In a class that makes sense, declare and initialize a Map to look up Players from:

private static Map<String, Player> players = new HashMap<String, Player>();

Throughout the game, when a Player is introduced, add it to the Map:

Player newPlayer = new Player(someUniqueName);
players.put(someUniqueName, newPlayer);

When an attack command is given, parse the names and use them to look up each attacked Player:

public void attackNamedPlayers(String[] names) {
   for (String name : names) {
      Player attackedPlayer = players.get(name); //look up enemy Player by name
      myPlayer.attack(attackedPlayer);           //attack enemy Player
   }
}

Don't forget to remove Players from the Map when they die/leave:

players.remove(deadPlayer.name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜