lundi 26 août 2019

Design pattern to customize possible statuses of a Character in a game

I decided to an solid rpg-like game structure with Java to practice design patterns.

Basically there are different types of characters in my game, which are all considered to be "game objects", having some common features:

public abstract class Character extends GameObject {
  Status status;
  //fields, methods, etc.
}

public abstract class Monster extends Character{
  //fields, methods, etc
}

public class Hero extends Character {
  //fields, methods, etc
}

Status here is an enumeration:

public enum Status {
  NORMAL,
  BURNT,
  POISONED,
  HEALED,
  FROZEN
}

I would like to make my code flexible, easy-to-modify, and I would like to follow the SOLID principles using the necessary design patterns effectively.

Let's suppose I would like to customize my characters, allowing to create custom Character extensions allowing to have only certain status changes. For example I would create a Monster called

public class FireGolem extends Monster{...}

, which is unable to get damaged by heat (hence is unable to get burnt).

I have 2 ideas to do this:

1) create a Set for the class Character, in which I would specify what kind of status changes can a Character have

2) create different interfaces (Burnable, Freezable, ...) and implement them when necessary.

What do you think? Which is better and why? Is there any better and cleaner option at all?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire