Tuesday, December 27, 2011

I command you to move

I have had a go at creating a very simple command structure that will allow me to just create a command anywhere in the game with a "name" that is the class name that should be executed, a priority that is not used yet and a list of parameters of any type.

The constructor looks like this :

public Command(String name, int priority, Object... obj){
 this.commandName = name;
 this.commandPriority = priority;
 this.parameters.addAll(Arrays.asList(obj));
}

To create a command I just decide on a set of parameters that I would need to get it working and add it to the command list :

Command com = new Command("Move", 0, Globals.NORTH, player);
commands.addCommand(com);

That command moves the player to the north. The actual Move class then checks the map, the player and decides if a move to the north is legal :

public void execute(ArrayList<Object> parameters) {
 Map map = (Map)Globals.gameObjects.get("map");
 GameObject mover = (GameObject)parameters.get(1);
 String direction = (String)parameters.get(0);
 if (moveOutsideMap(mover, direction, map.getMaxMapX(), map.getMaxMapY())) return;
 if (direction.equals(Globals.NORTH)){
 mover.setMapPositionY(mover.getMapPositionY()-1);
and so on......
}

It needs a few checks more to be valid, like is the tile blocked or not. Is there an NPC there blocking the path. Those can be added at a later time.

Just for fun, I added a Teleport command that gets activated when the player presses "t" on the keyboard and sends the player to a random location on the map, it looks like this :

public void execute(ArrayList<Object> parameters) {
 Map map = (Map)Globals.gameObjects.get("map");
 GameObject mover = (GameObject)parameters.get(0);
 int x = (Integer)parameters.get(1);
 int y = (Integer)parameters.get(2);
 if (moveOutsideMap(mover, x, y, map.getMaxMapX(), map.getMaxMapY())) return;
 mover.setMapPositionX(x);
 mover.setMapPositionY(y);
}

As long as I always supply the parameters in the right order, there is no limit to what I can send to a command. All my commands implements an interface with an execute function in it to make sure they can all be executed by the command processor.

 The command processor that extracts commands from the list and executes the execute funtion on the specified command looks like this :

Command command = commands.getNextCommand();
String className = command.getCommandName();
CommandI comI;
try {
 comI = (CommandI) Class.forName("rpe.rpg.game.command.commands." + className).newInstance();
 comI.execute(command.getParameters());
 } catch (InstantiationException e) {
  e.printStackTrace();
 } catch (IllegalAccessException e) {
  e.printStackTrace();
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 }

CommandI is the interface that every command have to implement. All my commands are located in package rpe.rpg.game.command.commands. If that changes later, I will have to find a way of detecting where the specified class is actually located.

No comments:

Post a Comment