Skip to content

Instantly share code, notes, and snippets.

@shoffing
Created September 19, 2018 22:30
Show Gist options
  • Save shoffing/59566cface24864249f593182e61a4c1 to your computer and use it in GitHub Desktop.
Save shoffing/59566cface24864249f593182e61a4c1 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const _ = require('lodash');
const D3Node = require('d3-node');
const ATTACKS_FILE = 'inferno.json';
const DAMAGE_ALPHA_SCALE = 0.9;
const BORDER = 2048;
const data = JSON.parse(fs.readFileSync(ATTACKS_FILE));
const attacks = data.attacks;
const spawns = data.spawns;
const plants = data.plants;
const minX = _.min(_.map(attacks, a => Math.min(a.attacker.pos.x, a.victim.pos.x)));
const maxX = _.max(_.map(attacks, a => Math.max(a.attacker.pos.x, a.victim.pos.x)));
const minY = _.min(_.map(attacks, a => Math.min(a.attacker.pos.y, a.victim.pos.y)));
const maxY = _.max(_.map(attacks, a => Math.max(a.attacker.pos.y, a.victim.pos.y)));
const maxDamage = _.max(_.map(attacks, a => a.amount));
const d3n = new D3Node(); // initializes D3 with container element
const svg = d3n.createSVG(2 * BORDER + maxX - minX, 2 * BORDER + maxY - minY)
.attr("transform", "scale(1,-1)");
attacks.forEach(a => {
const alpha = (1 - DAMAGE_ALPHA_SCALE) + DAMAGE_ALPHA_SCALE * (a.amount / maxDamage);
const color = a.attacker.team === 'T' ? `rgba(237,163,56,${alpha})` : `rgba(104,163,229,${alpha})`;
svg.append("line")
.attr("x1", a.attacker.pos.x + BORDER - minX)
.attr("y1", a.attacker.pos.y + BORDER - minY)
.attr("x2", a.victim.pos.x + BORDER - minX)
.attr("y2", a.victim.pos.y + BORDER - minY)
.attr("stroke", color)
.attr("stroke-width", '5');
svg.append("circle")
.attr("cx", a.attacker.pos.x + BORDER - minX)
.attr("cy", a.attacker.pos.y + BORDER - minY)
.attr("r", 8)
.attr("fill", color);
});
spawns.forEach(s => {
svg.append("circle")
.attr("cx", s.x + BORDER - minX)
.attr("cy", s.y + BORDER - minY)
.attr("r", "20")
.attr("fill", "purple");
});
plants.forEach(s => {
svg.append("circle")
.attr("cx", s.x + BORDER - minX)
.attr("cy", s.y + BORDER - minY)
.attr("r", "10")
.attr("fill", "red");
});
fs.writeFileSync(`inferno_attacks.svg`, d3n.svgString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment