ujs/src/objects.js

225 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-11-14 00:16:47 +03:00
class DefaultObject {
2023-04-06 16:52:57 +03:00
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
2023-04-23 17:01:17 +03:00
this.ticker = null;
2023-04-06 16:52:57 +03:00
}
}
2023-04-30 20:39:57 +03:00
export class Empty {
constructor() {
this.ticker = null;
}
}
2023-11-14 00:16:47 +03:00
export class Rect extends DefaultObject {
2023-04-06 16:52:57 +03:00
constructor(x, y, w, h, fillColor = 'white') {
super(x, y, w, h);
this.fillColor = fillColor;
}
2023-04-23 18:50:36 +03:00
draw(context, debug = false) {
context.fillStyle = this.fillColor;
context.fillRect(this.x, this.y, this.width, this.height);
if (debug) {
drawDebug(context, this.x, this.y, this.width, this.height);
}
}
2023-04-06 16:52:57 +03:00
}
2023-04-06 18:46:01 +03:00
export class StrokeRect extends Rect {
2023-04-06 16:52:57 +03:00
constructor(x, y, w, h, fillColor = 'white', strokeColor = 'black', strokeWidth = 1) {
super(x, y, w, h, fillColor);
this.strokeWidth = strokeWidth;
this.strokeColor = strokeColor;
}
2023-04-23 18:50:36 +03:00
draw(context, debug = false) {
context.fillStyle = this.fillColor;
context.fillRect(this.x, this.y, this.width, this.height);
context.lineWidth = this.strokeWidth;
context.strokeStyle = this.strokeColor;
context.strokeRect(this.x, this.y, this.width, this.height);
if (debug) {
drawDebug(context, this.x, this.y, this.width, this.height);
}
}
}
export class Sprite {
#loaded;
#image;
constructor(src = null, x, y) {
this.x = x;
this.y = y;
this.width = null;
this.height = null;
2023-04-23 21:18:27 +03:00
this.angle = 0;
2023-04-30 20:39:57 +03:00
this.src = src;
this.srcOld = this.src;
2023-12-01 16:45:48 +03:00
this.ticker = null;
2023-04-23 18:50:36 +03:00
this.loaded = false;
this.#image = new Image();
this.#image.onload = this.#onload();
2023-04-30 20:39:57 +03:00
this.#image.src = this.src;
2023-04-23 18:50:36 +03:00
}
#onload() {
this.width = this.#image.width;
this.height = this.#image.height;
this.loaded = true;
}
image() {
return this.#image;
}
draw(context, debug = false) {
if (this.loaded != true) {
return -1;
}
2023-04-30 20:39:57 +03:00
if (this.srcOld != this.src) {
this.srcOld = this.src;
this.#image.src = this.src;
}
2023-04-23 21:18:27 +03:00
context.save();
context.translate(this.x + this.#image.width / 2, this.y + this.#image.height / 2);
context.rotate((this.angle * Math.PI) / 180);
context.drawImage(this.#image, -this.#image.width / 2, -this.#image.height / 2);
context.restore();
2023-04-23 18:50:36 +03:00
if (debug) {
drawDebug(context, this.x, this.y, this.#image.width, this.#image.height);
}
}
}
2023-04-23 21:18:27 +03:00
export class TiledSprite extends Sprite {
2023-04-30 20:42:30 +03:00
#image;
2023-04-30 18:54:53 +03:00
constructor(src = null, x, y, only = null) {
2023-04-23 21:18:27 +03:00
super(src, x, y);
2023-04-30 18:54:53 +03:00
2023-04-30 20:39:57 +03:00
this.src = src;
this.srcOld = this.src;
2023-04-30 18:54:53 +03:00
this.only = only;
2023-04-23 21:18:27 +03:00
}
draw(context, debug = false) {
if (this.loaded != true) {
return -1;
}
2023-04-30 20:39:57 +03:00
if (this.srcOld != this.src) {
this.srcOld = this.src;
this.#image.src = this.src;
}
2023-04-30 18:34:17 +03:00
// draw single image
2023-04-23 21:18:27 +03:00
context.drawImage(this.image(), this.x, this.y);
2023-04-30 18:34:17 +03:00
2023-04-23 21:18:27 +03:00
let copyUpByY = Math.ceil(this.y / this.image().height);
2023-04-30 18:34:17 +03:00
let copyUpByX = Math.ceil(this.y / this.image().width);
2023-04-23 21:18:27 +03:00
let copyDownByY = Math.ceil(
2023-12-01 15:55:39 +03:00
(context.canvas.height - this.y - this.image().height) / this.image().height
2023-04-23 21:18:27 +03:00
);
2023-04-30 18:34:17 +03:00
let copyDownByX = Math.ceil(
2023-12-01 15:55:39 +03:00
(context.canvas.width - this.y - this.image().width) / this.image().width
2023-04-30 18:34:17 +03:00
);
2023-04-23 21:18:27 +03:00
2023-04-30 18:34:17 +03:00
for (let iy = 0; iy <= copyUpByY; iy++) {
2023-04-23 21:18:27 +03:00
if (copyUpByY === Infinity) {
break;
}
2023-04-30 18:34:17 +03:00
for (let ix = 0; ix <= copyUpByX + 1; ix++) {
if (copyUpByX === Infinity) {
break;
}
2023-04-30 18:54:53 +03:00
switch (this.only) {
case 'y':
context.drawImage(this.image(), this.x, this.y - this.image().height * iy);
break;
2023-04-30 18:59:50 +03:00
case 'x':
context.drawImage(this.image(), this.x - this.image().width * ix, this.y);
break;
2023-04-30 18:54:53 +03:00
case null:
default:
context.drawImage(
this.image(),
this.x + this.image().width * ix,
2023-12-01 15:55:39 +03:00
this.y - this.image().height * iy
2023-04-30 18:54:53 +03:00
);
context.drawImage(
this.image(),
this.x - this.image().width * ix,
2023-12-01 15:55:39 +03:00
this.y - this.image().height * iy
2023-04-30 18:54:53 +03:00
);
break;
}
2023-04-30 18:34:17 +03:00
}
2023-04-23 21:18:27 +03:00
}
2023-04-30 18:34:17 +03:00
for (let iy = 0; iy <= copyDownByY; iy++) {
2023-04-23 21:18:27 +03:00
if (copyDownByY === Infinity) {
break;
}
2023-04-30 18:34:17 +03:00
for (let ix = 0; ix <= copyUpByX + 1; ix++) {
if (copyUpByX === Infinity) {
break;
}
2023-04-30 18:54:53 +03:00
switch (this.only) {
case 'y':
context.drawImage(this.image(), this.x, this.y + this.image().height * iy);
break;
2023-04-30 18:59:50 +03:00
case 'x':
context.drawImage(this.image(), this.x + this.image().width * ix, this.y);
break;
2023-04-30 18:54:53 +03:00
case null:
default:
context.drawImage(
this.image(),
this.x + this.image().width * ix,
2023-12-01 15:55:39 +03:00
this.y + this.image().height * iy
2023-04-30 18:54:53 +03:00
);
context.drawImage(
this.image(),
this.x - this.image().width * ix,
2023-12-01 15:55:39 +03:00
this.y + this.image().height * iy
2023-04-30 18:54:53 +03:00
);
break;
}
2023-04-30 18:34:17 +03:00
}
2023-04-23 21:18:27 +03:00
}
}
}
2023-04-23 18:50:36 +03:00
function drawDebug(context, x, y, w, h) {
2023-04-23 19:28:58 +03:00
context.font = '16px Monogram';
2023-04-23 18:50:36 +03:00
context.fillStyle = '#ffffff';
context.fillText(`${x}x${y}`, x + 1, y - 2);
context.fillStyle = '#000000';
context.fillText(`${x}x${y}`, x, y - 3);
context.lineWidth = 1;
context.strokeStyle = 'blue';
context.strokeRect(x, y, w, h);
2023-04-06 16:52:57 +03:00
}