简体   繁体   English

如何在Java游戏中进行命令?

[英]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. 基本上,我想这样做,以便当用户在textfield / attack动物中键入内容时,它会执行Attack方法并以animal为参数。 So if player typed /attack foo it do player.attack(foo). 因此,如果玩家键入/ attack foo,则执行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. 我只需要知道如何协调/ attack之后的内容,并将其作为player.attack()的参数,该参数将Player对象作为参数。 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: 在我的Gui班上,并且:

public void chat(String chat){
    playerGui.printText(this.getName() + ": " + chat);
    playerGui.textField.selectAll();
}

Which is in the player class. 在播放器类中。 A gui instance is passed to Player() which creates the variable playerGui. gui实例将传递到Player(),后者创建变量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. 您必须对其进行调整以适合您希望用户使用的语法,但是您可能需要更改actionPerformed方法以检查用户可以执行的不同操作,而不仅仅是聊天。

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. "/attack arg1 arg2 arg3".split("\\\\s+")将为您提供一个等效于new String[]{"/attack", "arg1", "arg2", "arg3"} ,您可以使用无论您想要什么。

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. 对于调用它,如果确实需要,您可以将其反映到对象中(有关更多详细信息,请参见Class.getMethod ),但是如果可以的话,最好避免这样做。

Every instance of your Player object should have its own name, a String that uniquely identifies it. Player对象的每个实例都应该有自己的名称,即唯一标识它的String 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). 将每个敌方Player存储在Map<String, Player> ,然后在从输入中解析其目标名称后使用它来查找目标目标( @mange解释了如何做)。

The Player class: Player类:

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 Player s from: 在一个有意义的类中,声明并初始化Map以从以下位置查找Player

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

Throughout the game, when a Player is introduced, add it to the Map : 在整个游戏中,引入Player ,将其添加到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 : 发出攻击命令后,解析名称并使用它们查找每个受攻击的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 Player s from the Map when they die/leave: 不要忘记在Player死亡/离开时将其从Map上移除:

players.remove(deadPlayer.name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM