I'm working on an rpg game, and one of the big issues that I had (have), is having the players actions change around other objects.
For example, normally the "a" key would attack, but when next to a person, it would trigger a dialog sequence, and when next to a rock, allow you to push/pull it around.
At first I thought to do something like this:
if(nextToPerson()) aCommand = talk;
else if(nextToRock()) aCommand = pushPull;
else aCommand = attack;
I then realized that this would be very inefficient in the long run, and cause the player class to be much larger than need-be.
I then came up with the idea of the surrounding objects controlling the players available actions.
example:
public interface ACommand{
int aRange(); // how far does the action effect?
void callACommand(/*args*/);
}
//========================
public class Person implements ACommand{
// Player's a command changes within 1 meter of a person
int aRange(){ return 1; }
void callACommand(/*args*/){ Player.talk(this); }
}
//========================
public class Player implements ACommand{ // default command
int aRange(){ return 150; } // default value (may be something else)
void callACommand(/*args*/){ Player.attack(); }
void keyPressed(KeyEvent e){
if(e.getKeyChar()=='a'){
Object closestActionChange = null;
if(closestActionChange = findClosestAction()){
if(closestActionChange instanceof Player)
this.callACommand();
if(closestActionChange instanceof Person)
((Person)(closestActionChange)).callACommand();
}
}
}
}
This is the solution that I came up with. It allows the Player class to remain somewhat simpler, and allows objects to govern themselves. findClosestAction() talks to the game engine to see what is around that can influence the Player's available actions. This is just something that I came up with, trying to make a game work a bit smoother. Most times, the interface would be implemented by the abstract super class of the object, and be overridden as necessary.
If you have any comments/suggestions, please let me know. I'm hoping that this is a good way to do this, and if not, I would like to know why, and where it could be improved. (This isn't really a question, rather a discussion about pros and cons of this way of doing things.)
Also, this is just a rough outline. The interfaces would be more structured, and things would fit together better than in the example. The keyPressed method in the player class would be better structured as well.
Edit:
Now, I'm thinking that the keyPressed method should go in the GameEngine class, which would hold the window, rather than in the player class