From ad598618f36fb4068a2a8a1d241f28544edbefce Mon Sep 17 00:00:00 2001 From: Shaun Date: Tue, 13 Jun 2017 18:58:41 +0200 Subject: [PATCH] initial commit --- src/client/js/components/components.js | 1 + src/client/js/components/lightningEffect.js | 51 ++++++++++++ src/client/js/components/player.js | 1 + src/client/js/rendering/lightningBuilder.js | 91 +++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 src/client/js/components/lightningEffect.js create mode 100644 src/client/js/rendering/lightningBuilder.js diff --git a/src/client/js/components/components.js b/src/client/js/components/components.js index 4a750700..61e10081 100644 --- a/src/client/js/components/components.js +++ b/src/client/js/components/components.js @@ -4,6 +4,7 @@ var components = [ 'player', 'pather', 'attackAnimation', + 'lightningEffect', 'moveAnimation', 'bumpAnimation', 'animation', diff --git a/src/client/js/components/lightningEffect.js b/src/client/js/components/lightningEffect.js new file mode 100644 index 00000000..99613265 --- /dev/null +++ b/src/client/js/components/lightningEffect.js @@ -0,0 +1,51 @@ +define([ + 'js/rendering/lightningBuilder', + 'js/rendering/effects' +], function( + lightningBuilder, + effects +) { + return { + type: 'lightningEffect', + + cd: 0, + cdMax: 1, + + effect: null, + + init: function() { + effects.register(this); + + this.effect = lightningBuilder.build({ + fromX: this.obj.x + 0, + fromY: this.obj.y + 0.5, + toX: this.obj.x - 2.5, + toY: this.obj.y - 6.5 + }); + }, + + renderManual: function() { + if (this.cd > 0) { + this.cd--; + return; + } + + this.cd = this.cdMax; + + lightningBuilder.destroy(this.effect); + + this.effect = lightningBuilder.build({ + fromX: this.obj.x + 0, + fromY: this.obj.y + 0.5, + toX: this.obj.x - 2.5, + toY: this.obj.y - 6.5 + }); + }, + + destroyManual: function() { + lightningBuilder.destroy(this.effect); + + effects.unregister(this); + } + }; +}); \ No newline at end of file diff --git a/src/client/js/components/player.js b/src/client/js/components/player.js index 1f45e376..f8167c46 100644 --- a/src/client/js/components/player.js +++ b/src/client/js/components/player.js @@ -19,6 +19,7 @@ define([ this.obj.addComponent('keyboardMover'); this.obj.addComponent('mouseMover'); this.obj.addComponent('serverActions'); + this.obj.addComponent('lightningEffect'); this.obj.addComponent('pather'); diff --git a/src/client/js/rendering/lightningBuilder.js b/src/client/js/rendering/lightningBuilder.js new file mode 100644 index 00000000..ba71bdda --- /dev/null +++ b/src/client/js/rendering/lightningBuilder.js @@ -0,0 +1,91 @@ +define([ + 'js/rendering/renderer' +], function( + renderer +) { + var scale = 40; + var scaleMult = 5; + + return { + build: function(config) { + var obj = { + lines: [] + }; + + var divisions = 25; + var maxDeviate = scale * 0.35; + + var fx = config.fromX * scale; + var fy = config.fromY * scale; + + var tx = config.toX * scale; + var ty = config.toY * scale; + + var angle = Math.atan2(ty - fy, tx - fx); + var distance = Math.sqrt(Math.pow(tx - fx, 2) + Math.pow(ty - fy, 2)); + var divDistance = distance / divisions; + + var x = fx; + var y = fy; + + for (var i = 0; i < divisions; i++) { + var line = { + sprites: [] + }; + + var ntx = fx + (Math.cos(angle) * (divDistance * i)) + ~~(Math.random() * (maxDeviate * 2)) - maxDeviate; + var nty = fy + (Math.sin(angle) * (divDistance * i)) + ~~(Math.random() * (maxDeviate * 2)) - maxDeviate; + + if (i == divisions - 1) { + ntx = tx; + nty = ty; + } + + var nAngle = Math.atan2(nty - y, ntx - x); + var steps = ~~(Math.sqrt(Math.pow(ntx - x, 2) + Math.pow(nty - y, 2)) / scaleMult); + + for (var j = 0; j < steps; j++) { + var c = 105 + ~~(Math.random() * 150); + line.sprites.push(renderer.buildRectangle({ + x: ~~(x / scaleMult) * scaleMult, + y: ~~(y / scaleMult) * scaleMult, + w: scaleMult, + h: scaleMult, + color: this.toHex(c, c, ~~(Math.random() * 100)), + layerName: 'effects' + })); + + line.sprites[line.sprites.length - 1].blendMode = PIXI.BLEND_MODES.ADD; + + x += Math.cos(nAngle) * scaleMult; + y += Math.sin(nAngle) * scaleMult; + } + + obj.lines.push(line); + } + + return obj; + }, + + toHex: function rgbToHex(r, g, b) { + var componentToHex = function(c) { + var hex = c.toString(16); + return hex.length == 1 ? '0' + hex : hex; + }; + + return '0x' + componentToHex(r) + componentToHex(g) + componentToHex(b); + }, + + update: function(obj) { + + }, + + destroy: function(obj) { + obj.lines.forEach(function(l) { + l.sprites.forEach(function(s) { + s.parent.removeChild(s); + }); + }); + } + }; +}); \ No newline at end of file