Skip to content

Commit 38816ad

Browse files
committed
falling sand
1 parent 5022a0d commit 38816ad

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Voxel.jl

+14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,23 @@ const STONE = 1
1010
const SAND = 2
1111

1212
const airColor = sfColor_fromRGBA(0, 0, 0, 255)
13+
const stoneColor = sfColor_fromRGBA(128, 128, 128, 255)
1314
const sandColor = sfColor_fromRGBA(254, 232, 176, 255)
15+
16+
# function getColor(type)
17+
# @match
18+
# type == AIR ? airColor
19+
# type == STONE ? stoneColor
20+
# type == SAND ? sandColor
21+
# _ => sfColor_fromRGBA(0, 0, 0, 255)
22+
# end
23+
1424
function newVoxel(type)
25+
1526
color = type == AIR ? airColor : sandColor
27+
if (type == STONE)
28+
color = stoneColor
29+
end
1630
return particle(color, type)
1731
end
1832

main.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ leftPressed = false
1717
# TODO figure out how I want to handle resizing and map size in general
1818

1919
board = [[newVoxel(AIR) for i in 0:windowWidth-1] for j in 0:windowHeight-1]
20+
moveableMap = Dict()
2021

2122
function getBoardColors(board)
2223
flatBoard = collect(Iterators.flatten(board))
@@ -70,7 +71,22 @@ function updateClickedPixel()
7071

7172
# set boards voxel to new color
7273
board[mousePos.y+1][mousePos.x+1] = newVoxel(SAND)
74+
moveableMap[[mousePos.y + 1, mousePos.x + 1]] = SAND
75+
end
7376

77+
function physicsTick(board)
78+
# iterate over moveableMap and move particles down if air below them
79+
for (i, j) in keys(moveableMap)
80+
if i < windowHeight
81+
down1 = board[i+1][j]
82+
if down1.type == AIR
83+
board[i+1][j] = board[i][j]
84+
board[i][j] = down1
85+
moveableMap[[i + 1, j]] = moveableMap[[i, j]]
86+
delete!(moveableMap, [i, j])
87+
end
88+
end
89+
end
7490
end
7591

7692
function mouseHandler(event::sfEvent)
@@ -90,6 +106,7 @@ function mouseHandler(event::sfEvent)
90106
end
91107
end
92108

109+
lastPhysicsTick = 0.0
93110
while Bool(sfRenderWindow_isOpen(window))
94111

95112
currentTime = sfTime_asSeconds(sfClock_getElapsedTime(clock))
@@ -134,14 +151,22 @@ while Bool(sfRenderWindow_isOpen(window))
134151
# draw the sprite
135152
# newBoardSprite = updateBoard()
136153

137-
#update board
154+
# make pysics happen
155+
156+
if (currentTime - lastPhysicsTick > 0.01)
157+
global lastPhysicsTick = currentTime
158+
physicsTick(board)
159+
end
160+
161+
#draw board
138162
updateImageBuffer()
139163
texture = sfTexture_createFromImage(imageBuffer, C_NULL)
140164
@assert texture != C_NULL
141165

142166
sfSprite_setTexture(sprite, texture, sfTrue)
143167
sfRenderWindow_drawSprite(window, sprite, C_NULL)
144168

169+
145170
sfRenderWindow_drawText(window, text, C_NULL)
146171
# update the window
147172
sfRenderWindow_display(window)

0 commit comments

Comments
 (0)