Skip to content

Instantly share code, notes, and snippets.

@Vap0r1ze
Last active July 25, 2020 18:36
Show Gist options
  • Save Vap0r1ze/8fab2a475a64335895145fabd9ee0edd to your computer and use it in GitHub Desktop.
Save Vap0r1ze/8fab2a475a64335895145fabd9ee0edd to your computer and use it in GitHub Desktop.
#!/usr/bin/moon
-- Imports
{ energy_device: cell, :gpu } = require('component')
event = require('event')
os = require('os')
-- Constants
running = true
width, height = gpu.getResolution()
data =
lastTick: 0
cellVal: 0
cellMax: 0
-- Config
pollRate = 0.5 -- Polling interval in seconds
labelMargin = 2
labelPadding = 1
colors =
bg: 0x000000
label: 0xFFFFFF
border: 0x5A5A5A
progFg: 0x00B600
progBg: 0x004900
-- Helpers
getTick = -> os.time() * (1000/60/60) - 6000
clear = ->
gpu.setBackground(colors.bg)
gpu.fill(1, 1, width, height, ' ')
drawBox = (x, y, w, h, label) ->
gpu.setBackground(colors.border)
gpu.fill(x, y, w, h, ' ')
gpu.setBackground(colors.bg)
gpu.fill(x + 1, y + 1, w - 2, h - 2, ' ')
gpu.fill(x + 1 + labelMargin, y, labelPadding * 2 + #label, 1, ' ')
gpu.setForeground(colors.label)
gpu.set(x + 1 + labelMargin + labelPadding, y, label)
drawPaddedBox = (x, y, w, h, pt, pr, pb, pl, label) ->
x += pl
y += pt
w -= pl + pr
h -= pt + pb
drawBox(x, y, w, h, label)
drawProgress = (x, y, w, h, bg, fg, val, max) ->
valW = math.ceil(val/max * w)
gpu.setBackground(bg)
gpu.fill(x, y, w, h, ' ')
gpu.setBackground(fg)
gpu.fill(x, y, valW, h, ' ')
-- Loops
logicLoop = ->
tickDiff = getTick() - data.lastTick
cellVal = math.floor(cell.getEnergyStored())
cellMax = math.floor(cell.getMaxEnergyStored())
data.cellRFpt = math.floor((cellVal - data.cellVal)/tickDiff)
data.cellVal = cellVal
data.cellMax = cellMax
data.lastTick = getTick()
drawLoop = ->
clear()
-- Energy Cell
drawPaddedBox(1, 1, width/2, height/2, 1, 1, 1, 2, 'Energy Cell')
gpu.set(5, 4, "Energy Delta: #{data.cellRFpt} RF/t")
gpu.set(5, 5, "Energy Stored: #{data.cellVal} RF")
gpu.set(5, 6, "Energy Capacity: #{data.cellMax} RF")
drawProgress(5, height/2 - 2 - 5, width/2 - 7, 5, colors.progBg, colors.progFg, data.cellVal, data.cellMax)
-- Essentia Storage
drawPaddedBox(1 + width/2, 1, width/2, height/2, 1, 2, 1, 1, 'Essentia')
drawPaddedBox(1, 1 + height/2, width/2, height/2, 0, 1, 1, 2, 'Unassigned')
drawPaddedBox(1 + width/2, 1 + height/2, width/2, height/2, 0, 2, 1, 1, 'Unassigned')
-- Event Handler
handleEvent = (ev) ->
{id, _, x, y} = ev
switch id
when 'interrupted'
clear()
running = false
when 'touch'
nil
while running
logicLoop()
drawLoop()
ev = {event.pull(pollRate)}
if ev[1] != nil
handleEvent(ev)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment