-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomespring-html.js
151 lines (123 loc) · 3.46 KB
/
homespring-html.js
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
143
144
145
146
147
148
149
150
151
import * as hs from './homespring.js';
const nodeTpl = document.getElementById('node');
const salmonTpl = document.getElementById('salmon');
const runsEl = document.querySelector('.runs');
const ticksNEl = document.querySelector('.ticksN');
const tickEl = document.querySelector('.tick');
const outputEl = document.querySelector('.output');
function print(str) {
outputEl.innerText += str;
}
class HTMLSalmon extends hs.Salmon {
constructor(name, age, direction) {
super(name, age, direction);
const tpl = document.importNode(salmonTpl.content, true);
this.el = tpl.querySelector('span');
this.setName(name);
this.setAge(age);
this.setDirection(direction);
}
setName(name) {
super.setName(name);
if (this.el) {
this.el.querySelector('.name').innerText = name;
}
}
setDirection(direction) {
super.setDirection(direction);
if (this.el) {
if (direction === hs.Salmon.Downstream) {
this.el.classList.remove('upstream');
} else if (direction === hs.Salmon.Upstream) {
this.el.classList.add('upstream');
}
}
}
}
class HTMLNode extends hs.Node {
constructor(runner, parent) {
super(runner, parent);
const tpl = document.importNode(nodeTpl.content, true);
this.el = tpl.querySelector('li');
}
setName(name) {
super.setName(name);
this.el.children[0].innerText = name;
}
generatePower() {
super.generatePower();
this.el.classList.add('is-generating-power');
}
water() {
super.water();
this.el.classList.add('is-watered');
}
makeSnowy() {
super.makeSnowy();
this.el.classList.add('is-snowy');
}
destroy() {
super.destroy();
this.el.classList.add('is-destroyed');
}
powered() {
return this.children.some(node => node.generatingPower);
}
addChild(childNode) {
super.addChild(childNode);
this.el.querySelector('.children').appendChild(childNode.el);
}
addSalmon(salmon) {
if (!super.addSalmon(salmon)) {
return false;
}
this.el.querySelector('.salmons').appendChild(salmon.el);
return true;
}
}
HTMLNode.prototype.SalmonType = HTMLSalmon;
HTMLNode.prototype.printer = (salmon) => {
print(salmon.name);
salmon.el.parentNode.removeChild(salmon.el);
};
class HTMLRunner extends hs.Runner {
doTick() {
if (!this.continue) {
runsEl.innerText = "Ended!";
} else {
runsEl.innerText = 1 + Math.floor(this.runs / hs.tickOrder.length);
ticksNEl.innerText = this.runs;
tickEl.innerText = this.currentTick.toString();
}
super.doTick();
}
}
function clear() {
document.querySelector('.target').innerHTML = '';
document.querySelector('.output').innerText = '';
}
document.querySelector(".boot").addEventListener("click", () => {
clear();
starter(document.querySelector(".program").value);
});
document.querySelector(".doTick").addEventListener("click", () => {
runner.doTick();
});
const tokens = hs.tokensFactory(HTMLNode);
let runner = null;
function starter(input) {
const parsed = hs.parser(input)
runner = new HTMLRunner();
const root = hs.tokensToTree(runner, tokens, parsed);
runner.setRoot(root);
document.querySelector(".target").appendChild(root.el);
}
function runny() {
if (!runner.continue) {
return;
}
runner.doTick();
setTimeout(runny, 200);
}
document.querySelector(".run").addEventListener("click", runny);
document.querySelector(".stop").addEventListener("click", () => { runner.continue = false; });