0
\$\begingroup\$

In the below code is the way I create a character in Entity System. But I think this is the decouple, I'm having hard time decoupling this class.

1st approach

public enum CharacterType {

     PLAYER_CHARACTER,      // PC or a character controlled by a player
     NON_PLAYER_CHARACTER   // NPC or a character controlled by a computer it self (artificial intelligence), but notice that it is a friendly character


}

public class Character {
     // Player-Character can be controlled by the player it self
     // None-Player-Character is a autonomous and cannot be controlled
     // Player-Enemy target a player-character
     // Player-Allied target a player-enemy and friendly to a player-character
     // All character is moving
     CharacterType type
     CharacterMovement movement
}

// or 

public class PlayerCharacter extends Character {

}


public class NonPlayerCharacter extends Character {

}

public class Merchant extends NonPlayerCharacter {

}

public class Enemy extends Character {

}

public class CharacterComponent implements Component {
     public Character character;
}

2nd approach

public class PlayerComponent implements Component {
     public static final int IDLE = 0;
     public static final int MOVE_UP = 1;
     ...
}

public class NonePlayerComponent implements Component {
     public static final int IDLE = 0;
     public static final int MOVE_UP = 1;
     ...
}

public class EnemyPlayerComponent implements Component {
     public static final int IDLE = 0;
     public static final int MOVE_UP = 1;
     ...
}

public class AlliedPlayerComponent implements Component {
     public static final int IDLE = 0;
     public static final int MOVE_UP = 1;
     ...
}

Noticed in the 2nd approach that it has a redundant data.

\$\endgroup\$
2
  • \$\begingroup\$ What do "IDLE" and "MOVE_UP" actually mean in context? Maybe it's something you should move to a separate component. Maybe not. Really depends on what that constant does. \$\endgroup\$
    – Philipp
    Sep 5 '16 at 12:12
  • \$\begingroup\$ @Philipp it is an state, yes your right maybe I should move that to a different component \$\endgroup\$
    – ronscript
    Sep 5 '16 at 13:42
1
\$\begingroup\$

Make the constants protected field variables under a new class: CharacterComponent, have the other components extend CharacterComponent (inheritance is not the devil)

\$\endgroup\$
1
  • \$\begingroup\$ You've got a points, but it will be better if you have a an explanation. Thanks \$\endgroup\$
    – ronscript
    Sep 5 '16 at 13:15
1
\$\begingroup\$

Instead of having:

public class PlayerComponent implements Component {
 public static final int IDLE = 0;
 public static final int MOVE_UP = 1;
 ...
}

public class NonePlayerComponent implements Component {
 public static final int IDLE = 0;
 public static final int MOVE_UP = 1;
 ...
}

public class EnemyPlayerComponent implements Component {
 public static final int IDLE = 0;
 public static final int MOVE_UP = 1;
 ...
}

public class AlliedPlayerComponent implements Component {
 public static final int IDLE = 0;
 public static final int MOVE_UP = 1;
 ...
}

Why not use something like:

public class Actor {
 public static final int IDLE = 0;
 public static final int MOVE_UP = 1;
 ...
 public InputComponent;
 public PhysicsComponent;
 ...
}

And then have your input components control the Actors regardless of what they are? For your player your input would be controlled by keyboard and your NPCs would be controlled by an AI.

\$\endgroup\$
1
  • \$\begingroup\$ I am using Ashley ECS, which it already has a Entity. Entity is a collection of components. \$\endgroup\$
    – ronscript
    Sep 5 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.