mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Initial commit
This commit is contained in:
90
src/js/game/items/color_item.js
Normal file
90
src/js/game/items/color_item.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import { DrawParameters } from "../../core/draw_parameters";
|
||||
import { createLogger } from "../../core/logging";
|
||||
import { extendSchema } from "../../savegame/serialization";
|
||||
import { BaseItem } from "../base_item";
|
||||
import { enumColorsToHexCode, enumColors } from "../colors";
|
||||
import { makeOffscreenBuffer } from "../../core/buffer_utils";
|
||||
import { globalConfig } from "../../core/config";
|
||||
import { round1Digit } from "../../core/utils";
|
||||
import { Math_max, Math_round } from "../../core/builtins";
|
||||
import { smoothenDpi } from "../../core/dpi_manager";
|
||||
|
||||
/** @enum {string} */
|
||||
const enumColorToMapBackground = {
|
||||
[enumColors.red]: "#ffbfc1",
|
||||
[enumColors.green]: "#cbffc4",
|
||||
[enumColors.blue]: "#bfdaff",
|
||||
};
|
||||
|
||||
export class ColorItem extends BaseItem {
|
||||
static getId() {
|
||||
return "color";
|
||||
}
|
||||
|
||||
static getSchema() {
|
||||
return extendSchema(BaseItem.getCachedSchema(), {
|
||||
// TODO
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} color
|
||||
*/
|
||||
constructor(color) {
|
||||
super();
|
||||
this.color = color;
|
||||
|
||||
this.bufferGenerator = this.internalGenerateColorBuffer.bind(this);
|
||||
}
|
||||
|
||||
getBackgroundColorAsResource() {
|
||||
return enumColorToMapBackground[this.color];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @param {number} size
|
||||
* @param {DrawParameters} parameters
|
||||
*/
|
||||
draw(x, y, parameters, size = 12) {
|
||||
const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel);
|
||||
|
||||
const key = size + "/" + dpi;
|
||||
const canvas = parameters.root.buffers.getForKey(
|
||||
key,
|
||||
this.color,
|
||||
size,
|
||||
size,
|
||||
dpi,
|
||||
this.bufferGenerator
|
||||
);
|
||||
parameters.context.drawImage(canvas, x - size / 2, y - size / 2, size, size);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {HTMLCanvasElement} canvas
|
||||
* @param {CanvasRenderingContext2D} context
|
||||
* @param {number} w
|
||||
* @param {number} h
|
||||
* @param {number} dpi
|
||||
*/
|
||||
internalGenerateColorBuffer(canvas, context, w, h, dpi) {
|
||||
context.translate((w * dpi) / 2, (h * dpi) / 2);
|
||||
context.scale((dpi * w) / 12, (dpi * h) / 12);
|
||||
|
||||
context.fillStyle = enumColorsToHexCode[this.color];
|
||||
context.strokeStyle = "rgba(100,102, 110, 1)";
|
||||
context.lineWidth = 2;
|
||||
context.beginCircle(2, -1, 3);
|
||||
context.stroke();
|
||||
context.fill();
|
||||
context.beginCircle(-2, -1, 3);
|
||||
context.stroke();
|
||||
context.fill();
|
||||
context.beginCircle(0, 2, 3);
|
||||
context.closePath();
|
||||
context.stroke();
|
||||
context.fill();
|
||||
}
|
||||
}
|
||||
42
src/js/game/items/shape_item.js
Normal file
42
src/js/game/items/shape_item.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import { BaseItem } from "../base_item";
|
||||
import { DrawParameters } from "../../core/draw_parameters";
|
||||
import { extendSchema } from "../../savegame/serialization";
|
||||
import { ShapeDefinition } from "../shape_definition";
|
||||
import { createLogger } from "../../core/logging";
|
||||
|
||||
const logger = createLogger("shape_item");
|
||||
|
||||
export class ShapeItem extends BaseItem {
|
||||
static getId() {
|
||||
return "shape";
|
||||
}
|
||||
|
||||
static getSchema() {
|
||||
return extendSchema(BaseItem.getCachedSchema(), {
|
||||
// TODO
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ShapeDefinition} definition
|
||||
*/
|
||||
constructor(definition) {
|
||||
super();
|
||||
// logger.log("New shape item for shape definition", definition.generateId(), "created");
|
||||
|
||||
/**
|
||||
* This property must not be modified on runtime, you have to clone the class in order to change the definition
|
||||
*/
|
||||
this.definition = definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @param {DrawParameters} parameters
|
||||
* @param {number=} size
|
||||
*/
|
||||
draw(x, y, parameters, size) {
|
||||
this.definition.draw(x, y, parameters, size);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user