Lately, as I was working on a project in group I had 2 principals problems :

1. When you work on the same class at the same times it can be a real waste of time to merge the change later. There is always merge conflict O_O even when you work on different things logic wise.

2. If you do all your logic the OOP way it seems to become nearly impossible to refactor/change your game logic later on. The classes become big with lot of functionalities and when you change something you need to make sure everything still work.

So, to fix those 2 problems, I made some very simple entity system. In fact it only have 2 classes. (Can't really be smaller than that). I would like to have your inputs on that idea. Do you think it could fix some of my problems or do you think it's a waste of time? Here is the code :

/** * * The world containing all the entities. * * The function of this class is simply to store all the entities and give the * programmer an easy access to every entities. * * @author Gudradain * */publicclassWorld {

/** * Get all the entity of this sort in this world. * * The entities that you will get will probably not be of the type entityID * because it will return all the entities having that sort of entities * within them. * * @param entityID * @return */publicArrayList<Entity> getEntityList(StringentityID){ArrayList<Entity> newEntityList = null;

/** * An entity exist in a world. * * An entity has a series of module that implement the different * functionalities this entity will have. A module is also an entity. * * An entity can only have 1 module of each sort. You add module to an entity * not to represent the data of this entity but to add functionality to this * entity. * * Example : You have a character in a RPG that has many items. Trying to * add each item to the character as an entity would be a bad idea. The good * way to do it would be to add an InventoryEntity to the character and then * that inventory could hold a reference to every item the player have. * * ---------------------------------------------------------------------------- * * Why should you use entity in your game? * * 1. It enables you to work in team! That is probably the biggest advantage. * Imagine you have the character with his inventory and that character can * also attack enemies. You can easily create a sort of inventory and the code * to attack the enemies inside the Character class. Now what if, you and your * friend want to work at the same time on the code for the inventory and on * the code for attacking enemies. If you put everything in the same class you * could get a lot of problem with merging both of your part later on. By * splitting the logic in module you can both work on the same entity at the * same time. * * 2. The code can be reuse more easily! Since you split everything in small * bit of logic, there is a lot more chance that you could reuse your code in * an other part of the project or in an other project. * * 3. Greatly reduce the size of the classes! Everything is split up in small * module so you don't have gigantic class with lot of functionalities. * * 4. Make refactoring a lot easier! Since you split up everything in small * chunk you can't produce code that are interrelated between itself and that * are practically impossible to refactor. I'm sure everyone has already done * it many times. You create a class with many functionalities and everything * in the class is interrelated. Then you want to change/remove/add a * functionalities but it is so hard to change that class because everything is * interrelated inside it and as soon as you make a small change the rest break * up. Well, that's something you want to avoid. Code that can't be change is * bad code. * * 5. Give easy access to everything in the world! Writing the code to store * and access thing in your game is probably the most boring one to do. Since * every entity has a connection to the world and the world has a connection to * every entity in it, it's always easy to get the information you want at any * time. * * @author Gudradain * */publicclassEntity {

privatefinalArrayList<Entity> entityList = newArrayList<Entity>();

privatefinalWorldworld;

publicEntity(Worldworld){this.world = world; }

/** * The world in which this entity exist * * @return */publicWorldgetWorld(){returnworld; }

/** * Add an entity to this one. * * An entity can only have 1 entity of each sort. If you try to add more * than one entity of the same sort it will throw an exception. * * @param entity * @throws Exception */publicvoidaddModule(Entityentity) throwsException{for(Entitye : entityList){if(e.getID().equals(entity.getID())){thrownewException("This entity already have an entity of this sort"); } }entityList.add(entity); }

Looks really good! There is one potential problem however. The "getID()" method in Entity returns the name of the class. Since you may have multiple entities from the same class and considering the fact that a key in HashMap can only map to 1 value, they will replace each other once they are added to the map.

Looks really good! There is one potential problem however. The "getID()" method in Entity returns the name of the class. Since you may have multiple entities from the same class and considering the fact that a key in HashMap can only map to 1 value, they will replace each other once they are added to the map.

The add method checks to see if there is an ArrayList for that set of ID's if there is it adds to it otherwise it creates a new one. I think...

I tried to do this for a 2D RPG Maker style game a very long time ago. I abandoned the game, partly because of how hard it was to get these entities working. I had a very similar system of GameObjects having modules, but it didn't make things any easier as all the modules needed access to almost all other modules... I wasn't a very good programmer at that time. xD

If you want a bunch of simple pieces then just make a component-based system. I personally like having everything in one place, it's often easier to figure out issues that way, Well, everything in moderation, I guess.

I tried to do this for a 2D RPG Maker style game a very long time ago. I abandoned the game, partly because of how hard it was to get these entities working. I had a very similar system of GameObjects having modules, but it didn't make things any easier as all the modules needed access to almost all other modules... I wasn't a very good programmer at that time. xD

Yup, I tried it too and I miserably failed too . But I think it was mostly related to my set of minds. I was trying to make general code that could work anywhere, but you should never do it. If you separate everything into module but you don't try to make general code it might work better.

@Eli : Yeah I think I didn't name it right. That is not really an entity system more like a module or component system.

@Eli : Yeah I think I didn't name it right. That is not really an entity system more like a module or component system.

Well you seem about halfway in-between if you ask me. For a component system you usually either make a bunch of Interfaces that define functionality and then you make your class implement however many you need, or you make a bunch of classes that do the functionality on some common object type and you add all those into a list. However, you do it, the general idea is that you effectively say, "My entity can Walk, it can Shoot, it can TakeDamage, it can Die, and it can be Drawn," or something along those lines. Then you add all those various components in, and some sort of priority/ordering system (can it die while it's already shooting? Can it draw?) and you're good to go.

@Eli : Yeah I think I didn't name it right. That is not really an entity system more like a module or component system.

Well you seem about halfway in-between if you ask me. For a component system you usually either make a bunch of Interfaces that define functionality and then you make your class implement however many you need, or you make a bunch of classes that do the functionality on some common object type and you add all those into a list. However, you do it, the general idea is that you effectively say, "My entity can Walk, it can Shoot, it can TakeDamage, it can Die, and it can be Drawn," or something along those lines. Then you add all those various components in, and some sort of priority/ordering system (can it die while it's already shooting? Can it draw?) and you're good to go.

That's something like that but what I will avoid at all cost is including the priority/ordering into the system because you lose half of the control this way.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org