forked from fuse-open/fuse-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLife.js
143 lines (120 loc) · 2.74 KB
/
Life.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
var Observable = require("FuseJS/Observable")
var cells = Observable()
exports.cells = cells
var numColumns = 30
var numRows = 30
exports.numColumns = Observable(numColumns)
exports.numRows = Observable(numRows)
exports.autoStep = Observable(true)
function Cell(on) {
this.on = Observable(on)
this.count = 0
}
for (var i=0; i < numRows; ++i) {
for (var j=0; j < numColumns; ++j) {
cells.add( new Cell( Math.random() > 0.8 ? true : false ) )
}
}
/**
Get the cell at these coordinates (with wrap-around).
*/
function getCell(row,col) {
if (row < 0) {
row += numRows
}
row = row % numRows
if (col < 0) {
col += numColumns
}
col = col % numColumns
return cells.getAt(row * numColumns + col)
}
/**
How many neighbours of this cell are "on"
*/
function liveCount(row,col) {
var c = 0
for (var i=-1; i <= 1; ++i) {
for (var j=-1; j <= 1; ++j) {
if (i==0 && j==0) {
continue;
}
if (getCell(i+row,j+col).on.value)
c++
}
}
return c
}
function autoStep() {
step()
//check speed change now instead of with valueChagned to avoid
//odd time stretching while the user is interacting
updateTimer(exports.autoStep.value, exports.speed.value)
}
function step() {
//count neighbours prior to changing anything
for (var i=0; i < numRows; ++i) {
for (var j=0; j < numColumns; ++j) {
var c = getCell(i,j)
c.count = liveCount(i,j)
}
}
for (var i=0; i < numRows; ++i) {
for (var j=0; j < numColumns; ++j) {
var c = getCell(i,j)
if (!c.on.value) {
c.on.value = c.count == 3
} else if (c.count <2) {
c.on.value = false
} else if (c.count > 3) {
c.on.value = false
}
}
}
}
/**
This callback is done in the context of a cell item in the UI, thus the `data` property is the Cell object.
*/
exports.toggleCell = function(args) {
args.data.on.value = !args.data.on.value
}
/**
Turn off all cells.
*/
exports.clear = function(args) {
for (var i=0; i < cells.length; ++i) {
cells.getAt(i).on.value = false
}
}
exports.step = step
//timer control
var intervalSpeed = 0 //0 indicates no interval setup
var interval = null
exports.speed = Observable(500)
updateTimer(exports.autoStep.value,exports.speed.value)
exports.autoStepChanged = function(args) {
//we must use the `args.value` since `exports.autoStep.value` may not yet be updated
updateTimer(args.value, exports.speed.value)
//it feels more responsive to do something immediately when turning on
if (args.value) {
step()
}
}
/**
Match the actual interval timer to the desired timer.
*/
function updateTimer(on, speed) {
if (!on) {
speed = 0
}
if (speed == intervalSpeed) {
return
}
if (interval != null) {
clearInterval(interval)
}
intervalSpeed = speed
if (speed > 0) {
interval = setInterval( autoStep, intervalSpeed )
}
}