Recently I started creating a game in java to hone my rusty java (I work primarily with cpp) and because I thought it to be a neat idea. To better illustrate the problem, let me explain a part of the game. The details aside, the game is in essence a dungeon builder and the problem pertains to the blocks you place down to create the maze adventurers have to travel through. Each block is a corridor and on each wall/floor/ceiling of a block you can place objects such as traps etc. However, while finishing up the game engine I realized I still lacked a list for blocks/objects that are special. Blocks or items that need a special animation (some extend spikes, some shoot fire, some contain a chest with inventory, etc). They're hard to bring together in one class and as we all know they can't be together in one list with different classes unless we want code-bloating. So my question is, what is the most efficient way to make collection of diversified game objects and how do other games do it?
Presently I have the following methods thought out.
I reduce all objects to instances of one class. They all fit in one list. However, to induce special methods I need a long list of methods that each do something else. Frankly, this method is inefficient, unpleasant and unpractical to work with.
I reduce all object to instances of one class. They all fit in one list. However, they each have an ID of a special action. If the need arises the ID gets compared with another list that's much shorter in length and uses generics or reflection to store the different effect. If it needs a special action it simply activates the effect (such as fire plume) on the given vector. The problem with this is that it does not allow for direct manipulation of the object such as a block that moves out to squash adventurers. Or if it could, it would be messy code, unnecessary throughput of code etc. I'd rather avoid it.
I make a list of the base class/interface and add the different child classes to it. Each child class can thus have a different implementation. However, to access it, I need to know to use reflection and for that I need to know the initial class, which, in many cases, I don't since I want a dynamic system. The result is that I need to store the child class name in the 'block' parent class and then when the need arises, use that name to reflect the new class. Once again, it is inefficient and far too slow.
I create a list of the block class and access it every time a generic block needs to be checked. However, if a creature/person walks over a tile it checks if the child class name in the block class is empty. If not, convert it to that class and execute the special function. This seems the best option as it gives the least code-bloating, is the most efficient, etc. However, it still needs to reflect and convert every time a creature goes onto a special block.
I create an event driven list in which each block instance has his own special event that links to a certain animation. This is very tedious, very inefficient and because it is so detached it is once again difficult to animate the basic block.
I use generics. Sadly, this causes far too much code-bloating and the accessing of the classes themselves gets difficult and messy.
I create a huge switch method and each block instance has a number. The number corresponds with the correct animation in the switch it needs to execute. I don't think I need to say why I don't want to do this.
And there are a few more, but each of them is still as inefficient as the next. Furthermore, every time I look around this site or another people say "use a base interface/class in a list" however they never say what the most efficient way is to get the class out of the list again. Am I missing something obvious? How should I tackle this?