jsettlers / settlers-remake

A Remake of "The Settlers III" for Windows, Linux, Mac and Android
http://www.settlers-android-clone.com
MIT License
356 stars 100 forks source link

Refactoring of MapObjects, MapObjectsManager and MapObjectDrawer and their interaction #245

Open andreas-eberle opened 8 years ago

andreas-eberle commented 8 years ago

Reason

The integration of decoration objects developed in #237 brought to attention that the current MapObject system, including the drawing and management of them, needs to be refactored and unified. At the moment a lot of special case handling is used for their management and rendering.

Background - Ideas of the Current System

The current system was build to achieve decouple the MapObjects from both, game logic and graphics. Reasons for that are:

After taking a look at you code in the "add_decoration_objects"-branch, I think your DrawableObjectFrame is a way to start. We really should try to find a unified way to render map objects.

Modus Operandi

The work will be coordinated in the branch https://github.com/jsettlers/settlers-remake/tree/refactoring_map_objects.

tomsoftware commented 8 years ago

Just one brainstorming idea: would it be possible to use something like a Union to store all Data for all kinds of objects. So we could separate date and logic:

//- create e.g. 1000 data container (may this have to be a list of arrays of union-Data-Object to increase the size)
AllObjectData[] = new union-Data-Object[1000];

//- call the static functions with the data for this object as argument (of cause as reference but this is just a example)
FarmBuilding.DoWorkTick(AllObjectData[ 5 ] );
FarmBuilding.getDrawStyleState(AllObjectData[ 5 ] );

Farmer.DoSomething(AllObjectData[ 6 ])
Farmer.getDrawStyleState(AllObjectData[ 6 ] );

benefit:

disadvantage:

andreas-eberle commented 8 years ago

I see exactly the disadvantage that you pointed out. The objects have very varying size and it is total C programming. Moreover, the code would get badly readable, as there is nothing like the union for structs in Java. Furthermore, all objects stored in an array are actually not stored in the array. In Java, every object type is always passed by reference and arrays only contain references to the objects on the heap. Therefore creating a "uninon datastructure" would only increase the memory usage and still require us to have various types of objects which we then would need to cast to be able to work with them.