init version

This commit is contained in:
Tangent 2020-05-21 21:33:32 -07:00
commit dda603ee58

73
src/main.moon Normal file
View File

@ -0,0 +1,73 @@
cells = {}
active = {}
sand = { 1, 1, 0, 1 }
cells.get = (x, y) ->
if y > 100
return sand
if cells[x]
if cells[x][y]
return cells[x][y]
cells.set = (x, y, v) ->
unless cells[x]
cells[x] = {}
cells[x][y] = v
love.update = (dt) ->
for cell in *active
if cells.get(cell.x, cell.y) == sand
if cells.get(cell.x, cell.y + 1) == nil
cells.set(cell.x, cell.y, nil)
cells.set(cell.x, cell.y + 1, sand)
cell.y += 1
else
if math.random! < 0.5
if cells.get(cell.x - 1, cell.y + 1) == nil
cells.set(cell.x, cell.y, nil)
cells.set(cell.x - 1, cell.y + 1, sand)
cell.x -= 1
cell.y += 1
elseif cells.get(cell.x + 1, cell.y + 1) == nil
cells.set(cell.x, cell.y, nil)
cells.set(cell.x + 1, cell.y + 1, sand)
cell.x += 1
cell.y += 1
else
cell.remove = true
else
if cells.get(cell.x + 1, cell.y + 1) == nil
cells.set(cell.x, cell.y, nil)
cells.set(cell.x + 1, cell.y + 1, sand)
cell.x += 1
cell.y += 1
elseif cells.get(cell.x - 1, cell.y + 1) == nil
cells.set(cell.x, cell.y, nil)
cells.set(cell.x - 1, cell.y + 1, sand)
cell.x -= 1
cell.y += 1
else
cell.remove = true
for i = #cells, 1, -1
cell = cells[i]
if cell and cell.remove
table.remove(cells, i)
if love.mouse.isDown 1
x, y = love.mouse.getPosition!
x = math.floor x / 5
y = math.floor y / 5
table.insert(active, { :x, :y })
cells.set(x, y, sand)
love.draw = ->
love.graphics.setColor sand
for x = 1, 120
for y = 1, 120
if cells.get(x, y)
love.graphics.rectangle "fill", x * 5, y * 5, 5, 5
love.graphics.print love.timer.getFPS!
love.keypressed = (key) ->
if key == "escape"
love.event.quit!