Skip to content

Commit c5b4ee0

Browse files
refactor: split main.ts with automaton creation as a method of a new Automaton class
1 parent 3ed019a commit c5b4ee0

File tree

3 files changed

+117
-116
lines changed

3 files changed

+117
-116
lines changed

core/Automaton.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import * as Sentry from "@sentry/browser"
2+
import { CCA1D } from "../1d/cca_1d/cca_1d"
3+
import { Rule30 } from "../1d/rule30/rule30"
4+
import { Rule90 } from "../1d/rule90/rule90"
5+
import { Rule110 } from "../1d/rule110/rule110"
6+
import { CCA2D } from "../2d/cca_2d/cca_2d"
7+
import { ConwayAutomaton } from "../2d/conway/conway"
8+
import { EntropyAutomaton } from "../2d/entropy/entropy"
9+
import { ImmigrationAutomaton } from "../2d/immigration/immigration"
10+
import { LangtonAutomaton } from "../2d/langton/langton"
11+
import { QuadLifeAutomaton } from "../2d/quadlife/quadlife"
12+
import { CCA3D } from "../3d/cca_3d"
13+
import type { Settings } from "../types/Settings"
14+
import { moviePalettes } from "../utils/fetchMoviePalettes"
15+
16+
export abstract class Automaton {
17+
renderInterval: NodeJS.Timer
18+
abstract clear(): void
19+
abstract start(intervalMs: number, maxIterations?: number): void
20+
21+
static async create(
22+
canvasEl: HTMLCanvasElement,
23+
width: number,
24+
height: number,
25+
settings: Settings,
26+
): Promise<Automaton> {
27+
try {
28+
const resolution: number = settings.resolution || 5
29+
const paletteColors = settings.palette
30+
? moviePalettes.get(settings.palette)?.colors
31+
: undefined
32+
33+
switch (settings.algo) {
34+
case "cca-1D":
35+
return new CCA1D(
36+
canvasEl,
37+
width,
38+
height,
39+
settings.cca1dColorsCount || 4,
40+
paletteColors,
41+
)
42+
case "rule30":
43+
return new Rule30(canvasEl, width, height, paletteColors)
44+
case "rule90":
45+
return new Rule90(canvasEl, width, height, paletteColors)
46+
case "rule110":
47+
return new Rule110(canvasEl, width, height, paletteColors)
48+
case "cca-2D":
49+
return new CCA2D(
50+
settings.cca2dThreshold,
51+
canvasEl,
52+
width,
53+
height,
54+
resolution,
55+
settings.cca2dColorsCount,
56+
paletteColors,
57+
)
58+
case "cca-3D":
59+
return new CCA3D(
60+
canvasEl,
61+
width,
62+
height,
63+
settings.cca3dCubeDimension,
64+
settings.cca3dThreshold,
65+
settings.cca3dColorsCount,
66+
paletteColors,
67+
)
68+
case "conway":
69+
return new ConwayAutomaton(canvasEl, width, height, resolution)
70+
case "immigration":
71+
return new ImmigrationAutomaton(
72+
canvasEl,
73+
width,
74+
height,
75+
resolution,
76+
undefined,
77+
paletteColors,
78+
)
79+
case "quadlife":
80+
return new QuadLifeAutomaton(
81+
canvasEl,
82+
width,
83+
height,
84+
resolution,
85+
undefined,
86+
paletteColors,
87+
)
88+
case "langton":
89+
return new LangtonAutomaton(canvasEl, width, height, resolution)
90+
case "entropy":
91+
return new EntropyAutomaton(
92+
canvasEl,
93+
width,
94+
height,
95+
resolution,
96+
settings.entropyColorsCount,
97+
)
98+
default:
99+
throw new Error(`Unknown algorithm: ${settings.algo}`)
100+
}
101+
} catch (error) {
102+
Sentry.captureException(error)
103+
console.error("Failed to create automaton:", error)
104+
throw error
105+
}
106+
}
107+
108+
static cleanup(automaton: Automaton): void {
109+
if (!automaton) return
110+
automaton.clear()
111+
}
112+
}

main.ts

Lines changed: 5 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import * as Sentry from "@sentry/browser"
22
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"
93
import { gosperGliderGunPattern } from "./2d/conway/patterns/guns"
104
import {
115
beaconPattern,
@@ -19,18 +13,13 @@ import {
1913
MWSSPattern,
2014
gliderPattern,
2115
} 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"
2817
import type { Settings } from "./types/Settings"
29-
import { fetchMoviePalettes, moviePalettes } from "./utils/fetchMoviePalettes"
18+
import { fetchMoviePalettes } from "./utils/fetchMoviePalettes"
3019

3120
let pane: Pane
3221
let settings: Settings
33-
let automaton: AutomatonBase
22+
let automaton: Automaton
3423

3524
const MOVIES_PALETTES_API = import.meta.env.VITE_MOVIES_PALETTES_API
3625

@@ -419,13 +408,6 @@ window.onload = () => {
419408
void fetchMoviePalettes(paletteSelector, MOVIES_PALETTES_API)
420409
}
421410

422-
const cleanupAutomaton = (automaton: AutomatonBase): void => {
423-
if (!automaton) return
424-
if ("clear" in automaton) {
425-
automaton.clear()
426-
}
427-
}
428-
429411
const getSettings = (pane: Pane): Settings => {
430412
const settings: Partial<Settings> = {}
431413
const state = pane.exportState()
@@ -446,96 +428,9 @@ const getSettings = (pane: Pane): Settings => {
446428
return settings as Settings
447429
}
448430

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-
536431
const reset = async (): Promise<void> => {
537432
// Cleanup existing automaton
538-
cleanupAutomaton(automaton)
433+
Automaton.cleanup(automaton)
539434
automaton = undefined
540435

541436
// Get new settings
@@ -547,7 +442,7 @@ const reset = async (): Promise<void> => {
547442
const height = window.innerHeight
548443

549444
// Create new automaton
550-
automaton = await createAutomaton(canvasEl, width, height, settings)
445+
automaton = await Automaton.create(canvasEl, width, height, settings)
551446
}
552447

553448
window.onresize = (): void => {

types/Automaton.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)