86 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-03-01 22:04:57 -07:00
import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config";
import { Entity, EntityNames } from ".";
2024-03-02 01:07:55 -07:00
import {
BoundingBox,
ComponentNames,
Grid,
2024-03-02 02:22:46 -07:00
Highlight,
2024-03-02 01:07:55 -07:00
Interactable,
2024-03-02 02:22:46 -07:00
Pushable,
2024-03-02 01:07:55 -07:00
Sprite,
} from "../components";
2024-03-01 22:04:57 -07:00
import { Coord2D } from "../interfaces";
2024-03-02 01:34:19 -07:00
import { openModal, closeModal } from "../utils";
2024-03-01 22:04:57 -07:00
export class FunctionBox extends Entity {
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
Sprites.FUNCTION_BOX,
) as SpriteSpec;
2024-03-02 01:07:55 -07:00
private code: string;
constructor(gridPosition: Coord2D, code: string) {
2024-03-01 22:04:57 -07:00
super(EntityNames.FunctionBox);
2024-03-02 01:07:55 -07:00
this.code = code;
2024-03-01 22:04:57 -07:00
this.addComponent(
new BoundingBox(
{
x: 0,
y: 0,
},
{
width: FunctionBox.spriteSpec.width,
height: FunctionBox.spriteSpec.height,
},
0,
),
);
2024-03-02 02:22:46 -07:00
this.addComponent(new Pushable());
this.addComponent(new Grid(gridPosition));
2024-03-01 22:04:57 -07:00
this.addComponent(
new Sprite(
IMAGES.get(FunctionBox.spriteSpec.sheet)!,
{ x: 0, y: 0 },
{
width: FunctionBox.spriteSpec.width,
height: FunctionBox.spriteSpec.height,
},
FunctionBox.spriteSpec.msPerFrame,
FunctionBox.spriteSpec.frames,
),
);
2024-03-02 01:07:55 -07:00
2024-03-02 02:22:46 -07:00
this.addComponent(
new Highlight(
2024-03-02 04:02:20 -07:00
(_direction) => this.onHighlight(),
2024-03-02 02:22:46 -07:00
() => this.onUnhighlight(),
),
);
}
private onUnhighlight() {
closeModal();
this.removeComponent(ComponentNames.Interactable);
}
2024-03-02 01:34:19 -07:00
2024-03-02 02:22:46 -07:00
private onHighlight() {
let modalOpen = false;
const interaction = () => {
if (modalOpen) {
modalOpen = false;
2024-03-02 01:34:19 -07:00
closeModal();
2024-03-02 02:22:46 -07:00
return;
}
modalOpen = true;
openModal(this.code);
};
this.addComponent(new Interactable(interaction));
2024-03-02 01:07:55 -07:00
}
2024-03-01 22:04:57 -07:00
}