commit e2abe34f01abe5d5a7836dee3c941036316fb0b2 Author: Paul Liverman III Date: Thu Mar 7 13:16:26 2019 -0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d907c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.lua diff --git a/src/Fluid.moon b/src/Fluid.moon new file mode 100644 index 0000000..e8a0c1d --- /dev/null +++ b/src/Fluid.moon @@ -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 diff --git a/src/Mix.moon b/src/Mix.moon new file mode 100644 index 0000000..44d65be --- /dev/null +++ b/src/Mix.moon @@ -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 diff --git a/src/main.moon b/src/main.moon new file mode 100644 index 0000000..4f6abab --- /dev/null +++ b/src/main.moon @@ -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"