This commit is contained in:
Paul Liverman III 2019-03-07 13:16:26 -08:00
commit e2abe34f01
4 changed files with 111 additions and 0 deletions

1
.gitignore vendored Normal file
View File

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

19
src/Fluid.moon Normal file
View File

@ -0,0 +1,19 @@
class Fluid
new: (@volume=1) =>
@contents = {}
@sum = 0
add: (type, amount) =>
@contents[type] = 0 unless @contents[type]
@contents[type] += amount
@sum += amount
remove: (type, amount) =>
@contents[type] = 0 unless @contents[type]
@contents[type] -= amount
@sum -= amount
pressure: =>
return @sum / @volume
percent: (type) =>
return 0 unless @contents[type]
return @contents[type] / @sum
amount: =>
return @contents[type] or 0

24
src/Mix.moon Normal file
View File

@ -0,0 +1,24 @@
class Mix
new: (@high, @low, @volume=1) =>
update: (dt) =>
a, b = @high\pressure!, @low\pressure!
if b > a
tmp = @high
@high = @low
@low = tmp
tmp = a
a = b
b = tmp
elseif a > b
@active = true
else
@active = false
-- print "High: #{a}", "Low: #{b}", "Active? #{@active}"
if @active
sum = (a - b) * @volume * dt
for type, amount in pairs @high.contents
tmp = sum * @high\percent type
-- print "Moving #{tmp} #{type}.."
@high\remove type, tmp
@low\add type, tmp

67
src/main.moon Normal file
View File

@ -0,0 +1,67 @@
Fluid = require "Fluid"
Mix = require "Mix"
volume = 4.2e12
air = { nitrogen: 0.775, oxygen: 0.21, argon: 0.01, co2: 0.005 }
earth = Fluid(volume)
for type, amount in pairs air
earth\add type, volume * amount
-- print "pressure", earth\pressure!
-- for type, amount in pairs earth.contents
-- print type, earth\percent(type), amount
ship = Fluid 600
for type, amount in pairs air
ship\add type, 600 * amount
h2tank = Fluid 40
h2tank\add "hydrogen", 40 * 6 -- volume * desired pressure
o2tank = Fluid 20
o2tank\add "oxygen", 20 * 12
mixes = { Mix(ship, h2tank), Mix(ship, o2tank) }
mixing = false
time,frequency = 0, 1/60
love.update = (dt) ->
if mixing
time += dt
if time >= frequency
time -= frequency
for mix in *mixes
mix\update 10
colors = {
hydrogen: { 1, 1, 0, 1 }
oxygen: { 0, 0, 1, 1 }
nitrogen: { 0, 1, 1, 1 }
argon: { 1, 0, 0, 1 }
co2: { 0.5, 0.5, 0.5, 1 }
}
w = love.graphics.getWidth!
h = love.graphics.getHeight!
h2 = h / 2
love.draw = ->
y = -25
for type in pairs ship.contents
percent = ship\percent type
love.graphics.setColor colors[type]
love.graphics.rectangle "fill", 0, h2 + y, percent * w, 10
y += 10
y = 0
for type in pairs h2tank.contents
percent = h2tank\percent type
love.graphics.setColor colors[type]
love.graphics.rectangle "fill", 0, y, percent * w, 10
y += 10
y = 10
for type in pairs o2tank.contents
percent = o2tank\percent type
love.graphics.setColor colors[type]
love.graphics.rectangle "fill", 0, h - y, percent * w, 10
y += 10
love.keypressed = (key) ->
love.event.quit! if key == "escape"
mixing = not mixing if key == "space"