I was prototyping a game in Ruby using Chris Powell’s example of an entity/component setup. I’ve started writing up the game in Java, and after doing a rough translation of the system, I’ve noticed it is a little awkward in Java. Here is the Manager.java https://gist.github.com/kabbotta/a12ed6d08cd491aaec60 that I’ve got. It works, but I was hoping to get some ideas on how it might be done better in Java. A lot of Chris’s system revolves around the dynamic nature of Ruby and the fact that all collections are pretty much the same.
In particular, one of the things I’m missing from Ruby is that I was able to use a direct reference to a class instead of a String in methods like comp(entity, comp_class). In Ruby, the method looked like this:
def comp(entity, component_class)
store = @component_stores[component_class]
return nil if store.nil?
components = store[entity]
return nil if components.nil? || components.empty?
components.first
end
And it could be used in this way:
manager.comp(player, Position)
Where Position is an actual class name and can be recovered by calling component.class. As opposed to how in Java I’m taking in a String and then pulling the class name out of what getClass() returns:
manager.comp(player, "Position");
Thanks for any suggestions you can offer!