11package fluid
22
3+ import "math/rand"
4+
35type number = float64
46
7+ // Option configures a Simulator.
8+ type Option func (* Simulator )
9+
10+ // WithDrain adds a hole in the bottom and top boundaries at random X
11+ // positions. Particles that fall through the bottom hole wrap to the top.
12+ func WithDrain () Option {
13+ return func (s * Simulator ) {
14+ holeWidth := s .width / 4
15+ maxOffset := s .width - holeWidth
16+ bottomStart := rand .Float64 () * maxOffset
17+ topStart := rand .Float64 () * maxOffset
18+ s .drainEnabled = true
19+ s .bottomHoleMinX = bottomStart
20+ s .bottomHoleMaxX = bottomStart + holeWidth
21+ s .topHoleMinX = topStart
22+ s .topHoleMaxX = topStart + holeWidth
23+ }
24+ }
25+
526type Simulator struct {
627 width number
728 height number
@@ -20,13 +41,24 @@ type Simulator struct {
2041 particleListNextIdx []int
2142
2243 material Material
44+
45+ // Drain mode: particles in the bottom hole wrap to the top hole
46+ drainEnabled bool
47+ bottomHoleMinX number
48+ bottomHoleMaxX number
49+ topHoleMinX number
50+ topHoleMaxX number
2351}
2452
2553func (s * Simulator ) Particles () []Particle {
2654 return s .particles
2755}
2856
29- func New (width , height number , particles []Particle ) * Simulator {
57+ func New (
58+ width , height number ,
59+ particles []Particle ,
60+ options ... Option ,
61+ ) * Simulator {
3062 s := & Simulator {
3163 width : width ,
3264 height : height ,
@@ -50,5 +82,10 @@ func New(width, height number, particles []Particle) *Simulator {
5082 s .particleListNextIdx = make ([]int , len (particles ))
5183
5284 s .material = NewMaterial ("water" , 4 , 0.5 , 0.5 , 40 )
85+
86+ for _ , opt := range options {
87+ opt (s )
88+ }
89+
5390 return s
5491}
0 commit comments