~hokiegeek/biologist-web

ref: bc62db7d94d86fe99d8b5a26a9fe18ba1ac67939 biologist-web/src/app/petridish/petridish.component.ts -rw-r--r-- 2.3 KiB
bc62db7dHokieGeek Tweaks 3 years ago
                                                                                
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
import { Component, OnInit, Input } from '@angular/core';

import { Generation, Experiment } from '../lab.service';
import { Cell } from '../cell';

/*
Petridish's reason to be is to provide controls and a viewer to the timline structure
*/

@Component({
  selector: 'app-petridish',
  templateUrl: './petridish.component.html',
  styleUrls: ['./petridish.component.css']
})
export class PetridishComponent implements OnInit {
    private playing = false;
    creating = true;
    cellSize = 3;
    cellSpacing = 1;
    playRateMs = 50;

    @Input() experiment: Experiment;
    currentGeneration = 0;

    constructor() { }

    ngOnInit() {
        this.createSeed();
        setInterval(() => {
                if (this.playing && this.experiment.numGenerations() > this.currentGeneration) {
                    this.nav('+', 1);
                }
            }, this.playRateMs);

        if (this.experiment.autoStart) {
            this.analyze();
        }
    }

    destroy() {
        this.experiment.incinerate();
    }

    createSeed() {
        this.experiment.seed = new Generation(0, 'Creating', this.generateSeed());
    }

    generateSeed() {
        const seed = [];
        for (let row = this.experiment.rows - 1; row >= 0; row--) {
            for (let col = this.experiment.columns - 1; col >= 0; col--) {
                const randomVal = Math.random() * 100;
                if (randomVal > this.experiment.initialDensity) {
                    continue;
                }
                seed.push(new Cell(col, row));
            }
        }
        return seed;
    }

    analyze() {
        this.creating = false;
        this.playing = true;
        this.experiment.start();
    }

    getCurrentGeneration() {
        return this.experiment.get(this.currentGeneration);
    }

    togglePlaying() {
        this.playing = !this.playing;
    }

    nav(direction, step) {
        // TODO: only perform navigation if experiment can go that far...
        if (direction === '+') {
            this.currentGeneration += step;
        } else if (direction === '-') {
            this.currentGeneration -= step;
        }
    }

    speed(direction, step) {
        if (direction === '+') {
            this.playRateMs += step;
        } else if (direction === '-' && (this.playRateMs - step > 0)) {
            this.playRateMs -= step;
        }
    }
}