Paul Liverman III 5 years ago
commit
e2abe34f01
4 changed files with 111 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 19
    0
      src/Fluid.moon
  3. 24
    0
      src/Mix.moon
  4. 67
    0
      src/main.moon

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.lua

+ 19
- 0
src/Fluid.moon View File

@@ -0,0 +1,19 @@
1
+class Fluid
2
+  new: (@volume=1) =>
3
+    @contents = {}
4
+    @sum = 0
5
+  add: (type, amount) =>
6
+    @contents[type] = 0 unless @contents[type]
7
+    @contents[type] += amount
8
+    @sum += amount
9
+  remove: (type, amount) =>
10
+    @contents[type] = 0 unless @contents[type]
11
+    @contents[type] -= amount
12
+    @sum -= amount
13
+  pressure: =>
14
+    return @sum / @volume
15
+  percent: (type) =>
16
+    return 0 unless @contents[type]
17
+    return @contents[type] / @sum
18
+  amount: =>
19
+    return @contents[type] or 0

+ 24
- 0
src/Mix.moon View File

@@ -0,0 +1,24 @@
1
+class Mix
2
+  new: (@high, @low, @volume=1) =>
3
+  update: (dt) =>
4
+    a, b = @high\pressure!, @low\pressure!
5
+    if b > a
6
+      tmp = @high
7
+      @high = @low
8
+      @low = tmp
9
+      tmp = a
10
+      a = b
11
+      b = tmp
12
+    elseif a > b
13
+      @active = true
14
+    else
15
+      @active = false
16
+
17
+    -- print "High: #{a}", "Low: #{b}", "Active? #{@active}"
18
+    if @active
19
+      sum = (a - b) * @volume * dt
20
+      for type, amount in pairs @high.contents
21
+        tmp = sum * @high\percent type
22
+        -- print "Moving #{tmp} #{type}.."
23
+        @high\remove type, tmp
24
+        @low\add type, tmp

+ 67
- 0
src/main.moon View File

@@ -0,0 +1,67 @@
1
+Fluid = require "Fluid"
2
+Mix = require "Mix"
3
+
4
+volume = 4.2e12
5
+air = { nitrogen: 0.775, oxygen: 0.21, argon: 0.01, co2: 0.005 }
6
+earth = Fluid(volume)
7
+for type, amount in pairs air
8
+  earth\add type, volume * amount
9
+
10
+-- print "pressure", earth\pressure!
11
+-- for type, amount in pairs earth.contents
12
+--   print type, earth\percent(type), amount
13
+
14
+ship = Fluid 600
15
+for type, amount in pairs air
16
+  ship\add type, 600 * amount
17
+
18
+h2tank = Fluid 40
19
+h2tank\add "hydrogen", 40 * 6 -- volume * desired pressure
20
+o2tank = Fluid 20
21
+o2tank\add "oxygen", 20 * 12
22
+mixes = { Mix(ship, h2tank), Mix(ship, o2tank) }
23
+
24
+mixing = false
25
+time,frequency = 0, 1/60
26
+love.update = (dt) ->
27
+  if mixing
28
+    time += dt
29
+    if time >= frequency
30
+      time -= frequency
31
+      for mix in *mixes
32
+        mix\update 10
33
+
34
+colors = {
35
+  hydrogen: { 1, 1, 0, 1 }
36
+  oxygen: { 0, 0, 1, 1 }
37
+  nitrogen: { 0, 1, 1, 1 }
38
+  argon: { 1, 0, 0, 1 }
39
+  co2: { 0.5, 0.5, 0.5, 1 }
40
+}
41
+
42
+w = love.graphics.getWidth!
43
+h = love.graphics.getHeight!
44
+h2 = h / 2
45
+love.draw = ->
46
+  y = -25
47
+  for type in pairs ship.contents
48
+    percent = ship\percent type
49
+    love.graphics.setColor colors[type]
50
+    love.graphics.rectangle "fill", 0, h2 + y, percent * w, 10
51
+    y += 10
52
+  y = 0
53
+  for type in pairs h2tank.contents
54
+    percent = h2tank\percent type
55
+    love.graphics.setColor colors[type]
56
+    love.graphics.rectangle "fill", 0, y, percent * w, 10
57
+    y += 10
58
+  y = 10
59
+  for type in pairs o2tank.contents
60
+    percent = o2tank\percent type
61
+    love.graphics.setColor colors[type]
62
+    love.graphics.rectangle "fill", 0, h - y, percent * w, 10
63
+    y += 10
64
+
65
+love.keypressed = (key) ->
66
+  love.event.quit! if key == "escape"
67
+  mixing = not mixing if key == "space"