R
R
roskom_nadzor2016-08-11 04:36:26
Programming
roskom_nadzor, 2016-08-11 04:36:26

What programming pattern to use in this situation?

I have already made a prototype game in es6 and I have a problem.
In short, there is only one unit class in the game and many instances of this class. And everything suited me until I decided to add my own skins for each unit.
Each skin is not just a sprite, but a certain combination of sprites and other things.
Initially, the structure looked like this
Game Loop:
unit1 = new Unit();
Unit Class

class Unit() {
var blabla = blabla
head = new Skin('HEAD');
arrayOfBody = new array;
for (i = 0; i > 10; i++){
child = new Skin('BODY');
array.push(child);
}
}

That is, in the Unit class, 1 Skin instance is created with the HEAD parameter
, and then a certain number of Skins are created with the BODY parameter
. And now the problem is that there are a lot of Skin type classes and a common method for creating instances from parameters is needed.
something like
Game Loop:
unit1 = new Unit('Skin10');
Unit Class
class Unit(Skin) {
this.Skin = Skin; 
var blabla = blabla
head = new Skin(this.Skin,'HEAD');
arrayOfBody = new array;
for (i = 0; i > 10; i++){
child = new Skin(this.Skin, 'BODY');
array.push(child);
}
}

What patterns are commonly used in this situation?
PS By the way, Skin classes will only have static methods and variables.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-08-11
@alexey-m-ukolov

What patterns are commonly used in this situation?
Builder is most suitable here .
Creational Design Patterns

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question