Преглед изворни кода

Move aura visual effect to its own file

tags/v0.10.6^2
kckckc пре 2 година
родитељ
комит
231accded5
3 измењених фајлова са 146 додато и 91 уклоњено
  1. +32
    -91
      src/server/clientComponents/effects.js
  2. +109
    -0
      src/server/clientComponents/effects/auras.js
  3. +5
    -0
      src/server/config/clientConfig.js

+ 32
- 91
src/server/clientComponents/effects.js Прегледај датотеку

@@ -3,133 +3,74 @@ define([
], function (
renderer
) {
let auras = {
reflectDamage: 0,
stealth: 1,
regenHp: 9,
regenMana: 10,
swiftness: 11,
holyVengeance: 8,
rare: 16
};

return {
type: 'effects',

alpha: 0,
alphaDir: 0.0025,
effects: [],

alphaMax: 0.6,
alphaMin: 0.35,
templates: {
},

alphaCutoff: 0.4,
init: function (blueprint) {
this.effects = this.effects.map(e => this.buildEffect(e));
},

effects: [],
buildEffect: function (data) {
if (typeof data === 'string')
data = { type: data };
let template = this.templates[data.type] || {};

init: function (blueprint) {
this.effects = this.effects
.filter(e => auras[e] !== null)
.map(e => {
return {
name: e,
sprite: renderer.buildObject({
layerName: 'effects',
sheetName: 'auras',
x: this.obj.x,
y: this.obj.y + 1,
w: scale * 3,
h: scale * 3,
cell: auras[e]
})
};
});
let effect = $.extend(true, {}, template, data);

effect.obj = this.obj;

if (effect.init)
effect.init();
return effect;
},

extend: function (blueprint) {
if (blueprint.addEffects) {
blueprint.addEffects = blueprint.addEffects
.filter(e => {
return (auras[e] !== null);
})
.map(e => {
return {
name: e,
sprite: renderer.buildObject({
layerName: 'effects',
sheetName: 'auras',
x: this.obj.x,
y: this.obj.y + 1,
w: scale * 3,
h: scale * 3,
cell: auras[e]
})
};
});
blueprint.addEffects = blueprint.addEffects.map(e => this.buildEffect(e));

this.effects.push.apply(this.effects, blueprint.addEffects || []);
}
if (blueprint.removeEffects) {
blueprint.removeEffects.forEach(r => {
let effect = this.effects.find(e => e.name === r);
let effect = this.effects.find(e => e.type === r);

if (!effect)
return;

renderer.destroyObject({
layerName: 'effects',
sprite: effect.sprite
});
if (effect.destroy)
effect.destroy();

this.effects.spliceFirstWhere(e => e.name === r);
this.effects.spliceFirstWhere(e => e.type === r);
});
}
},

update: function () {
this.alpha += this.alphaDir;
if ((this.alphaDir > 0) && (this.alpha >= this.alphaMax)) {
this.alpha = this.alphaMax;
this.alphaDir *= -1;
} else if ((this.alphaDir < 0) && (this.alpha <= this.alphaMin)) {
this.alpha = this.alphaMin;
this.alphaDir *= -1;
}

let x = this.obj.x;
let y = this.obj.y;

let useAlpha = this.alpha;
if (useAlpha < this.alphaCutoff)
useAlpha = 0;
else {
useAlpha -= this.alphaCutoff;
useAlpha /= (this.alphaMax - this.alphaCutoff);
}

this.effects.forEach(e => {
renderer.setSpritePosition({
x,
y: y + 1,
sprite: e.sprite
});

e.sprite.alpha = useAlpha;

e.sprite.visible = this.obj.isVisible;
if (e.update)
e.update();
});
},

setVisible: function (visible) {
this.effects.forEach(e => {
e.sprite.visible = visible;
if (e.setVisible)
e.setVisible(visible);
});
},

destroy: function () {
this.effects.forEach(e => {
renderer.destroyObject({
layerName: 'effects',
sprite: e.sprite
});
if (e.destroy)
e.destroy();
});
}
};


+ 109
- 0
src/server/clientComponents/effects/auras.js Прегледај датотеку

@@ -0,0 +1,109 @@
define([
'js/rendering/renderer'
], function (
renderer
) {
let auras = {
reflectDamage: 0,
stealth: 1,
regenHp: 9,
regenMana: 10,
swiftness: 11,
holyVengeance: 8,
rare: 16
};

let templates = {};

Object.keys(auras).forEach(type => {
let cell = auras[type];

templates[type] = {
sprite: null,

alpha: 0,
alphaDir: 0.0025,

alphaMax: 0.6,
alphaMin: 0.35,

alphaCutoff: 0.4,
init: function () {
this.sprite = renderer.buildObject({
layerName: 'effects',
sheetName: 'auras',
x: this.obj.x,
y: this.obj.y + 1,
w: scale * 3,
h: scale * 3,
cell: cell
});
},

getAlpha: function () {
let listAuras = this.obj.effects.effects.filter(e => auras[e.type]);
let first = listAuras[0];

// The first aura in the list should do all the updating so that all auras pulse together
if (first === this) {
this.alpha += this.alphaDir;
if ((this.alphaDir > 0) && (this.alpha >= this.alphaMax)) {
this.alpha = this.alphaMax;
this.alphaDir *= -1;
} else if ((this.alphaDir < 0) && (this.alpha <= this.alphaMin)) {
this.alpha = this.alphaMin;
this.alphaDir *= -1;
}
} else {
this.alpha = first.alpha;
this.alphaDir = first.alphaDir;
}

let useAlpha = this.alpha;
if (useAlpha < this.alphaCutoff)
useAlpha = 0;
else {
useAlpha -= this.alphaCutoff;
useAlpha /= (this.alphaMax - this.alphaCutoff);
}

return useAlpha;
},

update: function () {
let useAlpha = this.getAlpha();

let x = this.obj.x;
let y = this.obj.y;

renderer.setSpritePosition({
x,
y: y + 1,
sprite: this.sprite
});

this.sprite.alpha = useAlpha;

this.sprite.visible = this.obj.isVisible;
},

destroy: function () {
renderer.destroyObject({
layerName: 'effects',
sprite: this.sprite
});
},

setVisible: function (visible) {
this.sprite.visible = visible;
}
};
});

return {
templates: {
...templates
}
};
});

+ 5
- 0
src/server/config/clientConfig.js Прегледај датотеку

@@ -221,6 +221,11 @@ module.exports = {
});
});

config.clientComponents.push({
extends: 'effects',
path: 'server/clientComponents/effects/auras.js'
});

events.emit('onBeforeGetClientConfig', config);

//Deprecated


Loading…
Откажи
Сачувај