2025-12-28 01:24:31 +00:00
|
|
|
import { VueLike, unwrap_vuelike } from "../helpers";
|
2024-08-23 16:38:04 +00:00
|
|
|
import { DoorSaved } from "../model";
|
2023-09-07 02:08:56 +00:00
|
|
|
import { Rectangle } from "./rectangle";
|
2023-09-07 03:30:21 +00:00
|
|
|
import { Vector2D } from "./vector2d";
|
|
|
|
|
|
2023-02-02 18:06:55 +00:00
|
|
|
export class Door {
|
2023-09-12 14:55:08 +00:00
|
|
|
public static readonly MIN_DAY = 1;
|
|
|
|
|
|
|
|
|
|
private _day = Door.MIN_DAY;
|
2023-02-02 18:06:55 +00:00
|
|
|
public position: Rectangle;
|
|
|
|
|
|
2025-12-28 01:24:31 +00:00
|
|
|
constructor(position: VueLike<Rectangle>);
|
|
|
|
|
constructor(position: VueLike<Rectangle>, day: number);
|
|
|
|
|
constructor(position: VueLike<Rectangle>, day = Door.MIN_DAY) {
|
2023-02-02 18:06:55 +00:00
|
|
|
this.day = day;
|
2025-12-28 01:24:31 +00:00
|
|
|
this.position = unwrap_vuelike(position);
|
2023-02-02 18:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get day(): number {
|
2023-09-07 01:17:14 +00:00
|
|
|
return this._day;
|
2023-02-02 18:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-25 19:56:25 +00:00
|
|
|
public set day(value: number | string) {
|
2023-09-07 00:34:42 +00:00
|
|
|
// integer coercion
|
2025-12-25 19:56:25 +00:00
|
|
|
let day = Number(value);
|
2023-09-07 00:34:42 +00:00
|
|
|
|
2025-12-25 19:56:25 +00:00
|
|
|
day =
|
|
|
|
|
!Number.isNaN(day) && Number.isFinite(day)
|
|
|
|
|
? Math.trunc(day)
|
|
|
|
|
: Door.MIN_DAY;
|
|
|
|
|
|
|
|
|
|
this._day = Math.max(day, Door.MIN_DAY);
|
2023-02-02 18:06:55 +00:00
|
|
|
}
|
2023-09-07 03:30:21 +00:00
|
|
|
|
2023-09-07 19:34:11 +00:00
|
|
|
public static load(serialized: DoorSaved): Door {
|
2023-09-07 03:30:21 +00:00
|
|
|
return new Door(
|
|
|
|
|
new Rectangle(
|
|
|
|
|
new Vector2D(serialized.x1, serialized.y1),
|
|
|
|
|
new Vector2D(serialized.x2, serialized.y2),
|
|
|
|
|
),
|
|
|
|
|
serialized.day,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 19:34:11 +00:00
|
|
|
public save(): DoorSaved {
|
2023-09-07 03:30:21 +00:00
|
|
|
return {
|
|
|
|
|
day: this.day,
|
|
|
|
|
x1: this.position.origin.x,
|
|
|
|
|
y1: this.position.origin.y,
|
|
|
|
|
x2: this.position.corner.x,
|
|
|
|
|
y2: this.position.corner.y,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-09-07 01:17:14 +00:00
|
|
|
}
|