Let's say I'm designing an RPG type game, and this game has a turn-based combat system. There are some things that the player character/non-player characters can do inside and outside of combat. For example, while they are not in combat, they can move around the world, use items, talk to other npcs, and the player (not their character) can open certain game menus. While the characters are in combat, they can also use items, and use certain combat abilities, but cannot move around the game world or talk to other npcs.
This question is intended to be language-agnostic, but the examples code will be in Java.
A first implementation of this might be something like
public class Character {
public void use(Item item) { /* ... */ }
public void talkTo(Npc npc) {
if (inCombat) {
throw new IllegalStateException("Unable to talk to npcs while in combat");
}
/* ... */
}
public void useAbility(Ability ability) {
if (!inCombat) {
throw new IllegalStateException("Unable to use ability while not in combat");
}
/* ... */
}
}
My question is targeted to the useAbility
and talkTo
method. Should these methods be callable all of the time, or should I restrict this in some manner, perhaps something like OverworldContext
or CombatContext
or an adapter/proxy of some sort to separate things that can be done? Perhaps something like
public class OverworldCharacter extends Character {
private Character character;
public OverworldCharacter(Character character) {
this.character = character;
}
public void talkTo(Npc npc) {
// do some stuff
}
// proxy character methods to field
}
And
public class InCombatCharacter {
// Same as other class
public void useAbility(Ability ability) {
// do some more stuff
}
}
This game is intended to allow plugins/mods to be created by third parties, so that's another things to consider in this design.