lundi 11 juillet 2016

Design pattern for loading many similar, static assets

I'm looking for a design pattern, or very brief explanation to simplify what is presently a ton of hard-coded logic to load and access many similar assets.

I'm writing a game that loads many 2D sprites. As an example, let's say that each enemy has 8 animations, and I have 10 enemies. That's 80 animations that I need to load in a way that I can call them when needed.

Presently, I'm initializing a variable for each animation. Then I create an array, which contains dictionaries of animations for each enemy. It looks like this:

enemyArray

enemy0 (array index 0)

\_ idle1 :  {enemy1_idle1_animation}
\_ idle2 :  {enemy1_idle2_animation}
\_ attack1 : {enemy1_attack1_animation}

enemy1 (array index 1)

\_ idle1 :  {enemy2_idle1_animation}
\_ idle2 :  {enemy2_idle2_animation}
\_ attack1 : {enemy2_attack1_animation}
\_ ...

With this structure, I can use the index number of the enemy to access the correct animation at any time, by accessing the array index for the enemy, and then get the correct animation from the dictionary (for instance, idle1).

This works... but it's horribly ugly. In reality I have more than 8 animations and more than 10 enemies, so as you can imagine, I have reams of code to load the assets into variables and then load the variables into the array of dictionaries to get it to the point where I can easily have a method that loads the right animation just by saying essentially calling "load attack1 for enemy 3"

Is there a design pattern that would eliminate so much of the hard-coded logic?

Aucun commentaire:

Enregistrer un commentaire