@ -1,148 +1,141 @@
|
||||
// @ts-ignore
|
||||
const path = require("path");
|
||||
|
||||
// Globs for non-ui resources
|
||||
const nonImageResourcesGlobs = ["../res/**/*.woff2", "../res/*.ico", "../res/**/*.webm"];
|
||||
|
||||
// Globs for ui resources
|
||||
const imageResourcesGlobs = ["../res/**/*.png", "../res/**/*.svg", "../res/**/*.jpg", "../res/**/*.gif"];
|
||||
|
||||
function gulptasksImageResources($, gulp, buildFolder) {
|
||||
// Lossless options
|
||||
const minifyImagesOptsLossless = () => [
|
||||
$.imageminJpegtran({
|
||||
progressive: true,
|
||||
}),
|
||||
$.imagemin.svgo({}),
|
||||
$.imagemin.optipng({
|
||||
optimizationLevel: 3,
|
||||
}),
|
||||
$.imageminGifsicle({
|
||||
optimizationLevel: 3,
|
||||
colors: 128,
|
||||
}),
|
||||
];
|
||||
|
||||
// Lossy options
|
||||
const minifyImagesOpts = () => [
|
||||
$.imagemin.mozjpeg({
|
||||
quality: 80,
|
||||
maxMemory: 1024 * 1024 * 8,
|
||||
}),
|
||||
$.imagemin.svgo({}),
|
||||
$.imageminPngquant({
|
||||
speed: 1,
|
||||
strip: true,
|
||||
quality: [0.65, 0.9],
|
||||
dithering: false,
|
||||
verbose: false,
|
||||
}),
|
||||
$.imagemin.optipng({
|
||||
optimizationLevel: 3,
|
||||
}),
|
||||
$.imageminGifsicle({
|
||||
optimizationLevel: 3,
|
||||
colors: 128,
|
||||
}),
|
||||
];
|
||||
|
||||
// Where the resources folder are
|
||||
const resourcesDestFolder = path.join(buildFolder, "res");
|
||||
|
||||
/**
|
||||
* Determines if an atlas must use lossless compression
|
||||
* @param {string} fname
|
||||
*/
|
||||
function fileMustBeLossless(fname) {
|
||||
return fname.indexOf("lossless") >= 0;
|
||||
}
|
||||
|
||||
/////////////// ATLAS /////////////////////
|
||||
|
||||
// Copies the atlas to the final destination
|
||||
gulp.task("imgres.atlas", () => {
|
||||
return gulp
|
||||
.src(["../res_built/atlas/*.png"])
|
||||
.pipe($.cached("imgres.atlas"))
|
||||
.pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
// Copies the atlas to the final destination after optimizing it (lossy compression)
|
||||
gulp.task("imgres.atlasOptimized", () => {
|
||||
return gulp
|
||||
.src(["../res_built/atlas/*.png"])
|
||||
.pipe($.cached("imgres.atlasOptimized"))
|
||||
.pipe(
|
||||
$.if(
|
||||
fname => fileMustBeLossless(fname.history[0]),
|
||||
$.imagemin(minifyImagesOptsLossless()),
|
||||
$.imagemin(minifyImagesOpts())
|
||||
)
|
||||
)
|
||||
.pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
//////////////////// RESOURCES //////////////////////
|
||||
|
||||
// Copies all resources which are no ui resources
|
||||
gulp.task("imgres.copyNonImageResources", () => {
|
||||
return gulp
|
||||
.src(nonImageResourcesGlobs)
|
||||
.pipe($.cached("imgres.copyNonImageResources"))
|
||||
.pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
// Copies all ui resources
|
||||
gulp.task("imgres.copyImageResources", () => {
|
||||
return gulp
|
||||
.src(imageResourcesGlobs)
|
||||
.pipe($.cached("copyImageResources"))
|
||||
.pipe(gulp.dest(path.join(resourcesDestFolder)));
|
||||
});
|
||||
|
||||
// Copies all ui resources and optimizes them
|
||||
gulp.task("imgres.copyImageResourcesOptimized", () => {
|
||||
return gulp
|
||||
.src(imageResourcesGlobs)
|
||||
.pipe($.cached("imgres.copyImageResourcesOptimized"))
|
||||
.pipe(
|
||||
$.if(
|
||||
fname => fileMustBeLossless(fname.history[0]),
|
||||
$.imagemin(minifyImagesOptsLossless()),
|
||||
$.imagemin(minifyImagesOpts())
|
||||
)
|
||||
)
|
||||
.pipe(gulp.dest(path.join(resourcesDestFolder)));
|
||||
});
|
||||
|
||||
// Copies all resources and optimizes them
|
||||
gulp.task(
|
||||
"imgres.allOptimized",
|
||||
gulp.parallel(
|
||||
"imgres.atlasOptimized",
|
||||
"imgres.copyNonImageResources",
|
||||
"imgres.copyImageResourcesOptimized"
|
||||
)
|
||||
);
|
||||
|
||||
// Cleans up unused images which are instead inline into the css
|
||||
gulp.task("imgres.cleanupUnusedCssInlineImages", () => {
|
||||
return gulp
|
||||
.src(
|
||||
[
|
||||
path.join(buildFolder, "res", "ui", "**", "*.png"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.jpg"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.svg"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.gif"),
|
||||
],
|
||||
{ read: false }
|
||||
)
|
||||
.pipe($.if(fname => fname.history[0].indexOf("noinline") < 0, $.clean({ force: true })));
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
nonImageResourcesGlobs,
|
||||
imageResourcesGlobs,
|
||||
gulptasksImageResources,
|
||||
};
|
||||
// @ts-ignore
|
||||
const path = require("path");
|
||||
|
||||
// Globs for non-ui resources
|
||||
const nonImageResourcesGlobs = ["../res/**/*.woff2", "../res/*.ico", "../res/**/*.webm"];
|
||||
|
||||
// Globs for ui resources
|
||||
const imageResourcesGlobs = ["../res/**/*.png", "../res/**/*.svg", "../res/**/*.jpg", "../res/**/*.gif"];
|
||||
|
||||
function gulptasksImageResources($, gulp, buildFolder) {
|
||||
// Lossless options
|
||||
const minifyImagesOptsLossless = () => [
|
||||
$.imageminJpegtran({
|
||||
progressive: true,
|
||||
}),
|
||||
$.imagemin.svgo({}),
|
||||
$.imagemin.optipng({
|
||||
optimizationLevel: 3,
|
||||
}),
|
||||
$.imageminGifsicle({
|
||||
optimizationLevel: 3,
|
||||
colors: 128,
|
||||
}),
|
||||
];
|
||||
|
||||
// Lossy options
|
||||
const minifyImagesOpts = () => [
|
||||
$.imagemin.mozjpeg({
|
||||
quality: 80,
|
||||
maxMemory: 1024 * 1024 * 8,
|
||||
}),
|
||||
$.imagemin.svgo({}),
|
||||
$.imageminPngquant({
|
||||
speed: 1,
|
||||
strip: true,
|
||||
quality: [0.65, 0.9],
|
||||
dithering: false,
|
||||
verbose: false,
|
||||
}),
|
||||
$.imagemin.optipng({
|
||||
optimizationLevel: 3,
|
||||
}),
|
||||
$.imageminGifsicle({
|
||||
optimizationLevel: 3,
|
||||
colors: 128,
|
||||
}),
|
||||
];
|
||||
|
||||
// Where the resources folder are
|
||||
const resourcesDestFolder = path.join(buildFolder, "res");
|
||||
|
||||
/**
|
||||
* Determines if an atlas must use lossless compression
|
||||
* @param {string} fname
|
||||
*/
|
||||
function fileMustBeLossless(fname) {
|
||||
return fname.indexOf("lossless") >= 0;
|
||||
}
|
||||
|
||||
/////////////// ATLAS /////////////////////
|
||||
|
||||
// Copies the atlas to the final destination
|
||||
gulp.task("imgres.atlas", () => {
|
||||
return gulp.src(["../res_built/atlas/*.png"]).pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
// Copies the atlas to the final destination after optimizing it (lossy compression)
|
||||
gulp.task("imgres.atlasOptimized", () => {
|
||||
return gulp
|
||||
.src(["../res_built/atlas/*.png"])
|
||||
.pipe(
|
||||
$.if(
|
||||
fname => fileMustBeLossless(fname.history[0]),
|
||||
$.imagemin(minifyImagesOptsLossless()),
|
||||
$.imagemin(minifyImagesOpts())
|
||||
)
|
||||
)
|
||||
.pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
//////////////////// RESOURCES //////////////////////
|
||||
|
||||
// Copies all resources which are no ui resources
|
||||
gulp.task("imgres.copyNonImageResources", () => {
|
||||
return gulp.src(nonImageResourcesGlobs).pipe(gulp.dest(resourcesDestFolder));
|
||||
});
|
||||
|
||||
// Copies all ui resources
|
||||
gulp.task("imgres.copyImageResources", () => {
|
||||
return gulp
|
||||
.src(imageResourcesGlobs)
|
||||
|
||||
.pipe($.cached("imgres.copyImageResources"))
|
||||
.pipe(gulp.dest(path.join(resourcesDestFolder)));
|
||||
});
|
||||
|
||||
// Copies all ui resources and optimizes them
|
||||
gulp.task("imgres.copyImageResourcesOptimized", () => {
|
||||
return gulp
|
||||
.src(imageResourcesGlobs)
|
||||
.pipe(
|
||||
$.if(
|
||||
fname => fileMustBeLossless(fname.history[0]),
|
||||
$.imagemin(minifyImagesOptsLossless()),
|
||||
$.imagemin(minifyImagesOpts())
|
||||
)
|
||||
)
|
||||
.pipe(gulp.dest(path.join(resourcesDestFolder)));
|
||||
});
|
||||
|
||||
// Copies all resources and optimizes them
|
||||
gulp.task(
|
||||
"imgres.allOptimized",
|
||||
gulp.parallel(
|
||||
"imgres.atlasOptimized",
|
||||
"imgres.copyNonImageResources",
|
||||
"imgres.copyImageResourcesOptimized"
|
||||
)
|
||||
);
|
||||
|
||||
// Cleans up unused images which are instead inline into the css
|
||||
gulp.task("imgres.cleanupUnusedCssInlineImages", () => {
|
||||
return gulp
|
||||
.src(
|
||||
[
|
||||
path.join(buildFolder, "res", "ui", "**", "*.png"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.jpg"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.svg"),
|
||||
path.join(buildFolder, "res", "ui", "**", "*.gif"),
|
||||
],
|
||||
{ read: false }
|
||||
)
|
||||
.pipe($.if(fname => fname.history[0].indexOf("noinline") < 0, $.clean({ force: true })));
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
nonImageResourcesGlobs,
|
||||
imageResourcesGlobs,
|
||||
gulptasksImageResources,
|
||||
};
|
||||
|
After Width: | Height: | Size: 5.8 KiB |
After Width: | Height: | Size: 8.0 KiB |
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 7.7 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 5.9 KiB |
After Width: | Height: | Size: 5.3 KiB |
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 7.2 KiB After Width: | Height: | Size: 5.8 KiB |
After Width: | Height: | Size: 233 KiB |
After Width: | Height: | Size: 281 KiB |
After Width: | Height: | Size: 287 KiB |
After Width: | Height: | Size: 283 KiB |
After Width: | Height: | Size: 286 KiB |
After Width: | Height: | Size: 260 KiB |
After Width: | Height: | Size: 116 KiB |
After Width: | Height: | Size: 116 KiB |
After Width: | Height: | Size: 106 KiB |
After Width: | Height: | Size: 121 KiB |
After Width: | Height: | Size: 121 KiB |
After Width: | Height: | Size: 107 KiB |
After Width: | Height: | Size: 138 KiB |
After Width: | Height: | Size: 91 KiB |
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 1.3 MiB |
Before Width: | Height: | Size: 278 KiB After Width: | Height: | Size: 279 KiB |
Before Width: | Height: | Size: 701 KiB After Width: | Height: | Size: 703 KiB |
@ -1,226 +1,213 @@
|
||||
/**
|
||||
*
|
||||
* Run `yarn global add canvas` first
|
||||
*/
|
||||
|
||||
const { createCanvas } = require("canvas");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const outputFolder = path.join(__dirname, "..", "wires", "sets");
|
||||
|
||||
const dimensions = 192;
|
||||
const lineSize = 12;
|
||||
const lowerLineSize = 20;
|
||||
|
||||
function hexToRGB(h) {
|
||||
let r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
|
||||
// 3 digits
|
||||
if (h.length == 4) {
|
||||
r = "0x" + h[1] + h[1];
|
||||
g = "0x" + h[2] + h[2];
|
||||
b = "0x" + h[3] + h[3];
|
||||
|
||||
// 6 digits
|
||||
} else if (h.length == 7) {
|
||||
r = "0x" + h[1] + h[2];
|
||||
g = "0x" + h[3] + h[4];
|
||||
b = "0x" + h[5] + h[6];
|
||||
}
|
||||
|
||||
return [+r, +g, +b];
|
||||
}
|
||||
|
||||
function RGBToHSL(r, g, b) {
|
||||
// Make r, g, and b fractions of 1
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
|
||||
// Find greatest and smallest channel values
|
||||
let cmin = Math.min(r, g, b),
|
||||
cmax = Math.max(r, g, b),
|
||||
delta = cmax - cmin,
|
||||
h = 0,
|
||||
s = 0,
|
||||
l = 0;
|
||||
// Calculate hue
|
||||
// No difference
|
||||
if (delta == 0) h = 0;
|
||||
// Red is max
|
||||
else if (cmax == r) h = ((g - b) / delta) % 6;
|
||||
// Green is max
|
||||
else if (cmax == g) h = (b - r) / delta + 2;
|
||||
// Blue is max
|
||||
else h = (r - g) / delta + 4;
|
||||
|
||||
h = Math.round(h * 60);
|
||||
|
||||
// Make negative hues positive behind 360°
|
||||
if (h < 0) h += 360;
|
||||
|
||||
// Calculate lightness
|
||||
l = (cmax + cmin) / 2;
|
||||
|
||||
// Calculate saturation
|
||||
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
|
||||
|
||||
// Multiply l and s by 100
|
||||
s = +(s * 100).toFixed(1);
|
||||
l = +(l * 100).toFixed(1);
|
||||
|
||||
return [h, s, l];
|
||||
}
|
||||
|
||||
function HSLToRGB(h, s, l) {
|
||||
// Must be fractions of 1
|
||||
s /= 100;
|
||||
l /= 100;
|
||||
|
||||
let c = (1 - Math.abs(2 * l - 1)) * s,
|
||||
x = c * (1 - Math.abs(((h / 60) % 2) - 1)),
|
||||
m = l - c / 2,
|
||||
r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
|
||||
if (0 <= h && h < 60) {
|
||||
r = c;
|
||||
g = x;
|
||||
b = 0;
|
||||
} else if (60 <= h && h < 120) {
|
||||
r = x;
|
||||
g = c;
|
||||
b = 0;
|
||||
} else if (120 <= h && h < 180) {
|
||||
r = 0;
|
||||
g = c;
|
||||
b = x;
|
||||
} else if (180 <= h && h < 240) {
|
||||
r = 0;
|
||||
g = x;
|
||||
b = c;
|
||||
} else if (240 <= h && h < 300) {
|
||||
r = x;
|
||||
g = 0;
|
||||
b = c;
|
||||
} else if (300 <= h && h < 360) {
|
||||
r = c;
|
||||
g = 0;
|
||||
b = x;
|
||||
}
|
||||
r = Math.round((r + m) * 255);
|
||||
g = Math.round((g + m) * 255);
|
||||
b = Math.round((b + m) * 255);
|
||||
|
||||
return [r, g, b];
|
||||
}
|
||||
|
||||
async function run() {
|
||||
console.log("Running");
|
||||
|
||||
const variants = {
|
||||
regular: "#25fff2",
|
||||
color: "#eba458",
|
||||
shape: "#8858eb",
|
||||
conflict: "#ff3e3e",
|
||||
};
|
||||
|
||||
const promises = [];
|
||||
|
||||
for (const variantId in variants) {
|
||||
const variantColor = variants[variantId];
|
||||
const variantHSL = RGBToHSL(...hexToRGB(variantColor));
|
||||
const darkenedColor = HSLToRGB(variantHSL[0], variantHSL[1] - 15, variantHSL[2] - 20);
|
||||
const hexDarkenedColor = "rgb(" + darkenedColor.join(",") + ")";
|
||||
|
||||
console.log(variantColor, "->", hexToRGB(variantColor), variantHSL, "->", darkenedColor);
|
||||
|
||||
const parts = {
|
||||
forward: [[0.5, 0, 0.5, 1]],
|
||||
turn: [
|
||||
[0.5, 0.5, 0.5, 1],
|
||||
[0.5, 0.5, 1, 0.5],
|
||||
],
|
||||
split: [
|
||||
[0.5, 0.5, 0.5, 1],
|
||||
[0, 0.5, 1, 0.5],
|
||||
],
|
||||
cross: [
|
||||
[0, 0.5, 1, 0.5],
|
||||
[0.5, 0, 0.5, 1],
|
||||
],
|
||||
};
|
||||
|
||||
for (const partId in parts) {
|
||||
const partLines = parts[partId];
|
||||
|
||||
const canvas = createCanvas(dimensions, dimensions);
|
||||
const context = canvas.getContext("2d");
|
||||
context.quality = "best";
|
||||
context.clearRect(0, 0, dimensions, dimensions);
|
||||
|
||||
context.strokeStyle = hexDarkenedColor;
|
||||
context.lineWidth = lowerLineSize;
|
||||
context.lineCap = "square";
|
||||
context.imageSmoothingEnabled = false;
|
||||
|
||||
// Draw lower lines
|
||||
partLines.forEach(([x1, y1, x2, y2]) => {
|
||||
context.beginPath();
|
||||
context.moveTo(x1 * dimensions, y1 * dimensions);
|
||||
context.lineTo(x2 * dimensions, y2 * dimensions);
|
||||
context.stroke();
|
||||
});
|
||||
|
||||
context.strokeStyle = variantColor;
|
||||
context.lineWidth = lineSize;
|
||||
|
||||
// Draw upper lines
|
||||
partLines.forEach(([x1, y1, x2, y2]) => {
|
||||
context.beginPath();
|
||||
context.moveTo(x1 * dimensions, y1 * dimensions);
|
||||
context.lineTo(x2 * dimensions, y2 * dimensions);
|
||||
context.stroke();
|
||||
});
|
||||
|
||||
const out = fs.createWriteStream(path.join(outputFolder, variantId + "_" + partId + ".png"));
|
||||
const stream = canvas.createPNGStream();
|
||||
stream.pipe(out);
|
||||
promises.push(new Promise(resolve => stream.on("end", resolve)));
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Waiting for completion");
|
||||
await Promise.all(promises);
|
||||
|
||||
// Also wait a bit more
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
console.log("Copying files to all locations");
|
||||
|
||||
// // Copy other files
|
||||
fs.copyFileSync(
|
||||
path.join(outputFolder, "regular_forward.png"),
|
||||
path.join(__dirname, "..", "buildings", "wire.png")
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(outputFolder, "regular_turn.png"),
|
||||
path.join(__dirname, "..", "buildings", "wire-turn.png")
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(outputFolder, "regular_split.png"),
|
||||
path.join(__dirname, "..", "buildings", "wire-split.png")
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(outputFolder, "regular_cross.png"),
|
||||
path.join(__dirname, "..", "buildings", "wire-cross.png")
|
||||
);
|
||||
|
||||
console.log("Done!");
|
||||
}
|
||||
|
||||
run();
|
||||
/**
|
||||
*
|
||||
* Run `yarn global add canvas` first
|
||||
*/
|
||||
|
||||
const { createCanvas } = require("canvas");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const outputFolder = path.join(__dirname, "..", "wires", "sets");
|
||||
|
||||
const dimensions = 192;
|
||||
const lineSize = 14;
|
||||
const lowerLineSize = 32;
|
||||
|
||||
const variants = {
|
||||
first: "#61ef6f",
|
||||
second: "#f0bd65",
|
||||
third: "#5fb2f1",
|
||||
conflict: "#f74c4c",
|
||||
};
|
||||
|
||||
function hexToRGB(h) {
|
||||
let r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
|
||||
// 3 digits
|
||||
if (h.length == 4) {
|
||||
r = "0x" + h[1] + h[1];
|
||||
g = "0x" + h[2] + h[2];
|
||||
b = "0x" + h[3] + h[3];
|
||||
|
||||
// 6 digits
|
||||
} else if (h.length == 7) {
|
||||
r = "0x" + h[1] + h[2];
|
||||
g = "0x" + h[3] + h[4];
|
||||
b = "0x" + h[5] + h[6];
|
||||
}
|
||||
|
||||
return [+r, +g, +b];
|
||||
}
|
||||
|
||||
function RGBToHSL(r, g, b) {
|
||||
// Make r, g, and b fractions of 1
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
|
||||
// Find greatest and smallest channel values
|
||||
let cmin = Math.min(r, g, b),
|
||||
cmax = Math.max(r, g, b),
|
||||
delta = cmax - cmin,
|
||||
h = 0,
|
||||
s = 0,
|
||||
l = 0;
|
||||
// Calculate hue
|
||||
// No difference
|
||||
if (delta == 0) h = 0;
|
||||
// Red is max
|
||||
else if (cmax == r) h = ((g - b) / delta) % 6;
|
||||
// Green is max
|
||||
else if (cmax == g) h = (b - r) / delta + 2;
|
||||
// Blue is max
|
||||
else h = (r - g) / delta + 4;
|
||||
|
||||
h = Math.round(h * 60);
|
||||
|
||||
// Make negative hues positive behind 360°
|
||||
if (h < 0) h += 360;
|
||||
|
||||
// Calculate lightness
|
||||
l = (cmax + cmin) / 2;
|
||||
|
||||
// Calculate saturation
|
||||
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
|
||||
|
||||
// Multiply l and s by 100
|
||||
s = +(s * 100).toFixed(1);
|
||||
l = +(l * 100).toFixed(1);
|
||||
|
||||
return [h, s, l];
|
||||
}
|
||||
|
||||
function HSLToRGB(h, s, l) {
|
||||
// Must be fractions of 1
|
||||
s /= 100;
|
||||
l /= 100;
|
||||
|
||||
let c = (1 - Math.abs(2 * l - 1)) * s,
|
||||
x = c * (1 - Math.abs(((h / 60) % 2) - 1)),
|
||||
m = l - c / 2,
|
||||
r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
|
||||
if (0 <= h && h < 60) {
|
||||
r = c;
|
||||
g = x;
|
||||
b = 0;
|
||||
} else if (60 <= h && h < 120) {
|
||||
r = x;
|
||||
g = c;
|
||||
b = 0;
|
||||
} else if (120 <= h && h < 180) {
|
||||
r = 0;
|
||||
g = c;
|
||||
b = x;
|
||||
} else if (180 <= h && h < 240) {
|
||||
r = 0;
|
||||
g = x;
|
||||
b = c;
|
||||
} else if (240 <= h && h < 300) {
|
||||
r = x;
|
||||
g = 0;
|
||||
b = c;
|
||||
} else if (300 <= h && h < 360) {
|
||||
r = c;
|
||||
g = 0;
|
||||
b = x;
|
||||
}
|
||||
r = Math.round((r + m) * 255);
|
||||
g = Math.round((g + m) * 255);
|
||||
b = Math.round((b + m) * 255);
|
||||
|
||||
return [r, g, b];
|
||||
}
|
||||
|
||||
async function run() {
|
||||
console.log("Running");
|
||||
|
||||
const promises = [];
|
||||
|
||||
for (const variantId in variants) {
|
||||
const variantColor = variants[variantId];
|
||||
const variantHSL = RGBToHSL(...hexToRGB(variantColor));
|
||||
const darkenedColor = HSLToRGB(variantHSL[0], variantHSL[1] - 15, variantHSL[2] - 20);
|
||||
const hexDarkenedColor = "rgb(" + darkenedColor.join(",") + ")";
|
||||
|
||||
console.log(variantColor, "->", hexToRGB(variantColor), variantHSL, "->", darkenedColor);
|
||||
|
||||
const parts = {
|
||||
forward: [[0.5, 0, 0.5, 1]],
|
||||
turn: [
|
||||
[0.5, 0.5, 0.5, 1],
|
||||
[0.5, 0.5, 1, 0.5],
|
||||
],
|
||||
split: [
|
||||
[0.5, 0.5, 0.5, 1],
|
||||
[0, 0.5, 1, 0.5],
|
||||
],
|
||||
cross: [
|
||||
[0, 0.5, 1, 0.5],
|
||||
[0.5, 0, 0.5, 1],
|
||||
],
|
||||
};
|
||||
|
||||
for (const partId in parts) {
|
||||
const partLines = parts[partId];
|
||||
|
||||
const canvas = createCanvas(dimensions, dimensions);
|
||||
const context = canvas.getContext("2d");
|
||||
context.quality = "best";
|
||||
context.clearRect(0, 0, dimensions, dimensions);
|
||||
|
||||
const lineCanvas = createCanvas(dimensions, dimensions);
|
||||
const lineContext = lineCanvas.getContext("2d");
|
||||
lineContext.quality = "best";
|
||||
lineContext.clearRect(0, 0, dimensions, dimensions);
|
||||
lineContext.strokeStyle = hexDarkenedColor;
|
||||
lineContext.lineWidth = lowerLineSize;
|
||||
lineContext.lineCap = "square";
|
||||
lineContext.imageSmoothingEnabled = false;
|
||||
|
||||
// Draw lower lines
|
||||
partLines.forEach(([x1, y1, x2, y2]) => {
|
||||
lineContext.beginPath();
|
||||
lineContext.moveTo(x1 * dimensions, y1 * dimensions);
|
||||
lineContext.lineTo(x2 * dimensions, y2 * dimensions);
|
||||
lineContext.stroke();
|
||||
});
|
||||
|
||||
context.globalAlpha = 0.4;
|
||||
context.drawImage(lineCanvas, 0, 0, dimensions, dimensions);
|
||||
|
||||
context.globalAlpha = 1;
|
||||
context.imageSmoothingEnabled = false;
|
||||
context.lineCap = "square";
|
||||
context.strokeStyle = variantColor;
|
||||
context.lineWidth = lineSize;
|
||||
|
||||
// Draw upper lines
|
||||
partLines.forEach(([x1, y1, x2, y2]) => {
|
||||
context.beginPath();
|
||||
context.moveTo(x1 * dimensions, y1 * dimensions);
|
||||
context.lineTo(x2 * dimensions, y2 * dimensions);
|
||||
context.stroke();
|
||||
});
|
||||
|
||||
const out = fs.createWriteStream(path.join(outputFolder, variantId + "_" + partId + ".png"));
|
||||
const stream = canvas.createPNGStream();
|
||||
stream.pipe(out);
|
||||
promises.push(new Promise(resolve => stream.on("end", resolve)));
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Waiting for completion");
|
||||
await Promise.all(promises);
|
||||
|
||||
console.log("Done!");
|
||||
}
|
||||
|
||||
run();
|
||||
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 8.3 KiB After Width: | Height: | Size: 8.3 KiB |
After Width: | Height: | Size: 7.3 KiB |
Before Width: | Height: | Size: 7.4 KiB After Width: | Height: | Size: 7.4 KiB |
Before Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 957 B |
Before Width: | Height: | Size: 854 B |
Before Width: | Height: | Size: 676 B |
Before Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 9.5 KiB After Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 7.6 KiB |
After Width: | Height: | Size: 6.8 KiB |
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 7.1 KiB |
Before Width: | Height: | Size: 695 B |
Before Width: | Height: | Size: 503 B |
Before Width: | Height: | Size: 498 B |
Before Width: | Height: | Size: 646 B |
Before Width: | Height: | Size: 4.5 KiB |
@ -1,110 +1,92 @@
|
||||
# Requirements: numpy, scipy, Pillow,
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import numpy as np
|
||||
from scipy import ndimage
|
||||
from PIL import Image, ImageFilter, ImageChops
|
||||
import math
|
||||
from os import listdir
|
||||
from os.path import isdir, isfile
|
||||
|
||||
roberts_cross_v = np.array([[0, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, -1]])
|
||||
|
||||
roberts_cross_h = np.array([[0, 0, 0],
|
||||
[0, 0, 1],
|
||||
[0, -1, 0]])
|
||||
|
||||
|
||||
def rgb2gray(rgb):
|
||||
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
|
||||
|
||||
|
||||
|
||||
|
||||
def save_image(data, outfilename, src_image):
|
||||
img = Image.fromarray(np.asarray(
|
||||
np.clip(data, 0, 255), dtype="uint8"), "L")
|
||||
dest = Image.new("RGBA", (img.width, img.height))
|
||||
src = img.load()
|
||||
dst = dest.load()
|
||||
|
||||
realSrc = src_image.load()
|
||||
mask = src_image.filter(ImageFilter.GaussianBlur(10)).load()
|
||||
orig = src_image.load()
|
||||
|
||||
|
||||
isWire = "wire" in outfilename
|
||||
|
||||
targetR = 104
|
||||
targetG = 200
|
||||
targetB = 255
|
||||
|
||||
if isWire:
|
||||
targetR = 255
|
||||
targetG = 104
|
||||
targetB = 232
|
||||
|
||||
for x in range(img.width):
|
||||
for y in range(img.height):
|
||||
realpixl = realSrc[x, y]
|
||||
greyval = float(src[x, y])
|
||||
greyval = min(255.0, greyval)
|
||||
greyval = math.pow(
|
||||
min(1, float(greyval / 255.0 * 1)), 1.5) * 255.0 * 1
|
||||
greyval = max(0, greyval)
|
||||
alpha = mask[x, y][3] / 255.0 * 1
|
||||
|
||||
edgeFactor = src[x, y] / 255.0
|
||||
noEdge = 1 - edgeFactor
|
||||
|
||||
shadow = min(1, 1 - realpixl[3] / 255.0 - edgeFactor)
|
||||
noShadow = 1 - shadow
|
||||
|
||||
dst[x, y] = (
|
||||
min(255, int((realpixl[0] / 255.0 * 0.4 + 0.6) * targetR * 1.1)),
|
||||
min(255, int((realpixl[1] / 255.0 * 0.4 + 0.6) * targetG * 1.1)),
|
||||
min(255, int((realpixl[2] / 255.0 * 0.4 + 0.6) * targetB * 1.1)),
|
||||
min(255, int(float(realpixl[3]) * (0.6 + 5 * edgeFactor))))
|
||||
|
||||
|
||||
dest.save(outfilename)
|
||||
|
||||
|
||||
def roberts_cross(infilename, outfilename):
|
||||
print("Processing", infilename)
|
||||
img = Image.open(infilename)
|
||||
img.load()
|
||||
img = img.filter(ImageFilter.GaussianBlur(0.5))
|
||||
|
||||
image = rgb2gray(np.asarray(img, dtype="int32"))
|
||||
vertical = ndimage.convolve(image, roberts_cross_v)
|
||||
horizontal = ndimage.convolve(image, roberts_cross_h)
|
||||
output_image = np.sqrt(np.square(horizontal) + np.square(vertical))
|
||||
save_image(output_image, outfilename, img)
|
||||
|
||||
|
||||
def generateUiPreview(srcPath, buildingId):
|
||||
print(srcPath, buildingId)
|
||||
img = Image.open(srcPath)
|
||||
img.load()
|
||||
img.thumbnail((110, 110), Image.ANTIALIAS)
|
||||
img.save("../res/ui/hud/building_previews/" + buildingId + ".png")
|
||||
|
||||
img = img.convert("LA")
|
||||
|
||||
data = img.load()
|
||||
for x in range(img.width):
|
||||
for y in range(img.height):
|
||||
data[x, y] = (data[x, y][0], int(data[x, y][1] * 0.5))
|
||||
|
||||
img.save("../res/ui/hud/building_previews/" + buildingId + "_disabled.png")
|
||||
|
||||
|
||||
buildings = listdir("buildings")
|
||||
|
||||
for buildingId in buildings:
|
||||
if "hub" in buildingId:
|
||||
continue
|
||||
roberts_cross("buildings/" + buildingId + "", "blueprints/" + buildingId + "")
|
||||
# Requirements: numpy, scipy, Pillow,
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import numpy as np
|
||||
from scipy import ndimage
|
||||
from PIL import Image, ImageFilter, ImageChops
|
||||
import math
|
||||
from os import listdir
|
||||
from os.path import isdir, isfile
|
||||
|
||||
generate_blueprint_sprite_v = np.array([[0, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, -1]])
|
||||
|
||||
generate_blueprint_sprite_h = np.array([[0, 0, 0],
|
||||
[0, 0, 1],
|
||||
[0, -1, 0]])
|
||||
|
||||
|
||||
def rgb2gray(rgb):
|
||||
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
|
||||
|
||||
def process_image(data, outfilename, src_image):
|
||||
img = Image.fromarray(np.asarray(
|
||||
np.clip(data, 0, 255), dtype="uint8"), "L")
|
||||
dest = Image.new("RGBA", (img.width, img.height))
|
||||
src = img.load()
|
||||
dst = dest.load()
|
||||
|
||||
realSrc = src_image.load()
|
||||
mask = src_image.filter(ImageFilter.GaussianBlur(10)).load()
|
||||
orig = src_image.load()
|
||||
|
||||
# isWire = "wire" in outfilename
|
||||
isWire = False
|
||||
|
||||
targetR = 104
|
||||
targetG = 200
|
||||
targetB = 255
|
||||
|
||||
if isWire:
|
||||
targetR = 255
|
||||
targetG = 104
|
||||
targetB = 232
|
||||
|
||||
for x in range(img.width):
|
||||
for y in range(img.height):
|
||||
realpixl = realSrc[x, y]
|
||||
greyval = float(src[x, y])
|
||||
greyval = min(255.0, greyval)
|
||||
greyval = math.pow(
|
||||
min(1, float(greyval / 255.0 * 1)), 1.5) * 255.0 * 1
|
||||
greyval = max(0, greyval)
|
||||
alpha = mask[x, y][3] / 255.0 * 1
|
||||
|
||||
edgeFactor = src[x, y] / 255.0
|
||||
noEdge = 1 - edgeFactor
|
||||
|
||||
shadow = min(1, 1 - realpixl[3] / 255.0 - edgeFactor)
|
||||
noShadow = 1 - shadow
|
||||
|
||||
dst[x, y] = (
|
||||
min(255, int((realpixl[0] / 255.0 * 0.4 + 0.6) * targetR * 1.1)),
|
||||
min(255, int((realpixl[1] / 255.0 * 0.4 + 0.6) * targetG * 1.1)),
|
||||
min(255, int((realpixl[2] / 255.0 * 0.4 + 0.6) * targetB * 1.1)),
|
||||
min(255, int(float(realpixl[3]) * (0.6 + 5 * edgeFactor))))
|
||||
|
||||
|
||||
dest.save(outfilename)
|
||||
|
||||
|
||||
def generate_blueprint_sprite(infilename, outfilename):
|
||||
print("Processing", infilename)
|
||||
img = Image.open(infilename)
|
||||
img.load()
|
||||
img = img.filter(ImageFilter.GaussianBlur(0.5))
|
||||
|
||||
image = rgb2gray(np.asarray(img, dtype="int32"))
|
||||
vertical = ndimage.convolve(image, generate_blueprint_sprite_v)
|
||||
horizontal = ndimage.convolve(image, generate_blueprint_sprite_h)
|
||||
output_image = np.sqrt(np.square(horizontal) + np.square(vertical))
|
||||
process_image(output_image, outfilename, img)
|
||||
|
||||
|
||||
buildings = listdir("buildings")
|
||||
|
||||
for buildingId in buildings:
|
||||
if "hub" in buildingId:
|
||||
continue
|
||||
if "wire-" in buildingId:
|
||||
continue
|
||||
generate_blueprint_sprite("buildings/" + buildingId + "", "blueprints/" + buildingId + "")
|
||||
|
Before Width: | Height: | Size: 701 B |
Before Width: | Height: | Size: 649 B |
Before Width: | Height: | Size: 502 B |
Before Width: | Height: | Size: 501 B |
Before Width: | Height: | Size: 690 B After Width: | Height: | Size: 703 B |
Before Width: | Height: | Size: 642 B After Width: | Height: | Size: 647 B |
Before Width: | Height: | Size: 501 B After Width: | Height: | Size: 520 B |
Before Width: | Height: | Size: 496 B After Width: | Height: | Size: 513 B |
After Width: | Height: | Size: 707 B |
After Width: | Height: | Size: 649 B |
After Width: | Height: | Size: 526 B |
After Width: | Height: | Size: 513 B |
Before Width: | Height: | Size: 695 B |
Before Width: | Height: | Size: 646 B |
Before Width: | Height: | Size: 503 B |
Before Width: | Height: | Size: 498 B |
After Width: | Height: | Size: 705 B |
Before Width: | Height: | Size: 648 B After Width: | Height: | Size: 651 B |
After Width: | Height: | Size: 525 B |
After Width: | Height: | Size: 516 B |
Before Width: | Height: | Size: 699 B |
Before Width: | Height: | Size: 501 B |
Before Width: | Height: | Size: 500 B |
After Width: | Height: | Size: 706 B |
After Width: | Height: | Size: 651 B |
After Width: | Height: | Size: 527 B |
After Width: | Height: | Size: 515 B |
@ -0,0 +1,79 @@
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
||||
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
|
||||
const overlayMatrix = generateMatrixRotations([1, 1, 0, 1, 1, 1, 0, 1, 0]);
|
||||
|
||||
export class MetaAnalyzerBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("analyzer");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#3a52bc";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getRenderPins() {
|
||||
// We already have it included
|
||||
return false;
|
||||
}
|
||||
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant) {
|
||||
return overlayMatrix[rotation];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(
|
||||
new WiredPinsComponent({
|
||||
slots: [
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.right,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.bottom,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
entity.addComponent(
|
||||
new LogicGateComponent({
|
||||
type: enumLogicGateType.analyzer,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
||||
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
|
||||
export class MetaComparatorBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("comparator");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#823cab";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getRenderPins() {
|
||||
// We already have it included
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(
|
||||
new WiredPinsComponent({
|
||||
slots: [
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.right,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
entity.addComponent(
|
||||
new LogicGateComponent({
|
||||
type: enumLogicGateType.compare,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@ -1,155 +1,151 @@
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding, defaultBuildingVariant } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumLogicGateVariants = {
|
||||
not: "not",
|
||||
xor: "xor",
|
||||
or: "or",
|
||||
transistor: "transistor",
|
||||
};
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumVariantToGate = {
|
||||
[defaultBuildingVariant]: enumLogicGateType.and,
|
||||
[enumLogicGateVariants.not]: enumLogicGateType.not,
|
||||
[enumLogicGateVariants.xor]: enumLogicGateType.xor,
|
||||
[enumLogicGateVariants.or]: enumLogicGateType.or,
|
||||
[enumLogicGateVariants.transistor]: enumLogicGateType.transistor,
|
||||
};
|
||||
|
||||
export class MetaLogicGateBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("logic_gate");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#89dc60";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getAvailableVariants() {
|
||||
return [
|
||||
defaultBuildingVariant,
|
||||
enumLogicGateVariants.not,
|
||||
enumLogicGateVariants.xor,
|
||||
enumLogicGateVariants.or,
|
||||
enumLogicGateVariants.transistor,
|
||||
];
|
||||
}
|
||||
|
||||
getRenderPins() {
|
||||
// We already have it included
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant, variant) {
|
||||
const gateType = enumVariantToGate[variant];
|
||||
entity.components.LogicGate.type = gateType;
|
||||
|
||||
const pinComp = entity.components.WiredPins;
|
||||
|
||||
switch (gateType) {
|
||||
case enumLogicGateType.and:
|
||||
case enumLogicGateType.xor:
|
||||
case enumLogicGateType.or: {
|
||||
pinComp.setSlots([
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.right,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
]);
|
||||
break;
|
||||
}
|
||||
case enumLogicGateType.transistor: {
|
||||
pinComp.setSlots([
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.bottom,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
case enumLogicGateType.not: {
|
||||
pinComp.setSlots([
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.bottom,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assertAlways("unknown logic gate type: " + gateType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(
|
||||
new WiredPinsComponent({
|
||||
slots: [],
|
||||
})
|
||||
);
|
||||
|
||||
entity.addComponent(new LogicGateComponent({}));
|
||||
}
|
||||
}
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding, defaultBuildingVariant } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumLogicGateVariants = {
|
||||
not: "not",
|
||||
xor: "xor",
|
||||
or: "or",
|
||||
};
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumVariantToGate = {
|
||||
[defaultBuildingVariant]: enumLogicGateType.and,
|
||||
[enumLogicGateVariants.not]: enumLogicGateType.not,
|
||||
[enumLogicGateVariants.xor]: enumLogicGateType.xor,
|
||||
[enumLogicGateVariants.or]: enumLogicGateType.or,
|
||||
};
|
||||
|
||||
const overlayMatrices = {
|
||||
[defaultBuildingVariant]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 1]),
|
||||
[enumLogicGateVariants.xor]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 1]),
|
||||
[enumLogicGateVariants.or]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 1]),
|
||||
[enumLogicGateVariants.not]: generateMatrixRotations([0, 1, 0, 0, 1, 0, 0, 1, 0]),
|
||||
};
|
||||
|
||||
const colors = {
|
||||
[defaultBuildingVariant]: "#f48d41",
|
||||
[enumLogicGateVariants.xor]: "#f4a241",
|
||||
[enumLogicGateVariants.or]: "#f4d041",
|
||||
[enumLogicGateVariants.not]: "#f44184",
|
||||
};
|
||||
|
||||
export class MetaLogicGateBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("logic_gate");
|
||||
}
|
||||
|
||||
getSilhouetteColor(variant) {
|
||||
return colors[variant];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant) {
|
||||
return overlayMatrices[variant][rotation];
|
||||
}
|
||||
|
||||
getAvailableVariants() {
|
||||
return [
|
||||
defaultBuildingVariant,
|
||||
enumLogicGateVariants.or,
|
||||
enumLogicGateVariants.not,
|
||||
enumLogicGateVariants.xor,
|
||||
];
|
||||
}
|
||||
|
||||
getRenderPins() {
|
||||
// We already have it included
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant, variant) {
|
||||
const gateType = enumVariantToGate[variant];
|
||||
entity.components.LogicGate.type = gateType;
|
||||
|
||||
const pinComp = entity.components.WiredPins;
|
||||
|
||||
switch (gateType) {
|
||||
case enumLogicGateType.and:
|
||||
case enumLogicGateType.xor:
|
||||
case enumLogicGateType.or: {
|
||||
pinComp.setSlots([
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.right,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
case enumLogicGateType.not: {
|
||||
pinComp.setSlots([
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.bottom,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assertAlways("unknown logic gate type: " + gateType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(
|
||||
new WiredPinsComponent({
|
||||
slots: [],
|
||||
})
|
||||
);
|
||||
|
||||
entity.addComponent(new LogicGateComponent({}));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,101 @@
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
||||
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
||||
import { Entity } from "../entity";
|
||||
import { defaultBuildingVariant, MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumTransistorVariants = {
|
||||
mirrored: "mirrored",
|
||||
};
|
||||
|
||||
const overlayMatrices = {
|
||||
[defaultBuildingVariant]: generateMatrixRotations([0, 1, 0, 1, 1, 0, 0, 1, 0]),
|
||||
[enumTransistorVariants.mirrored]: generateMatrixRotations([0, 1, 0, 0, 1, 1, 0, 1, 0]),
|
||||
};
|
||||
|
||||
export class MetaTransistorBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("transistor");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#bc3a61";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getAvailableVariants() {
|
||||
return [defaultBuildingVariant, enumTransistorVariants.mirrored];
|
||||
}
|
||||
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant) {
|
||||
return overlayMatrices[variant][rotation];
|
||||
}
|
||||
|
||||
getRenderPins() {
|
||||
// We already have it included
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant, variant) {
|
||||
entity.components.WiredPins.slots[1].direction =
|
||||
variant === enumTransistorVariants.mirrored ? enumDirection.right : enumDirection.left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(
|
||||
new WiredPinsComponent({
|
||||
slots: [
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.top,
|
||||
type: enumPinSlotType.logicalEjector,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.left,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
{
|
||||
pos: new Vector(0, 0),
|
||||
direction: enumDirection.bottom,
|
||||
type: enumPinSlotType.logicalAcceptor,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
entity.addComponent(
|
||||
new LogicGateComponent({
|
||||
type: enumLogicGateType.transistor,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@ -1,263 +1,272 @@
|
||||
import { Loader } from "../../core/loader";
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
import { enumDirection, enumDirectionToAngle, enumDirectionToVector, Vector } from "../../core/vector";
|
||||
import { SOUNDS } from "../../platform/sound";
|
||||
import { enumWireType, WireComponent } from "../components/wire";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
|
||||
export const arrayWireRotationVariantToType = [
|
||||
enumWireType.regular,
|
||||
enumWireType.turn,
|
||||
enumWireType.split,
|
||||
enumWireType.cross,
|
||||
];
|
||||
|
||||
export const wireOverlayMatrices = {
|
||||
[enumWireType.regular]: generateMatrixRotations([0, 1, 0, 0, 1, 0, 0, 1, 0]),
|
||||
[enumWireType.split]: generateMatrixRotations([0, 0, 0, 1, 1, 1, 0, 1, 0]),
|
||||
[enumWireType.turn]: generateMatrixRotations([0, 0, 0, 0, 1, 1, 0, 1, 0]),
|
||||
[enumWireType.cross]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 0]),
|
||||
};
|
||||
|
||||
export class MetaWireBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("wire");
|
||||
}
|
||||
|
||||
getHasDirectionLockAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#25fff2";
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getStayInPlacementMode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
getPlacementSound() {
|
||||
return SOUNDS.placeBelt;
|
||||
}
|
||||
|
||||
getRotateAutomaticallyWhilePlacing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getSprite() {
|
||||
return null;
|
||||
}
|
||||
|
||||
getIsReplaceable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
// @todo
|
||||
entity.addComponent(new WireComponent({}));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant) {
|
||||
entity.components.Wire.type = arrayWireRotationVariantToType[rotationVariant];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} rotation
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant, entity) {
|
||||
return wireOverlayMatrices[entity.components.Wire.type][rotation];
|
||||
}
|
||||
|
||||
getPreviewSprite(rotationVariant) {
|
||||
switch (arrayWireRotationVariantToType[rotationVariant]) {
|
||||
case enumWireType.regular: {
|
||||
return Loader.getSprite("sprites/buildings/wire.png");
|
||||
}
|
||||
case enumWireType.turn: {
|
||||
return Loader.getSprite("sprites/buildings/wire-turn.png");
|
||||
}
|
||||
case enumWireType.split: {
|
||||
return Loader.getSprite("sprites/buildings/wire-split.png");
|
||||
}
|
||||
case enumWireType.cross: {
|
||||
return Loader.getSprite("sprites/buildings/wire-cross.png");
|
||||
}
|
||||
default: {
|
||||
assertAlways(false, "Invalid wire rotation variant");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getBlueprintSprite(rotationVariant) {
|
||||
switch (arrayWireRotationVariantToType[rotationVariant]) {
|
||||
case enumWireType.regular: {
|
||||
return Loader.getSprite("sprites/blueprints/wire.png");
|
||||
}
|
||||
case enumWireType.turn: {
|
||||
return Loader.getSprite("sprites/blueprints/wire-turn.png");
|
||||
}
|
||||
case enumWireType.split: {
|
||||
return Loader.getSprite("sprites/blueprints/wire-split.png");
|
||||
}
|
||||
case enumWireType.cross: {
|
||||
return Loader.getSprite("sprites/blueprints/wire-cross.png");
|
||||
}
|
||||
default: {
|
||||
assertAlways(false, "Invalid wire rotation variant");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should compute the optimal rotation variant on the given tile
|
||||
* @param {object} param0
|
||||
* @param {GameRoot} param0.root
|
||||
* @param {Vector} param0.tile
|
||||
* @param {number} param0.rotation
|
||||
* @param {string} param0.variant
|
||||
* @param {string} param0.layer
|
||||
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
|
||||
*/
|
||||
computeOptimalDirectionAndRotationVariantAtTile({ root, tile, rotation, variant, layer }) {
|
||||
const connections = {
|
||||
top: root.logic.computeWireEdgeStatus({ tile, rotation, edge: enumDirection.top }),
|
||||
right: root.logic.computeWireEdgeStatus({ tile, rotation, edge: enumDirection.right }),
|
||||
bottom: root.logic.computeWireEdgeStatus({ tile, rotation, edge: enumDirection.bottom }),
|
||||
left: root.logic.computeWireEdgeStatus({ tile, rotation, edge: enumDirection.left }),
|
||||
};
|
||||
|
||||
let flag = 0;
|
||||
flag |= connections.top ? 0x1000 : 0;
|
||||
flag |= connections.right ? 0x100 : 0;
|
||||
flag |= connections.bottom ? 0x10 : 0;
|
||||
flag |= connections.left ? 0x1 : 0;
|
||||
|
||||
let targetType = enumWireType.regular;
|
||||
|
||||
// First, reset rotation
|
||||
rotation = 0;
|
||||
|
||||
switch (flag) {
|
||||
case 0x0000:
|
||||
// Nothing
|
||||
break;
|
||||
|
||||
case 0x0001:
|
||||
// Left
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0010:
|
||||
// Bottom
|
||||
// END
|
||||
break;
|
||||
|
||||
case 0x0011:
|
||||
// Bottom | Left
|
||||
targetType = enumWireType.turn;
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0100:
|
||||
// Right
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0101:
|
||||
// Right | Left
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0110:
|
||||
// Right | Bottom
|
||||
targetType = enumWireType.turn;
|
||||
break;
|
||||
|
||||
case 0x0111:
|
||||
// Right | Bottom | Left
|
||||
targetType = enumWireType.split;
|
||||
break;
|
||||
|
||||
case 0x1000:
|
||||
// Top
|
||||
break;
|
||||
|
||||
case 0x1001:
|
||||
// Top | Left
|
||||
targetType = enumWireType.turn;
|
||||
rotation += 180;
|
||||
break;
|
||||
|
||||
case 0x1010:
|
||||
// Top | Bottom
|
||||
break;
|
||||
|
||||
case 0x1011:
|
||||
// Top | Bottom | Left
|
||||
targetType = enumWireType.split;
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x1100:
|
||||
// Top | Right
|
||||
targetType = enumWireType.turn;
|
||||
rotation -= 90;
|
||||
break;
|
||||
|
||||
case 0x1101:
|
||||
// Top | Right | Left
|
||||
targetType = enumWireType.split;
|
||||
rotation += 180;
|
||||
break;
|
||||
|
||||
case 0x1110:
|
||||
// Top | Right | Bottom
|
||||
targetType = enumWireType.split;
|
||||
rotation -= 90;
|
||||
break;
|
||||
|
||||
case 0x1111:
|
||||
// Top | Right | Bottom | Left
|
||||
targetType = enumWireType.cross;
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
// Clamp rotation
|
||||
rotation: (rotation + 360 * 10) % 360,
|
||||
rotationVariant: arrayWireRotationVariantToType.indexOf(targetType),
|
||||
};
|
||||
}
|
||||
}
|
||||
import { Loader } from "../../core/loader";
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
import { SOUNDS } from "../../platform/sound";
|
||||
import { enumWireType, enumWireVariant, WireComponent } from "../components/wire";
|
||||
import { Entity } from "../entity";
|
||||
import { defaultBuildingVariant, MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
|
||||
export const arrayWireRotationVariantToType = [
|
||||
enumWireType.forward,
|
||||
enumWireType.turn,
|
||||
enumWireType.split,
|
||||
enumWireType.cross,
|
||||
];
|
||||
|
||||
export const wireOverlayMatrices = {
|
||||
[enumWireType.forward]: generateMatrixRotations([0, 1, 0, 0, 1, 0, 0, 1, 0]),
|
||||
[enumWireType.split]: generateMatrixRotations([0, 0, 0, 1, 1, 1, 0, 1, 0]),
|
||||
[enumWireType.turn]: generateMatrixRotations([0, 0, 0, 0, 1, 1, 0, 1, 0]),
|
||||
[enumWireType.cross]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 0]),
|
||||
};
|
||||
|
||||
/** @enum {string} */
|
||||
export const wireVariants = {
|
||||
second: "second",
|
||||
third: "third",
|
||||
};
|
||||
|
||||
const enumWireVariantToVariant = {
|
||||
[defaultBuildingVariant]: enumWireVariant.first,
|
||||
[wireVariants.second]: enumWireVariant.second,
|
||||
[wireVariants.third]: enumWireVariant.third,
|
||||
};
|
||||
|
||||
export class MetaWireBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("wire");
|
||||
}
|
||||
|
||||
getHasDirectionLockAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#61ef6f";
|
||||
}
|
||||
|
||||
getAvailableVariants() {
|
||||
return [defaultBuildingVariant, wireVariants.second, wireVariants.third];
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getStayInPlacementMode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
getPlacementSound() {
|
||||
return SOUNDS.placeBelt;
|
||||
}
|
||||
|
||||
getRotateAutomaticallyWhilePlacing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getSprite() {
|
||||
return null;
|
||||
}
|
||||
|
||||
getIsReplaceable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(new WireComponent({}));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant, variant) {
|
||||
entity.components.Wire.type = arrayWireRotationVariantToType[rotationVariant];
|
||||
entity.components.Wire.variant = enumWireVariantToVariant[variant];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} rotation
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant, entity) {
|
||||
return wireOverlayMatrices[entity.components.Wire.type][rotation];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
* @returns {import("../../core/draw_utils").AtlasSprite}
|
||||
*/
|
||||
getPreviewSprite(rotationVariant, variant) {
|
||||
const wireVariant = enumWireVariantToVariant[variant];
|
||||
switch (arrayWireRotationVariantToType[rotationVariant]) {
|
||||
case enumWireType.forward: {
|
||||
return Loader.getSprite("sprites/wires/sets/" + wireVariant + "_forward.png");
|
||||
}
|
||||
case enumWireType.turn: {
|
||||
return Loader.getSprite("sprites/wires/sets/" + wireVariant + "_turn.png");
|
||||
}
|
||||
case enumWireType.split: {
|
||||
return Loader.getSprite("sprites/wires/sets/" + wireVariant + "_split.png");
|
||||
}
|
||||
case enumWireType.cross: {
|
||||
return Loader.getSprite("sprites/wires/sets/" + wireVariant + "_cross.png");
|
||||
}
|
||||
default: {
|
||||
assertAlways(false, "Invalid wire rotation variant");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getBlueprintSprite(rotationVariant, variant) {
|
||||
return this.getPreviewSprite(rotationVariant, variant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should compute the optimal rotation variant on the given tile
|
||||
* @param {object} param0
|
||||
* @param {GameRoot} param0.root
|
||||
* @param {Vector} param0.tile
|
||||
* @param {number} param0.rotation
|
||||
* @param {string} param0.variant
|
||||
* @param {string} param0.layer
|
||||
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
|
||||
*/
|
||||
computeOptimalDirectionAndRotationVariantAtTile({ root, tile, rotation, variant, layer }) {
|
||||
const wireVariant = enumWireVariantToVariant[variant];
|
||||
const connections = {
|
||||
top: root.logic.computeWireEdgeStatus({ tile, wireVariant, edge: enumDirection.top }),
|
||||
right: root.logic.computeWireEdgeStatus({ tile, wireVariant, edge: enumDirection.right }),
|
||||
bottom: root.logic.computeWireEdgeStatus({ tile, wireVariant, edge: enumDirection.bottom }),
|
||||
left: root.logic.computeWireEdgeStatus({ tile, wireVariant, edge: enumDirection.left }),
|
||||
};
|
||||
|
||||
let flag = 0;
|
||||
flag |= connections.top ? 0x1000 : 0;
|
||||
flag |= connections.right ? 0x100 : 0;
|
||||
flag |= connections.bottom ? 0x10 : 0;
|
||||
flag |= connections.left ? 0x1 : 0;
|
||||
|
||||
let targetType = enumWireType.forward;
|
||||
|
||||
// First, reset rotation
|
||||
rotation = 0;
|
||||
|
||||
switch (flag) {
|
||||
case 0x0000:
|
||||
// Nothing
|
||||
break;
|
||||
|
||||
case 0x0001:
|
||||
// Left
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0010:
|
||||
// Bottom
|
||||
// END
|
||||
break;
|
||||
|
||||
case 0x0011:
|
||||
// Bottom | Left
|
||||
targetType = enumWireType.turn;
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0100:
|
||||
// Right
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0101:
|
||||
// Right | Left
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x0110:
|
||||
// Right | Bottom
|
||||
targetType = enumWireType.turn;
|
||||
break;
|
||||
|
||||
case 0x0111:
|
||||
// Right | Bottom | Left
|
||||
targetType = enumWireType.split;
|
||||
break;
|
||||
|
||||
case 0x1000:
|
||||
// Top
|
||||
break;
|
||||
|
||||
case 0x1001:
|
||||
// Top | Left
|
||||
targetType = enumWireType.turn;
|
||||
rotation += 180;
|
||||
break;
|
||||
|
||||
case 0x1010:
|
||||
// Top | Bottom
|
||||
break;
|
||||
|
||||
case 0x1011:
|
||||
// Top | Bottom | Left
|
||||
targetType = enumWireType.split;
|
||||
rotation += 90;
|
||||
break;
|
||||
|
||||
case 0x1100:
|
||||
// Top | Right
|
||||
targetType = enumWireType.turn;
|
||||
rotation -= 90;
|
||||
break;
|
||||
|
||||
case 0x1101:
|
||||
// Top | Right | Left
|
||||
targetType = enumWireType.split;
|
||||
rotation += 180;
|
||||
break;
|
||||
|
||||
case 0x1110:
|
||||
// Top | Right | Bottom
|
||||
targetType = enumWireType.split;
|
||||
rotation -= 90;
|
||||
break;
|
||||
|
||||
case 0x1111:
|
||||
// Top | Right | Bottom | Left
|
||||
targetType = enumWireType.cross;
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
// Clamp rotation
|
||||
rotation: (rotation + 360 * 10) % 360,
|
||||
rotationVariant: arrayWireRotationVariantToType.indexOf(targetType),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +1,58 @@
|
||||
import { Vector } from "../../core/vector";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding, defaultBuildingVariant } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
import { WireTunnelComponent } from "../components/wire_tunnel";
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
|
||||
/** @enum {string} */
|
||||
export const enumWireTunnelVariants = {
|
||||
coating: "coating",
|
||||
};
|
||||
|
||||
const wireTunnelOverlayMatrices = {
|
||||
[defaultBuildingVariant]: generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 0]),
|
||||
[enumWireTunnelVariants.coating]: generateMatrixRotations([0, 1, 0, 0, 1, 0, 0, 1, 0]),
|
||||
};
|
||||
|
||||
export class MetaWireTunnelBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("wire_tunnel");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#777a86";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
// @todo
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} rotation
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant, entity) {
|
||||
return wireTunnelOverlayMatrices[variant][rotation];
|
||||
}
|
||||
|
||||
getIsRotateable(variant) {
|
||||
return variant !== defaultBuildingVariant;
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
getAvailableVariants() {
|
||||
return [defaultBuildingVariant, enumWireTunnelVariants.coating];
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
getRotateAutomaticallyWhilePlacing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
getStayInPlacementMode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(new WireTunnelComponent({}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Entity} entity
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
*/
|
||||
updateVariants(entity, rotationVariant, variant) {
|
||||
entity.components.WireTunnel.multipleDirections = variant === defaultBuildingVariant;
|
||||
}
|
||||
}
|
||||
import { generateMatrixRotations } from "../../core/utils";
|
||||
import { Vector } from "../../core/vector";
|
||||
import { WireTunnelComponent } from "../components/wire_tunnel";
|
||||
import { Entity } from "../entity";
|
||||
import { MetaBuilding } from "../meta_building";
|
||||
import { GameRoot } from "../root";
|
||||
import { enumHubGoalRewards } from "../tutorial_goals";
|
||||
|
||||
const wireTunnelOverlayMatrix = generateMatrixRotations([0, 1, 0, 1, 1, 1, 0, 1, 0]);
|
||||
|
||||
export class MetaWireTunnelBuilding extends MetaBuilding {
|
||||
constructor() {
|
||||
super("wire_tunnel");
|
||||
}
|
||||
|
||||
getSilhouetteColor() {
|
||||
return "#777a86";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {GameRoot} root
|
||||
*/
|
||||
getIsUnlocked(root) {
|
||||
return root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_wires_filters_and_levers);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} rotation
|
||||
* @param {number} rotationVariant
|
||||
* @param {string} variant
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant, entity) {
|
||||
return wireTunnelOverlayMatrix[rotation];
|
||||
}
|
||||
|
||||
getIsRotateable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
getDimensions() {
|
||||
return new Vector(1, 1);
|
||||
}
|
||||
|
||||
/** @returns {"wires"} **/
|
||||
getLayer() {
|
||||
return "wires";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity at the given location
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(new WireTunnelComponent({}));
|
||||
}
|
||||
}
|
||||
|