I finally re wrote my rendering system for my puzzle game that I restarted a while ago and I need a good way to register blocks. Here is what the game looks like so you can get a feel for what I have:

So there are blocks, but I need a way to register the different types. Eg, trampoline block, or normal wall block and so forth. Right now I am just creating a new instance of the class of that block which then registers a Box to jME. I then join all the meshes together at the end of building the current level. So performance isn't a problem.What's the best way to approach it so that I can, in the level creation code, call:

1

makeBlock(newVector3f(3, 40, 40), Block_Wall.class);

Therefore, creating the block of whichever block.class I put in there?The constructor for the makeBlock is:

So as you can see I just create a BlockSuper there, which is the superclass for all blocks. I need to take the .class of whatever block I want there and create an instance of it from that. I know about reflection:

It would seem that factories are basically just things to create new objects of a class that extends another one, but you don't know which subclass it will be. Why not just use reflection? Please correct me if I'm wrong, I know next to nothing about factories.

Actually, I did some research and didn't find much on the subject. Would you mind giving a very brief explanation of them?

A factory is a class or group of functions that creates a "new" instance of an object in java. It is useful for tile maps because each tile is different and needs a different object. If you didn't have a factory you run the risk of overwriting tile data in more than one tile.

In short, factories just make sure that each tile can store its own data.

Is the piece that creates the new object. The only thing I don't get about this is that you wrote it as creating WallBlocks instead of the superclass, BlockSuper. Does this mean I have to create one of these methods (or are these their own weird classes.. Unfamiliar syntax for me) for every single block I make, or did you mean to put the superclass and accidentally wrote WallBlock? If it's not the latter it doesn't seem very dynamic, no?

Then this code:

1

makeWall(start, end, wallBlockFactory);

Is the bit that actually makes the blocks. This is the bit that leads me to believe that I have to create one of those strange

Yes, you would have to create a factory for each block type you have. In my previous code I created an anonymous class and stored it in a "singleton", I did it that way so that you don't need to create a new instance of the factory on each "makeBlock" call.

The benefits of not using reflection for this task is type safety and better performance.

So this is not very dynamic is it? Let's say we have a game with many, many blocks, like the game Minecraft. Does that game use factories? It seems for a game like that this way would not be very good at all.

Excuse me if I'm being a bit irritating, I am trying to get as in depth as possible in order to fully understand it. Feel free to tell me to back off

How would that let one do specific things with a single type? Let's say I want to do something when the player ball rolls onto a block, like fill his hearts back up. Wouldn't I need a specific subclass like, RegenBlock or something that overrides onPlayerCollide(PlayerBall player)?

Note that:- This is quite a long piece of code, written in a haste, so I probably forgot something.- Read this post multiple times, until you understand it.- This can also be optimized a LOT, and can be made much more dynamic.- If you don't understand something, ask me or anyone that understands this.

That's a very nice approach actually, it makes it so that I only need to create one instance of each block. There are only a few downers I can see that can come from using this approach! I'll go and code it all in and report back if I have any problems. Thanks a bunch!

Edit:

Okay I decided to go with Factories as I decided that I really don't need a large amount of blocks. Because of my collision system sometimes I was getting null when colliding with something because of some positioning readjustments I had to do. So I ended up making one instance of a new class, Block_Air, and setting everything that wasn't occupied to that one instance. Obviously there are some drawbacks to that, but hey, it works.

Thanks a lot for the help guys. By the way, I re did some of the rendering stuff, check out how it looks now:

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