forked from watabou/TownGeneratorOS
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.tsx
More file actions
99 lines (93 loc) · 2.92 KB
/
Copy pathApp.tsx
File metadata and controls
99 lines (93 loc) · 2.92 KB
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
import React, { useState } from 'react';
import { VillagePane } from './components/VillagePane';
import {
generateWfcGrid,
transformGridToLayout,
VillageLayout,
VillageOptions,
} from './services/villageGenerationService';
const defaultOptions: VillageOptions = {
type: 'farming',
size: 'small',
includeFarmland: true,
includeMarket: true,
includeWalls: true,
includeWells: true,
};
export const App: React.FC = () => {
const [options, setOptions] = useState<VillageOptions>(defaultOptions);
const [layout, setLayout] = useState<VillageLayout>();
const handleCheckbox = (key: keyof VillageOptions) => (
e: React.ChangeEvent<HTMLInputElement>
) => {
setOptions((prev) => ({ ...prev, [key]: e.target.checked }));
};
const handleSelect = (
key: 'type' | 'size'
) => (e: React.ChangeEvent<HTMLSelectElement>) => {
setOptions((prev) => ({ ...prev, [key]: e.target.value as any }));
};
const handleGenerate = async () => {
const seed = Date.now().toString();
const grid = await generateWfcGrid(seed, options);
const l = transformGridToLayout(grid, options);
setLayout(l);
};
return (
<div>
<div style={{ marginBottom: '1rem' }}>
<label>
Type:
<select value={options.type} onChange={handleSelect('type')}>
<option value="farming">farming</option>
<option value="fishing">fishing</option>
<option value="fortified">fortified</option>
</select>
</label>
<label style={{ marginLeft: '1rem' }}>
Size:
<select value={options.size} onChange={handleSelect('size')}>
<option value="small">small</option>
<option value="medium">medium</option>
</select>
</label>
<label style={{ marginLeft: '1rem' }}>
<input
type="checkbox"
checked={options.includeFarmland !== false}
onChange={handleCheckbox('includeFarmland')}
/>
Farmland
</label>
<label style={{ marginLeft: '1rem' }}>
<input
type="checkbox"
checked={options.includeMarket !== false}
onChange={handleCheckbox('includeMarket')}
/>
Market
</label>
<label style={{ marginLeft: '1rem' }}>
<input
type="checkbox"
checked={options.includeWalls !== false}
onChange={handleCheckbox('includeWalls')}
/>
Walls
</label>
<label style={{ marginLeft: '1rem' }}>
<input
type="checkbox"
checked={options.includeWells !== false}
onChange={handleCheckbox('includeWells')}
/>
Wells
</label>
</div>
<button onClick={handleGenerate}>Generate Village</button>
<div style={{ marginTop: '1rem' }}>
{layout && <VillagePane layout={layout} />}
</div>
</div>
);
};