-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
147 lines (129 loc) · 3.77 KB
/
index.html
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
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js" type="text/javascript"></script>
<script src="watersim-util.js" type="text/javascript"></script>
<script src="watersim-Cell.js" type="text/javascript"></script>
<script src="watersim-World.js" type="text/javascript"></script>
<style>
canvas { border: 1px solid #ccc;}
#speed { width: 3em;}
</style>
<title>Water Sim</title>
</head>
<body>
<p>left click for ground, middle click for water, and right click for air (erase)</p>
<canvas id="grid" width="600" height="600"></canvas>
<div>
<button id='step'>step</button>
<button id='play'>play</button>
<button id='pause'>pause</button>
<label for'speed'>Update Speed: </label><input id="speed" maxlength='3' value='25'/>
<button id='clear'>clear</button>
</div>
<div id="log"></div>
<script type="text/javascript">
var WIDTH = 15;
var HEIGHT = 15;
var GRID = 40;
var MaxMass = 1.0; //full cell of water
var MaxCompress = 0.02; //excess water a cell can store when compressed
var MinWater = 0.001; // if a cell has only this much water, the water "evaporates"
var MinFlow = 0.01;
var MaxSpeed = 1.0; //max amount of water that can move out of one cell in a generation
function log(msg) {
$("log").innerHTML += "<p>" + msg + "</p>";
}
var theWorld = new World( WIDTH, HEIGHT, $("grid").getContext('2d') );
theWorld.draw();
var animation;
function update() {
theWorld.tick();
theWorld.draw();
}
$("step").observe("click", function (e) {
update();
Event.stop(e);
});
$("play").observe("click", function (e) {
if( animation ) {
clearInterval( animation );
}
animation = setInterval( update, parseInt($("speed").value) );
});
$("pause").observe("click", function (e) {
if( animation ) {
clearInterval( animation );
animation = false;
}
});
$("speed").observe("blur", function (e) {
if( animation ) {
clearInterval( animation );
animation = setInterval( update, parseInt($("speed").value) );
}
});
$("clear").observe("click", function (e) {
theWorld.clear();
theWorld.draw();
});
$("grid").observe("mousedown", function (e) {
if( e.isLeftClick() ) {
addCell(e);
e.element().observe("mousemove", addCell );
}
if( e.isRightClick() ) {
removeCell(e);
e.element().observe("mousemove", removeCell );
}
if( e.isMiddleClick() ) {
addWater(e);
e.element().observe("mousemove", addWater );
}
Event.stop(e);
});
$("grid").observe("mouseup", function (e) {
e.element().stopObserving("mousemove");
Event.stop(e);
});
$("grid").observe("mouseout", function (e) {
e.element().stopObserving("mousemove");
});
function addCell(e) {
var grid = e.element().getLayout();
var x = Math.floor((e.pointerX() - grid.get("left"))/GRID);
var y = Math.floor((e.pointerY() - grid.get("top"))/GRID);
if( x >= WIDTH || y >= HEIGHT ) {
e.element().stopObserving("mousemove");
return;
}
//log( "adding ground at "+x+","+y+"...");
theWorld.pieces[x][y].setGround();
theWorld.draw();
}
function removeCell(e) {
var grid = e.element().getLayout();
var x = Math.floor((e.pointerX() - grid.get("left"))/GRID);
var y = Math.floor((e.pointerY() - grid.get("top"))/GRID);
if( x >= WIDTH || y >= HEIGHT ) {
e.element().stopObserving("mousemove");
return;
}
theWorld.pieces[x][y].clear();
theWorld.draw();
}
function addWater(e) {
var grid = e.element().getLayout();
var x = Math.floor((e.pointerX() - grid.get("left"))/GRID);
var y = Math.floor((e.pointerY() - grid.get("top"))/GRID);
if( x >= WIDTH || y >= HEIGHT ) {
e.element().stopObserving("mousemove");
return;
}
theWorld.pieces[x][y].setWater();
theWorld.draw();
}
$("grid").observe("contextmenu", Event.stop);
</script>
</body>
</html>