Intentando agregar un poco de lógica al juego [cerrado]

I have fight game and want to add a bit more interaction, some kinds of abilities. Is it a good way to do this? Constructor and a list of all abilities or I've missed some more simple method for this?

var abilityConstructor = function(name, desc, icon, target, param, rate, duration) {
    this.name = name;         // Name of ability
    this.desc = desc;         // Ability's description
    this.icon = icon;         // Ability's icon
    this.target = target;     // If for self usage - 0, if for enemy - 1
    this.param = param;       // Which parameter is influenced (health - 0, damage - 1, speed - 2, missing rate - 3)
    this.rate = rate;         // Factor for dealing (ability's strength) 
    this.duration = duration; // Spells' duration (current round - 1, two rounds - 2, three rounds - 3)
}

// List of available rates for abilities
var lowRate = 0.1;
var midRate = 0.25;
var highRate = 0.5;

var testAbility = new abilityConstructor('Health reduction', 'Reduces health of the opponent for 2 rounds', 'icon_path_here', 1, 0, midRate, 2);

preguntado el 04 de septiembre de 13 a las 02:09

Looks nice. But this kind of question is a better fit for CR. Though even then it's a bit of primarily opinion based. -

Yes, you could use a single object as parameter and extend it with a default object. function constructor(params) { this.params = extend(params, defaults) }). Look for extend scripts online, there are plenty, depending on your case you may want "deep extend". -

1 Respuestas

Well, first of all by convention it's probably better to have constructor functions capitalized since it helps to identity them as such and you generally dont append the constructor word either.

Also, it's mostly a preference, but when you start to have a lot of parameters, I prefer to take a single data or options objeto como argumento.

Finally, if you are simply copying over configurations, you could do it by looping over the configuration keys.

function Ability(data) {
    for (var k in data) {
        this[k] = data;
    }
}

However, you might not be able to generalize as much, so instead of looping over you could simply manually copy the values like you were doing.

function Ability(data) {
    this.name = data.name;
    //...
}

Here's how you would call the constructor:

var testAbility = new Ability({
    name: 'Health reduction',
    ...
});

EDIT:

The advantage of this construct is that you do not have to remember the parameters order and it's also simplifying your life when some arguments might be optionnal. Instead of passing undefined, you can simply omit the key in the data object. This pattern is used in pretty much every JavaScript libraries, including jQuery. However, you would be sacrificing slightly on performance since a data object has to be created every time. I would strongly advise you to try this pattern and fallback on your initial solution if performance is a real issue.

Respondido el 04 de Septiembre de 13 a las 15:09

As I can see second variant is almost the same as mine. Only constructor now a bit shorter, but new objects instead take more place. Isn't it a good idea to keep statements as short as they could be, if will have hundread of such "abilities", for example? - Paul Lazy

I added more precisions in the answer itself. - plax

Yes, see it now, tried all patterns, it is most clear. Thank you. - Paul Lazy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.