1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import {
IMAGES,
Miscellaneous,
SPRITE_SPECS,
SpriteSpec,
Sprites,
} from "../config";
import { Entity, EntityNames } from ".";
import {
BoundingBox,
Colliding,
ComponentNames,
Grid,
Highlight,
Interactable,
LambdaSpawn,
Sprite,
Text,
} from "../components";
import { Coord2D, Direction } from "../interfaces";
import { openModal, closeModal } from "../utils";
export class LambdaFactory extends Entity {
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
Sprites.LAMBDA_FACTORY,
) as SpriteSpec;
private code: string;
private spawns: number;
constructor(gridPosition: Coord2D, code: string, spawns: number) {
super(EntityNames.LambdaFactory);
this.code = code;
this.spawns = spawns;
this.addComponent(
new BoundingBox(
{
x: 0,
y: 0,
},
{
width: LambdaFactory.spriteSpec.width,
height: LambdaFactory.spriteSpec.height,
},
0,
),
);
this.addComponent(new Text(spawns.toString()));
this.addComponent(new Colliding());
this.addComponent(new LambdaSpawn(this.spawns, this.code));
this.addComponent(new Grid(gridPosition));
this.addComponent(
new Sprite(
IMAGES.get(LambdaFactory.spriteSpec.sheet)!,
{ x: 0, y: 0 },
{
width: LambdaFactory.spriteSpec.width,
height: LambdaFactory.spriteSpec.height,
},
LambdaFactory.spriteSpec.msPerFrame,
LambdaFactory.spriteSpec.frames,
),
);
this.addComponent(
new Highlight(
(direction) => this.onHighlight(direction),
() => this.onUnhighlight(),
),
);
}
private onUnhighlight() {
closeModal();
this.removeComponent(ComponentNames.Interactable);
}
private onHighlight(direction: Direction) {
if (direction === Direction.LEFT || direction === Direction.RIGHT) {
const interaction = () => {
const spawner = this.getComponent<LambdaSpawn>(
ComponentNames.LambdaSpawn,
);
spawner.spawn(direction);
const text = this.getComponent<Text>(ComponentNames.Text);
text.text = spawner.spawnsLeft.toString();
this.addComponent(text);
};
this.addComponent(new Interactable(interaction));
return;
}
let modalOpen = false;
const close = () => {
modalOpen = false;
closeModal();
const spawner = this.getComponent<LambdaSpawn>(
ComponentNames.LambdaSpawn,
);
spawner.code = (
document.getElementById("code") as HTMLTextAreaElement
).value;
this.addComponent(spawner);
document.getElementById(Miscellaneous.CANVAS_ID)!.focus();
return;
};
const interaction = () => {
if (modalOpen) {
close();
return;
}
modalOpen = true;
openModal(this.codeEditor(this.code));
document.getElementById("close-modal")!.addEventListener("click", close);
};
this.addComponent(new Interactable(interaction));
}
private codeEditor(code: string) {
return `
<div>
<textarea id="code" rows="10" cols="50">${code}</textarea>
<button id="close-modal">Close</button>
</div>
`;
}
}
|