1
1
import * as Sentry from "@sentry/browser"
2
2
import { Pane } from "tweakpane"
3
- import { CCA1D } from "./1d/cca_1d/cca_1d"
4
- import { Rule30 } from "./1d/rule30/rule30"
5
- import { Rule90 } from "./1d/rule90/rule90"
6
- import { Rule110 } from "./1d/rule110/rule110"
7
- import { CCA2D } from "./2d/cca_2d/cca_2d"
8
- import { ConwayAutomaton } from "./2d/conway/conway"
9
3
import { gosperGliderGunPattern } from "./2d/conway/patterns/guns"
10
4
import {
11
5
beaconPattern ,
@@ -19,18 +13,13 @@ import {
19
13
MWSSPattern ,
20
14
gliderPattern ,
21
15
} from "./2d/conway/patterns/spaceships"
22
- import { EntropyAutomaton } from "./2d/entropy/entropy"
23
- import { ImmigrationAutomaton } from "./2d/immigration/immigration"
24
- import { LangtonAutomaton } from "./2d/langton/langton"
25
- import { QuadLifeAutomaton } from "./2d/quadlife/quadlife"
26
- import { CCA3D } from "./3d/cca_3d"
27
- import type { AutomatonBase } from "./types/Automaton"
16
+ import { Automaton } from "./core/Automaton"
28
17
import type { Settings } from "./types/Settings"
29
- import { fetchMoviePalettes , moviePalettes } from "./utils/fetchMoviePalettes"
18
+ import { fetchMoviePalettes } from "./utils/fetchMoviePalettes"
30
19
31
20
let pane : Pane
32
21
let settings : Settings
33
- let automaton : AutomatonBase
22
+ let automaton : Automaton
34
23
35
24
const MOVIES_PALETTES_API = import . meta. env . VITE_MOVIES_PALETTES_API
36
25
@@ -419,13 +408,6 @@ window.onload = () => {
419
408
void fetchMoviePalettes ( paletteSelector , MOVIES_PALETTES_API )
420
409
}
421
410
422
- const cleanupAutomaton = ( automaton : AutomatonBase ) : void => {
423
- if ( ! automaton ) return
424
- if ( "clear" in automaton ) {
425
- automaton . clear ( )
426
- }
427
- }
428
-
429
411
const getSettings = ( pane : Pane ) : Settings => {
430
412
const settings : Partial < Settings > = { }
431
413
const state = pane . exportState ( )
@@ -446,96 +428,9 @@ const getSettings = (pane: Pane): Settings => {
446
428
return settings as Settings
447
429
}
448
430
449
- const createAutomaton = async (
450
- canvasEl : HTMLCanvasElement ,
451
- width : number ,
452
- height : number ,
453
- settings : Settings ,
454
- ) : Promise < AutomatonBase > => {
455
- try {
456
- const resolution : number = settings . resolution || 5
457
- const paletteColors = settings . palette
458
- ? moviePalettes . get ( settings . palette ) ?. colors
459
- : undefined
460
-
461
- switch ( settings . algo ) {
462
- case "cca-1D" :
463
- return new CCA1D (
464
- canvasEl ,
465
- width ,
466
- height ,
467
- settings . cca1dColorsCount || 4 ,
468
- paletteColors ,
469
- )
470
- case "rule30" :
471
- return new Rule30 ( canvasEl , width , height , paletteColors )
472
- case "rule90" :
473
- return new Rule90 ( canvasEl , width , height , paletteColors )
474
- case "rule110" :
475
- return new Rule110 ( canvasEl , width , height , paletteColors )
476
- case "cca-2D" :
477
- return new CCA2D (
478
- settings . cca2dThreshold ,
479
- canvasEl ,
480
- width ,
481
- height ,
482
- resolution ,
483
- settings . cca2dColorsCount ,
484
- paletteColors ,
485
- )
486
- case "cca-3D" :
487
- return new CCA3D (
488
- canvasEl ,
489
- width ,
490
- height ,
491
- settings . cca3dCubeDimension ,
492
- settings . cca3dThreshold ,
493
- settings . cca3dColorsCount ,
494
- paletteColors ,
495
- )
496
- case "conway" :
497
- return new ConwayAutomaton ( canvasEl , width , height , resolution )
498
- case "immigration" :
499
- return new ImmigrationAutomaton (
500
- canvasEl ,
501
- width ,
502
- height ,
503
- resolution ,
504
- undefined , // colorsCount (will be forced to 3)
505
- paletteColors , // Pass palette colors
506
- )
507
- case "quadlife" :
508
- return new QuadLifeAutomaton (
509
- canvasEl ,
510
- width ,
511
- height ,
512
- resolution ,
513
- undefined , // colorsCount (will be forced to 5)
514
- paletteColors , // Pass palette colors
515
- )
516
- case "langton" :
517
- return new LangtonAutomaton ( canvasEl , width , height , resolution )
518
- case "entropy" :
519
- return new EntropyAutomaton (
520
- canvasEl ,
521
- width ,
522
- height ,
523
- resolution ,
524
- settings . entropyColorsCount ,
525
- )
526
- default :
527
- throw new Error ( `Unknown algorithm: ${ settings . algo } ` )
528
- }
529
- } catch ( error ) {
530
- Sentry . captureException ( error )
531
- console . error ( "Failed to create automaton:" , error )
532
- throw error
533
- }
534
- }
535
-
536
431
const reset = async ( ) : Promise < void > => {
537
432
// Cleanup existing automaton
538
- cleanupAutomaton ( automaton )
433
+ Automaton . cleanup ( automaton )
539
434
automaton = undefined
540
435
541
436
// Get new settings
@@ -547,7 +442,7 @@ const reset = async (): Promise<void> => {
547
442
const height = window . innerHeight
548
443
549
444
// Create new automaton
550
- automaton = await createAutomaton ( canvasEl , width , height , settings )
445
+ automaton = await Automaton . create ( canvasEl , width , height , settings )
551
446
}
552
447
553
448
window . onresize = ( ) : void => {
0 commit comments