Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 1.3 MiB |
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 281 KiB |
Before Width: | Height: | Size: 677 KiB After Width: | Height: | Size: 661 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 16 KiB |
@ -1,360 +1,393 @@
|
|||||||
import { DrawParameters } from "./draw_parameters";
|
import { DrawParameters } from "./draw_parameters";
|
||||||
import { Rectangle } from "./rectangle";
|
import { Rectangle } from "./rectangle";
|
||||||
import { round3Digits } from "./utils";
|
import { round3Digits } from "./utils";
|
||||||
|
|
||||||
const floorSpriteCoordinates = false;
|
export const ORIGINAL_SPRITE_SCALE = "0.75";
|
||||||
|
export const FULL_CLIP_RECT = new Rectangle(0, 0, 1, 1);
|
||||||
export const ORIGINAL_SPRITE_SCALE = "0.75";
|
|
||||||
|
export class BaseSprite {
|
||||||
export class BaseSprite {
|
/**
|
||||||
/**
|
* Returns the raw handle
|
||||||
* Returns the raw handle
|
* @returns {HTMLImageElement|HTMLCanvasElement}
|
||||||
* @returns {HTMLImageElement|HTMLCanvasElement}
|
*/
|
||||||
*/
|
getRawTexture() {
|
||||||
getRawTexture() {
|
abstract;
|
||||||
abstract;
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Draws the sprite
|
||||||
* Draws the sprite
|
* @param {CanvasRenderingContext2D} context
|
||||||
* @param {CanvasRenderingContext2D} context
|
* @param {number} x
|
||||||
* @param {number} x
|
* @param {number} y
|
||||||
* @param {number} y
|
* @param {number} w
|
||||||
* @param {number} w
|
* @param {number} h
|
||||||
* @param {number} h
|
*/
|
||||||
*/
|
draw(context, x, y, w, h) {
|
||||||
draw(context, x, y, w, h) {
|
// eslint-disable-line no-unused-vars
|
||||||
// eslint-disable-line no-unused-vars
|
abstract;
|
||||||
abstract;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Position of a sprite within an atlas
|
||||||
* Position of a sprite within an atlas
|
*/
|
||||||
*/
|
export class SpriteAtlasLink {
|
||||||
export class SpriteAtlasLink {
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {object} param0
|
||||||
* @param {object} param0
|
* @param {number} param0.packedX
|
||||||
* @param {number} param0.packedX
|
* @param {number} param0.packedY
|
||||||
* @param {number} param0.packedY
|
* @param {number} param0.packOffsetX
|
||||||
* @param {number} param0.packOffsetX
|
* @param {number} param0.packOffsetY
|
||||||
* @param {number} param0.packOffsetY
|
* @param {number} param0.packedW
|
||||||
* @param {number} param0.packedW
|
* @param {number} param0.packedH
|
||||||
* @param {number} param0.packedH
|
* @param {number} param0.w
|
||||||
* @param {number} param0.w
|
* @param {number} param0.h
|
||||||
* @param {number} param0.h
|
* @param {HTMLImageElement|HTMLCanvasElement} param0.atlas
|
||||||
* @param {HTMLImageElement|HTMLCanvasElement} param0.atlas
|
*/
|
||||||
*/
|
constructor({ w, h, packedX, packedY, packOffsetX, packOffsetY, packedW, packedH, atlas }) {
|
||||||
constructor({ w, h, packedX, packedY, packOffsetX, packOffsetY, packedW, packedH, atlas }) {
|
this.packedX = packedX;
|
||||||
this.packedX = packedX;
|
this.packedY = packedY;
|
||||||
this.packedY = packedY;
|
this.packedW = packedW;
|
||||||
this.packedW = packedW;
|
this.packedH = packedH;
|
||||||
this.packedH = packedH;
|
this.packOffsetX = packOffsetX;
|
||||||
this.packOffsetX = packOffsetX;
|
this.packOffsetY = packOffsetY;
|
||||||
this.packOffsetY = packOffsetY;
|
this.atlas = atlas;
|
||||||
this.atlas = atlas;
|
this.w = w;
|
||||||
this.w = w;
|
this.h = h;
|
||||||
this.h = h;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
export class AtlasSprite extends BaseSprite {
|
||||||
export class AtlasSprite extends BaseSprite {
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {string} spriteName
|
||||||
* @param {string} spriteName
|
*/
|
||||||
*/
|
constructor(spriteName = "sprite") {
|
||||||
constructor(spriteName = "sprite") {
|
super();
|
||||||
super();
|
/** @type {Object.<string, SpriteAtlasLink>} */
|
||||||
/** @type {Object.<string, SpriteAtlasLink>} */
|
this.linksByResolution = {};
|
||||||
this.linksByResolution = {};
|
this.spriteName = spriteName;
|
||||||
this.spriteName = spriteName;
|
}
|
||||||
}
|
|
||||||
|
getRawTexture() {
|
||||||
getRawTexture() {
|
return this.linksByResolution[ORIGINAL_SPRITE_SCALE].atlas;
|
||||||
return this.linksByResolution[ORIGINAL_SPRITE_SCALE].atlas;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Draws the sprite onto a regular context using no contexts
|
||||||
* Draws the sprite onto a regular context using no contexts
|
* @see {BaseSprite.draw}
|
||||||
* @see {BaseSprite.draw}
|
*/
|
||||||
*/
|
draw(context, x, y, w, h) {
|
||||||
draw(context, x, y, w, h) {
|
if (G_IS_DEV) {
|
||||||
if (G_IS_DEV) {
|
assert(context instanceof CanvasRenderingContext2D, "Not a valid context");
|
||||||
assert(context instanceof CanvasRenderingContext2D, "Not a valid context");
|
}
|
||||||
}
|
|
||||||
|
const link = this.linksByResolution[ORIGINAL_SPRITE_SCALE];
|
||||||
const link = this.linksByResolution[ORIGINAL_SPRITE_SCALE];
|
|
||||||
|
assert(
|
||||||
assert(
|
link,
|
||||||
link,
|
"Link not known: " +
|
||||||
"Link not known: " +
|
ORIGINAL_SPRITE_SCALE +
|
||||||
ORIGINAL_SPRITE_SCALE +
|
" (having " +
|
||||||
" (having " +
|
Object.keys(this.linksByResolution) +
|
||||||
Object.keys(this.linksByResolution) +
|
")"
|
||||||
")"
|
);
|
||||||
);
|
|
||||||
|
const width = w || link.w;
|
||||||
const width = w || link.w;
|
const height = h || link.h;
|
||||||
const height = h || link.h;
|
|
||||||
|
const scaleW = width / link.w;
|
||||||
const scaleW = width / link.w;
|
const scaleH = height / link.h;
|
||||||
const scaleH = height / link.h;
|
|
||||||
|
context.drawImage(
|
||||||
context.drawImage(
|
link.atlas,
|
||||||
link.atlas,
|
|
||||||
|
link.packedX,
|
||||||
link.packedX,
|
link.packedY,
|
||||||
link.packedY,
|
link.packedW,
|
||||||
link.packedW,
|
link.packedH,
|
||||||
link.packedH,
|
|
||||||
|
x + link.packOffsetX * scaleW,
|
||||||
x + link.packOffsetX * scaleW,
|
y + link.packOffsetY * scaleH,
|
||||||
y + link.packOffsetY * scaleH,
|
link.packedW * scaleW,
|
||||||
link.packedW * scaleW,
|
link.packedH * scaleH
|
||||||
link.packedH * scaleH
|
);
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {DrawParameters} parameters
|
||||||
* @param {DrawParameters} parameters
|
* @param {number} x
|
||||||
* @param {number} x
|
* @param {number} y
|
||||||
* @param {number} y
|
* @param {number} size
|
||||||
* @param {number} size
|
* @param {boolean=} clipping
|
||||||
* @param {boolean=} clipping
|
*/
|
||||||
*/
|
drawCachedCentered(parameters, x, y, size, clipping = true) {
|
||||||
drawCachedCentered(parameters, x, y, size, clipping = true) {
|
this.drawCached(parameters, x - size / 2, y - size / 2, size, size, clipping);
|
||||||
this.drawCached(parameters, x - size / 2, y - size / 2, size, size, clipping);
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {CanvasRenderingContext2D} context
|
||||||
* @param {CanvasRenderingContext2D} context
|
* @param {number} x
|
||||||
* @param {number} x
|
* @param {number} y
|
||||||
* @param {number} y
|
* @param {number} size
|
||||||
* @param {number} size
|
*/
|
||||||
*/
|
drawCentered(context, x, y, size) {
|
||||||
drawCentered(context, x, y, size) {
|
this.draw(context, x - size / 2, y - size / 2, size, size);
|
||||||
this.draw(context, x - size / 2, y - size / 2, size, size);
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Draws the sprite
|
||||||
* Draws the sprite
|
* @param {DrawParameters} parameters
|
||||||
* @param {DrawParameters} parameters
|
* @param {number} x
|
||||||
* @param {number} x
|
* @param {number} y
|
||||||
* @param {number} y
|
* @param {number} w
|
||||||
* @param {number} w
|
* @param {number} h
|
||||||
* @param {number} h
|
* @param {boolean=} clipping Whether to perform culling
|
||||||
* @param {boolean=} clipping Whether to perform culling
|
*/
|
||||||
*/
|
drawCached(parameters, x, y, w = null, h = null, clipping = true) {
|
||||||
drawCached(parameters, x, y, w = null, h = null, clipping = true) {
|
if (G_IS_DEV) {
|
||||||
if (G_IS_DEV) {
|
assert(parameters instanceof DrawParameters, "Not a valid context");
|
||||||
assert(parameters instanceof DrawParameters, "Not a valid context");
|
assert(!!w && w > 0, "Not a valid width:" + w);
|
||||||
assert(!!w && w > 0, "Not a valid width:" + w);
|
assert(!!h && h > 0, "Not a valid height:" + h);
|
||||||
assert(!!h && h > 0, "Not a valid height:" + h);
|
}
|
||||||
}
|
|
||||||
|
const visibleRect = parameters.visibleRect;
|
||||||
const visibleRect = parameters.visibleRect;
|
|
||||||
|
const scale = parameters.desiredAtlasScale;
|
||||||
const scale = parameters.desiredAtlasScale;
|
const link = this.linksByResolution[scale];
|
||||||
const link = this.linksByResolution[scale];
|
|
||||||
|
if (!link) {
|
||||||
if (!link) {
|
assert(false, `Link not known: ${scale} (having ${Object.keys(this.linksByResolution)})`);
|
||||||
assert(false, `Link not known: ${scale} (having ${Object.keys(this.linksByResolution)})`);
|
}
|
||||||
}
|
|
||||||
|
const scaleW = w / link.w;
|
||||||
const scaleW = w / link.w;
|
const scaleH = h / link.h;
|
||||||
const scaleH = h / link.h;
|
|
||||||
|
let destX = x + link.packOffsetX * scaleW;
|
||||||
let destX = x + link.packOffsetX * scaleW;
|
let destY = y + link.packOffsetY * scaleH;
|
||||||
let destY = y + link.packOffsetY * scaleH;
|
let destW = link.packedW * scaleW;
|
||||||
let destW = link.packedW * scaleW;
|
let destH = link.packedH * scaleH;
|
||||||
let destH = link.packedH * scaleH;
|
|
||||||
|
let srcX = link.packedX;
|
||||||
let srcX = link.packedX;
|
let srcY = link.packedY;
|
||||||
let srcY = link.packedY;
|
let srcW = link.packedW;
|
||||||
let srcW = link.packedW;
|
let srcH = link.packedH;
|
||||||
let srcH = link.packedH;
|
|
||||||
|
let intersection = null;
|
||||||
let intersection = null;
|
|
||||||
|
if (clipping) {
|
||||||
if (clipping) {
|
const rect = new Rectangle(destX, destY, destW, destH);
|
||||||
const rect = new Rectangle(destX, destY, destW, destH);
|
intersection = rect.getIntersection(visibleRect);
|
||||||
intersection = rect.getIntersection(visibleRect);
|
if (!intersection) {
|
||||||
if (!intersection) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
srcX += (intersection.x - destX) / scaleW;
|
||||||
srcX += (intersection.x - destX) / scaleW;
|
srcY += (intersection.y - destY) / scaleH;
|
||||||
srcY += (intersection.y - destY) / scaleH;
|
|
||||||
|
srcW *= intersection.w / destW;
|
||||||
srcW *= intersection.w / destW;
|
srcH *= intersection.h / destH;
|
||||||
srcH *= intersection.h / destH;
|
|
||||||
|
destX = intersection.x;
|
||||||
destX = intersection.x;
|
destY = intersection.y;
|
||||||
destY = intersection.y;
|
|
||||||
|
destW = intersection.w;
|
||||||
destW = intersection.w;
|
destH = intersection.h;
|
||||||
destH = intersection.h;
|
}
|
||||||
}
|
|
||||||
|
parameters.context.drawImage(
|
||||||
if (floorSpriteCoordinates) {
|
link.atlas,
|
||||||
parameters.context.drawImage(
|
|
||||||
link.atlas,
|
// atlas src pos
|
||||||
|
srcX,
|
||||||
// atlas src pos
|
srcY,
|
||||||
Math.floor(srcX),
|
|
||||||
Math.floor(srcY),
|
// atlas src siize
|
||||||
|
srcW,
|
||||||
// atlas src size
|
srcH,
|
||||||
Math.floor(srcW),
|
|
||||||
Math.floor(srcH),
|
// dest pos and size
|
||||||
|
destX,
|
||||||
// dest pos
|
destY,
|
||||||
Math.floor(destX),
|
destW,
|
||||||
Math.floor(destY),
|
destH
|
||||||
|
);
|
||||||
// dest size
|
}
|
||||||
Math.floor(destW),
|
|
||||||
Math.floor(destH)
|
/**
|
||||||
);
|
* Draws a subset of the sprite. Does NO culling
|
||||||
} else {
|
* @param {DrawParameters} parameters
|
||||||
parameters.context.drawImage(
|
* @param {number} x
|
||||||
link.atlas,
|
* @param {number} y
|
||||||
|
* @param {number} w
|
||||||
// atlas src pos
|
* @param {number} h
|
||||||
srcX,
|
* @param {Rectangle=} clipRect The rectangle in local space (0 ... 1) to draw of the image
|
||||||
srcY,
|
*/
|
||||||
|
drawCachedWithClipRect(parameters, x, y, w = null, h = null, clipRect = FULL_CLIP_RECT) {
|
||||||
// atlas src siize
|
if (G_IS_DEV) {
|
||||||
srcW,
|
assert(parameters instanceof DrawParameters, "Not a valid context");
|
||||||
srcH,
|
assert(!!w && w > 0, "Not a valid width:" + w);
|
||||||
|
assert(!!h && h > 0, "Not a valid height:" + h);
|
||||||
// dest pos and size
|
assert(clipRect, "No clip rect given!");
|
||||||
destX,
|
}
|
||||||
destY,
|
|
||||||
destW,
|
const scale = parameters.desiredAtlasScale;
|
||||||
destH
|
const link = this.linksByResolution[scale];
|
||||||
);
|
|
||||||
}
|
if (!link) {
|
||||||
}
|
assert(false, `Link not known: ${scale} (having ${Object.keys(this.linksByResolution)})`);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Renders into an html element
|
const scaleW = w / link.w;
|
||||||
* @param {HTMLElement} element
|
const scaleH = h / link.h;
|
||||||
* @param {number} w
|
|
||||||
* @param {number} h
|
let destX = x + link.packOffsetX * scaleW + clipRect.x * w;
|
||||||
*/
|
let destY = y + link.packOffsetY * scaleH + clipRect.y * h;
|
||||||
renderToHTMLElement(element, w = 1, h = 1) {
|
let destW = link.packedW * scaleW * clipRect.w;
|
||||||
element.style.position = "relative";
|
let destH = link.packedH * scaleH * clipRect.h;
|
||||||
element.innerHTML = this.getAsHTML(w, h);
|
|
||||||
}
|
let srcX = link.packedX + clipRect.x * link.packedW;
|
||||||
|
let srcY = link.packedY + clipRect.y * link.packedH;
|
||||||
/**
|
let srcW = link.packedW * clipRect.w;
|
||||||
* Returns the html to render as icon
|
let srcH = link.packedH * clipRect.h;
|
||||||
* @param {number} w
|
|
||||||
* @param {number} h
|
parameters.context.drawImage(
|
||||||
*/
|
link.atlas,
|
||||||
getAsHTML(w, h) {
|
|
||||||
const link = this.linksByResolution["0.5"];
|
// atlas src pos
|
||||||
|
srcX,
|
||||||
// Find out how much we have to scale it so that it fits
|
srcY,
|
||||||
const scaleX = w / link.w;
|
|
||||||
const scaleY = h / link.h;
|
// atlas src siize
|
||||||
|
srcW,
|
||||||
// Find out how big the scaled atlas is
|
srcH,
|
||||||
const atlasW = link.atlas.width * scaleX;
|
|
||||||
const atlasH = link.atlas.height * scaleY;
|
// dest pos and size
|
||||||
|
destX,
|
||||||
// @ts-ignore
|
destY,
|
||||||
const srcSafe = link.atlas.src.replaceAll("\\", "/");
|
destW,
|
||||||
|
destH
|
||||||
// Find out how big we render the sprite
|
);
|
||||||
const widthAbsolute = scaleX * link.packedW;
|
}
|
||||||
const heightAbsolute = scaleY * link.packedH;
|
|
||||||
|
/**
|
||||||
// Compute the position in the relative container
|
* Renders into an html element
|
||||||
const leftRelative = (link.packOffsetX * scaleX) / w;
|
* @param {HTMLElement} element
|
||||||
const topRelative = (link.packOffsetY * scaleY) / h;
|
* @param {number} w
|
||||||
const widthRelative = widthAbsolute / w;
|
* @param {number} h
|
||||||
const heightRelative = heightAbsolute / h;
|
*/
|
||||||
|
renderToHTMLElement(element, w = 1, h = 1) {
|
||||||
// Scale the atlas relative to the width and height of the element
|
element.style.position = "relative";
|
||||||
const bgW = atlasW / widthAbsolute;
|
element.innerHTML = this.getAsHTML(w, h);
|
||||||
const bgH = atlasH / heightAbsolute;
|
}
|
||||||
|
|
||||||
// Figure out what the position of the atlas is
|
/**
|
||||||
const bgX = link.packedX * scaleX;
|
* Returns the html to render as icon
|
||||||
const bgY = link.packedY * scaleY;
|
* @param {number} w
|
||||||
|
* @param {number} h
|
||||||
// Fuck you, whoever thought its a good idea to make background-position work like it does now
|
*/
|
||||||
const bgXRelative = -bgX / (widthAbsolute - atlasW);
|
getAsHTML(w, h) {
|
||||||
const bgYRelative = -bgY / (heightAbsolute - atlasH);
|
const link = this.linksByResolution["0.5"];
|
||||||
|
|
||||||
return `
|
// Find out how much we have to scale it so that it fits
|
||||||
<span class="spritesheetImage" style="
|
const scaleX = w / link.w;
|
||||||
background-image: url('${srcSafe}');
|
const scaleY = h / link.h;
|
||||||
left: ${round3Digits(leftRelative * 100.0)}%;
|
|
||||||
top: ${round3Digits(topRelative * 100.0)}%;
|
// Find out how big the scaled atlas is
|
||||||
width: ${round3Digits(widthRelative * 100.0)}%;
|
const atlasW = link.atlas.width * scaleX;
|
||||||
height: ${round3Digits(heightRelative * 100.0)}%;
|
const atlasH = link.atlas.height * scaleY;
|
||||||
background-repeat: repeat;
|
|
||||||
background-position: ${round3Digits(bgXRelative * 100.0)}% ${round3Digits(
|
// @ts-ignore
|
||||||
bgYRelative * 100.0
|
const srcSafe = link.atlas.src.replaceAll("\\", "/");
|
||||||
)}%;
|
|
||||||
background-size: ${round3Digits(bgW * 100.0)}% ${round3Digits(bgH * 100.0)}%;
|
// Find out how big we render the sprite
|
||||||
"></span>
|
const widthAbsolute = scaleX * link.packedW;
|
||||||
`;
|
const heightAbsolute = scaleY * link.packedH;
|
||||||
}
|
|
||||||
}
|
// Compute the position in the relative container
|
||||||
|
const leftRelative = (link.packOffsetX * scaleX) / w;
|
||||||
export class RegularSprite extends BaseSprite {
|
const topRelative = (link.packOffsetY * scaleY) / h;
|
||||||
constructor(sprite, w, h) {
|
const widthRelative = widthAbsolute / w;
|
||||||
super();
|
const heightRelative = heightAbsolute / h;
|
||||||
this.w = w;
|
|
||||||
this.h = h;
|
// Scale the atlas relative to the width and height of the element
|
||||||
this.sprite = sprite;
|
const bgW = atlasW / widthAbsolute;
|
||||||
}
|
const bgH = atlasH / heightAbsolute;
|
||||||
|
|
||||||
getRawTexture() {
|
// Figure out what the position of the atlas is
|
||||||
return this.sprite;
|
const bgX = link.packedX * scaleX;
|
||||||
}
|
const bgY = link.packedY * scaleY;
|
||||||
|
|
||||||
/**
|
// Fuck you, whoever thought its a good idea to make background-position work like it does now
|
||||||
* Draws the sprite, do *not* use this for sprites which are rendered! Only for drawing
|
const bgXRelative = -bgX / (widthAbsolute - atlasW);
|
||||||
* images into buffers
|
const bgYRelative = -bgY / (heightAbsolute - atlasH);
|
||||||
* @param {CanvasRenderingContext2D} context
|
|
||||||
* @param {number} x
|
return `
|
||||||
* @param {number} y
|
<span class="spritesheetImage" style="
|
||||||
* @param {number} w
|
background-image: url('${srcSafe}');
|
||||||
* @param {number} h
|
left: ${round3Digits(leftRelative * 100.0)}%;
|
||||||
*/
|
top: ${round3Digits(topRelative * 100.0)}%;
|
||||||
draw(context, x, y, w, h) {
|
width: ${round3Digits(widthRelative * 100.0)}%;
|
||||||
assert(context, "No context given");
|
height: ${round3Digits(heightRelative * 100.0)}%;
|
||||||
assert(x !== undefined, "No x given");
|
background-repeat: repeat;
|
||||||
assert(y !== undefined, "No y given");
|
background-position: ${round3Digits(bgXRelative * 100.0)}% ${round3Digits(
|
||||||
assert(w !== undefined, "No width given");
|
bgYRelative * 100.0
|
||||||
assert(h !== undefined, "No height given");
|
)}%;
|
||||||
context.drawImage(this.sprite, x, y, w, h);
|
background-size: ${round3Digits(bgW * 100.0)}% ${round3Digits(bgH * 100.0)}%;
|
||||||
}
|
"></span>
|
||||||
|
`;
|
||||||
/**
|
}
|
||||||
* Draws the sprite, do *not* use this for sprites which are rendered! Only for drawing
|
}
|
||||||
* images into buffers
|
|
||||||
* @param {CanvasRenderingContext2D} context
|
export class RegularSprite extends BaseSprite {
|
||||||
* @param {number} x
|
constructor(sprite, w, h) {
|
||||||
* @param {number} y
|
super();
|
||||||
* @param {number} w
|
this.w = w;
|
||||||
* @param {number} h
|
this.h = h;
|
||||||
*/
|
this.sprite = sprite;
|
||||||
drawCentered(context, x, y, w, h) {
|
}
|
||||||
assert(context, "No context given");
|
|
||||||
assert(x !== undefined, "No x given");
|
getRawTexture() {
|
||||||
assert(y !== undefined, "No y given");
|
return this.sprite;
|
||||||
assert(w !== undefined, "No width given");
|
}
|
||||||
assert(h !== undefined, "No height given");
|
|
||||||
context.drawImage(this.sprite, x - w / 2, y - h / 2, w, h);
|
/**
|
||||||
}
|
* Draws the sprite, do *not* use this for sprites which are rendered! Only for drawing
|
||||||
}
|
* images into buffers
|
||||||
|
* @param {CanvasRenderingContext2D} context
|
||||||
|
* @param {number} x
|
||||||
|
* @param {number} y
|
||||||
|
* @param {number} w
|
||||||
|
* @param {number} h
|
||||||
|
*/
|
||||||
|
draw(context, x, y, w, h) {
|
||||||
|
assert(context, "No context given");
|
||||||
|
assert(x !== undefined, "No x given");
|
||||||
|
assert(y !== undefined, "No y given");
|
||||||
|
assert(w !== undefined, "No width given");
|
||||||
|
assert(h !== undefined, "No height given");
|
||||||
|
context.drawImage(this.sprite, x, y, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the sprite, do *not* use this for sprites which are rendered! Only for drawing
|
||||||
|
* images into buffers
|
||||||
|
* @param {CanvasRenderingContext2D} context
|
||||||
|
* @param {number} x
|
||||||
|
* @param {number} y
|
||||||
|
* @param {number} w
|
||||||
|
* @param {number} h
|
||||||
|
*/
|
||||||
|
drawCentered(context, x, y, w, h) {
|
||||||
|
assert(context, "No context given");
|
||||||
|
assert(x !== undefined, "No x given");
|
||||||
|
assert(y !== undefined, "No y given");
|
||||||
|
assert(w !== undefined, "No width given");
|
||||||
|
assert(h !== undefined, "No height given");
|
||||||
|
context.drawImage(this.sprite, x - w / 2, y - h / 2, w, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,50 +1,86 @@
|
|||||||
import { createLogger } from "./logging";
|
import { Component } from "../game/component";
|
||||||
import { Rectangle } from "./rectangle";
|
import { Entity } from "../game/entity";
|
||||||
import { globalConfig } from "./config";
|
import { globalConfig } from "./config";
|
||||||
|
import { createLogger } from "./logging";
|
||||||
const logger = createLogger("stale_areas");
|
import { Rectangle } from "./rectangle";
|
||||||
|
|
||||||
export class StaleAreaDetector {
|
const logger = createLogger("stale_areas");
|
||||||
/**
|
|
||||||
*
|
export class StaleAreaDetector {
|
||||||
* @param {object} param0
|
/**
|
||||||
* @param {import("../game/root").GameRoot} param0.root
|
*
|
||||||
* @param {string} param0.name The name for reference
|
* @param {object} param0
|
||||||
* @param {(Rectangle) => void} param0.recomputeMethod Method which recomputes the given area
|
* @param {import("../game/root").GameRoot} param0.root
|
||||||
*/
|
* @param {string} param0.name The name for reference
|
||||||
constructor({ root, name, recomputeMethod }) {
|
* @param {(Rectangle) => void} param0.recomputeMethod Method which recomputes the given area
|
||||||
this.root = root;
|
*/
|
||||||
this.name = name;
|
constructor({ root, name, recomputeMethod }) {
|
||||||
this.recomputeMethod = recomputeMethod;
|
this.root = root;
|
||||||
|
this.name = name;
|
||||||
/** @type {Rectangle} */
|
this.recomputeMethod = recomputeMethod;
|
||||||
this.staleArea = null;
|
|
||||||
}
|
/** @type {Rectangle} */
|
||||||
|
this.staleArea = null;
|
||||||
/**
|
}
|
||||||
* Invalidates the given area
|
|
||||||
* @param {Rectangle} area
|
/**
|
||||||
*/
|
* Invalidates the given area
|
||||||
invalidate(area) {
|
* @param {Rectangle} area
|
||||||
// logger.log(this.name, "invalidated", area.toString());
|
*/
|
||||||
if (this.staleArea) {
|
invalidate(area) {
|
||||||
this.staleArea = this.staleArea.getUnion(area);
|
// logger.log(this.name, "invalidated", area.toString());
|
||||||
} else {
|
if (this.staleArea) {
|
||||||
this.staleArea = area.clone();
|
this.staleArea = this.staleArea.getUnion(area);
|
||||||
}
|
} else {
|
||||||
}
|
this.staleArea = area.clone();
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* Updates the stale area
|
|
||||||
*/
|
/**
|
||||||
update() {
|
* Makes this detector recompute the area of an entity whenever
|
||||||
if (this.staleArea) {
|
* it changes in any way
|
||||||
logger.log(this.name, "is recomputing", this.staleArea.toString());
|
* @param {Array<typeof Component>} components
|
||||||
if (G_IS_DEV && globalConfig.debug.renderChanges) {
|
* @param {number} tilesAround
|
||||||
this.root.hud.parts.changesDebugger.renderChange(this.name, this.staleArea, "#fd145b");
|
*/
|
||||||
}
|
recomputeOnComponentsChanged(components, tilesAround = 1) {
|
||||||
this.recomputeMethod(this.staleArea);
|
const componentIds = components.map(component => component.getId());
|
||||||
this.staleArea = null;
|
|
||||||
}
|
/**
|
||||||
}
|
* Internal checker method
|
||||||
}
|
* @param {Entity} entity
|
||||||
|
*/
|
||||||
|
const checker = entity => {
|
||||||
|
// Check for all components
|
||||||
|
for (let i = 0; i < componentIds.length; ++i) {
|
||||||
|
if (entity.components[componentIds[i]]) {
|
||||||
|
// Entity is relevant, compute affected area
|
||||||
|
const area = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections(
|
||||||
|
tilesAround
|
||||||
|
);
|
||||||
|
this.invalidate(area);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.root.signals.entityAdded.add(checker);
|
||||||
|
this.root.signals.entityChanged.add(checker);
|
||||||
|
this.root.signals.entityComponentRemoved.add(checker);
|
||||||
|
this.root.signals.entityGotNewComponent.add(checker);
|
||||||
|
this.root.signals.entityDestroyed.add(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the stale area
|
||||||
|
*/
|
||||||
|
update() {
|
||||||
|
if (this.staleArea) {
|
||||||
|
logger.log(this.name, "is recomputing", this.staleArea.toString());
|
||||||
|
if (G_IS_DEV && globalConfig.debug.renderChanges) {
|
||||||
|
this.root.hud.parts.changesDebugger.renderChange(this.name, this.staleArea, "#fd145b");
|
||||||
|
}
|
||||||
|
this.recomputeMethod(this.staleArea);
|
||||||
|
this.staleArea = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,33 +1,56 @@
|
|||||||
import { Component } from "../component";
|
import { enumDirection, Vector } from "../../core/vector";
|
||||||
import { types } from "../../savegame/serialization";
|
import { Component } from "../component";
|
||||||
import { enumDirection, Vector } from "../../core/vector";
|
|
||||||
|
/**
|
||||||
export class BeltUnderlaysComponent extends Component {
|
* Store which type an underlay is, this is cached so we can easily
|
||||||
static getId() {
|
* render it.
|
||||||
return "BeltUnderlays";
|
*
|
||||||
}
|
* Full: Render underlay at top and bottom of tile
|
||||||
|
* Bottom Only: Only render underlay at the bottom half
|
||||||
duplicateWithoutContents() {
|
* Top Only:
|
||||||
const beltUnderlaysCopy = [];
|
* @enum {string}
|
||||||
for (let i = 0; i < this.underlays.length; ++i) {
|
*/
|
||||||
const underlay = this.underlays[i];
|
export const enumClippedBeltUnderlayType = {
|
||||||
beltUnderlaysCopy.push({
|
full: "full",
|
||||||
pos: underlay.pos.copy(),
|
bottomOnly: "bottomOnly",
|
||||||
direction: underlay.direction,
|
topOnly: "topOnly",
|
||||||
});
|
none: "none",
|
||||||
}
|
};
|
||||||
|
|
||||||
return new BeltUnderlaysComponent({
|
/**
|
||||||
underlays: beltUnderlaysCopy,
|
* @typedef {{
|
||||||
});
|
* pos: Vector,
|
||||||
}
|
* direction: enumDirection,
|
||||||
|
* cachedType?: enumClippedBeltUnderlayType
|
||||||
/**
|
* }} BeltUnderlayTile
|
||||||
* @param {object} param0
|
*/
|
||||||
* @param {Array<{pos: Vector, direction: enumDirection}>=} param0.underlays Where to render belt underlays
|
|
||||||
*/
|
export class BeltUnderlaysComponent extends Component {
|
||||||
constructor({ underlays }) {
|
static getId() {
|
||||||
super();
|
return "BeltUnderlays";
|
||||||
this.underlays = underlays;
|
}
|
||||||
}
|
|
||||||
}
|
duplicateWithoutContents() {
|
||||||
|
const beltUnderlaysCopy = [];
|
||||||
|
for (let i = 0; i < this.underlays.length; ++i) {
|
||||||
|
const underlay = this.underlays[i];
|
||||||
|
beltUnderlaysCopy.push({
|
||||||
|
pos: underlay.pos.copy(),
|
||||||
|
direction: underlay.direction,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BeltUnderlaysComponent({
|
||||||
|
underlays: beltUnderlaysCopy,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} param0
|
||||||
|
* @param {Array<BeltUnderlayTile>=} param0.underlays Where to render belt underlays
|
||||||
|
*/
|
||||||
|
constructor({ underlays = [] }) {
|
||||||
|
super();
|
||||||
|
this.underlays = underlays;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,161 +1,159 @@
|
|||||||
import { enumDirection, enumDirectionToVector, Vector } from "../../core/vector";
|
import { enumDirection, enumDirectionToVector, Vector } from "../../core/vector";
|
||||||
import { types } from "../../savegame/serialization";
|
import { types } from "../../savegame/serialization";
|
||||||
import { BaseItem } from "../base_item";
|
import { BaseItem } from "../base_item";
|
||||||
import { BeltPath } from "../belt_path";
|
import { BeltPath } from "../belt_path";
|
||||||
import { Component } from "../component";
|
import { Component } from "../component";
|
||||||
import { Entity } from "../entity";
|
import { Entity } from "../entity";
|
||||||
import { typeItemSingleton } from "../item_resolver";
|
import { typeItemSingleton } from "../item_resolver";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* pos: Vector,
|
* pos: Vector,
|
||||||
* direction: enumDirection,
|
* direction: enumDirection,
|
||||||
* item: BaseItem,
|
* item: BaseItem,
|
||||||
* progress: number?,
|
* progress: number?,
|
||||||
* cachedDestSlot?: import("./item_acceptor").ItemAcceptorLocatedSlot,
|
* cachedDestSlot?: import("./item_acceptor").ItemAcceptorLocatedSlot,
|
||||||
* cachedBeltPath?: BeltPath,
|
* cachedBeltPath?: BeltPath,
|
||||||
* cachedTargetEntity?: Entity
|
* cachedTargetEntity?: Entity
|
||||||
* }} ItemEjectorSlot
|
* }} ItemEjectorSlot
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class ItemEjectorComponent extends Component {
|
export class ItemEjectorComponent extends Component {
|
||||||
static getId() {
|
static getId() {
|
||||||
return "ItemEjector";
|
return "ItemEjector";
|
||||||
}
|
}
|
||||||
|
|
||||||
static getSchema() {
|
static getSchema() {
|
||||||
// The cachedDestSlot, cachedTargetEntity fields are not serialized.
|
// The cachedDestSlot, cachedTargetEntity fields are not serialized.
|
||||||
return {
|
return {
|
||||||
slots: types.array(
|
slots: types.array(
|
||||||
types.structured({
|
types.structured({
|
||||||
item: types.nullable(typeItemSingleton),
|
item: types.nullable(typeItemSingleton),
|
||||||
progress: types.float,
|
progress: types.float,
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
duplicateWithoutContents() {
|
duplicateWithoutContents() {
|
||||||
const slotsCopy = [];
|
const slotsCopy = [];
|
||||||
for (let i = 0; i < this.slots.length; ++i) {
|
for (let i = 0; i < this.slots.length; ++i) {
|
||||||
const slot = this.slots[i];
|
const slot = this.slots[i];
|
||||||
slotsCopy.push({
|
slotsCopy.push({
|
||||||
pos: slot.pos.copy(),
|
pos: slot.pos.copy(),
|
||||||
direction: slot.direction,
|
direction: slot.direction,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ItemEjectorComponent({
|
return new ItemEjectorComponent({
|
||||||
slots: slotsCopy,
|
slots: slotsCopy,
|
||||||
});
|
renderFloatingItems: this.renderFloatingItems,
|
||||||
}
|
});
|
||||||
|
}
|
||||||
/**
|
|
||||||
*
|
/**
|
||||||
* @param {object} param0
|
*
|
||||||
* @param {Array<{pos: Vector, direction: enumDirection }>=} param0.slots The slots to eject on
|
* @param {object} param0
|
||||||
*/
|
* @param {Array<{pos: Vector, direction: enumDirection }>=} param0.slots The slots to eject on
|
||||||
constructor({ slots = [] }) {
|
* @param {boolean=} param0.renderFloatingItems Whether to render items even if they are not connected
|
||||||
super();
|
*/
|
||||||
|
constructor({ slots = [], renderFloatingItems = true }) {
|
||||||
this.setSlots(slots);
|
super();
|
||||||
|
|
||||||
/**
|
this.setSlots(slots);
|
||||||
* Whether this ejector slot is enabled
|
this.renderFloatingItems = renderFloatingItems;
|
||||||
*/
|
}
|
||||||
this.enabled = true;
|
|
||||||
}
|
/**
|
||||||
|
* @param {Array<{pos: Vector, direction: enumDirection }>} slots The slots to eject on
|
||||||
/**
|
*/
|
||||||
* @param {Array<{pos: Vector, direction: enumDirection }>} slots The slots to eject on
|
setSlots(slots) {
|
||||||
*/
|
/** @type {Array<ItemEjectorSlot>} */
|
||||||
setSlots(slots) {
|
this.slots = [];
|
||||||
/** @type {Array<ItemEjectorSlot>} */
|
for (let i = 0; i < slots.length; ++i) {
|
||||||
this.slots = [];
|
const slot = slots[i];
|
||||||
for (let i = 0; i < slots.length; ++i) {
|
this.slots.push({
|
||||||
const slot = slots[i];
|
pos: slot.pos,
|
||||||
this.slots.push({
|
direction: slot.direction,
|
||||||
pos: slot.pos,
|
item: null,
|
||||||
direction: slot.direction,
|
progress: 0,
|
||||||
item: null,
|
cachedDestSlot: null,
|
||||||
progress: 0,
|
cachedTargetEntity: null,
|
||||||
cachedDestSlot: null,
|
});
|
||||||
cachedTargetEntity: null,
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* Returns where this slot ejects to
|
||||||
/**
|
* @param {ItemEjectorSlot} slot
|
||||||
* Returns where this slot ejects to
|
* @returns {Vector}
|
||||||
* @param {ItemEjectorSlot} slot
|
*/
|
||||||
* @returns {Vector}
|
getSlotTargetLocalTile(slot) {
|
||||||
*/
|
const directionVector = enumDirectionToVector[slot.direction];
|
||||||
getSlotTargetLocalTile(slot) {
|
return slot.pos.add(directionVector);
|
||||||
const directionVector = enumDirectionToVector[slot.direction];
|
}
|
||||||
return slot.pos.add(directionVector);
|
|
||||||
}
|
/**
|
||||||
|
* Returns whether any slot ejects to the given local tile
|
||||||
/**
|
* @param {Vector} tile
|
||||||
* Returns whether any slot ejects to the given local tile
|
*/
|
||||||
* @param {Vector} tile
|
anySlotEjectsToLocalTile(tile) {
|
||||||
*/
|
for (let i = 0; i < this.slots.length; ++i) {
|
||||||
anySlotEjectsToLocalTile(tile) {
|
if (this.getSlotTargetLocalTile(this.slots[i]).equals(tile)) {
|
||||||
for (let i = 0; i < this.slots.length; ++i) {
|
return true;
|
||||||
if (this.getSlotTargetLocalTile(this.slots[i]).equals(tile)) {
|
}
|
||||||
return true;
|
}
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
/**
|
||||||
|
* Returns if we can eject on a given slot
|
||||||
/**
|
* @param {number} slotIndex
|
||||||
* Returns if we can eject on a given slot
|
* @returns {boolean}
|
||||||
* @param {number} slotIndex
|
*/
|
||||||
* @returns {boolean}
|
canEjectOnSlot(slotIndex) {
|
||||||
*/
|
assert(slotIndex >= 0 && slotIndex < this.slots.length, "Invalid ejector slot: " + slotIndex);
|
||||||
canEjectOnSlot(slotIndex) {
|
return !this.slots[slotIndex].item;
|
||||||
assert(slotIndex >= 0 && slotIndex < this.slots.length, "Invalid ejector slot: " + slotIndex);
|
}
|
||||||
return !this.slots[slotIndex].item;
|
|
||||||
}
|
/**
|
||||||
|
* Returns the first free slot on this ejector or null if there is none
|
||||||
/**
|
* @returns {number?}
|
||||||
* Returns the first free slot on this ejector or null if there is none
|
*/
|
||||||
* @returns {number?}
|
getFirstFreeSlot() {
|
||||||
*/
|
for (let i = 0; i < this.slots.length; ++i) {
|
||||||
getFirstFreeSlot() {
|
if (this.canEjectOnSlot(i)) {
|
||||||
for (let i = 0; i < this.slots.length; ++i) {
|
return i;
|
||||||
if (this.canEjectOnSlot(i)) {
|
}
|
||||||
return i;
|
}
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
/**
|
||||||
|
* Tries to eject a given item
|
||||||
/**
|
* @param {number} slotIndex
|
||||||
* Tries to eject a given item
|
* @param {BaseItem} item
|
||||||
* @param {number} slotIndex
|
* @returns {boolean}
|
||||||
* @param {BaseItem} item
|
*/
|
||||||
* @returns {boolean}
|
tryEject(slotIndex, item) {
|
||||||
*/
|
if (!this.canEjectOnSlot(slotIndex)) {
|
||||||
tryEject(slotIndex, item) {
|
return false;
|
||||||
if (!this.canEjectOnSlot(slotIndex)) {
|
}
|
||||||
return false;
|
this.slots[slotIndex].item = item;
|
||||||
}
|
this.slots[slotIndex].progress = 0;
|
||||||
this.slots[slotIndex].item = item;
|
return true;
|
||||||
this.slots[slotIndex].progress = 0;
|
}
|
||||||
return true;
|
|
||||||
}
|
/**
|
||||||
|
* Clears the given slot and returns the item it had
|
||||||
/**
|
* @param {number} slotIndex
|
||||||
* Clears the given slot and returns the item it had
|
* @returns {BaseItem|null}
|
||||||
* @param {number} slotIndex
|
*/
|
||||||
* @returns {BaseItem|null}
|
takeSlotItem(slotIndex) {
|
||||||
*/
|
const slot = this.slots[slotIndex];
|
||||||
takeSlotItem(slotIndex) {
|
const item = slot.item;
|
||||||
const slot = this.slots[slotIndex];
|
slot.item = null;
|
||||||
const item = slot.item;
|
slot.progress = 0.0;
|
||||||
slot.item = null;
|
return item;
|
||||||
slot.progress = 0.0;
|
}
|
||||||
return item;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,84 +1,300 @@
|
|||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { drawRotatedSprite } from "../../core/draw_utils";
|
import { DrawParameters } from "../../core/draw_parameters";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
import { enumDirectionToAngle } from "../../core/vector";
|
import { Rectangle } from "../../core/rectangle";
|
||||||
import { BeltUnderlaysComponent } from "../components/belt_underlays";
|
import { FULL_CLIP_RECT } from "../../core/sprites";
|
||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
||||||
import { BELT_ANIM_COUNT } from "./belt";
|
import {
|
||||||
import { MapChunkView } from "../map_chunk_view";
|
enumDirection,
|
||||||
import { DrawParameters } from "../../core/draw_parameters";
|
enumDirectionToAngle,
|
||||||
|
enumDirectionToVector,
|
||||||
export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
enumInvertedDirections,
|
||||||
constructor(root) {
|
Vector,
|
||||||
super(root, [BeltUnderlaysComponent]);
|
} from "../../core/vector";
|
||||||
|
import { BeltComponent } from "../components/belt";
|
||||||
this.underlayBeltSprites = [];
|
import { BeltUnderlaysComponent, enumClippedBeltUnderlayType } from "../components/belt_underlays";
|
||||||
|
import { ItemAcceptorComponent } from "../components/item_acceptor";
|
||||||
for (let i = 0; i < BELT_ANIM_COUNT; ++i) {
|
import { ItemEjectorComponent } from "../components/item_ejector";
|
||||||
this.underlayBeltSprites.push(Loader.getSprite("sprites/belt/built/forward_" + i + ".png"));
|
import { Entity } from "../entity";
|
||||||
}
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
}
|
import { MapChunkView } from "../map_chunk_view";
|
||||||
|
import { BELT_ANIM_COUNT } from "./belt";
|
||||||
/**
|
|
||||||
* Draws a given chunk
|
/**
|
||||||
* @param {DrawParameters} parameters
|
* Mapping from underlay type to clip rect
|
||||||
* @param {MapChunkView} chunk
|
* @type {Object<enumClippedBeltUnderlayType, Rectangle>}
|
||||||
*/
|
*/
|
||||||
drawChunk(parameters, chunk) {
|
const enumUnderlayTypeToClipRect = {
|
||||||
// Limit speed to avoid belts going backwards
|
[enumClippedBeltUnderlayType.none]: null,
|
||||||
const speedMultiplier = Math.min(this.root.hubGoals.getBeltBaseSpeed(), 10);
|
[enumClippedBeltUnderlayType.full]: FULL_CLIP_RECT,
|
||||||
|
[enumClippedBeltUnderlayType.topOnly]: new Rectangle(0, 0, 1, 0.5),
|
||||||
const contents = chunk.containedEntitiesByLayer.regular;
|
[enumClippedBeltUnderlayType.bottomOnly]: new Rectangle(0, 0.5, 1, 0.5),
|
||||||
for (let i = 0; i < contents.length; ++i) {
|
};
|
||||||
const entity = contents[i];
|
|
||||||
const underlayComp = entity.components.BeltUnderlays;
|
export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
||||||
if (!underlayComp) {
|
constructor(root) {
|
||||||
continue;
|
super(root, [BeltUnderlaysComponent]);
|
||||||
}
|
|
||||||
|
this.underlayBeltSprites = [];
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
|
||||||
const underlays = underlayComp.underlays;
|
for (let i = 0; i < BELT_ANIM_COUNT; ++i) {
|
||||||
for (let i = 0; i < underlays.length; ++i) {
|
this.underlayBeltSprites.push(Loader.getSprite("sprites/belt/built/forward_" + i + ".png"));
|
||||||
const { pos, direction } = underlays[i];
|
}
|
||||||
const transformedPos = staticComp.localTileToWorld(pos);
|
|
||||||
|
// Automatically recompute areas
|
||||||
// Culling
|
this.staleArea = new StaleAreaDetector({
|
||||||
if (!chunk.tileSpaceRectangle.containsPoint(transformedPos.x, transformedPos.y)) {
|
root,
|
||||||
continue;
|
name: "belt-underlay",
|
||||||
}
|
recomputeMethod: this.recomputeStaleArea.bind(this),
|
||||||
|
});
|
||||||
const destX = transformedPos.x * globalConfig.tileSize;
|
|
||||||
const destY = transformedPos.y * globalConfig.tileSize;
|
this.staleArea.recomputeOnComponentsChanged(
|
||||||
|
[BeltUnderlaysComponent, BeltComponent, ItemAcceptorComponent, ItemEjectorComponent],
|
||||||
// Culling, #2
|
1
|
||||||
if (
|
);
|
||||||
!parameters.visibleRect.containsRect4Params(
|
}
|
||||||
destX,
|
|
||||||
destY,
|
update() {
|
||||||
globalConfig.tileSize,
|
this.staleArea.update();
|
||||||
globalConfig.tileSize
|
}
|
||||||
)
|
|
||||||
) {
|
/**
|
||||||
continue;
|
* Called when an area changed - Resets all caches in the given area
|
||||||
}
|
* @param {Rectangle} area
|
||||||
|
*/
|
||||||
const angle = enumDirectionToAngle[staticComp.localDirectionToWorld(direction)];
|
recomputeStaleArea(area) {
|
||||||
|
for (let x = 0; x < area.w; ++x) {
|
||||||
// SYNC with systems/belt.js:drawSingleEntity!
|
for (let y = 0; y < area.h; ++y) {
|
||||||
const animationIndex = Math.floor(
|
const tileX = area.x + x;
|
||||||
((this.root.time.realtimeNow() * speedMultiplier * BELT_ANIM_COUNT * 126) / 42) *
|
const tileY = area.y + y;
|
||||||
globalConfig.itemSpacingOnBelts
|
const entity = this.root.map.getLayerContentXY(tileX, tileY, "regular");
|
||||||
);
|
if (entity) {
|
||||||
|
const underlayComp = entity.components.BeltUnderlays;
|
||||||
drawRotatedSprite({
|
if (underlayComp) {
|
||||||
parameters,
|
for (let i = 0; i < underlayComp.underlays.length; ++i) {
|
||||||
sprite: this.underlayBeltSprites[animationIndex % this.underlayBeltSprites.length],
|
underlayComp.underlays[i].cachedType = null;
|
||||||
x: destX + globalConfig.halfTileSize,
|
}
|
||||||
y: destY + globalConfig.halfTileSize,
|
}
|
||||||
angle: Math.radians(angle),
|
}
|
||||||
size: globalConfig.tileSize,
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
}
|
* Checks if a given tile is connected and has an acceptor
|
||||||
|
* @param {Vector} tile
|
||||||
|
* @param {enumDirection} fromDirection
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
checkIsAcceptorConnected(tile, fromDirection) {
|
||||||
|
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "regular");
|
||||||
|
if (!contents) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = contents.components.StaticMapEntity;
|
||||||
|
|
||||||
|
// Check if its a belt, since then its simple
|
||||||
|
const beltComp = contents.components.Belt;
|
||||||
|
if (beltComp) {
|
||||||
|
return staticComp.localDirectionToWorld(enumDirection.bottom) === fromDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if there's an item acceptor
|
||||||
|
const acceptorComp = contents.components.ItemAcceptor;
|
||||||
|
if (acceptorComp) {
|
||||||
|
// Check each slot to see if its connected
|
||||||
|
for (let i = 0; i < acceptorComp.slots.length; ++i) {
|
||||||
|
const slot = acceptorComp.slots[i];
|
||||||
|
const slotTile = staticComp.localTileToWorld(slot.pos);
|
||||||
|
|
||||||
|
// Step 1: Check if the tile matches
|
||||||
|
if (!slotTile.equals(tile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Check if any of the directions matches
|
||||||
|
for (let j = 0; j < slot.directions.length; ++j) {
|
||||||
|
const slotDirection = staticComp.localDirectionToWorld(slot.directions[j]);
|
||||||
|
if (slotDirection === fromDirection) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given tile is connected and has an ejector
|
||||||
|
* @param {Vector} tile
|
||||||
|
* @param {enumDirection} toDirection
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
checkIsEjectorConnected(tile, toDirection) {
|
||||||
|
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "regular");
|
||||||
|
if (!contents) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = contents.components.StaticMapEntity;
|
||||||
|
|
||||||
|
// Check if its a belt, since then its simple
|
||||||
|
const beltComp = contents.components.Belt;
|
||||||
|
if (beltComp) {
|
||||||
|
return staticComp.localDirectionToWorld(beltComp.direction) === toDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for an ejector
|
||||||
|
const ejectorComp = contents.components.ItemEjector;
|
||||||
|
if (ejectorComp) {
|
||||||
|
// Check each slot to see if its connected
|
||||||
|
for (let i = 0; i < ejectorComp.slots.length; ++i) {
|
||||||
|
const slot = ejectorComp.slots[i];
|
||||||
|
const slotTile = staticComp.localTileToWorld(slot.pos);
|
||||||
|
|
||||||
|
// Step 1: Check if the tile matches
|
||||||
|
if (!slotTile.equals(tile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Check if the direction matches
|
||||||
|
const slotDirection = staticComp.localDirectionToWorld(slot.direction);
|
||||||
|
if (slotDirection === toDirection) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the flag for a given tile
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @param {import("../components/belt_underlays").BeltUnderlayTile} underlayTile
|
||||||
|
* @returns {enumClippedBeltUnderlayType} The type of the underlay
|
||||||
|
*/
|
||||||
|
computeBeltUnderlayType(entity, underlayTile) {
|
||||||
|
if (underlayTile.cachedType) {
|
||||||
|
return underlayTile.cachedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
|
||||||
|
const transformedPos = staticComp.localTileToWorld(underlayTile.pos);
|
||||||
|
const destX = transformedPos.x * globalConfig.tileSize;
|
||||||
|
const destY = transformedPos.y * globalConfig.tileSize;
|
||||||
|
|
||||||
|
// Extract direction and angle
|
||||||
|
const worldDirection = staticComp.localDirectionToWorld(underlayTile.direction);
|
||||||
|
const worldDirectionVector = enumDirectionToVector[worldDirection];
|
||||||
|
|
||||||
|
// Figure out if there is anything connected at the top
|
||||||
|
const connectedTop = this.checkIsAcceptorConnected(
|
||||||
|
transformedPos.add(worldDirectionVector),
|
||||||
|
enumInvertedDirections[worldDirection]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Figure out if there is anything connected at the bottom
|
||||||
|
const connectedBottom = this.checkIsEjectorConnected(
|
||||||
|
transformedPos.sub(worldDirectionVector),
|
||||||
|
worldDirection
|
||||||
|
);
|
||||||
|
|
||||||
|
let flag = enumClippedBeltUnderlayType.none;
|
||||||
|
|
||||||
|
if (connectedTop && connectedBottom) {
|
||||||
|
flag = enumClippedBeltUnderlayType.full;
|
||||||
|
} else if (connectedTop) {
|
||||||
|
flag = enumClippedBeltUnderlayType.topOnly;
|
||||||
|
} else if (connectedBottom) {
|
||||||
|
flag = enumClippedBeltUnderlayType.bottomOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (underlayTile.cachedType = flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws a given chunk
|
||||||
|
* @param {DrawParameters} parameters
|
||||||
|
* @param {MapChunkView} chunk
|
||||||
|
*/
|
||||||
|
drawChunk(parameters, chunk) {
|
||||||
|
// Limit speed to avoid belts going backwards
|
||||||
|
const speedMultiplier = Math.min(this.root.hubGoals.getBeltBaseSpeed(), 10);
|
||||||
|
|
||||||
|
const contents = chunk.containedEntitiesByLayer.regular;
|
||||||
|
for (let i = 0; i < contents.length; ++i) {
|
||||||
|
const entity = contents[i];
|
||||||
|
const underlayComp = entity.components.BeltUnderlays;
|
||||||
|
if (!underlayComp) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
const underlays = underlayComp.underlays;
|
||||||
|
for (let i = 0; i < underlays.length; ++i) {
|
||||||
|
// Extract underlay parameters
|
||||||
|
const { pos, direction } = underlays[i];
|
||||||
|
const transformedPos = staticComp.localTileToWorld(pos);
|
||||||
|
const destX = transformedPos.x * globalConfig.tileSize;
|
||||||
|
const destY = transformedPos.y * globalConfig.tileSize;
|
||||||
|
|
||||||
|
// Culling, Part 1: Check if the chunk contains the tile
|
||||||
|
if (!chunk.tileSpaceRectangle.containsPoint(transformedPos.x, transformedPos.y)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Culling, Part 2: Check if the overlay is visible
|
||||||
|
if (
|
||||||
|
!parameters.visibleRect.containsRect4Params(
|
||||||
|
destX,
|
||||||
|
destY,
|
||||||
|
globalConfig.tileSize,
|
||||||
|
globalConfig.tileSize
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract direction and angle
|
||||||
|
const worldDirection = staticComp.localDirectionToWorld(direction);
|
||||||
|
const angle = enumDirectionToAngle[worldDirection];
|
||||||
|
|
||||||
|
const underlayType = this.computeBeltUnderlayType(entity, underlays[i]);
|
||||||
|
const clipRect = enumUnderlayTypeToClipRect[underlayType];
|
||||||
|
if (!clipRect) {
|
||||||
|
// Empty
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actually draw the sprite
|
||||||
|
const x = destX + globalConfig.halfTileSize;
|
||||||
|
const y = destY + globalConfig.halfTileSize;
|
||||||
|
const angleRadians = Math.radians(angle);
|
||||||
|
|
||||||
|
// SYNC with systems/belt.js:drawSingleEntity!
|
||||||
|
const animationIndex = Math.floor(
|
||||||
|
((this.root.time.realtimeNow() * speedMultiplier * BELT_ANIM_COUNT * 126) / 42) *
|
||||||
|
globalConfig.itemSpacingOnBelts
|
||||||
|
);
|
||||||
|
parameters.context.translate(x, y);
|
||||||
|
parameters.context.rotate(angleRadians);
|
||||||
|
this.underlayBeltSprites[
|
||||||
|
animationIndex % this.underlayBeltSprites.length
|
||||||
|
].drawCachedWithClipRect(
|
||||||
|
parameters,
|
||||||
|
-globalConfig.halfTileSize,
|
||||||
|
-globalConfig.halfTileSize,
|
||||||
|
globalConfig.tileSize,
|
||||||
|
globalConfig.tileSize,
|
||||||
|
clipRect
|
||||||
|
);
|
||||||
|
parameters.context.rotate(-angleRadians);
|
||||||
|
parameters.context.translate(-x, -y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|